feature. See also
. The project being documented here (as the example) is the Zig library itself.
addf3.addf3
pub inline fn addf3(comptime T: type, a: T, b: T) T
File
Code
pub inline fn addf3(comptime T: type, a: T, b: T) T {
const bits = @typeInfo(T).float.bits;
const Z = @Int(.unsigned, bits);
const typeWidth = bits;
const significandBits = math.floatMantissaBits(T);
const fractionalBits = math.floatFractionalBits(T);
const exponentBits = math.floatExponentBits(T);
const signBit = (@as(Z, 1) << (significandBits + exponentBits));
const maxExponent = ((1 << exponentBits) - 1);
const integerBit = (@as(Z, 1) << fractionalBits);
const quietBit = integerBit >> 1;
const significandMask = (@as(Z, 1) << significandBits) - 1;
const absMask = signBit - 1;
const qnanRep = @as(Z, @bitCast(math.nan(T))) | quietBit;
var aRep: Z = @bitCast(a);
var bRep: Z = @bitCast(b);
const aAbs = aRep & absMask;
const bAbs = bRep & absMask;
const infRep: Z = @bitCast(math.inf(T));
if (aAbs -% @as(Z, 1) >= infRep - @as(Z, 1) or
bAbs -% @as(Z, 1) >= infRep - @as(Z, 1))
{
if (aAbs > infRep) return @bitCast(@as(Z, @bitCast(a)) | quietBit);
if (bAbs > infRep) return @bitCast(@as(Z, @bitCast(b)) | quietBit);
if (aAbs == infRep) {
if ((@as(Z, @bitCast(a)) ^ @as(Z, @bitCast(b))) == signBit) {
return @bitCast(qnanRep);
}
else {
return a;
}
}
if (bAbs == infRep) return b;
if (aAbs == 0) {
if (bAbs == 0) {
return @bitCast(@as(Z, @bitCast(a)) & @as(Z, @bitCast(b)));
} else {
return b;
}
}
if (bAbs == 0) return a;
}
if (bAbs > aAbs) {
const temp = aRep;
aRep = bRep;
bRep = temp;
}
var aExponent: i32 = @intCast((aRep >> significandBits) & maxExponent);
var bExponent: i32 = @intCast((bRep >> significandBits) & maxExponent);
var aSignificand = aRep & significandMask;
var bSignificand = bRep & significandMask;
if (aExponent == 0) aExponent = normalize(T, &aSignificand);
if (bExponent == 0) bExponent = normalize(T, &bSignificand);
// have opposite signs, we are performing a subtraction; otherwise addition.
const resultSign = aRep & signBit;
const subtraction = (aRep ^ bRep) & signBit != 0;
// implicit significand bit. (If we fell through from the denormal path it
// was already set by normalize( ), but setting it twice won't hurt
// anything.)
aSignificand = (aSignificand | integerBit) << 3;
bSignificand = (bSignificand | integerBit) << 3;
// bottom bit to get rounding correct.
const @"align": u32 = @intCast(aExponent - bExponent);
if (@"align" != 0) {
if (@"align" < typeWidth) {
const sticky = if (bSignificand << @intCast(typeWidth - @"align") != 0) @as(Z, 1) else 0;
bSignificand = (bSignificand >> @truncate(@"align")) | sticky;
} else {
bSignificand = 1;
}
}
if (subtraction) {
aSignificand -= bSignificand;
if (aSignificand == 0) return @bitCast(@as(Z, 0));
// and adjust the exponent:
if (aSignificand < integerBit << 3) {
const shift = @as(i32, @intCast(@clz(aSignificand))) - @as(i32, @intCast(@clz(integerBit << 3)));
aSignificand <<= @intCast(shift);
aExponent -= shift;
}
} else {
aSignificand += bSignificand;
// adjust the exponent:
if (aSignificand & (integerBit << 4) != 0) {
const sticky = aSignificand & 1;
aSignificand = aSignificand >> 1 | sticky;
aExponent += 1;
}
}
if (aExponent >= maxExponent) return @bitCast(infRep | resultSign);
if (aExponent <= 0) {
// All we need to do is shift the significand and apply the correct sign.
aSignificand >>= @intCast(4 - aExponent);
return @bitCast(resultSign | aSignificand);
}
const roundGuardSticky = aSignificand & 0x7;
var result = (aSignificand >> 3) & significandMask;
result |= @as(Z, @intCast(aExponent)) << significandBits;
result |= resultSign;
// correct result in that case.
if (roundGuardSticky > 0x4) result += 1;
if (roundGuardSticky == 0x4) result += result & 1;
if (significandBits != fractionalBits) {
if ((result >> significandBits) != 0) result |= integerBit;
}
return @bitCast(result);
}