Skip to content

Commit 73c7dd0

Browse files
committed
Remove unnecessary Int-Word conversions
1 parent d2a508a commit 73c7dd0

File tree

5 files changed

+29
-33
lines changed

5 files changed

+29
-33
lines changed

containers/src/Data/IntMap/Internal.hs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,6 @@ module Data.IntMap.Internal (
267267
, Nat
268268

269269
-- * Utility
270-
, natFromInt
271-
, intFromNat
272270
, link
273271
, linkKey
274272
, linkWithMask
@@ -313,8 +311,9 @@ import Data.IntSet.Internal.IntTreeCommons
313311
, branchMask
314312
, TreeTreeBranch(..)
315313
, treeTreeBranch
314+
, i2w
316315
)
317-
import Utils.Containers.Internal.BitUtil
316+
import Utils.Containers.Internal.BitUtil (shiftLL, shiftRL, iShiftRL)
318317
import Utils.Containers.Internal.StrictPair
319318

320319
#ifdef __GLASGOW_HASKELL__
@@ -337,14 +336,6 @@ import qualified Control.Category as Category
337336
-- A "Nat" is a natural machine word (an unsigned Int)
338337
type Nat = Word
339338

340-
natFromInt :: Key -> Nat
341-
natFromInt = fromIntegral
342-
{-# INLINE natFromInt #-}
343-
344-
intFromNat :: Nat -> Key
345-
intFromNat = fromIntegral
346-
{-# INLINE intFromNat #-}
347-
348339
{--------------------------------------------------------------------
349340
Types
350341
--------------------------------------------------------------------}
@@ -2146,7 +2137,7 @@ mergeA
21462137
-> Int -> f (IntMap a)
21472138
-> f (IntMap a)
21482139
linkA k1 t1 k2 t2
2149-
| natFromInt k1 < natFromInt k2 = binA p t1 t2
2140+
| i2w k1 < i2w k2 = binA p t1 t2
21502141
| otherwise = binA p t2 t1
21512142
where
21522143
m = branchMask k1 k2
@@ -3178,7 +3169,7 @@ fromSet f (IntSet.Tip kx bm) = buildTree f kx bm (IntSet.suffixBitMask + 1)
31783169
-- and we construct the IntMap from that half.
31793170
buildTree g !prefix !bmask bits = case bits of
31803171
0 -> Tip prefix (g prefix)
3181-
_ -> case intFromNat ((natFromInt bits) `shiftRL` 1) of
3172+
_ -> case bits `iShiftRL` 1 of
31823173
bits2
31833174
| bmask .&. ((1 `shiftLL` bits2) - 1) == 0 ->
31843175
buildTree g (prefix + bits2) (bmask `shiftRL` bits2) bits2
@@ -3552,7 +3543,7 @@ link k1 t1 k2 t2 = linkWithMask (branchMask k1 k2) k1 t1 k2 t2
35523543
-- `linkWithMask` is useful when the `branchMask` has already been computed
35533544
linkWithMask :: Int -> Key -> IntMap a -> Key -> IntMap a -> IntMap a
35543545
linkWithMask m k1 t1 k2 t2
3555-
| natFromInt k1 < natFromInt k2 = Bin p t1 t2
3546+
| i2w k1 < i2w k2 = Bin p t1 t2
35563547
| otherwise = Bin p t2 t1
35573548
where
35583549
p = Prefix (mask k1 m .|. m)

containers/src/Data/IntMap/Strict/Internal.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,6 @@ import Data.IntSet.Internal.IntTreeCommons
266266
(Key, Prefix(..), nomatch, left, signBranch, mask, branchMask)
267267
import Data.IntMap.Internal
268268
( IntMap (..)
269-
, natFromInt
270-
, intFromNat
271269
, bin
272270
, binCheckLeft
273271
, binCheckRight
@@ -346,7 +344,7 @@ import Data.IntMap.Internal
346344
, withoutKeys
347345
)
348346
import qualified Data.IntSet.Internal as IntSet
349-
import Utils.Containers.Internal.BitUtil
347+
import Utils.Containers.Internal.BitUtil (iShiftRL, shiftLL, shiftRL)
350348
import Utils.Containers.Internal.StrictPair
351349
import qualified Data.Foldable as Foldable
352350

@@ -1056,7 +1054,7 @@ fromSet f (IntSet.Tip kx bm) = buildTree f kx bm (IntSet.suffixBitMask + 1)
10561054
-- one of them is nonempty and we construct the IntMap from that half.
10571055
buildTree g !prefix !bmask bits = case bits of
10581056
0 -> Tip prefix $! g prefix
1059-
_ -> case intFromNat ((natFromInt bits) `shiftRL` 1) of
1057+
_ -> case bits `iShiftRL` 1 of
10601058
bits2 | bmask .&. ((1 `shiftLL` bits2) - 1) == 0 ->
10611059
buildTree g (prefix + bits2) (bmask `shiftRL` bits2) bits2
10621060
| (bmask `shiftRL` bits2) .&. ((1 `shiftLL` bits2) - 1) == 0 ->

containers/src/Data/IntSet/Internal.hs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ import Data.IntSet.Internal.IntTreeCommons
217217
, branchMask
218218
, TreeTreeBranch(..)
219219
, treeTreeBranch
220+
, i2w
220221
)
221222

222223
#if __GLASGOW_HASKELL__
@@ -243,14 +244,6 @@ infixl 9 \\{-This comment teaches CPP correct behaviour -}
243244
-- A "Nat" is a natural machine word (an unsigned Int)
244245
type Nat = Word
245246

246-
natFromInt :: Int -> Nat
247-
natFromInt i = fromIntegral i
248-
{-# INLINE natFromInt #-}
249-
250-
intFromNat :: Nat -> Int
251-
intFromNat w = fromIntegral w
252-
{-# INLINE intFromNat #-}
253-
254247
{--------------------------------------------------------------------
255248
Operators
256249
--------------------------------------------------------------------}
@@ -1388,10 +1381,10 @@ fromRange (lx,rx)
13881381
| m < suffixBitMask = Tip p (complement 0)
13891382
| otherwise = Bin (Prefix (p .|. m)) (goFull p (shr1 m)) (goFull (p .|. m) (shr1 m))
13901383
lbm :: Int -> Int
1391-
lbm p = intFromNat (lowestBitMask (natFromInt p))
1384+
lbm p = p .&. negate p -- lowest bit mask
13921385
{-# INLINE lbm #-}
13931386
shr1 :: Int -> Int
1394-
shr1 m = intFromNat (natFromInt m `shiftRL` 1)
1387+
shr1 m = m `iShiftRL` 1
13951388
{-# INLINE shr1 #-}
13961389

13971390
-- | \(O(n)\). Build a set from an ascending list of elements.
@@ -1621,7 +1614,7 @@ link k1 t1 k2 t2 = linkWithMask (branchMask k1 k2) k1 t1 k2 t2
16211614
-- `linkWithMask` is useful when the `branchMask` has already been computed
16221615
linkWithMask :: Int -> Key -> IntSet -> Key -> IntSet -> IntSet
16231616
linkWithMask m k1 t1 k2 t2
1624-
| natFromInt k1 < natFromInt k2 = Bin p t1 t2
1617+
| i2w k1 < i2w k2 = Bin p t1 t2
16251618
| otherwise = Bin p t2 t1
16261619
where
16271620
p = Prefix (mask k1 m .|. m)
@@ -1707,12 +1700,12 @@ takeWhileAntitoneBits :: Int -> (Int -> Bool) -> Nat -> Nat
17071700
{-# INLINE foldMapBits #-}
17081701
{-# INLINE takeWhileAntitoneBits #-}
17091702

1703+
#if defined(__GLASGOW_HASKELL__)
1704+
17101705
lowestBitMask :: Nat -> Nat
17111706
lowestBitMask x = x .&. negate x
17121707
{-# INLINE lowestBitMask #-}
17131708

1714-
#if defined(__GLASGOW_HASKELL__)
1715-
17161709
lowestBitSet x = countTrailingZeros x
17171710

17181711
highestBitSet x = WORD_SIZE_IN_BITS - 1 - countLeadingZeros x

containers/src/Data/IntSet/Internal/IntTreeCommons.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module Data.IntSet.Internal.IntTreeCommons
3535
, treeTreeBranch
3636
, mask
3737
, branchMask
38+
, i2w
3839
) where
3940

4041
import Data.Bits (Bits(..))

containers/src/Utils/Containers/Internal/BitUtil.hs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE CPP #-}
2-
#if !defined(TESTING) && defined(__GLASGOW_HASKELL__)
3-
{-# LANGUAGE Safe #-}
2+
#ifdef __GLASGOW_HASKELL__
3+
{-# LANGUAGE MagicHash #-}
4+
{-# LANGUAGE Trustworthy #-}
45
#endif
56

67
#include "containers.h"
@@ -32,11 +33,15 @@ module Utils.Containers.Internal.BitUtil
3233
, shiftLL
3334
, shiftRL
3435
, wordSize
36+
, iShiftRL
3537
) where
3638

3739
import Data.Bits (unsafeShiftL, unsafeShiftR
3840
, countLeadingZeros, finiteBitSize
3941
)
42+
#ifdef __GLASGOW_HASKELL__
43+
import GHC.Exts (Int(..), uncheckedIShiftRL#)
44+
#endif
4045

4146
-- | Return a word where only the highest bit is set.
4247
highestBitMask :: Word -> Word
@@ -51,3 +56,11 @@ shiftLL = unsafeShiftL
5156
{-# INLINE wordSize #-}
5257
wordSize :: Int
5358
wordSize = finiteBitSize (0 :: Word)
59+
60+
-- Right logical shift.
61+
iShiftRL :: Int -> Int -> Int
62+
#ifdef __GLASGOW_HASKELL__
63+
iShiftRL (I# x#) (I# sh#) = I# (uncheckedIShiftRL# x# sh#)
64+
#else
65+
iShiftRL x sh = fromIntegral (unsafeShiftR (fromIntegral x :: Word) sh)
66+
#endif

0 commit comments

Comments
 (0)