1% 2% Copyright 2017, Data61, CSIRO 3% 4% This software may be distributed and modified according to the terms of 5% the GNU General Public License version 2. Note that NO WARRANTY is provided. 6% See "LICENSE_GPLv2.txt" for details. 7% 8% @TAG(DATA61_GPL) 9% 10% 11 12> -- Foundational Word definitions and their consequences 13> 14> module Data.WordLib where 15> 16> import Data.Word 17> import Data.Bits 18> 19> -- Convenience functions dealing with properties of the machine word: 20> -- * Number of bits in a word (architecture specific) 21> -- * Radix $n$ such that $2^n$ is the number of bits in the word 22> -- * Bytes required to store a word 23> -- * Selecting one of two alternatives depending on the size of the machine word 24> -- (32 or 64 bits) 25> 26> wordBits :: Int 27> wordBits = finiteBitSize (undefined::Word) 28> 29> wordSize :: Int 30> wordSize = wordBits `div` 8 31> 32> wordSizeCase :: a -> a -> a 33> wordSizeCase a b = case wordBits of 34> 32 -> a 35> 64 -> b 36> _ -> error "Unknown word size" 37> 38> wordRadix :: Int 39> wordRadix = wordSizeCase 5 6 40 41