Index: lib/builtins/fp_lib.h =================================================================== --- lib/builtins/fp_lib.h +++ lib/builtins/fp_lib.h @@ -259,6 +259,18 @@ } } +// Iff `rep` represents the bit pattern for a NaN then return a canonical +// representation of NaN, otherwise return `rep`. +static __inline rep_t canonicalizeNaNRep(rep_t rep) { + rep_t exp = (rep & exponentMask) >> significandBits; + rep_t significand = (rep & significandMask); + if (exp == maxExponent && significand) { + // `rep` is a NaN + return qnanRep; + } + return rep; +} + // Implements logb methods (logb, logbf, logbl) for IEEE-754. This avoids // pulling in a libm dependency from compiler-rt, but is not meant to replace // it (i.e. code calling logb() should get the one from libm, not this), hence Index: test/builtins/Unit/compiler_rt_logbf_test.c =================================================================== --- test/builtins/Unit/compiler_rt_logbf_test.c +++ test/builtins/Unit/compiler_rt_logbf_test.c @@ -21,7 +21,8 @@ fp_t crt_value = __compiler_rt_logbf(x); fp_t libm_value = logbf(x); // Compare actual rep, e.g. to avoid NaN != the same NaN - if (toRep(crt_value) != toRep(libm_value)) { + if (canonicalizeNaNRep(toRep(crt_value)) != + canonicalizeNaNRep(toRep(libm_value))) { printf("error: in __compiler_rt_logb(%a [%X]) = %a [%X] != %a [%X]\n", x, toRep(x), crt_value, toRep(crt_value), libm_value, toRep(libm_value));