Skip to content

Commit 54133b3

Browse files
authored
No benefit from constant shifts (#326)
* No benefit from constant shifts Safe shifts with statically known amount of shift are optimized by GHC to unsafe shifts automatically. * Remove Data.ByteString.Builder.Prim.Internal.UncheckedShifts
1 parent e278a3d commit 54133b3

File tree

10 files changed

+59
-179
lines changed

10 files changed

+59
-179
lines changed

.hlint.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
name: Use camelCase
33
within:
44
- Data.ByteString.Builder.Internal
5-
- Data.ByteString.Builder.Prim.Internal.UncheckedShifts
65
- Data.ByteString.Short.Internal
76
- ignore:
87
name: Use fewer imports

Data/ByteString/Builder/ASCII.hs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,11 @@ import Data.Monoid (mappend)
104104
import GHC.Num (quotRemInteger)
105105
# endif
106106

107-
# if __GLASGOW_HASKELL__ < 611
108-
import GHC.Integer.Internals
109-
# else
110107
import GHC.Integer.GMP.Internals
111-
# endif
112108
#endif
113109

114110
#if HAS_INTEGER_CONSTR
115111
import qualified Data.ByteString.Builder.Prim.Internal as P
116-
import Data.ByteString.Builder.Prim.Internal.UncheckedShifts
117-
( caseWordSize_32_64 )
118112
import Foreign.C.Types
119113
import GHC.Types (Int(..))
120114
#endif
@@ -329,7 +323,7 @@ lazyByteStringHex = P.primMapLazyByteStringFixed P.word8HexFixed
329323
-- FIXME: Think about also using the MSB. For 64 bit 'Int's this makes a
330324
-- difference.
331325
maxPow10 :: Integer
332-
maxPow10 = toInteger $ (10 :: Int) ^ caseWordSize_32_64 (9 :: Int) 18
326+
maxPow10 = toInteger $ (10 :: Int) ^ P.caseWordSize_32_64 (9 :: Int) 18
333327

334328
-- | Decimal encoding of an 'Integer' using the ASCII digits.
335329
integerDec :: Integer -> Builder
@@ -386,7 +380,7 @@ foreign import ccall unsafe "static _hs_bytestring_long_long_int_dec_padded18"
386380

387381
{-# INLINE intDecPadded #-}
388382
intDecPadded :: P.BoundedPrim Int
389-
intDecPadded = P.liftFixedToBounded $ caseWordSize_32_64
383+
intDecPadded = P.liftFixedToBounded $ P.caseWordSize_32_64
390384
(P.fixedPrim 9 $ c_int_dec_padded9 . fromIntegral)
391385
(P.fixedPrim 18 $ c_long_long_int_dec_padded18 . fromIntegral)
392386

Data/ByteString/Builder/Internal.hs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,6 @@ putLiftIO io = put $ \k br -> io >>= (`k` br)
608608
-- buffer is too small to execute one step of the 'Put' action, then
609609
-- it is replaced with a large enough buffer.
610610
hPut :: forall a. Handle -> Put a -> IO a
611-
#if __GLASGOW_HASKELL__ >= 611
612611
hPut h p = do
613612
fillHandle 1 (runPut p)
614613
where
@@ -655,12 +654,7 @@ hPut h p = do
655654

656655
| freeSpace buf < minFree = flushWriteBuffer h_
657656
| otherwise =
658-
#if __GLASGOW_HASKELL__ >= 613
659657
return ()
660-
#else
661-
-- required for ghc-6.12
662-
flushWriteBuffer h_
663-
#endif
664658

665659
fillBuffer buf
666660
| freeSpace buf < minFree =
@@ -709,15 +703,6 @@ hPut h p = do
709703
return $ do
710704
S.hPut h bs
711705
fillHandle 1 nextStep
712-
#else
713-
hPut h p =
714-
go =<< buildStepToCIOS strategy (runPut p)
715-
where
716-
strategy = untrimmedStrategy L.smallChunkSize L.defaultChunkSize
717-
718-
go (Finished buf x) = S.hPut h (byteStringFromBuffer buf) >> return x
719-
go (Yield1 bs io) = S.hPut h bs >> io >>= go
720-
#endif
721706

722707
-- | Execute a 'Put' and return the computed result and the bytes
723708
-- written during the computation as a lazy 'L.ByteString'.

Data/ByteString/Builder/Prim/ASCII.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ import Data.ByteString.Builder.Prim.Binary
8484
import Data.ByteString.Builder.Prim.Internal
8585
import Data.ByteString.Builder.Prim.Internal.Floating
8686
import Data.ByteString.Builder.Prim.Internal.Base16
87-
import Data.ByteString.Builder.Prim.Internal.UncheckedShifts
8887

8988
import Data.Char (ord)
9089

@@ -242,20 +241,20 @@ word8HexFixed = fixedPrim 2 $
242241
{-# INLINE word16HexFixed #-}
243242
word16HexFixed :: FixedPrim Word16
244243
word16HexFixed =
245-
(\x -> (fromIntegral $ x `shiftr_w16` 8, fromIntegral x))
244+
(\x -> (fromIntegral $ x `shiftR` 8, fromIntegral x))
246245
>$< pairF word8HexFixed word8HexFixed
247246

248247
-- | Encode a 'Word32' using 8 nibbles.
249248
{-# INLINE word32HexFixed #-}
250249
word32HexFixed :: FixedPrim Word32
251250
word32HexFixed =
252-
(\x -> (fromIntegral $ x `shiftr_w32` 16, fromIntegral x))
251+
(\x -> (fromIntegral $ x `shiftR` 16, fromIntegral x))
253252
>$< pairF word16HexFixed word16HexFixed
254253
-- | Encode a 'Word64' using 16 nibbles.
255254
{-# INLINE word64HexFixed #-}
256255
word64HexFixed :: FixedPrim Word64
257256
word64HexFixed =
258-
(\x -> (fromIntegral $ x `shiftr_w64` 32, fromIntegral x))
257+
(\x -> (fromIntegral $ x `shiftR` 32, fromIntegral x))
259258
>$< pairF word32HexFixed word32HexFixed
260259

261260
-- | Encode a 'Int8' using 2 nibbles (hexadecimal digits).

Data/ByteString/Builder/Prim/Binary.hs

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ module Data.ByteString.Builder.Prim.Binary (
5555
) where
5656

5757
import Data.ByteString.Builder.Prim.Internal
58-
import Data.ByteString.Builder.Prim.Internal.UncheckedShifts
5958
import Data.ByteString.Builder.Prim.Internal.Floating
6059

6160
import Foreign
@@ -87,7 +86,7 @@ word16BE :: FixedPrim Word16
8786
word16BE = word16Host
8887
#else
8988
word16BE = fixedPrim 2 $ \w p -> do
90-
poke p (fromIntegral (shiftr_w16 w 8) :: Word8)
89+
poke p (fromIntegral (shiftR w 8) :: Word8)
9190
poke (p `plusPtr` 1) (fromIntegral w :: Word8)
9291
#endif
9392

@@ -97,7 +96,7 @@ word16LE :: FixedPrim Word16
9796
#ifdef WORDS_BIGENDIAN
9897
word16LE = fixedPrim 2 $ \w p -> do
9998
poke p (fromIntegral w :: Word8)
100-
poke (p `plusPtr` 1) (fromIntegral (shiftr_w16 w 8) :: Word8)
99+
poke (p `plusPtr` 1) (fromIntegral (shiftR w 8) :: Word8)
101100
#else
102101
word16LE = word16Host
103102
#endif
@@ -109,9 +108,9 @@ word32BE :: FixedPrim Word32
109108
word32BE = word32Host
110109
#else
111110
word32BE = fixedPrim 4 $ \w p -> do
112-
poke p (fromIntegral (shiftr_w32 w 24) :: Word8)
113-
poke (p `plusPtr` 1) (fromIntegral (shiftr_w32 w 16) :: Word8)
114-
poke (p `plusPtr` 2) (fromIntegral (shiftr_w32 w 8) :: Word8)
111+
poke p (fromIntegral (shiftR w 24) :: Word8)
112+
poke (p `plusPtr` 1) (fromIntegral (shiftR w 16) :: Word8)
113+
poke (p `plusPtr` 2) (fromIntegral (shiftR w 8) :: Word8)
115114
poke (p `plusPtr` 3) (fromIntegral w :: Word8)
116115
#endif
117116

@@ -121,9 +120,9 @@ word32LE :: FixedPrim Word32
121120
#ifdef WORDS_BIGENDIAN
122121
word32LE = fixedPrim 4 $ \w p -> do
123122
poke p (fromIntegral w :: Word8)
124-
poke (p `plusPtr` 1) (fromIntegral (shiftr_w32 w 8) :: Word8)
125-
poke (p `plusPtr` 2) (fromIntegral (shiftr_w32 w 16) :: Word8)
126-
poke (p `plusPtr` 3) (fromIntegral (shiftr_w32 w 24) :: Word8)
123+
poke (p `plusPtr` 1) (fromIntegral (shiftR w 8) :: Word8)
124+
poke (p `plusPtr` 2) (fromIntegral (shiftR w 16) :: Word8)
125+
poke (p `plusPtr` 3) (fromIntegral (shiftR w 24) :: Word8)
127126
#else
128127
word32LE = word32Host
129128
#endif
@@ -144,25 +143,25 @@ word64BE = word64Host
144143
--
145144
word64BE =
146145
fixedPrim 8 $ \w p -> do
147-
let a = fromIntegral (shiftr_w64 w 32) :: Word32
146+
let a = fromIntegral (shiftR w 32) :: Word32
148147
b = fromIntegral w :: Word32
149-
poke p (fromIntegral (shiftr_w32 a 24) :: Word8)
150-
poke (p `plusPtr` 1) (fromIntegral (shiftr_w32 a 16) :: Word8)
151-
poke (p `plusPtr` 2) (fromIntegral (shiftr_w32 a 8) :: Word8)
148+
poke p (fromIntegral (shiftR a 24) :: Word8)
149+
poke (p `plusPtr` 1) (fromIntegral (shiftR a 16) :: Word8)
150+
poke (p `plusPtr` 2) (fromIntegral (shiftR a 8) :: Word8)
152151
poke (p `plusPtr` 3) (fromIntegral a :: Word8)
153-
poke (p `plusPtr` 4) (fromIntegral (shiftr_w32 b 24) :: Word8)
154-
poke (p `plusPtr` 5) (fromIntegral (shiftr_w32 b 16) :: Word8)
155-
poke (p `plusPtr` 6) (fromIntegral (shiftr_w32 b 8) :: Word8)
152+
poke (p `plusPtr` 4) (fromIntegral (shiftR b 24) :: Word8)
153+
poke (p `plusPtr` 5) (fromIntegral (shiftR b 16) :: Word8)
154+
poke (p `plusPtr` 6) (fromIntegral (shiftR b 8) :: Word8)
156155
poke (p `plusPtr` 7) (fromIntegral b :: Word8)
157156
#else
158157
word64BE = fixedPrim 8 $ \w p -> do
159-
poke p (fromIntegral (shiftr_w64 w 56) :: Word8)
160-
poke (p `plusPtr` 1) (fromIntegral (shiftr_w64 w 48) :: Word8)
161-
poke (p `plusPtr` 2) (fromIntegral (shiftr_w64 w 40) :: Word8)
162-
poke (p `plusPtr` 3) (fromIntegral (shiftr_w64 w 32) :: Word8)
163-
poke (p `plusPtr` 4) (fromIntegral (shiftr_w64 w 24) :: Word8)
164-
poke (p `plusPtr` 5) (fromIntegral (shiftr_w64 w 16) :: Word8)
165-
poke (p `plusPtr` 6) (fromIntegral (shiftr_w64 w 8) :: Word8)
158+
poke p (fromIntegral (shiftR w 56) :: Word8)
159+
poke (p `plusPtr` 1) (fromIntegral (shiftR w 48) :: Word8)
160+
poke (p `plusPtr` 2) (fromIntegral (shiftR w 40) :: Word8)
161+
poke (p `plusPtr` 3) (fromIntegral (shiftR w 32) :: Word8)
162+
poke (p `plusPtr` 4) (fromIntegral (shiftR w 24) :: Word8)
163+
poke (p `plusPtr` 5) (fromIntegral (shiftR w 16) :: Word8)
164+
poke (p `plusPtr` 6) (fromIntegral (shiftR w 8) :: Word8)
166165
poke (p `plusPtr` 7) (fromIntegral w :: Word8)
167166
#endif
168167
#endif
@@ -174,26 +173,26 @@ word64LE :: FixedPrim Word64
174173
#if WORD_SIZE_IN_BITS < 64
175174
word64LE =
176175
fixedPrim 8 $ \w p -> do
177-
let b = fromIntegral (shiftr_w64 w 32) :: Word32
176+
let b = fromIntegral (shiftR w 32) :: Word32
178177
a = fromIntegral w :: Word32
179178
poke (p) (fromIntegral a :: Word8)
180-
poke (p `plusPtr` 1) (fromIntegral (shiftr_w32 a 8) :: Word8)
181-
poke (p `plusPtr` 2) (fromIntegral (shiftr_w32 a 16) :: Word8)
182-
poke (p `plusPtr` 3) (fromIntegral (shiftr_w32 a 24) :: Word8)
179+
poke (p `plusPtr` 1) (fromIntegral (shiftR a 8) :: Word8)
180+
poke (p `plusPtr` 2) (fromIntegral (shiftR a 16) :: Word8)
181+
poke (p `plusPtr` 3) (fromIntegral (shiftR a 24) :: Word8)
183182
poke (p `plusPtr` 4) (fromIntegral b :: Word8)
184-
poke (p `plusPtr` 5) (fromIntegral (shiftr_w32 b 8) :: Word8)
185-
poke (p `plusPtr` 6) (fromIntegral (shiftr_w32 b 16) :: Word8)
186-
poke (p `plusPtr` 7) (fromIntegral (shiftr_w32 b 24) :: Word8)
183+
poke (p `plusPtr` 5) (fromIntegral (shiftR b 8) :: Word8)
184+
poke (p `plusPtr` 6) (fromIntegral (shiftR b 16) :: Word8)
185+
poke (p `plusPtr` 7) (fromIntegral (shiftR b 24) :: Word8)
187186
#else
188187
word64LE = fixedPrim 8 $ \w p -> do
189188
poke p (fromIntegral w :: Word8)
190-
poke (p `plusPtr` 1) (fromIntegral (shiftr_w64 w 8) :: Word8)
191-
poke (p `plusPtr` 2) (fromIntegral (shiftr_w64 w 16) :: Word8)
192-
poke (p `plusPtr` 3) (fromIntegral (shiftr_w64 w 24) :: Word8)
193-
poke (p `plusPtr` 4) (fromIntegral (shiftr_w64 w 32) :: Word8)
194-
poke (p `plusPtr` 5) (fromIntegral (shiftr_w64 w 40) :: Word8)
195-
poke (p `plusPtr` 6) (fromIntegral (shiftr_w64 w 48) :: Word8)
196-
poke (p `plusPtr` 7) (fromIntegral (shiftr_w64 w 56) :: Word8)
189+
poke (p `plusPtr` 1) (fromIntegral (shiftR w 8) :: Word8)
190+
poke (p `plusPtr` 2) (fromIntegral (shiftR w 16) :: Word8)
191+
poke (p `plusPtr` 3) (fromIntegral (shiftR w 24) :: Word8)
192+
poke (p `plusPtr` 4) (fromIntegral (shiftR w 32) :: Word8)
193+
poke (p `plusPtr` 5) (fromIntegral (shiftR w 40) :: Word8)
194+
poke (p `plusPtr` 6) (fromIntegral (shiftR w 48) :: Word8)
195+
poke (p `plusPtr` 7) (fromIntegral (shiftR w 56) :: Word8)
197196
#endif
198197
#else
199198
word64LE = word64Host

Data/ByteString/Builder/Prim/Internal.hs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,17 @@ module Data.ByteString.Builder.Prim.Internal (
6464
, (>$<)
6565
, (>*<)
6666

67+
-- * Helpers
68+
, caseWordSize_32_64
69+
6770
-- * Deprecated
6871
, boudedPrim
6972
) where
7073

7174
import Foreign
7275
import Prelude hiding (maxBound)
7376

74-
#if !(__GLASGOW_HASKELL__ >= 612)
75-
-- ghc-6.10 and older do not support {-# INLINE CONLIKE #-}
76-
#define CONLIKE
77-
#endif
77+
#include "MachDeps.h"
7878

7979
------------------------------------------------------------------------------
8080
-- Supporting infrastructure
@@ -298,3 +298,17 @@ eitherB (BP b1 io1) (BP b2 io2) =
298298
condB :: (a -> Bool) -> BoundedPrim a -> BoundedPrim a -> BoundedPrim a
299299
condB p be1 be2 =
300300
contramapB (\x -> if p x then Left x else Right x) (eitherB be1 be2)
301+
302+
-- | Select an implementation depending on bitness.
303+
-- Throw a compile time error if bitness is neither 32 nor 64.
304+
{-# INLINE caseWordSize_32_64 #-}
305+
caseWordSize_32_64
306+
:: a -- Value for 32-bit architecture
307+
-> a -- Value for 64-bit architecture
308+
-> a
309+
#if WORD_SIZE_IN_BITS == 32
310+
caseWordSize_32_64 = const
311+
#endif
312+
#if WORD_SIZE_IN_BITS == 64
313+
caseWordSize_32_64 = const id
314+
#endif

Data/ByteString/Builder/Prim/Internal/UncheckedShifts.hs

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)