Skip to content

Commit 016b42e

Browse files
committed
Pepare to GHC 7.8
1 parent cf7dfae commit 016b42e

File tree

2 files changed

+82
-8
lines changed

2 files changed

+82
-8
lines changed

cbits/gmp-extras.cmm

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,70 @@
1+
#if __GLASGOW_HASKELL__ >= 707
2+
#include "Cmm.h"
3+
#include "GmpDerivedConstants.h"
4+
5+
// TODO(superbobry): in the future release the syntax for calling
6+
// foreign funcations will CHANGE.
7+
8+
import "integer-gmp" __gmpz_init_set;
9+
import "integer-gmp" __gmpz_popcount;
10+
import "integer-gmp" __gmpz_setbit;
11+
import "integer-gmp" __gmpz_clrbit;
12+
13+
#define GMP_TAKE1_UL1_RET1(name,mp_fun) \
14+
name (W_ ws1, P_ d1, W_ wul) \
15+
{ \
16+
CInt s1; \
17+
CLong ul; \
18+
W_ mp_tmp; \
19+
W_ mp_result; \
20+
\
21+
/* call doYouWantToGC() */ \
22+
again: \
23+
STK_CHK_GEN_N (2 * SIZEOF_MP_INT); \
24+
MAYBE_GC(again); \
25+
\
26+
s1 = W_TO_INT(ws1); \
27+
ul = W_TO_LONG(wul); \
28+
\
29+
mp_tmp = Sp - 1 * SIZEOF_MP_INT; \
30+
mp_result = Sp - 2 * SIZEOF_MP_INT; \
31+
MP_INT__mp_alloc(mp_tmp) = W_TO_INT(BYTE_ARR_WDS(d1)); \
32+
MP_INT__mp_size(mp_tmp) = (s1); \
33+
MP_INT__mp_d(mp_tmp) = BYTE_ARR_CTS(d1); \
34+
\
35+
ccall __gmpz_init_set(mp_result "ptr", mp_tmp "ptr"); \
36+
\
37+
/* Perform the operation */ \
38+
ccall mp_fun(mp_result "ptr", ul); \
39+
\
40+
return(TO_W_(MP_INT__mp_size(mp_result)), \
41+
MP_INT__mp_d(mp_result) - SIZEOF_StgArrWords); \
42+
}
43+
44+
GMP_TAKE1_UL1_RET1(integer_cmm_setBitIntegerzh, __gmpz_setbit)
45+
GMP_TAKE1_UL1_RET1(integer_cmm_clearBitIntegerzh, __gmpz_clrbit)
46+
47+
integer_cmm_popCountIntegerzh (W_ ws, W_ d)
48+
{
49+
CInt s, res;
50+
W_ mp_tmp;
51+
52+
again:
53+
STK_CHK_P_LL(SIZEOF_MP_INT, integer_cmm_popCountIntegerzh, R2);
54+
MAYBE_GC(again);
55+
56+
s = W_TO_INT(ws);
57+
58+
mp_tmp = Sp - 1 * SIZEOF_MP_INT;
59+
MP_INT__mp_alloc(mp_tmp) = W_TO_INT(BYTE_ARR_WDS(d));
60+
MP_INT__mp_size(mp_tmp) = (s);
61+
MP_INT__mp_d(mp_tmp) = BYTE_ARR_CTS(d);
62+
63+
(res) = foreign "C" __gmpz_popcount(mp_tmp "ptr");
64+
65+
return (TO_W_(res));
66+
}
67+
#else
168
#include "Cmm.h"
269
#include "GmpDerivedConstants.h"
370

@@ -89,3 +156,4 @@ integer_cmm_popCountIntegerzh
89156

90157
RET_N(TO_W_(res));
91158
}
159+
#endif

src/GHC/Integer/GMP/TypeExt.hs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,32 @@ import GHC.Prim (Int#, (/=#), (>=#), (<#), (-#),
2121
import GHC.Integer.GMP.PrimExt (popCountInteger#, testBitInteger#,
2222
setBitInteger#, clearBitInteger#)
2323

24+
#if __GLASGOW_HASKELL__ >= 707
25+
import GHC.Exts (isTrue#)
26+
#else
27+
isTrue# = id
28+
#endif
29+
2430
popCountInteger :: Integer -> Int#
2531
popCountInteger (S# i) = word2Int# (popCnt# (int2Word# i))
2632
popCountInteger (J# s d) = popCountInteger# s d
2733
{-# NOINLINE popCountInteger #-}
2834

2935
testBitInteger :: Integer -> Int# -> Bool
3036
testBitInteger (S# j) i
31-
| i <# 0# = False
32-
| i <# (WORD_SIZE_IN_BITS# -# 1#) =
37+
| isTrue# (i <# 0#) = False
38+
| isTrue# (i <# (WORD_SIZE_IN_BITS# -# 1#)) =
3339
let !mask = 1# `uncheckedIShiftL#` i in
34-
word2Int# (int2Word# j `and#` int2Word# mask) /=# 0#
40+
isTrue# (word2Int# (int2Word# j `and#` int2Word# mask) /=# 0#)
3541
| otherwise =
3642
let !(# s, d #) = int2Integer# j in testBitInteger (J# s d) i
37-
testBitInteger (J# s d) i = testBitInteger# s d i /=# 0#
43+
testBitInteger (J# s d) i = isTrue# (testBitInteger# s d i /=# 0#)
3844
{-# NOINLINE testBitInteger #-}
3945

4046
setBitInteger :: Integer -> Int# -> Integer
4147
setBitInteger (S# j) i
42-
| i <# 0# = S# j
43-
| i <# (WORD_SIZE_IN_BITS# -# 1#) =
48+
| isTrue# (i <# 0#) = S# j
49+
| isTrue# (i <# (WORD_SIZE_IN_BITS# -# 1#)) =
4450
let !mask = 1# `uncheckedIShiftL#` i in
4551
S# (word2Int# (int2Word# j `or#` int2Word# mask))
4652
| otherwise =
@@ -51,12 +57,12 @@ setBitInteger (J# s d) i =
5157

5258
clearBitInteger :: Integer -> Int# -> Integer
5359
clearBitInteger (S# j) i
54-
| i <# 0# || i >=# (WORD_SIZE_IN_BITS# -# 1#) = S# j
60+
| isTrue# (i <# 0#) || isTrue# (i >=# (WORD_SIZE_IN_BITS# -# 1#)) = S# j
5561
| otherwise =
5662
let !mask =
5763
int2Word# (1# `uncheckedIShiftL#` i) `xor#`
5864
int2Word# (negateInt# 1#)
5965
in S# (word2Int# (int2Word# j `and#` mask))
6066
clearBitInteger (J# s d) i =
6167
let !(# s', d' #) = clearBitInteger# s d i in J# s' d'
62-
{-# NOINLINE clearBitInteger #-}
68+
{-# NOINLINE clearBitInteger #-}

0 commit comments

Comments
 (0)