|
| 1 | +#include <stdbool.h> |
| 2 | +#include <stdint.h> |
| 3 | +#include "platform.h" |
| 4 | +#include "internals.h" |
| 5 | +#include "specialize.h" |
| 6 | +#include "softfloat.h" |
| 7 | + |
| 8 | +struct ui12_f12 |
| 9 | +{ |
| 10 | + |
| 11 | +}; |
| 12 | +float12_t |
| 13 | + |
| 14 | +#define packToF12UI( sign, exp, sig ) (((uint12_t) (sign)<<15) + ((uint12_t) (exp)<<10) + (sig)) |
| 15 | +#define softfloat_commonNaNToF12UI(aPtr) (uint_fast12_t) ((aPtr)->sign<<15 | 0x7E00 | aPtr->v64>>54) |
| 16 | + |
| 17 | + |
| 18 | +int_float12_t |
| 19 | +uint_float12_t |
| 20 | + |
| 21 | +// f8 is typically 1 4 3 |
| 22 | +// f12 is typically 1 5 6 or 1 4 7 |
| 23 | +// f16 is 1 6 9 |
| 24 | +template <int exponentbits, int mantissabits, int mantissamask> |
| 25 | +float12_t f32_to_f12( float32_t a ) |
| 26 | +{ |
| 27 | + union ui32_f32 uA; |
| 28 | + uint_fast32_t uiA; |
| 29 | + bool sign; |
| 30 | + int_fast12_t exp; |
| 31 | + uint_fast32_t frac; |
| 32 | + struct commonNaN commonNaN; |
| 33 | + uint_fast12_t uiZ, frac16; |
| 34 | + union ui12_f12 uZ; |
| 35 | + |
| 36 | + /*------------------------------------------------------------------------ |
| 37 | + *------------------------------------------------------------------------*/ |
| 38 | + uA.f = a; |
| 39 | + uiA = uA.ui; |
| 40 | + sign = signF32UI( uiA ); |
| 41 | + exp = expF32UI( uiA ); |
| 42 | + frac = fracF32UI( uiA ); |
| 43 | + /*------------------------------------------------------------------------ |
| 44 | + *------------------------------------------------------------------------*/ |
| 45 | + if ( exp == 0xFF ) { |
| 46 | + if ( frac ) { |
| 47 | + softfloat_f32UIToCommonNaN( uiA, &commonNaN ); |
| 48 | + uiZ = softfloat_commonNaNToF12UI( &commonNaN ); |
| 49 | + } else { |
| 50 | + uiZ = packToF12UI( sign, 0x1F, 0 ); |
| 51 | + } |
| 52 | + goto uiZ; |
| 53 | + } |
| 54 | + /*------------------------------------------------------------------------ |
| 55 | + *------------------------------------------------------------------------*/ |
| 56 | + frac16 = frac>> mantissabits| ((frac & mantissamask) != 0); |
| 57 | + if ( ! (exp | frac16) ) { |
| 58 | + uiZ = packToF12UI( sign, 0, 0 ); |
| 59 | + goto uiZ; |
| 60 | + } |
| 61 | + /*------------------------------------------------------------------------ |
| 62 | + *------------------------------------------------------------------------*/ |
| 63 | + return softfloat_roundPackToF12( sign, exp - 0x71, frac16 | 0x4000 ); |
| 64 | + uiZ: |
| 65 | + uZ.ui = uiZ; |
| 66 | + return uZ.f; |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +float12_t |
| 73 | + softfloat_roundPackToF12( bool sign, int_fast12_t exp, uint_fast12_t sig ) |
| 74 | +{ |
| 75 | + uint_fast8_t roundingMode; |
| 76 | + bool roundNearEven; |
| 77 | + uint_fast8_t roundIncrement, roundBits; |
| 78 | + bool isTiny; |
| 79 | + uint_fast12_t uiZ; |
| 80 | + union ui12_f12 uZ; |
| 81 | + |
| 82 | + /*------------------------------------------------------------------------ |
| 83 | + *------------------------------------------------------------------------*/ |
| 84 | + roundingMode = softfloat_roundingMode; |
| 85 | + roundNearEven = (roundingMode == softfloat_round_near_even); |
| 86 | + roundIncrement = 0x8; |
| 87 | + if ( ! roundNearEven && (roundingMode != softfloat_round_near_maxMag) ) { |
| 88 | + roundIncrement = |
| 89 | + (roundingMode |
| 90 | + == (sign ? softfloat_round_min : softfloat_round_max)) |
| 91 | + ? 0xF |
| 92 | + : 0; |
| 93 | + } |
| 94 | + roundBits = sig & 0xF; |
| 95 | + /*------------------------------------------------------------------------ |
| 96 | + *------------------------------------------------------------------------*/ |
| 97 | + if ( 0x1D <= (unsigned int) exp ) { |
| 98 | + if ( exp < 0 ) { |
| 99 | + /*---------------------------------------------------------------- |
| 100 | + *----------------------------------------------------------------*/ |
| 101 | + isTiny = |
| 102 | + (softfloat_detectTininess == softfloat_tininess_beforeRounding) |
| 103 | + || (exp < -1) || (sig + roundIncrement < 0x8000); |
| 104 | + sig = softfloat_shiftRightJam32( sig, -exp ); |
| 105 | + exp = 0; |
| 106 | + roundBits = sig & 0xF; |
| 107 | + if ( isTiny && roundBits ) { |
| 108 | + softfloat_raiseFlags( softfloat_flag_underflow ); |
| 109 | + } |
| 110 | + } else if ( (0x1D < exp) || (0x8000 <= sig + roundIncrement) ) { |
| 111 | + /*---------------------------------------------------------------- |
| 112 | + *----------------------------------------------------------------*/ |
| 113 | + softfloat_raiseFlags( |
| 114 | + softfloat_flag_overflow | softfloat_flag_inexact ); |
| 115 | + uiZ = packToF12UI( sign, 0x1F, 0 ) - ! roundIncrement; |
| 116 | + goto uiZ; |
| 117 | + } |
| 118 | + } |
| 119 | + /*------------------------------------------------------------------------ |
| 120 | + *------------------------------------------------------------------------*/ |
| 121 | + sig = (sig + roundIncrement)>>4; |
| 122 | + if ( roundBits ) { |
| 123 | + softfloat_exceptionFlags |= softfloat_flag_inexact; |
| 124 | +#ifdef SOFTFLOAT_ROUND_ODD |
| 125 | + if ( roundingMode == softfloat_round_odd ) { |
| 126 | + sig |= 1; |
| 127 | + goto packReturn; |
| 128 | + } |
| 129 | +#endif |
| 130 | + } |
| 131 | + sig &= ~(uint_fast12_t) (! (roundBits ^ 8) & roundNearEven); |
| 132 | + if ( ! sig ) exp = 0; |
| 133 | + /*------------------------------------------------------------------------ |
| 134 | + *------------------------------------------------------------------------*/ |
| 135 | + packReturn: |
| 136 | + uiZ = packToF12UI( sign, exp, sig ); |
| 137 | + uiZ: |
| 138 | + uZ.ui = uiZ; |
| 139 | + return uZ.f; |
| 140 | + |
| 141 | +} |
| 142 | + |
| 143 | +struct exp8_sig16 softfloat_normSubnormalF12Sig( uint_fast12_t sig ) |
| 144 | +{ |
| 145 | + int_fast8_t shiftDist; |
| 146 | + struct exp8_sig16 z; |
| 147 | + |
| 148 | + shiftDist = softfloat_countLeadingZeros16( sig ) - 5; // TODO |
| 149 | + z.exp = 1 - shiftDist; |
| 150 | + z.sig = sig<<shiftDist; |
| 151 | + return z; |
| 152 | + |
| 153 | +} |
| 154 | + |
| 155 | + |
| 156 | +void softfloat_f12UIToCommonNaN( uint_fast12_t uiA, struct commonNaN *zPtr ) |
| 157 | +{ |
| 158 | + if ( softfloat_isSigNaNF12UI( uiA ) ) { |
| 159 | + softfloat_raiseFlags( softfloat_flag_invalid ); |
| 160 | + } |
| 161 | + zPtr->sign = uiA>>15; |
| 162 | + zPtr->v64 = (uint_fast64_t) uiA<<54; |
| 163 | + zPtr->v0 = 0; |
| 164 | +} |
| 165 | + |
| 166 | + |
| 167 | +float32_t f12_to_f32( float12_t a ) |
| 168 | +{ |
| 169 | + union ui12_f12 uA; |
| 170 | + uint_fast12_t uiA; |
| 171 | + bool sign; |
| 172 | + int_fast8_t exp; |
| 173 | + uint_fast12_t frac; |
| 174 | + struct commonNaN commonNaN; |
| 175 | + uint_fast32_t uiZ; |
| 176 | + struct exp8_sig16 normExpSig; |
| 177 | + union ui32_f32 uZ; |
| 178 | + |
| 179 | + /*------------------------------------------------------------------------ |
| 180 | + *------------------------------------------------------------------------*/ |
| 181 | + uA.f = a; |
| 182 | + uiA = uA.ui; |
| 183 | + sign = signF12UI( uiA ); |
| 184 | + exp = expF12UI( uiA ); |
| 185 | + frac = fracF12UI( uiA ); |
| 186 | + /*------------------------------------------------------------------------ |
| 187 | + *------------------------------------------------------------------------*/ |
| 188 | + if ( exp == 0x1F ) { |
| 189 | + if ( frac ) { |
| 190 | + softfloat_f12UIToCommonNaN( uiA, &commonNaN ); |
| 191 | + uiZ = softfloat_commonNaNToF32UI( &commonNaN ); |
| 192 | + } else { |
| 193 | + uiZ = packToF32UI( sign, 0xFF, 0 ); |
| 194 | + } |
| 195 | + goto uiZ; |
| 196 | + } |
| 197 | + /*------------------------------------------------------------------------ |
| 198 | + *------------------------------------------------------------------------*/ |
| 199 | + if ( ! exp ) { |
| 200 | + if ( ! frac ) { |
| 201 | + uiZ = packToF32UI( sign, 0, 0 ); |
| 202 | + goto uiZ; |
| 203 | + } |
| 204 | + normExpSig = softfloat_normSubnormalF12Sig( frac ); |
| 205 | + exp = normExpSig.exp - 1; |
| 206 | + frac = normExpSig.sig; |
| 207 | + } |
| 208 | + /*------------------------------------------------------------------------ |
| 209 | + *------------------------------------------------------------------------*/ |
| 210 | + uiZ = packToF32UI( sign, exp + 0x70, (uint_fast32_t) frac<<13 ); |
| 211 | + uiZ: |
| 212 | + uZ.ui = uiZ; |
| 213 | + return uZ.f; |
| 214 | + |
| 215 | +} |
| 216 | + |
| 217 | + |
0 commit comments