From b2ecb210a13f40f538099a3a9fd3644a85ce86e1 Mon Sep 17 00:00:00 2001 From: Runa <84859860+runiixx@users.noreply.github.com> Date: Mon, 9 Dec 2024 03:17:39 +0200 Subject: [PATCH] Fix Issue 10386: Make std.math.isIdentical work in CTFE with coverage tests (#10576) * Fix Issue 20197 - Make std.math.isIdentical work in CTFE * Added tests for function isIdentical Signed-off-by: runiixx * Fix Issue 10386 Signed-off-by: runiixx * fixing style Signed-off-by: runiixx --------- Signed-off-by: runiixx Co-authored-by: Nathan Sashihara <21227491+n8sh@users.noreply.github.com> --- std/math/traits.d | 52 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/std/math/traits.d b/std/math/traits.d index 81ab1b789db..8aba6a6b1c6 100644 --- a/std/math/traits.d +++ b/std/math/traits.d @@ -21,6 +21,7 @@ module std.math.traits; import std.traits : isFloatingPoint, isIntegral, isNumeric, isSigned; + /********************************* * Determines if $(D_PARAM x) is NaN. * Params: @@ -466,8 +467,27 @@ if (isFloatingPoint!(X)) */ bool isIdentical(real x, real y) @trusted pure nothrow @nogc { - import std.math.traits : floatTraits, RealFormat; - + if (__ctfe) + { + if (x !is y) return false; + if (x == x) return true; // If not NaN `is` implies identical representation. + static if (double.mant_dig != real.mant_dig) + { + // Works because we are in CTFE and there is no way in CTFE to set more + // bits of NaN payload than can fit in a double, and since 2.087 + // changed real.init to be non-signaling I *think* there is no way in + // CTFE for a real to be a signaling NaN unless real and double have + // the same representation so real's bits can be manipulated directly. + double d1 = x, d2 = y; + } + else + { + // Alias to avoid converting signaling to quiet. + alias d1 = x; + alias d2 = y; + } + return *cast(long*) &d1 == *cast(long*) &d2; + } // We're doing a bitwise comparison so the endianness is irrelevant. long* pxs = cast(long *)&x; long* pys = cast(long *)&y; @@ -491,20 +511,44 @@ bool isIdentical(real x, real y) @trusted pure nothrow @nogc assert(0, "isIdentical not implemented"); } } - /// @safe @nogc pure nothrow unittest { + // We're forcing the CTFE to run by assigning the result of the function to an enum + enum test1 = isIdentical(1.0,1.0); + enum test2 = isIdentical(real.nan,real.nan); + enum test3 = isIdentical(real.infinity, real.infinity); + enum test4 = isIdentical(real.infinity, real.infinity); + enum test5 = isIdentical(0.0, 0.0); + + assert(test1); + assert(test2); + assert(test3); + assert(test4); + assert(test5); + + enum test6 = !isIdentical(0.0, -0.0); + enum test7 = !isIdentical(real.nan, -real.nan); + enum test8 = !isIdentical(real.infinity, -real.infinity); + + assert(test6); + assert(test7); + assert(test8); +} + +@safe @nogc pure nothrow unittest +{ + assert( !isIdentical(1.2,1.3)); assert( isIdentical(0.0, 0.0)); assert( isIdentical(1.0, 1.0)); assert( isIdentical(real.infinity, real.infinity)); assert( isIdentical(-real.infinity, -real.infinity)); + assert( isIdentical(real.nan, real.nan)); assert(!isIdentical(0.0, -0.0)); assert(!isIdentical(real.nan, -real.nan)); assert(!isIdentical(real.infinity, -real.infinity)); } - /********************************* * Return 1 if sign bit of e is set, 0 if not. */