authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2023-01-04 15:18:17-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-05 02:22:30-07:00
loge0fb4c29cb618fe912c82fed06bb305db14606d8
tree8443bb64aaf65db00dc760c659c93754031fd824
parentb89158d6fd7ce0960b3f8085368ae102b48747d0

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

3 files changed, 32 insertions(+), 4 deletions(-)

src/codegen/llvm.zig+13-4
......@@ -3287,15 +3287,24 @@ pub const DeclGen = struct {
32873287 .Float => {
32883288 const llvm_ty = try dg.lowerType(tv.ty);
32893289 switch (tv.ty.floatBits(target)) {
3290 16 => if (intrinsicsAllowed(tv.ty, target)) {
3291 return llvm_ty.constReal(tv.val.toFloat(f16));
3292 } else {
3290 16 => {
32933291 const repr = @bitCast(u16, tv.val.toFloat(f16));
32943292 const llvm_i16 = dg.context.intType(16);
32953293 const int = llvm_i16.constInt(repr, .False);
32963294 return int.constBitCast(llvm_ty);
32973295 },
3298 32, 64 => return llvm_ty.constReal(tv.val.toFloat(f64)),
3296 32 => {
3297 const repr = @bitCast(u32, tv.val.toFloat(f32));
3298 const llvm_i32 = dg.context.intType(32);
3299 const int = llvm_i32.constInt(repr, .False);
3300 return int.constBitCast(llvm_ty);
3301 },
3302 64 => {
3303 const repr = @bitCast(u64, tv.val.toFloat(f64));
3304 const llvm_i64 = dg.context.intType(64);
3305 const int = llvm_i64.constInt(repr, .False);
3306 return int.constBitCast(llvm_ty);
3307 },
32993308 80 => {
33003309 const float = tv.val.toFloat(f80);
33013310 const repr = std.math.break_f80(float);
test/behavior.zig+1
......@@ -241,6 +241,7 @@ test {
241241 {
242242 _ = @import("behavior/bugs/13063.zig");
243243 _ = @import("behavior/bugs/11227.zig");
244 _ = @import("behavior/bugs/14198.zig");
244245 _ = @import("behavior/export.zig");
245246 }
246247
test/behavior/bugs/14198.zig created+18
......@@ -0,0 +1,18 @@
1const std = @import("std");
2const math = std.math;
3const mem = std.mem;
4const testing = std.testing;
5
6test "nan memory equality" {
7 // signaled
8 try testing.expect(mem.eql(u8, mem.asBytes(&math.nan_u16), mem.asBytes(&math.nan_f16)));
9 try testing.expect(mem.eql(u8, mem.asBytes(&math.nan_u32), mem.asBytes(&math.nan_f32)));
10 try testing.expect(mem.eql(u8, mem.asBytes(&math.nan_u64), mem.asBytes(&math.nan_f64)));
11 try testing.expect(mem.eql(u8, mem.asBytes(&math.nan_u128), mem.asBytes(&math.nan_f128)));
12
13 // quiet
14 try testing.expect(mem.eql(u8, mem.asBytes(&math.qnan_u16), mem.asBytes(&math.qnan_f16)));
15 try testing.expect(mem.eql(u8, mem.asBytes(&math.qnan_u32), mem.asBytes(&math.qnan_f32)));
16 try testing.expect(mem.eql(u8, mem.asBytes(&math.qnan_u64), mem.asBytes(&math.qnan_f64)));
17 try testing.expect(mem.eql(u8, mem.asBytes(&math.qnan_u128), mem.asBytes(&math.qnan_f128)));
18}