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-neg
posit16-neg
posit32-neg
posit8=
posit8<
posit8<=
posit8>
posit8>=
posit16-round-to-int
posit16-add
posit16-sub
posit16-mul
posit16-div
posit16mul  Add
posit16-sqrt
posit16=
posit16<
posit16<=
posit16>
posit16>=
posit32-round-to-int
posit32-add
posit32-sub
posit32-mul
posit32-div
posit32mul  Add
posit32-sqrt
posit32=
posit32<
posit32<=
posit32>
posit32>=
2.7 Quires
2.8 Quire Types
_  quire8
_  quire16
_  quire32
quire8?
quire16?
quire32?
2.9 Creating Quires
create-quire8
create-quire16
create-quire32
posit8->quire8
posit16->quire16
posit32->quire32
double>quire8
double>quire16
double>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 Values from Quires
quire8->posit8
quire16->posit16
quire32->posit32
quire8->double
quire16->double
quire32->double
2.12 Generating Random Posits and Quires
random-posit8
random-posit16
random-posit32
random-posit64
random-posit128
random-quire8
random-quire16
random-quire32
2.13 Posit Constants
posit8-nar
posit16-nar
posit32-nar
posit64-nar
posit128-nar
2.14 Posit Ordinal Functions
p8->ordinal
p16->ordinal
p32->ordinal
ordinal->p8>ordinal
ordinal->p16>ordinal
ordinal->p32>ordinal
8.12

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-neg p1)  posit8?

  p1 : posit8?
Negates p1.

procedure

(posit16-neg p1)  posit16?

  p1 : posit16?
Negates p1.

procedure

(posit32-neg p1)  posit32?

  p1 : posit32?
Negates p1.

procedure

(posit8= p1 p2)  boolean?

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

procedure

(posit8< p1 p2)  boolean?

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

procedure

(posit8<= p1 p2)  boolean?

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

procedure

(posit8> p1 p2)  boolean?

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

procedure

(posit8>= p1 p2)  boolean?

  p1 : posit8?
  p2 : posit8?
Returns #t if p1 is greater 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= p1 p2)  boolean?

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

procedure

(posit16< p1 p2)  boolean?

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

procedure

(posit16<= p1 p2)  boolean?

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

procedure

(posit16> p1 p2)  boolean?

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

procedure

(posit16>= p1 p2)  boolean?

  p1 : posit16?
  p2 : posit16?
Returns #t if p1 is greater 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= p1 p2)  boolean?

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

procedure

(posit32< p1 p2)  boolean?

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

procedure

(posit32<= p1 p2)  boolean?

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

procedure

(posit32> p1 p2)  boolean?

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

procedure

(posit32>= p1 p2)  boolean?

  p1 : posit32?
  p2 : posit32?
Returns #t if p1 is greater 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.

procedure

(posit8->quire8 p)  quire8?

  p : posit8?
Creates a quire8 with the value of q.

procedure

(posit16->quire16 p)  quire16?

  p : posit16?
Creates a quire16 with the value of q.

procedure

(posit32->quire32 p)  quire32?

  p : posit32?
Creates a quire32 with the value of q.

procedure

(double>quire8 n)  quire8?

  n : real?
Creates a quire8 with the value of n after n is converted to a posit8.

procedure

(double>quire16 n)  quire16?

  n : real?
Creates a quire16 with the value of n after n is converted to a posit16.

procedure

(double>quire32 n)  quire32?

  n : real?
Creates a quire32 with the value of n after n is converted to a posit32.

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 Values 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.

procedure

(quire8->double q)  real?

  q : quire8?
Casts q to a double.

procedure

(quire16->double q)  real?

  q : quire16?
Casts q to a double.

procedure

(quire32->double q)  real?

  q : quire32?
Casts q to a double.

2.12 Generating Random Posits and Quires🔗ℹ

Also included is functionality to generate random posits and quires.

procedure

(random-posit8)  posit8?

Generates a random posit8.

procedure

(random-posit16)  posit16?

Generates a random posit16.

procedure

(random-posit32)  posit32?

Generates a random posit32.

procedure

(random-posit64)  posit64?

Generates a random posit64.

procedure

(random-posit128)  posit128?

Generates a random posit128.

procedure

(random-quire8)  quire8?

Generates a random quire8.

procedure

(random-quire16)  quire16?

Generates a random quire16.

procedure

(random-quire32)  quire32?

Generates a random quire32.

2.13 Posit Constants🔗ℹ

procedure

(posit8-nar)  posit8?

Returns the "not a real" value for posit8s.

procedure

(posit16-nar)  posit16?

Returns the "not a real" value for posit16s.

procedure

(posit32-nar)  posit32?

Returns the "not a real" value for posit32s.

procedure

(posit64-nar)  posit64?

Returns the "not a real" value for posit64s.

procedure

(posit128-nar)  posit128?

Returns the "not a real" value for posit128s.

2.14 Posit Ordinal Functions🔗ℹ

These functions convert to and from the ordinal representation of a posit. NaR returns 0.

procedure

(p8->ordinal p)  natural?

  p : posit8?
Returns the ordinal of p.

procedure

(p16->ordinal p)  natural?

  p : posit16?
Returns the ordinal of p.

procedure

(p32->ordinal p)  natural?

  p : posit32?
Returns the ordinal of p.

procedure

(ordinal->p8>ordinal n)  posit8?

  n : natural?
Returns the posit8 of the ordinal n.

procedure

(ordinal->p16>ordinal n)  posit16?

  n : natural?
Returns the posit16 of the ordinal n.

procedure

(ordinal->p32>ordinal n)  posit32?

  n : natural?
Returns the posit32 of the ordinal n.