Direct conversion functions between Ints and Words.
This library defines a complete collection of conversion functions between machine integers: wordToWord32
, word32ToWord64
etc.
All these functions behave like fromIntegral
, but avoid going through Integer
. (GHC has a specific optimisation to remove an intermediate Integer
from fromIntegral
conversions, but if we can be explicit, and wont need to rely on an optimisation, why wouldn't we?)
There are also conversion to/from Char
. Convertions to Char
are unsafe, as these allow to create invalid Char
values (negative, or larger then maximum codepoint). In particular intToChar
is not the same as chr
; chr
performs bounds check; intToChar
doesn't. Conversion from Char
are equivalent to fromIntegral . ord
. Conversion to Char
are equivalent to intToChar . fromIntegral
, which does not perform bounds checks.
Only GHC>=9.4 is supported, as the primitive integers had been reworked in GHC-9.4.
This package deliberately doesn't provide any abstraction over the conversion functions.