2017-08-18 13:26:09 -07:00
|
|
|
const is_test = @import("builtin").is_test;
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-18 22:32:15 -07:00
|
|
|
const Log2Int = @import("../../math/index.zig").Log2Int;
|
2017-08-18 13:26:09 -07:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn fixuint(comptime fp_t: type, comptime fixuint_t: type, a: fp_t) fixuint_t {
|
2018-01-24 22:46:12 -08:00
|
|
|
@setRuntimeSafety(is_test);
|
2017-08-16 16:07:35 -07:00
|
|
|
|
|
|
|
const rep_t = switch (fp_t) {
|
|
|
|
f32 => u32,
|
|
|
|
f64 => u64,
|
|
|
|
f128 => u128,
|
|
|
|
else => unreachable,
|
|
|
|
};
|
|
|
|
const srep_t = @IntType(true, rep_t.bit_count);
|
|
|
|
const significandBits = switch (fp_t) {
|
|
|
|
f32 => 23,
|
|
|
|
f64 => 52,
|
|
|
|
f128 => 112,
|
|
|
|
else => unreachable,
|
|
|
|
};
|
|
|
|
const typeWidth = rep_t.bit_count;
|
|
|
|
const exponentBits = (typeWidth - significandBits - 1);
|
|
|
|
const signBit = (rep_t(1) << (significandBits + exponentBits));
|
|
|
|
const maxExponent = ((1 << exponentBits) - 1);
|
|
|
|
const exponentBias = (maxExponent >> 1);
|
|
|
|
|
|
|
|
const implicitBit = (rep_t(1) << significandBits);
|
|
|
|
const significandMask = (implicitBit - 1);
|
|
|
|
|
|
|
|
// Break a into sign, exponent, significand
|
|
|
|
const aRep: rep_t = @bitCast(rep_t, a);
|
|
|
|
const absMask = signBit - 1;
|
|
|
|
const aAbs: rep_t = aRep & absMask;
|
|
|
|
|
|
|
|
const sign = if ((aRep & signBit) != 0) i32(-1) else i32(1);
|
|
|
|
const exponent = i32(aAbs >> significandBits) - exponentBias;
|
|
|
|
const significand: rep_t = (aAbs & significandMask) | implicitBit;
|
|
|
|
|
|
|
|
// If either the value or the exponent is negative, the result is zero.
|
|
|
|
if (sign == -1 or exponent < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// If the value is too large for the integer type, saturate.
|
|
|
|
if (c_uint(exponent) >= fixuint_t.bit_count)
|
|
|
|
return ~fixuint_t(0);
|
|
|
|
|
|
|
|
// If 0 <= exponent < significandBits, right shift to get the result.
|
|
|
|
// Otherwise, shift left.
|
|
|
|
if (exponent < significandBits) {
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-18 22:32:15 -07:00
|
|
|
// TODO this is a workaround for the mysterious "integer cast truncated bits"
|
|
|
|
// happening on the next line
|
2018-01-24 22:46:12 -08:00
|
|
|
@setRuntimeSafety(false);
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-18 22:32:15 -07:00
|
|
|
return fixuint_t(significand >> Log2Int(rep_t)(significandBits - exponent));
|
2017-08-16 16:07:35 -07:00
|
|
|
} else {
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-18 22:32:15 -07:00
|
|
|
// TODO this is a workaround for the mysterious "integer cast truncated bits"
|
|
|
|
// happening on the next line
|
2018-01-24 22:46:12 -08:00
|
|
|
@setRuntimeSafety(false);
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-18 22:32:15 -07:00
|
|
|
return fixuint_t(significand) << Log2Int(fixuint_t)(exponent - significandBits);
|
2017-08-16 16:07:35 -07:00
|
|
|
}
|
|
|
|
}
|