From 466328d1ca29f3f6dd142f74dda13b26687e71e0 Mon Sep 17 00:00:00 2001 From: mlugg Date: Tue, 9 May 2023 17:06:10 +0100 Subject: InternPool: transition float values --- src/Module.zig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/Module.zig') diff --git a/src/Module.zig b/src/Module.zig index 6bcd148e67..b32904e165 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -6940,6 +6940,24 @@ pub fn unionValue(mod: *Module, union_ty: Type, tag: Value, val: Value) Allocato return i.toValue(); } +/// This function casts the float representation down to the representation of the type, potentially +/// losing data if the representation wasn't correct. +pub fn floatValue(mod: *Module, ty: Type, x: anytype) Allocator.Error!Value { + const storage: InternPool.Key.Float.Storage = switch (ty.floatBits(mod.getTarget())) { + 16 => .{ .f16 = @floatCast(f16, x) }, + 32 => .{ .f32 = @floatCast(f32, x) }, + 64 => .{ .f64 = @floatCast(f64, x) }, + 80 => .{ .f80 = @floatCast(f80, x) }, + 128 => .{ .f128 = @floatCast(f128, x) }, + else => unreachable, + }; + const i = try intern(mod, .{ .float = .{ + .ty = ty.ip_index, + .storage = storage, + } }); + return i.toValue(); +} + pub fn smallestUnsignedInt(mod: *Module, max: u64) Allocator.Error!Type { return intType(mod, .unsigned, Type.smallestUnsignedBits(max)); } -- cgit v1.2.3