-
Notifications
You must be signed in to change notification settings - Fork 41
Refactor little endian binary decoding into a shared internal utility #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mchav
merged 2 commits into
DataHaskell:main
from
Sao-Ali:issue-164-unroll-loops-decode-unsigned-integers
Mar 15, 2026
+208
−83
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| module DataFrame.Internal.Binary where | ||
|
|
||
| import Data.Bits | ||
| import qualified Data.ByteString as BS | ||
| import Data.Int | ||
| import Data.Word | ||
|
|
||
| littleEndianWord32 :: BS.ByteString -> Word32 | ||
| littleEndianWord32 bytes | ||
| | len >= 4 = | ||
| assembleWord32 | ||
| (BS.index bytes 0) | ||
| (BS.index bytes 1) | ||
| (BS.index bytes 2) | ||
| (BS.index bytes 3) | ||
| | otherwise = | ||
| assembleWord32 | ||
| (byteAtOrZero len bytes 0) | ||
| (byteAtOrZero len bytes 1) | ||
| (byteAtOrZero len bytes 2) | ||
| (byteAtOrZero len bytes 3) | ||
| where | ||
| len = BS.length bytes | ||
| {-# INLINE littleEndianWord32 #-} | ||
|
|
||
| littleEndianWord64 :: BS.ByteString -> Word64 | ||
| littleEndianWord64 bytes | ||
| | len >= 8 = | ||
| assembleWord64 | ||
| (BS.index bytes 0) | ||
| (BS.index bytes 1) | ||
| (BS.index bytes 2) | ||
| (BS.index bytes 3) | ||
| (BS.index bytes 4) | ||
| (BS.index bytes 5) | ||
| (BS.index bytes 6) | ||
| (BS.index bytes 7) | ||
| | otherwise = | ||
| assembleWord64 | ||
| (byteAtOrZero len bytes 0) | ||
| (byteAtOrZero len bytes 1) | ||
| (byteAtOrZero len bytes 2) | ||
| (byteAtOrZero len bytes 3) | ||
| (byteAtOrZero len bytes 4) | ||
| (byteAtOrZero len bytes 5) | ||
| (byteAtOrZero len bytes 6) | ||
| (byteAtOrZero len bytes 7) | ||
| where | ||
| len = BS.length bytes | ||
| {-# INLINE littleEndianWord64 #-} | ||
|
|
||
| littleEndianInt32 :: BS.ByteString -> Int32 | ||
| littleEndianInt32 = fromIntegral . littleEndianWord32 | ||
| {-# INLINE littleEndianInt32 #-} | ||
|
|
||
| word64ToLittleEndian :: Word64 -> BS.ByteString | ||
| word64ToLittleEndian w = | ||
| BS.pack | ||
| [ fromIntegral w | ||
| , fromIntegral (w `unsafeShiftR` 8) | ||
| , fromIntegral (w `unsafeShiftR` 16) | ||
| , fromIntegral (w `unsafeShiftR` 24) | ||
| , fromIntegral (w `unsafeShiftR` 32) | ||
| , fromIntegral (w `unsafeShiftR` 40) | ||
| , fromIntegral (w `unsafeShiftR` 48) | ||
| , fromIntegral (w `unsafeShiftR` 56) | ||
| ] | ||
| {-# INLINE word64ToLittleEndian #-} | ||
|
|
||
| word32ToLittleEndian :: Word32 -> BS.ByteString | ||
| word32ToLittleEndian w = | ||
| BS.pack | ||
| [ fromIntegral w | ||
| , fromIntegral (w `unsafeShiftR` 8) | ||
| , fromIntegral (w `unsafeShiftR` 16) | ||
| , fromIntegral (w `unsafeShiftR` 24) | ||
| ] | ||
| {-# INLINE word32ToLittleEndian #-} | ||
|
|
||
| byteAtOrZero :: Int -> BS.ByteString -> Int -> Word8 | ||
| byteAtOrZero len bytes i | ||
| | i >= 0 && i < len = BS.index bytes i | ||
| | otherwise = 0 | ||
| {-# INLINE byteAtOrZero #-} | ||
|
|
||
| assembleWord32 :: Word8 -> Word8 -> Word8 -> Word8 -> Word32 | ||
| assembleWord32 b0 b1 b2 b3 = | ||
| fromIntegral b0 | ||
| .|. (fromIntegral b1 `unsafeShiftL` 8) | ||
| .|. (fromIntegral b2 `unsafeShiftL` 16) | ||
| .|. (fromIntegral b3 `unsafeShiftL` 24) | ||
| {-# INLINE assembleWord32 #-} | ||
|
|
||
| assembleWord64 :: | ||
| Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word64 | ||
| assembleWord64 b0 b1 b2 b3 b4 b5 b6 b7 = | ||
| fromIntegral b0 | ||
| .|. (fromIntegral b1 `unsafeShiftL` 8) | ||
| .|. (fromIntegral b2 `unsafeShiftL` 16) | ||
| .|. (fromIntegral b3 `unsafeShiftL` 24) | ||
| .|. (fromIntegral b4 `unsafeShiftL` 32) | ||
| .|. (fromIntegral b5 `unsafeShiftL` 40) | ||
| .|. (fromIntegral b6 `unsafeShiftL` 48) | ||
| .|. (fromIntegral b7 `unsafeShiftL` 56) | ||
| {-# INLINE assembleWord64 #-} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsafe index is fine here.