FixedPoint-simple-0.1: Fixed point, large word, and large int numerical representations (types and common class instances)

Safe HaskellSafe-Infered




This FixedPoint module implements arbitrary sized fixed point types and computations. This module intentionally avoids converting to Integer for computations because one purpose is to allow easy translation to other languages to produce stand-alone fixed point libraries. Instead of using Integer, elementary long multiplication and long division are implemented explicitly along with sqrt, exp, and erf functions that are implemented using only primitive operations.


Fixedpoint types

Common Operations

erf' :: (Eq a, Ord a, Num a, Fractional a) => Int -> a -> aSource

exp' :: (Ord a, Fractional a, Eq a) => Int -> a -> aSource

sqrt' :: (Eq a, Integral a, Num a, Bits a, Integral b, Bits b, Bits c) => Int -> GenericFixedPoint a b c -> GenericFixedPoint a b cSource

The square root operation uses Newton's method but is parameterized by the number of iterations and stops early if we have arrived at a fixed point (no pun intended). Suggested iterations: 500 (But it increases with the size of the input!)

pi' :: (Integral a, Bits a, Integral b, Num a, Bits b, Bits c) => GenericFixedPoint a b cSource

Big Int Types

type Int128 = BigInt Word128Source

A 128 bit int (signed)

type Int256 = BigInt Word256Source

A 256 bit int (signed)

type Int512 = BigInt Word512Source

A 512 bit int (signed)

type Int1024 = BigInt Word1024Source

A 1024 bit int (signed)

type Int2048 = BigInt Word2048Source

A 2048 bit int (signed)

type Int4096 = BigInt Word4096Source

A 4096 bit int (signed)

type Int8192 = BigInt Word8192Source

A 8192 bit int (signed)

Big Word Types

type Word256 = BigWord Word128Source

A 256 bit unsigned word

type Word512 = BigWord Word256Source

A 512 bit unsigned word

type Word1024 = BigWord Word512Source

A 1024 bit unsigned word

type Word2048 = BigWord Word1024Source

A 2048 bit unsigned word

type Word4096 = BigWord Word2048Source

A 4096 bit unsigned word

type Word8192 = BigWord Word4096Source

A 8192 bit unsigned word

Type Constructors

data GenericFixedPoint flat internal fracBitRepr Source

GenericFixedPoitn is a type constructor for arbitrarily-sized fixed point tyes. Take note the first type variable, flat, should be a signed int equal to the size of the fixed point integral plus fractional bits. The second type variable, internal, should be unsigned and twice as large a bit size as the flat type. The final type variable, fracBitRepr, should be a data structure of equal bit size to the fractional bits in the fixed point type. See the existing type aliases, such as FixedPoint4816, for examples.


FixedPoint flat 


(Enum a, Num a, Bits a, Bits c) => Enum (GenericFixedPoint a b c) 
Eq flat => Eq (GenericFixedPoint flat internal fracBitRepr) 
(Ord a, Integral a, Bits a, Num a, Bits b, Integral b, Bits c) => Fractional (GenericFixedPoint a b c) 
(Ord a, Num a, Bits a, Bits b, Integral a, Integral b, Bits c) => Num (GenericFixedPoint a b c) 
Ord flat => Ord (GenericFixedPoint flat internal fracBitRepr) 
(Integral a, Bits a, Integral b, Num a, Bits b, Bits c) => Read (GenericFixedPoint a b c) 
(Integral a, Ord a, Num a, Bits a, Bits b, Integral b, Bits c) => Real (GenericFixedPoint a b c) 
(Integral a, Integral b, Bits a, Bits b, Bits c) => Show (GenericFixedPoint a b c) 
(Ord a, Bits a, Bits b, Integral a, Integral b, Bits c) => Bits (GenericFixedPoint a b c) 

newtype BigInt a Source

A type constructor for building 2^n bit signed ints. BigInt is normally just used as a wrapper around BigWord since twos-complement arithmatic is the same, we simply need to provide alternate show, read, and comparison operations.




unBI :: a


(Bounded a, Ord a, Bits a) => Bounded (BigInt a) 
(Bits a, Ord a, Integral a, Bounded a, Num a) => Enum (BigInt a) 
Eq a => Eq (BigInt a) 
(Integral a, Bits a, Bounded a) => Integral (BigInt a) 
(Num a, Bits a, Ord a) => Num (BigInt a) 
(Ord a, Bits a) => Ord (BigInt a) 
(Num a, Bits a, Ord a) => Read (BigInt a) 
(Real a, Bounded a, Integral a, Bits a) => Real (BigInt a) 
(Show a, Num a, Bits a, Ord a) => Show (BigInt a) 
(Bits a, Ord a) => Bits (BigInt a) 

data BigWord a Source

A type constuctor allowing construction of 2^n bit unsigned words The type variable represents half the underlying representation, so type Foo = BigWord Word13 would have a bit size of 26 (2*13).


BigWord a a 


Bounded a => Bounded (BigWord a) 
(Bounded a, Eq a, Num a, Enum a) => Enum (BigWord a) 
Ord a => Eq (BigWord a) 
(Bounded a, Integral a, Bits a) => Integral (BigWord a) 
(Bits a, Num a, Ord a) => Num (BigWord a) 
Ord a => Ord (BigWord a) 
(Num a, Bits a, Ord a) => Read (BigWord a) 
(Bits a, Real a, Bounded a, Integral a) => Real (BigWord a) 
(Bounded a, Bits a, Integral a) => Show (BigWord a) 
(Ord a, Bits a) => Bits (BigWord a)