softposit-rkt
1 Introduction
2 API
2.1 Posits
2.2 Posit Types
_  posit8
_  posit16
_  posit32
_  posit64
_  posit128
posit8?
posit16?
posit32?
posit64?
posit128?
2.3 Creating Posits
uint32->posit8
uint32->posit16
uint32->posit32
uint64->posit8
uint64->posit16
uint64->posit32
int32->posit8
int32->posit16
int32->posit32
int64->posit8
int64->posit16
int64->posit32
double->posit8
double->posit16
double->posit32
2.4 Converting Between Posit Types
posit8->posit16
posit8->posit32
posit16->posit8
posit16->posit32
posit32->posit8
posit32->posit16
2.5 Converting Back from Posits
posit8->uint32
posit16->uint32
posit32->uint32
posit8->uint64
posit16->uint64
posit32->uint64
posit8->int32
posit16->int32
posit32->int32
posit8->int64
posit16->int64
posit32->int64
posit8->double
posit16->double
posit32->double
2.6 Posit Operations
posit8-round-to-int
posit8-add
posit8-sub
posit8-mul
posit8-div
posit8mul  Add
posit8-sqrt
posit8-eq?
posit8-lt?
posit8-le?
posit16-round-to-int
posit16-add
posit16-sub
posit16-mul
posit16-div
posit16mul  Add
posit16-sqrt
posit16-eq?
posit16-lt?
posit16-le?
posit32-round-to-int
posit32-add
posit32-sub
posit32-mul
posit32-div
posit32mul  Add
posit32-sqrt
posit32-eq?
posit32-lt?
posit32-le?
2.7 Quires
2.8 Quire Types
_  quire8
_  quire16
_  quire32
quire8?
quire16?
quire32?
2.9 Creating Quires
create-quire8
create-quire16
create-quire32
2.10 Quire Operations
quire8-fdp-add
quire8-fdp-sub
quire16-fdp-add
quire16-fdp-sub
quire16-twos-complement
quire32-fdp-add
quire32-fdp-sub
quire32-twos-complement
2.11 Retrieving Posits from Quires
quire8->posit8
quire16->posit16
quire32->posit32
7.0

softposit-rkt

David Thien

1 Introduction

This package is a set of racket bindings for the SoftPosit library. More information on posits can be found on Posithub. Posits are an alternative to floating point numbers designed to improve performance and accuracy.

2 API

 (require softposit-rkt) package: softposit-rkt

2.1 Posits

Posits themselves are a representation of the projective reals. The different sizes of posits are included this package are _posit8 _posit16 _posit32 _posit64 and _posit128. Note thought that only the 8, 16, and 32 bit posits currently have arithmetic operators implemented.

The posit types in this package are bindings for c types. Note that only the types and type predicates are bound. All other operations with posits should be accessed with the other provided functions.

2.2 Posit Types

value

_posit8 : ctype?

An 8-bit posit.

value

_posit16 : ctype?

A 16-bit posit.

value

_posit32 : ctype?

A 32-bit posit.

value

_posit64 : ctype?

A 64-bit posit.

value

_posit128 : ctype?

A 128-bit posit.

procedure

(posit8? v)  boolean?

  v : any/c
Returns #t if v is a _posit8, #f otherwise.

procedure

(posit16? v)  boolean?

  v : any/c
Returns #t if v is a _posit16, #f otherwise.

procedure

(posit32? v)  boolean?

  v : any/c
Returns #t if v is a _posit32, #f otherwise.

procedure

(posit64? v)  boolean?

  v : any/c
Returns #t if v is a _posit64, #f otherwise.

procedure

(posit128? v)  boolean?

  v : any/c
Returns #t if v is a _posit128, #f otherwise.

2.3 Creating Posits

procedure

(uint32->posit8 n)  posit8?

  n : natural?
Creates a new _posit8 that is the closest representable value to n.

procedure

(uint32->posit16 n)  posit16?

  n : natural?
Creates a new _posit16 that is the closest representable value to n.

procedure

(uint32->posit32 n)  posit32?

  n : natural?
Creates a new _posit32 that is the closest representable value to n.

procedure

(uint64->posit8 n)  posit8?

  n : natural?
Creates a new _posit8 that is the closest representable value to n.

procedure

(uint64->posit16 n)  posit16?

  n : natural?
Creates a new _posit16 that is the closest representable value to n.

procedure

(uint64->posit32 n)  posit32?

  n : natural?
Creates a new _posit32 that is the closest representable value to n.

procedure

(int32->posit8 n)  posit8?

  n : integer?
Creates a new _posit8 that is the closest representable value to n.

procedure

(int32->posit16 n)  posit16?

  n : integer?
Creates a new _posit16 that is the closest representable value to n.

procedure

(int32->posit32 n)  posit32?

  n : integer?
Creates a new _posit32 that is the closest representable value to n.

procedure

(int64->posit8 n)  posit8?

  n : integer?
Creates a new _posit8 that is the closest representable value to n.

procedure

(int64->posit16 n)  posit16?

  n : integer?
Creates a new _posit16 that is the closest representable value to n.

procedure

(int64->posit32 n)  posit32?

  n : integer?
Creates a new _posit32 that is the closest representable value to n.

procedure

(double->posit8 n)  posit8?

  n : real?
Creates a new _posit8 that is the closest representable value to n.

procedure

(double->posit16 n)  posit16?

  n : real?
Creates a new _posit16 that is the closest representable value to n.

procedure

(double->posit32 n)  posit32?

  n : real?
Creates a new _posit32 that is the closest representable value to n.

2.4 Converting Between Posit Types

procedure

(posit8->posit16 p)  posit16?

  p : posit8?
Converts a _posit8 to a _posit16.

procedure

(posit8->posit32 p)  posit32?

  p : posit8?
Converts a _posit8 to a _posit32.

procedure

(posit16->posit8 p)  posit8?

  p : posit16?
Converts a _posit16 to a _posit8.

procedure

(posit16->posit32 p)  posit32?

  p : posit16?
Converts a _posit16 to a _posit32.

procedure

(posit32->posit8 p)  posit8?

  p : posit32?
Converts a _posit32 to a _posit8.

procedure

(posit32->posit16 p)  posit16?

  p : posit32?
Converts a _posit32 to a _posit16.

2.5 Converting Back from Posits

Note that there are multiple bindings exposed which cast back to natural numbers and to integers (e.g. posit16->uint32 and posit16->uint64). Although these will cast back to the same racket type, they are different functions in the underlying C code, so be careful to avoid unexpected behavior when hitting the maximum value for uint32s. It is recommended to always cast to a double to avoid losing decimal accuracy.

procedure

(posit8->uint32 p)  natural?

  p : posit8?
Casts a _posit8 to a uint32.

procedure

(posit16->uint32 p)  natural?

  p : posit16?
Casts a _posit16 to a uint32.

procedure

(posit32->uint32 p)  natural?

  p : posit32?
Casts a _posit32 to a uint32.

procedure

(posit8->uint64 p)  natural?

  p : posit8?
Casts a _posit8 to a uint64.

procedure

(posit16->uint64 p)  natural?

  p : posit16?
Casts a _posit16 to a uint64.

procedure

(posit32->uint64 p)  natural?

  p : posit32?
Casts a _posit32 to a uint64.

procedure

(posit8->int32 p)  integer?

  p : posit8?
Casts a _posit8 to a int32.

procedure

(posit16->int32 p)  integer?

  p : posit16?
Casts a _posit16 to a int32.

procedure

(posit32->int32 p)  integer?

  p : posit32?
Casts a _posit32 to a int32.

procedure

(posit8->int64 p)  integer?

  p : posit8?
Casts a _posit8 to a int64.

procedure

(posit16->int64 p)  integer?

  p : posit16?
Casts a _posit16 to a int64.

procedure

(posit32->int64 p)  integer?

  p : posit32?
Casts a _posit32 to a int64.

procedure

(posit8->double p)  real?

  p : posit8?
Casts a _posit8 to a double.

procedure

(posit16->double p)  real?

  p : posit16?
Casts a _posit16 to a int64.

procedure

(posit32->double p)  real?

  p : posit32?
Casts a _posit32 to a int64.

2.6 Posit Operations

procedure

(posit8-round-to-int p)  posit8?

  p : posit8?
Rounds p to the nearest integer.

procedure

(posit8-add p1 p2)  posit8?

  p1 : posit8?
  p2 : posit8?
Adds p1 and p2.

procedure

(posit8-sub p1 p2)  posit8?

  p1 : posit8?
  p2 : posit8?
Subtracts p2 from p1.

procedure

(posit8-mul p1 p2)  posit8?

  p1 : posit8?
  p2 : posit8?
Multiplies p1 and p2.

procedure

(posit8-div p1 p2)  posit8?

  p1 : posit8?
  p2 : posit8?
Divides p1 by p2.

procedure

(posit8mulAdd p1 p2 p3)  posit8?

  p1 : posit8?
  p2 : posit8?
  p3 : posit8?
Performs the operation (p1 * p2) + p3 (similar to the fma operator).

procedure

(posit8-sqrt p1)  posit8?

  p1 : posit8?
Takes the square root of p1.

procedure

(posit8-eq? p1 p2)  boolean?

  p1 : posit8?
  p2 : posit8?
Returns #t if p1 and p2 are equal.

procedure

(posit8-lt? p1 p2)  posit8?

  p1 : posit8?
  p2 : posit8?
Returns #t if p1 is strictly less than p2.

procedure

(posit8-le? p1 p2)  posit8?

  p1 : posit8?
  p2 : posit8?
Returns #t if p1 is less than or equal to p2.

procedure

(posit16-round-to-int p)  posit16?

  p : posit16?
Rounds p to the nearest integer.

procedure

(posit16-add p1 p2)  posit16?

  p1 : posit16?
  p2 : posit16?
Adds p1 and p2.

procedure

(posit16-sub p1 p2)  posit16?

  p1 : posit16?
  p2 : posit16?
Subtracts p2 from p1.

procedure

(posit16-mul p1 p2)  posit16?

  p1 : posit16?
  p2 : posit16?
Multiplies p1 and p2.

procedure

(posit16-div p1 p2)  posit16?

  p1 : posit16?
  p2 : posit16?
Divides p1 by p2.

procedure

(posit16mulAdd p1 p2 p3)  posit16?

  p1 : posit16?
  p2 : posit16?
  p3 : posit16?
Performs the operation (p1 * p2) + p3 (similar to the fma operator).

procedure

(posit16-sqrt p1)  posit16?

  p1 : posit16?
Takes the square root of p1.

procedure

(posit16-eq? p1 p2)  boolean?

  p1 : posit16?
  p2 : posit16?
Returns #t if p1 and p2 are equal.

procedure

(posit16-lt? p1 p2)  posit16?

  p1 : posit16?
  p2 : posit16?
Returns #t if p1 is strictly less than p2.

procedure

(posit16-le? p1 p2)  posit16?

  p1 : posit16?
  p2 : posit16?
Returns #t if p1 is less than or equal to p2.

procedure

(posit32-round-to-int p)  posit32?

  p : posit32?
Rounds p to the nearest integer.

procedure

(posit32-add p1 p2)  posit32?

  p1 : posit32?
  p2 : posit32?
Adds p1 and p2.

procedure

(posit32-sub p1 p2)  posit32?

  p1 : posit32?
  p2 : posit32?
Subtracts p2 from p1.

procedure

(posit32-mul p1 p2)  posit32?

  p1 : posit32?
  p2 : posit32?
Multiplies p1 and p2.

procedure

(posit32-div p1 p2)  posit32?

  p1 : posit32?
  p2 : posit32?
Divides p1 by p2.

procedure

(posit32mulAdd p1 p2 p3)  posit32?

  p1 : posit32?
  p2 : posit32?
  p3 : posit32?
Performs the operation (p1 * p2) + p3 (similar to the fma operator).

procedure

(posit32-sqrt p1)  posit32?

  p1 : posit32?
Takes the square root of p1.

procedure

(posit32-eq? p1 p2)  boolean?

  p1 : posit32?
  p2 : posit32?
Returns #t if p1 and p2 are equal.

procedure

(posit32-lt? p1 p2)  posit32?

  p1 : posit32?
  p2 : posit32?
Returns #t if p1 is strictly less than p2.

procedure

(posit32-le? p1 p2)  posit32?

  p1 : posit32?
  p2 : posit32?
Returns #t if p1 is less than or equal to p2.

2.7 Quires

Quires are another feature of the Posit specication. They can be thought of as a large accumulator designed to hold enough bits of information so as to not introduce any rounding error. Note that these accumulators have a fixed number of bits, so only a limited (but very large) number of accumulations can be done on them safely. A _quire8 has 4 times the number of bits as a _posit8, a _quire16 has 8 times the bits as a _posit16, and a _quire32 has 16 times the bits as a _posit32.

2.8 Quire Types

value

_quire8 : ctype?

An 8-bit quire.

value

_quire16 : ctype?

An 16-bit quire.

value

_quire32 : ctype?

An 32-bit quire.

procedure

(quire8? v)  boolean?

  v : any/c
Returns #t if v is a _quire8, #f otherwise.

procedure

(quire16? v)  boolean?

  v : any/c
Returns #t if v is a _quire16, #f otherwise.

procedure

(quire32? v)  boolean?

  v : any/c
Returns #t if v is a _quire32, #f otherwise.

2.9 Creating Quires

All quires are initialized to 0.

procedure

(create-quire8)  quire8?

Creates a new _quire8.

procedure

(create-quire16)  quire16?

Creates a new _quire16.

procedure

(create-quire32)  quire32?

Creates a new _quire32.

2.10 Quire Operations

Note that there is currently no twos-complement for _quire8s.

procedure

(quire8-fdp-add q p1 p2)  quire8?

  q : quire8?
  p1 : posit8?
  p2 : posit8?
Returns the result of adding p1 times p2 to q.

procedure

(quire8-fdp-sub q p1 p2)  quire8?

  q : quire8?
  p1 : posit8?
  p2 : posit8?
Returns the result of subtracting p1 times p2 from q.

procedure

(quire16-fdp-add q p1 p2)  quire16?

  q : quire16?
  p1 : posit16?
  p2 : posit16?
Returns the result of adding p1 times p2 to q.

procedure

(quire16-fdp-sub q p1 p2)  quire16?

  q : quire16?
  p1 : posit16?
  p2 : posit16?
Returns the result of subtracting p1 times p2 from q.

procedure

(quire16-twos-complement q)  quire16?

  q : quire16?
Returns the twos complement of q.

procedure

(quire32-fdp-add q p1 p2)  quire32?

  q : quire32?
  p1 : posit32?
  p2 : posit32?
Returns the result of adding p1 times p2 to q.

procedure

(quire32-fdp-sub q p1 p2)  quire32?

  q : quire32?
  p1 : posit32?
  p2 : posit32?
Returns the result of subtracting p1 times p2 from q.

procedure

(quire32-twos-complement q)  quire32?

  q : quire32?
Returns the twos complement of q.

2.11 Retrieving Posits from Quires

procedure

(quire8->posit8 q)  posit8?

  q : quire8?
Casts q to a _posit8.

procedure

(quire16->posit16 q)  posit16?

  q : quire16?
Casts q to a _posit16.

procedure

(quire32->posit32 q)  posit32?

  q : quire32?
Casts q to a _posit32.