ieee_754
The ieee_754 library is a support package for parsing and generating
IEEE 754 floating-point encodings shared by binary interchange libraries
such as message_pack, avro, protobuf, cbor, and
wkt_wkb.
Loading
To load all entities in this library, load the loader.lgt file:
| ?- logtalk_load(ieee_754(loader)).
Testing
To test this library predicates, load the tester.lgt file:
| ?- logtalk_load(ieee_754(tester)).
Object model
The library provides two parameterized implementation objects:
`ieee_754(Precision, ByteOrder, NaNRepresentation)`
ieee_754_fields(Precision, ByteOrder)
with the following parameter values:
Precision:half,single, ordoubleByteOrder:bigorlittleNaNRepresentation:canonicalorpayloads
The parameterization keeps format-specific policy out of the shared
core. Advanced callers can use ieee_754_fields to inspect exact
sign, exponent, mantissa, finite binary-rational decompositions, and NaN
payload bits without going through backend float values.
Value representation
The intended term representation is:
finite values: backend Prolog floats
positive infinity:
@infinitynegative infinity:
@negative_infinitycanonical NaN:
@not_a_numberpayload-preserving NaN values:
not_a_number(Bytes)
The not_a_number(Bytes) representation is only intended to be
accepted and produced by objects configured with
NaNRepresentation = payloads.
Under the payloads policy, decoding a canonical quiet NaN should
still yield @not_a_number. Non-canonical NaN encodings should decode
to not_a_number(Bytes) using canonical byte order for the selected
precision.
The high-level ieee_754/3 object API deliberately abstracts away the
IEEE 754 quiet-versus-signaling NaN distinction. In canonical mode,
all NaN encodings decode to @not_a_number. In payloads mode, the
designated canonical quiet NaN encoding decodes to @not_a_number
while all other NaN encodings decode to not_a_number(Bytes) for
roundtrip preservation. Callers that need exact NaN inspection should
use ieee_754_fields/2 object API.
Public API
The high-level value API is defined in ieee_754_protocol.lgt and
consists of:
parse/2Decodesbytes(Bytes)orbits(Bits)into a value term.generate/2Encodes a value term intobytes(Bytes)orbits(Bits).generate/3Encodes a value term to a byte list with an open tail for difference-list based binary generators.valid/1Checks whether a term is a valid value representation for the selected object.exactly_representable/1Checks whether a term can be encoded and decoded in the selected precision without loss of value information.precision/1Returns the selected precision.order/1Returns the selected byte order.nan_representation/1Returns the selected NaN representation policy.byte_count/1Returns the number of bytes used by the selected precision.
The low-level exact field API is defined in
ieee_754_fields_protocol.lgt and consists of:
classify/2Classifiesbytes(Bytes)orbits(Bits)aszero,subnormal,normal,infinity, ornot_a_number.fields/5Extracts the exact sign bit, exponent bits, mantissa bits, and class.finite_binary_rational/4Extracts finite encodings as exact(-1)^Sign * Significand * 2^Exponentterms.nan_payload/2Extracts the raw NaN mantissa payload bits.nan_kind/2Classifies a NaN encoding asquietorsignaling.precision/1Returns the selected precision.order/1Returns the selected byte order.byte_count/1Returns the number of bytes used by the selected precision.
Semantics
The intended shared semantics are:
finite encoding rounds according to the selected IEEE 754 precision
exactly_representable/1succeeds only when no value information is lostsigned zero is preserved when supported by the backend Prolog compiler
infinities and NaN values are represented independently from backend float syntax
subnormal values are supported for all selected precisions
payload-preserving NaN decoding is available when explicitly requested
the
ieee_754_fieldsAPI works from exact encodings and therefore does not depend on backend float decomposition behaviorexact quiet/signaling NaN inspection is exposed by
ieee_754_fields/2instead of the high-level value codec API