|
| 1 | +## Documentation for libdfloat |
| 2 | + |
| 3 | +### Data Types: |
| 4 | + |
| 5 | +libdfloat defines the following data types: |
| 6 | + |
| 7 | +`dfloat16_t`: 16-bit decimal float with 8-bit mantissa and exponent |
| 8 | + |
| 9 | +`dfloat32_t`: 32-bit decimal float with 16-bit mantissa and exponent |
| 10 | + |
| 11 | +`dfloat64_t`: 64-bit decimal float with 32-bit mantissa and exponent |
| 12 | + |
| 13 | +`dfloat128_t`: 128-bit decimal float with 64-bit mantissa and exponent |
| 14 | + |
| 15 | +The proper way to declare a `dfloat` is to specify a decimal literal in |
| 16 | +string format and convert it to a `dfloat` using one of the `dfloatN_atof()` |
| 17 | +functions. |
| 18 | + |
| 19 | +--------------------------------------------------------------------------- |
| 20 | + |
| 21 | +### Standard Functions: |
| 22 | + |
| 23 | +In the following function definitions, the capital letters *M* and *N* stand |
| 24 | +for integers, either 16, 32, 64, or 128. These numbers can be substituted |
| 25 | +as desired, as there are macros that generate the corresponding functions. |
| 26 | + |
| 27 | +(Example: `dfloat64_add()`, no spaces) |
| 28 | + |
| 29 | +libdfloat defines the following groups of functions: |
| 30 | + |
| 31 | +`void dfloatN_add( dfloatN_t *dst, dfloatN_t *src )` |
| 32 | + |
| 33 | +Add *N*-bit `src` and `dst` operands together and store the result in `dst`. |
| 34 | + |
| 35 | +`void dfloatN_sub( dfloatN_t *dst, dfloatN_t *src )` |
| 36 | + |
| 37 | +Subtract *N*-bit `src` from *N*-bit `dst` and store the result in `dst`. |
| 38 | + |
| 39 | +`void dfloatN_mul( dfloatN_t *dst, dfloatN_t *src )` |
| 40 | + |
| 41 | +Multiply *N*-bit `src` and `dst` operands together and store the result in `dst`. |
| 42 | + |
| 43 | +`void dfloatN_div( dfloatN_t *dst, dfloatN_t *src, int precision )` |
| 44 | + |
| 45 | +Divide *N*-bit `dst` by `src` and store the result in `src`, with up to `precision` |
| 46 | +digits past the decimal point. |
| 47 | + |
| 48 | +`int dfloatN_cmp( dfloatN_t *df1, dfloatN_t *df2 )` |
| 49 | + |
| 50 | +Compares *N*-bit operands `df1` and `df2`. |
| 51 | + |
| 52 | +Returns: |
| 53 | + |
| 54 | +`1` if `df1 > df2` |
| 55 | + |
| 56 | +`-1` if `df1 < df2` |
| 57 | + |
| 58 | +`0` if `df1 == df2` |
| 59 | + |
| 60 | +`dfloatN_t *dfloatN_atof( char *str )` |
| 61 | + |
| 62 | +Reads an *N*-bit `dfloat` from input string `str`. |
| 63 | + |
| 64 | +Input string must be in the following format (BNF): |
| 65 | + |
| 66 | +`[-]<digit><digit>*[.<digit><digits>*]` |
| 67 | + |
| 68 | +`char *dfloatN_ftoa( dfloatN_t *df )` |
| 69 | + |
| 70 | +Generates a string representation of *N*-bit `dfloat` value `df`. |
| 71 | + |
| 72 | +`void dfloatN_cpy( dfloatN_t *dst, dfloatN_t *src )` |
| 73 | + |
| 74 | +Copies the mantissa and exponent from `src` to `dst`. |
| 75 | + |
| 76 | +`dfloatN_t *dfloatM_castN( dfloatM *src )` |
| 77 | + |
| 78 | +Takes a `dfloat` of size `M` and typecasts it, returning a `dfloat` of size `N`. |
| 79 | + |
| 80 | +Cast functions where `M == N` have not been implemented as the author |
| 81 | +saw no need for such functions. |
| 82 | + |
| 83 | +*Note: Overflow errors may occur if the result of an operation is too |
| 84 | +large to fit into the designated space. These will not be reported by |
| 85 | +the compiler and will simply result in incorrect values. It is up to |
| 86 | +the programmer to account for these overflows.* |
| 87 | + |
| 88 | +--------------------------------------------------------------------------- |
| 89 | + |
| 90 | +### Free Functions: |
| 91 | + |
| 92 | +As of Version 0.2, libdfloat has versions of some of the above functions |
| 93 | +that free their source operands, allowing the user to build more complex |
| 94 | +expressions using these functions without having to worry about lost |
| 95 | +objects accumulating. These "free versions" are as follows: |
| 96 | + |
| 97 | +`dfloatN_t *dfloatN_addf( dfloatN_t *arg1, dfloatN_t *arg2 )` |
| 98 | + |
| 99 | +Adds `arg1` and `arg2` and returns the result |
| 100 | + |
| 101 | +`dfloatN_t *dfloatN_subf( dfloatN_t *arg1, dfloatN_t *arg2 )` |
| 102 | + |
| 103 | +Subtracts `arg2` from `arg1` and returns the result |
| 104 | + |
| 105 | +`dfloatN_t *dfloatN_mulf( dfloatN_t *arg1, dfloatN_t *arg2 )` |
| 106 | + |
| 107 | +Multiplies `arg1` by `arg2` and returns the result |
| 108 | + |
| 109 | +`dfloatN_t *dfloatN_divf( dfloatN_t *arg1, dfloatN_t *arg2, int precision )` |
| 110 | + |
| 111 | +Divides `arg1` by `arg2` with the given precision and returns the result |
| 112 | + |
| 113 | +`int dfloatN_cmpf( dfloatN_t *arg1, dfloatN_t *arg2 )` |
| 114 | + |
| 115 | +Like `dfloatN_cmp()`, but frees the source operands |
| 116 | + |
| 117 | +`char *dfloatN_ftoaf( dfloat_t *src )` |
| 118 | + |
| 119 | +Like `dfloatN_ftoa()`, but frees the source operand |
| 120 | + |
| 121 | +`dfloatN_t *dfloatM_castNf( dfloatM_t *src )` |
| 122 | + |
| 123 | +Like `dfloatM_castN()`, but frees the source operand |
| 124 | + |
| 125 | +*Note: Because these functions free their operands, they should not be |
| 126 | +used on variables. They should only be used on immediate operands, |
| 127 | +where the output of one function is directly supplied as a parameter to |
| 128 | +another.* |
| 129 | + |
| 130 | +Example: |
| 131 | + |
| 132 | +`dfloat64_t *sum = dfloat64_addf( dfloat64_atof( "1.2" ), dfloat64_atof( "3.4" ) );` |
| 133 | + |
| 134 | +-------------------------------------------------------------------------- |
| 135 | + |
| 136 | +Any questions or problems? Feel free to contact me at the following: |
| 137 | + |
| 138 | +Github: github.com/PsychoCod3r |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | +Submit issues at github.com/PsychoCod3r/libdfloat |
0 commit comments