|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdio.h> |
| 8 | + |
| 9 | +#include <sfloat.h> |
| 10 | + |
| 11 | +#include <zephyr/sys/byteorder.h> |
| 12 | + |
| 13 | +/* Maximum mantissa value: 2047. */ |
| 14 | +#define SFLOAT_MANTISSA_MAX ((1 << 11) - 1) |
| 15 | +/* Minimum mantissa value: -2048. */ |
| 16 | +#define SFLOAT_MANTISSA_MIN (-(1 << 11)) |
| 17 | +/* Mantissa bit position in the SFLOAT encoding. */ |
| 18 | +#define SFLOAT_MANTISSA_BIT_POS (0) |
| 19 | +/* Mantissa mask in the SFLOAT encoding. */ |
| 20 | +#define SFLOAT_MANTISSA_MASK (0x0FFF) |
| 21 | + |
| 22 | +/* Minimum exponent value. */ |
| 23 | +#define SFLOAT_EXP_MIN (-8) |
| 24 | +/* Maximum exponent value. */ |
| 25 | +#define SFLOAT_EXP_MAX (7) |
| 26 | +/* Maximum exponentiation (exponent function) value: 10^7. */ |
| 27 | +#define SFLOAT_EXP_FUNC_MAX (10000000) |
| 28 | +/* Exponent bit position in the SFLOAT encoding. */ |
| 29 | +#define SFLOAT_EXP_BIT_POS (12) |
| 30 | +/* Exponent mask in the SFLOAT encoding. */ |
| 31 | +#define SFLOAT_EXP_MASK (0xF000) |
| 32 | + |
| 33 | +/* Size in bits of the float sign encoding. */ |
| 34 | +#define FLOAT_SIGN_BIT_SIZE 1 |
| 35 | +/* Size in bits of the float exponent encoding. */ |
| 36 | +#define FLOAT_EXP_BIT_SIZE 8 |
| 37 | +/* Size in bits of the float mantissa encoding. */ |
| 38 | +#define FLOAT_MANTISSA_BIT_SIZE 23 |
| 39 | + |
| 40 | +/* Absolute minimum float resolution. */ |
| 41 | +#define FLOAT_ABS_MIN (0.000001f) |
| 42 | +/* Minimum negative float resolution. */ |
| 43 | +#define FLOAT_NEG_MIN (SFLOAT_EXP_FUNC_MAX * 1.0f * SFLOAT_MANTISSA_MIN) |
| 44 | +/* Maximum positive float resolution. */ |
| 45 | +#define FLOAT_POS_MAX (SFLOAT_EXP_FUNC_MAX * 1.0f * SFLOAT_MANTISSA_MAX) |
| 46 | + |
| 47 | +/* Helper macro for getting absolute value. */ |
| 48 | +#define ABS(a) ((a < 0) ? (-a) : (a)) |
| 49 | + |
| 50 | +/* Float type should use binary32 notation from the IEEE 754-2008 specification. */ |
| 51 | +BUILD_ASSERT(sizeof(float) == sizeof(uint32_t)); |
| 52 | + |
| 53 | +/* SFLOAT descriptor. */ |
| 54 | +struct sfloat_desc { |
| 55 | + uint8_t exponent; |
| 56 | + uint16_t mantissa; |
| 57 | +}; |
| 58 | + |
| 59 | +/* FLOAT encoding descriptor. */ |
| 60 | +union float_enc { |
| 61 | + uint32_t val; |
| 62 | + struct { |
| 63 | + uint32_t mantissa: FLOAT_MANTISSA_BIT_SIZE; |
| 64 | + uint32_t exp: FLOAT_EXP_BIT_SIZE; |
| 65 | + uint32_t sign: FLOAT_SIGN_BIT_SIZE; |
| 66 | + }; |
| 67 | +}; |
| 68 | + |
| 69 | +static struct sfloat_desc sfloat_desc_from_float(float float_num) |
| 70 | +{ |
| 71 | + struct sfloat_desc sfloat = {0}; |
| 72 | + union float_enc float_enc; |
| 73 | + uint16_t mantissa_max; |
| 74 | + uint16_t mantissa; |
| 75 | + float float_abs; |
| 76 | + bool inc_exp; |
| 77 | + int8_t exp = 0; |
| 78 | + |
| 79 | + float_enc.val = sys_get_le32((uint8_t *) &float_num); |
| 80 | + |
| 81 | + /* Handle zero float values. */ |
| 82 | + if ((float_enc.exp == 0) && (float_enc.mantissa == 0)) { |
| 83 | + return sfloat; |
| 84 | + } |
| 85 | + |
| 86 | + /* Handle special float values. */ |
| 87 | + if (float_enc.exp == UINT8_MAX) { |
| 88 | + if (float_enc.mantissa == 0) { |
| 89 | + sfloat.mantissa = float_enc.sign ? |
| 90 | + SFLOAT_NEG_INFINITY : SFLOAT_POS_INFINITY; |
| 91 | + } else { |
| 92 | + sfloat.mantissa = SFLOAT_NAN; |
| 93 | + } |
| 94 | + |
| 95 | + return sfloat; |
| 96 | + } |
| 97 | + |
| 98 | + /* Verify if the SFLOAT has a proper resolution for the conversion. */ |
| 99 | + float_abs = float_enc.sign ? (-float_num) : float_num; |
| 100 | + if ((float_abs < FLOAT_ABS_MIN) || |
| 101 | + (float_num < FLOAT_NEG_MIN) || |
| 102 | + (float_num > FLOAT_POS_MAX)) { |
| 103 | + sfloat.mantissa = SFLOAT_NRES; |
| 104 | + |
| 105 | + return sfloat; |
| 106 | + } |
| 107 | + |
| 108 | + /* Find mantissa and exponent for the SFLOAT type. */ |
| 109 | + mantissa_max = float_enc.sign ? ABS(SFLOAT_MANTISSA_MIN) : SFLOAT_MANTISSA_MAX; |
| 110 | + inc_exp = float_abs > mantissa_max; |
| 111 | + while (exp > SFLOAT_EXP_MIN && exp < SFLOAT_EXP_MAX) { |
| 112 | + if (inc_exp) { |
| 113 | + if (float_abs <= mantissa_max) { |
| 114 | + break; |
| 115 | + } |
| 116 | + |
| 117 | + float_abs /= 10; |
| 118 | + exp++; |
| 119 | + } else { |
| 120 | + if ((float_abs * 10) > mantissa_max) { |
| 121 | + break; |
| 122 | + } |
| 123 | + |
| 124 | + float_abs *= 10; |
| 125 | + exp--; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /* Round up the mantisssa. */ |
| 130 | + mantissa = (uint16_t) float_abs; |
| 131 | + if (((float_abs - mantissa) * 10 >= 5) && (mantissa + 1 <= mantissa_max)) { |
| 132 | + mantissa++; |
| 133 | + } |
| 134 | + |
| 135 | + /* Encode mantissa and exponent in the two's-complement form. */ |
| 136 | + if (exp >= 0) { |
| 137 | + sfloat.exponent = ((uint8_t) exp) & 0x0F; |
| 138 | + } else { |
| 139 | + sfloat.exponent = ((uint8_t) -exp) & 0x0F; |
| 140 | + sfloat.exponent = (~sfloat.exponent & 0x0F) + 1; |
| 141 | + } |
| 142 | + |
| 143 | + sfloat.mantissa = mantissa & SFLOAT_MANTISSA_MASK; |
| 144 | + if (float_enc.sign) { |
| 145 | + sfloat.mantissa = (~sfloat.mantissa & SFLOAT_MANTISSA_MASK) + 1; |
| 146 | + } |
| 147 | + |
| 148 | + return sfloat; |
| 149 | +} |
| 150 | + |
| 151 | +static struct sfloat sfloat_encode(const struct sfloat_desc *sfloat_desc) |
| 152 | +{ |
| 153 | + struct sfloat sfloat; |
| 154 | + |
| 155 | + sfloat.val = ((sfloat_desc->exponent << SFLOAT_EXP_BIT_POS) & SFLOAT_EXP_MASK); |
| 156 | + sfloat.val |= ((sfloat_desc->mantissa << SFLOAT_MANTISSA_BIT_POS) & SFLOAT_MANTISSA_MASK); |
| 157 | + |
| 158 | + return sfloat; |
| 159 | +} |
| 160 | + |
| 161 | +struct sfloat sfloat_from_float(float float_num) |
| 162 | +{ |
| 163 | + struct sfloat sfloat; |
| 164 | + struct sfloat_desc sfloat_desc; |
| 165 | + |
| 166 | + sfloat_desc = sfloat_desc_from_float(float_num); |
| 167 | + sfloat = sfloat_encode(&sfloat_desc); |
| 168 | + |
| 169 | + return sfloat; |
| 170 | +} |
0 commit comments