int float_to_half(float f) { unsigned int sign_mask = 0x80000000u; int o; int fint = FloatToBits(f); int sign = fint & sign_mask; fint ^= sign; // NOTE all the integer compares in this function can be safely // compiled into signed compares since all operands are below // 0x80000000. Important if you want fast straight SSE2 code (since // there's no unsigned PCMPGTD). // Inf or NaN (all exponent bits set) // NaN->qNaN and Inf->Inf // unconditional assignment here, will override with right value for // the regular case below. int f32infty = 255ul << 23; o = (fint > f32infty) ? 0x7e00u : 0x7c00u; // (De)normalized number or zero // update fint unconditionally to save the blending; we don't need it // anymore for the Inf/NaN case anyway. const unsigned int round_mask = ~0xfffu; const uint32_t magic = 15ul << 23; const int f16infty = 31ul << 23; int fint2 = FloatToBits(BitsToFloat(fint & round_mask) * BitsToFloat(magic)) - round_mask; // Clamp to signed infinity if overflowed fint2 = (fint2 > f16infty) ? f16infty : fint2; if (fint < f32infty) o = fint2 >> 13; // Take the bits! return (o | (sign >> 16)); }
bool isInlinableLiteral32(int32_t Literal, bool IsVI) { if (Literal >= -16 && Literal <= 64) return true; float F = BitsToFloat(Literal); if (F == 0.5 || F == -0.5 || F == 1.0 || F == -1.0 || F == 2.0 || F == -2.0 || F == 4.0 || F == -4.0) return true; if (IsVI && Literal == 0x3e22f983) return true; return false; }