Skip to content

Commit e0fb4c2

Browse files
mikdusanandrewrk
authored andcommitted
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
1 parent b89158d commit e0fb4c2

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/codegen/llvm.zig

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const std = @import("std");
2+
const math = std.math;
3+
const mem = std.mem;
4+
const testing = std.testing;
5+
6+
test "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+
}

0 commit comments

Comments
 (0)