Skip to content

Commit

Permalink
Use byteSwapX# in WORDS_BIGENDIAN
Browse files Browse the repository at this point in the history
  • Loading branch information
sgraf812 authored and andreasabel committed Jul 14, 2024
1 parent 4b22981 commit 3f7fc0c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Changes in 3.5.2.0

* Use `byteSwap16#` and `byteSwap32#` on big-endian architectures instead of
handrolling the implementation.

## Changes in 3.5.1.0

* Drop generating output for GHC < 6.4.
Expand Down
38 changes: 14 additions & 24 deletions data/AlexTemplate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,15 @@ data AlexAddr = AlexA# Addr#
{-# INLINE alexIndexInt16OffAddr #-}
alexIndexInt16OffAddr :: AlexAddr -> Int# -> Int#
alexIndexInt16OffAddr (AlexA# arr) off =
#ifdef WORDS_BIGENDIAN
narrow16Int# i
where
i = word2Int# ((high `uncheckedShiftL#` 8#) `or#` low)
high = int2Word# (ord# (indexCharOffAddr# arr (off' +# 1#)))
low = int2Word# (ord# (indexCharOffAddr# arr off'))
off' = off *# 2#
#else
#if __GLASGOW_HASKELL__ >= 901
GHC.Exts.int16ToInt#
GHC.Exts.int16ToInt# -- qualified import because it doesn't exist on older GHC's
#endif
(indexInt16OffAddr# arr off)
#ifdef WORDS_BIGENDIAN
(GHC.Exts.word16ToInt16# (GHC.Exts.wordToWord16# (GHC.Exts.byteSwap16# (GHC.Exts.word16ToWord# (GHC.Exts.int16ToWord16#
#endif
(indexInt16OffAddr# arr off)
#ifdef WORDS_BIGENDIAN
)))))
#endif
#else
alexIndexInt16OffAddr = (Data.Array.!)
Expand All @@ -65,22 +62,15 @@ alexIndexInt16OffAddr = (Data.Array.!)
{-# INLINE alexIndexInt32OffAddr #-}
alexIndexInt32OffAddr :: AlexAddr -> Int# -> Int#
alexIndexInt32OffAddr (AlexA# arr) off =
#ifdef WORDS_BIGENDIAN
narrow32Int# i
where
i = word2Int# ((b3 `uncheckedShiftL#` 24#) `or#`
(b2 `uncheckedShiftL#` 16#) `or#`
(b1 `uncheckedShiftL#` 8#) `or#` b0)
b3 = int2Word# (ord# (indexCharOffAddr# arr (off' +# 3#)))
b2 = int2Word# (ord# (indexCharOffAddr# arr (off' +# 2#)))
b1 = int2Word# (ord# (indexCharOffAddr# arr (off' +# 1#)))
b0 = int2Word# (ord# (indexCharOffAddr# arr off'))
off' = off *# 4#
#else
#if __GLASGOW_HASKELL__ >= 901
GHC.Exts.int32ToInt#
GHC.Exts.int32ToInt# -- qualified import because it doesn't exist on older GHC's
#endif
(indexInt32OffAddr# arr off)
#ifdef WORDS_BIGENDIAN
(GHC.Exts.word32ToInt32# (GHC.Exts.wordToWord32# (GHC.Exts.byteSwap32# (GHC.Exts.word32ToWord# (GHC.Exts.int32ToWord32#
#endif
(indexInt32OffAddr# arr off)
#ifdef WORDS_BIGENDIAN
)))))
#endif
#else
alexIndexInt32OffAddr = (Data.Array.!)
Expand Down

0 comments on commit 3f7fc0c

Please sign in to comment.