|
| 1 | +#if defined(__linux__) |
| 2 | +#include <limits.h> |
| 3 | +#include <stdint.h> |
| 4 | + |
| 5 | +typedef float src_t; |
| 6 | +typedef uint32_t src_rep_t; |
| 7 | +#define SRC_REP_C UINT32_C |
| 8 | +static const int srcSigBits = 23; |
| 9 | + |
| 10 | +typedef uint16_t dst_t; |
| 11 | +typedef uint16_t dst_rep_t; |
| 12 | +#define DST_REP_C UINT16_C |
| 13 | +static const int dstSigBits = 10; |
| 14 | + |
| 15 | +// End of specialization parameters. Two helper routines for conversion to and |
| 16 | +// from the representation of floating-point data as integer values follow. |
| 17 | + |
| 18 | +static __inline src_rep_t srcToRep(src_t x) { |
| 19 | + const union { src_t f; src_rep_t i; } rep = {.f = x}; |
| 20 | + return rep.i; |
| 21 | +} |
| 22 | + |
| 23 | +static __inline dst_t dstFromRep(dst_rep_t x) { |
| 24 | + const union { dst_t f; dst_rep_t i; } rep = {.i = x}; |
| 25 | + return rep.f; |
| 26 | +} |
| 27 | + |
| 28 | +static __inline dst_t __truncXfYf2__(src_t a) { |
| 29 | + // Various constants whose values follow from the type parameters. |
| 30 | + // Any reasonable optimizer will fold and propagate all of these. |
| 31 | + const int srcBits = sizeof(src_t)*CHAR_BIT; |
| 32 | + const int srcExpBits = srcBits - srcSigBits - 1; |
| 33 | + const int srcInfExp = (1 << srcExpBits) - 1; |
| 34 | + const int srcExpBias = srcInfExp >> 1; |
| 35 | + |
| 36 | + const src_rep_t srcMinNormal = SRC_REP_C(1) << srcSigBits; |
| 37 | + const src_rep_t srcSignificandMask = srcMinNormal - 1; |
| 38 | + const src_rep_t srcInfinity = (src_rep_t)srcInfExp << srcSigBits; |
| 39 | + const src_rep_t srcSignMask = SRC_REP_C(1) << (srcSigBits + srcExpBits); |
| 40 | + const src_rep_t srcAbsMask = srcSignMask - 1; |
| 41 | + const src_rep_t roundMask = (SRC_REP_C(1) << (srcSigBits - dstSigBits)) - 1; |
| 42 | + const src_rep_t halfway = SRC_REP_C(1) << (srcSigBits - dstSigBits - 1); |
| 43 | + const src_rep_t srcQNaN = SRC_REP_C(1) << (srcSigBits - 1); |
| 44 | + const src_rep_t srcNaNCode = srcQNaN - 1; |
| 45 | + |
| 46 | + const int dstBits = sizeof(dst_t)*CHAR_BIT; |
| 47 | + const int dstExpBits = dstBits - dstSigBits - 1; |
| 48 | + const int dstInfExp = (1 << dstExpBits) - 1; |
| 49 | + const int dstExpBias = dstInfExp >> 1; |
| 50 | + const int underflowExponent = srcExpBias + 1 - dstExpBias; |
| 51 | + const int overflowExponent = srcExpBias + dstInfExp - dstExpBias; |
| 52 | + const src_rep_t underflow = (src_rep_t)underflowExponent << srcSigBits; |
| 53 | + const src_rep_t overflow = (src_rep_t)overflowExponent << srcSigBits; |
| 54 | + |
| 55 | + const dst_rep_t dstQNaN = DST_REP_C(1) << (dstSigBits - 1); |
| 56 | + const dst_rep_t dstNaNCode = dstQNaN - 1; |
| 57 | + |
| 58 | + // Break a into a sign and representation of the absolute value |
| 59 | + const src_rep_t aRep = srcToRep(a); |
| 60 | + const src_rep_t aAbs = aRep & srcAbsMask; |
| 61 | + const src_rep_t sign = aRep & srcSignMask; |
| 62 | + dst_rep_t absResult; |
| 63 | + |
| 64 | + if (aAbs - underflow < aAbs - overflow) { |
| 65 | + // The exponent of a is within the range of normal numbers in the |
| 66 | + // destination format. We can convert by simply right-shifting with |
| 67 | + // rounding and adjusting the exponent. |
| 68 | + absResult = aAbs >> (srcSigBits - dstSigBits); |
| 69 | + absResult -= (dst_rep_t)(srcExpBias - dstExpBias) << dstSigBits; |
| 70 | + |
| 71 | + const src_rep_t roundBits = aAbs & roundMask; |
| 72 | + // Round to nearest |
| 73 | + if (roundBits > halfway) |
| 74 | + absResult++; |
| 75 | + // Ties to even |
| 76 | + else if (roundBits == halfway) |
| 77 | + absResult += absResult & 1; |
| 78 | + } |
| 79 | + else if (aAbs > srcInfinity) { |
| 80 | + // a is NaN. |
| 81 | + // Conjure the result by beginning with infinity, setting the qNaN |
| 82 | + // bit and inserting the (truncated) trailing NaN field. |
| 83 | + absResult = (dst_rep_t)dstInfExp << dstSigBits; |
| 84 | + absResult |= dstQNaN; |
| 85 | + absResult |= ((aAbs & srcNaNCode) >> (srcSigBits - dstSigBits)) & dstNaNCode; |
| 86 | + } |
| 87 | + else if (aAbs >= overflow) { |
| 88 | + // a overflows to infinity. |
| 89 | + absResult = (dst_rep_t)dstInfExp << dstSigBits; |
| 90 | + } |
| 91 | + else { |
| 92 | + // a underflows on conversion to the destination type or is an exact |
| 93 | + // zero. The result may be a denormal or zero. Extract the exponent |
| 94 | + // to get the shift amount for the denormalization. |
| 95 | + const int aExp = aAbs >> srcSigBits; |
| 96 | + const int shift = srcExpBias - dstExpBias - aExp + 1; |
| 97 | + |
| 98 | + const src_rep_t significand = (aRep & srcSignificandMask) | srcMinNormal; |
| 99 | + |
| 100 | + // Right shift by the denormalization amount with sticky. |
| 101 | + if (shift > srcSigBits) { |
| 102 | + absResult = 0; |
| 103 | + } else { |
| 104 | + const bool sticky = significand << (srcBits - shift); |
| 105 | + src_rep_t denormalizedSignificand = significand >> shift | sticky; |
| 106 | + absResult = denormalizedSignificand >> (srcSigBits - dstSigBits); |
| 107 | + const src_rep_t roundBits = denormalizedSignificand & roundMask; |
| 108 | + // Round to nearest |
| 109 | + if (roundBits > halfway) |
| 110 | + absResult++; |
| 111 | + // Ties to even |
| 112 | + else if (roundBits == halfway) |
| 113 | + absResult += absResult & 1; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Apply the signbit to (dst_t)abs(a). |
| 118 | + const dst_rep_t result = absResult | sign >> (srcBits - dstBits); |
| 119 | + return dstFromRep(result); |
| 120 | +} |
| 121 | + |
| 122 | +// Use a forwarding definition and noinline to implement a poor man's alias, |
| 123 | +// as there isn't a good cross-platform way of defining one. |
| 124 | +__attribute__((noinline)) uint16_t __truncsfhf2(float a) { |
| 125 | + return __truncXfYf2__(a); |
| 126 | +} |
| 127 | + |
| 128 | +extern "C" uint16_t __truncdfhf2(double a) { |
| 129 | + return __truncsfhf2((double)a); |
| 130 | +} |
| 131 | + |
| 132 | +extern "C" uint16_t __gnu_f2h_ieee(float a) { |
| 133 | + return __truncsfhf2(a); |
| 134 | +} |
| 135 | +#endif // #if defined(__linux__) |
0 commit comments