motiejus/zig

fork of https://codeberg.org/ziglang/zig
git clone https://git.jakstys.lt/motiejus/zig.git
Log | Tree | Refs | README | LICENSE

commit 86f4dfe5a8a7632b26de5f1d109fba369095b0f7 (tree)
parent dc18b8174ae6dacb3bc32fc842363159916bd657
Author: Michael Dusan <michael.dusan@gmail.com>
Date:   Wed,  4 Jan 2023 15:18:17 -0500

llvm codegen: fix f16,f32,f64 nan bitcasts

@bitCast from integer NaN representation to float NaN resulted in
changed bits in float. This only happened with signaled NaN.

- added test for signaled NaN
- added tests for quiet NaN (for completeness)

closes #14198

Diffstat:
Msrc/codegen/llvm.zig | 17+++++++++++++----
Mtest/behavior.zig | 1+
Atest/behavior/bugs/14198.zig | 18++++++++++++++++++
3 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig @@ -3275,15 +3275,24 @@ pub const DeclGen = struct { .Float => { const llvm_ty = try dg.lowerType(tv.ty); switch (tv.ty.floatBits(target)) { - 16 => if (intrinsicsAllowed(tv.ty, target)) { - return llvm_ty.constReal(tv.val.toFloat(f16)); - } else { + 16 => { const repr = @bitCast(u16, tv.val.toFloat(f16)); const llvm_i16 = dg.context.intType(16); const int = llvm_i16.constInt(repr, .False); return int.constBitCast(llvm_ty); }, - 32, 64 => return llvm_ty.constReal(tv.val.toFloat(f64)), + 32 => { + const repr = @bitCast(u32, tv.val.toFloat(f32)); + const llvm_i32 = dg.context.intType(32); + const int = llvm_i32.constInt(repr, .False); + return int.constBitCast(llvm_ty); + }, + 64 => { + const repr = @bitCast(u64, tv.val.toFloat(f64)); + const llvm_i64 = dg.context.intType(64); + const int = llvm_i64.constInt(repr, .False); + return int.constBitCast(llvm_ty); + }, 80 => { const float = tv.val.toFloat(f80); const repr = std.math.break_f80(float); diff --git a/test/behavior.zig b/test/behavior.zig @@ -218,6 +218,7 @@ test { { _ = @import("behavior/bugs/13063.zig"); _ = @import("behavior/bugs/11227.zig"); + _ = @import("behavior/bugs/14198.zig"); _ = @import("behavior/export.zig"); } diff --git a/test/behavior/bugs/14198.zig b/test/behavior/bugs/14198.zig @@ -0,0 +1,18 @@ +const std = @import("std"); +const math = std.math; +const mem = std.mem; +const testing = std.testing; + +test "nan memory equality" { + // signaled + try testing.expect(mem.eql(u8, mem.asBytes(&math.nan_u16), mem.asBytes(&math.nan_f16))); + try testing.expect(mem.eql(u8, mem.asBytes(&math.nan_u32), mem.asBytes(&math.nan_f32))); + try testing.expect(mem.eql(u8, mem.asBytes(&math.nan_u64), mem.asBytes(&math.nan_f64))); + try testing.expect(mem.eql(u8, mem.asBytes(&math.nan_u128), mem.asBytes(&math.nan_f128))); + + // quiet + try testing.expect(mem.eql(u8, mem.asBytes(&math.qnan_u16), mem.asBytes(&math.qnan_f16))); + try testing.expect(mem.eql(u8, mem.asBytes(&math.qnan_u32), mem.asBytes(&math.qnan_f32))); + try testing.expect(mem.eql(u8, mem.asBytes(&math.qnan_u64), mem.asBytes(&math.qnan_f64))); + try testing.expect(mem.eql(u8, mem.asBytes(&math.qnan_u128), mem.asBytes(&math.qnan_f128))); +}