Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.numeric: Hack with quadruple-precision real type #8478

Merged
merged 5 commits into from
Jun 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions std/numeric.d
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ public enum CustomFloatFlags
none = 0
}

private enum isIEEEQuadruple = floatTraits!real.realFormat == RealFormat.ieeeQuadruple;

private template CustomFloatParams(uint bits)
{
enum CustomFloatFlags flags = CustomFloatFlags.ieee
^ ((bits == 80) ? CustomFloatFlags.storeNormalized : CustomFloatFlags.none);
^ ((bits == 80 && !isIEEEQuadruple) ? CustomFloatFlags.storeNormalized : CustomFloatFlags.none);
static if (bits == 8) alias CustomFloatParams = CustomFloatParams!( 4, 3, flags);
static if (bits == 16) alias CustomFloatParams = CustomFloatParams!(10, 5, flags);
static if (bits == 32) alias CustomFloatParams = CustomFloatParams!(23, 8, flags);
Expand Down Expand Up @@ -367,11 +369,36 @@ private:
public:
static if (precision == 64) // CustomFloat!80 support hack
{
ulong significand;
enum ulong significand_max = ulong.max;
mixin(bitfields!(
T_exp , "exponent", exponentWidth,
bool , "sign" , flags & flags.signed ));
static if (isIEEEQuadruple)
{
// Only use highest 64 significand bits from 112 explicitly stored
align (1):
enum ulong significand_max = ulong.max;
version (LittleEndian)
{
private ubyte[6] _padding; // 48-bit of padding
ulong significand;
mixin(bitfields!(
T_exp , "exponent", exponentWidth,
bool , "sign" , flags & flags.signed ));
}
else
{
mixin(bitfields!(
T_exp , "exponent", exponentWidth,
bool , "sign" , flags & flags.signed ));
ulong significand;
private ubyte[6] _padding; // 48-bit of padding
}
}
else
{
ulong significand;
enum ulong significand_max = ulong.max;
mixin(bitfields!(
T_exp , "exponent", exponentWidth,
bool , "sign" , flags & flags.signed ));
}
}
else
{
Expand Down Expand Up @@ -631,23 +658,28 @@ public:
auto x = F(0.125);
assert(x.get!float == 0.125F);
assert(x.get!double == 0.125);
assert(x.get!real == 0.125L);

x -= 0.0625;
assert(x.get!float == 0.0625F);
assert(x.get!double == 0.0625);
assert(x.get!real == 0.0625L);

x *= 2;
assert(x.get!float == 0.125F);
assert(x.get!double == 0.125);
assert(x.get!real == 0.125L);

x /= 4;
assert(x.get!float == 0.03125);
assert(x.get!double == 0.03125);
assert(x.get!real == 0.03125L);

x = 0.5;
x ^^= 4;
assert(x.get!float == 1 / 16.0F);
assert(x.get!double == 1 / 16.0);
assert(x.get!real == 1 / 16.0L);
}
}

Expand Down