libwebsockets
Lightweight C library for HTML5 websockets
|
Lws provides reasonably fast fixed-point 32:32 arithmetic functions so code can be designed to work without floating-point support.
The underlying type is
Either or both of whole or frac may be negative, indicating that the combined scalar is negative. This is to deal with numbers less than 0 but greater than -1 not being able to use whole to indicating signedness, since it's zero. This scheme allows .whole to be used as a signed int32_t
naturally.
The fractional part counts parts per 100M and is restricted to the range 0 .. 99999999. For convenience a constant LWS_FX_FRACTION_MSD
is defined with the value 100M.
It's possible to declare constants naturally, but leading zeroes are not valid on the fractional part, since C parses a leading 0 as indicating the number is octal.
For the case of negative values less than 1, the fractional part bears the sign.
Eg to declare 12.5, 6.0, -6.0, 0.1 and -0.1
There are some helpers
Helper | Function |
---|---|
lws_neg(a) | nonzero if a is negative in whole or fractional part |
lws_fx_set(a,w,f) | Convenience to set lws_fx_t a in code, notices if w is negative and also marks f the same |
The APIs are given the storage for the result along with the const args. The result pointer is also returned from the operation to make operation chaining more natural.
div and sqrt operations are iterative, up to 64 loops. Multiply is relatively cheap since it devolves to four integer multiply-adds. Add and Sub are trivially cheap.