Use explicitly sized word types (Word8#,Word16#,Word32#,Word64#) for bit operations (popCnt,pdep,pext,clz,ctz,byteswap)
Summary
Please forgive my confusion here - it seems like the following signatures should be used in ghc-prim
:
ctz8# :: Word8# -> Word#
ctz16# :: Word16# -> Word#
ctz32# :: Word32# -> Word#
ctz64# :: Word64# -> Word#
clz8# :: Word8# -> Word#
clz16# :: Word16# -> Word#
clz32# :: Word32# -> Word#
clz64# :: Word64# -> Word#
popCnt8# :: Word8# -> Word#
popCnt16# :: Word16# -> Word#
popCnt32# :: Word32# -> Word#
popCnt64# :: Word64# -> Word#
pdep8# :: Word8# -> Word8# -> Word8#
pdep16# :: Word16# -> Word16# -> Word16#
pdep32# :: Word32# -> Word32# -> Word32#
pdep64# :: Word64# -> Word64# -> Word64#
pext8# :: Word8# -> Word8# -> Word8#
pext16# :: Word16# -> Word16# -> Word16#
pext32# :: Word32# -> Word32# -> Word32#
pext64# :: Word64# -> Word64# -> Word64#
byteSwap16# :: Word16# -> Word16#
byteSwap32# :: Word32# -> Word32#
bitReverse8# :: Word8# -> Word8#
bitReverse16# :: Word16# -> Word16#
bitReverse32# :: Word32# -> Word32#
bitReverse64# :: Word64# -> Word64#
but currently they all still use Word#
except the Word64#
versions which use the correct type.
This, together with the comment "The higher bytes are undefined" for byteSwap
leads me to think that they're meant to all use the explicitly sized types (such that this is a bug rather than a feature request).
However, I don't fully understand the nuance of code generation / performance for wordToWord8#
family of functions.
Is there any reason not to simply replace them with the correct types?
OTOH I notice the narrow8Int#
family of functions still exist alongside the intToInt8#
family so I'm wondering if there's ever a case sticking to machine-sized types is preferable or if the narrow
functions should be removed too?
Environment
- GHC version used: 9.4.2
- ghc-prim: 0.9.0
Optional:
- Operating System: Ubuntu 22.04
- System Architecture: x86_64