Don't put "extra-libraries: gmp" in the cabal file; it comes from the buildinfo file
[packages/integer-gmp.git] / cbits / longlong.c
1 /* -----------------------------------------------------------------------------
2 * $Id: longlong.c,v 1.4 2002/12/13 14:23:42 simonmar Exp $
3 *
4 * (c) The GHC Team, 1998-1999
5 *
6 * Primitive operations over (64-bit) long longs
7 * (only used on 32-bit platforms.)
8 *
9 * ---------------------------------------------------------------------------*/
10
11
12 /*
13 Primitive Integer conversions to/from HsInt64 and HsWord64s.
14 N.B. These are not primops!
15
16 Instead of going the normal (boring) route of making the list
17 of primitive operations even longer to cope with operations
18 over 64-bit entities, we implement them instead 'out-of-line'.
19
20 The primitive ops get their own routine (in C) that implements
21 the operation, requiring the caller to _ccall_ out. This has
22 performance implications of course, but we currently don't
23 expect intensive use of either Int64 or Word64 types.
24 */
25
26 #include "Rts.h"
27
28 #ifdef SUPPORT_LONG_LONGS
29
30 HsWord64 hs_integerToWord64 (HsInt sa, StgByteArray /* Really: mp_limb_t* */ da)
31 {
32 mp_limb_t* d;
33 HsInt s;
34 HsWord64 res;
35 d = (mp_limb_t *)da;
36 s = sa;
37 switch (s) {
38 case 0: res = 0; break;
39 case 1: res = d[0]; break;
40 case -1: res = -(HsWord64)d[0]; break;
41 default:
42 res = (HsWord64)d[0] + ((HsWord64)d[1] << (BITS_IN (mp_limb_t)));
43 if (s < 0) res = -res;
44 }
45 return res;
46 }
47
48 HsInt64 hs_integerToInt64 (HsInt sa, StgByteArray /* Really: mp_limb_t* */ da)
49 {
50 mp_limb_t* d;
51 HsInt s;
52 HsInt64 res;
53 d = (mp_limb_t *)da;
54 s = (sa);
55 switch (s) {
56 case 0: res = 0; break;
57 case 1: res = d[0]; break;
58 case -1: res = -(HsInt64)d[0]; break;
59 default:
60 res = (HsInt64)d[0] + ((HsWord64)d[1] << (BITS_IN (mp_limb_t)));
61 if (s < 0) res = -res;
62 }
63 return res;
64 }
65
66 #endif /* SUPPORT_LONG_LONGS */