InternPool: transition float values

This commit is contained in:
mlugg 2023-05-09 17:06:10 +01:00 committed by Andrew Kelley
parent 5881a2d637
commit 466328d1ca
8 changed files with 793 additions and 976 deletions

View File

@ -155,6 +155,7 @@ pub const Key = union(enum) {
lib_name: u32,
},
int: Key.Int,
float: Key.Float,
ptr: Ptr,
opt: Opt,
enum_tag: struct {
@ -361,6 +362,20 @@ pub const Key = union(enum) {
};
};
pub const Float = struct {
ty: Index,
/// The storage used must match the size of the float type being represented.
storage: Storage,
pub const Storage = union(enum) {
f16: f16,
f32: f32,
f64: f64,
f80: f80,
f128: f128,
};
};
pub const Ptr = struct {
ty: Index,
addr: Addr,
@ -436,6 +451,16 @@ pub const Key = union(enum) {
for (big_int.limbs) |limb| std.hash.autoHash(hasher, limb);
},
.float => |float| {
std.hash.autoHash(hasher, float.ty);
switch (float.storage) {
inline else => |val| std.hash.autoHash(
hasher,
@bitCast(std.meta.Int(.unsigned, @bitSizeOf(@TypeOf(val))), val),
),
}
},
.ptr => |ptr| {
std.hash.autoHash(hasher, ptr.ty);
// Int-to-ptr pointers are hashed separately than decl-referencing pointers.
@ -561,6 +586,32 @@ pub const Key = union(enum) {
};
},
.float => |a_info| {
const b_info = b.float;
if (a_info.ty != b_info.ty)
return false;
if (a_info.ty == .c_longdouble_type and a_info.storage != .f80) {
// These are strange: we'll sometimes represent them as f128, even if the
// underlying type is smaller. f80 is an exception: see float_c_longdouble_f80.
const a_val = switch (a_info.storage) {
inline else => |val| @floatCast(f128, val),
};
const b_val = switch (b_info.storage) {
inline else => |val| @floatCast(f128, val),
};
return a_val == b_val;
}
const StorageTag = @typeInfo(Key.Float.Storage).Union.tag_type.?;
assert(@as(StorageTag, a_info.storage) == @as(StorageTag, b_info.storage));
return switch (a_info.storage) {
inline else => |val, tag| val == @field(b_info.storage, @tagName(tag)),
};
},
.enum_tag => |a_info| {
const b_info = b.enum_tag;
_ = a_info;
@ -601,6 +652,7 @@ pub const Key = union(enum) {
inline .ptr,
.int,
.float,
.opt,
.extern_func,
.enum_tag,
@ -1115,15 +1167,35 @@ pub const Tag = enum(u8) {
/// An enum tag identified by a negative integer value.
/// data is a limbs index to Int.
enum_tag_negative,
/// An f16 value.
/// data is float value bitcasted to u16 and zero-extended.
float_f16,
/// An f32 value.
/// data is float value bitcasted to u32.
float_f32,
/// An f64 value.
/// data is extra index to Float64.
float_f64,
/// An f80 value.
/// data is extra index to Float80.
float_f80,
/// An f128 value.
/// data is extra index to Float128.
float_f128,
/// A c_longdouble value of 80 bits.
/// data is extra index to Float80.
/// This is used when a c_longdouble value is provided as an f80, because f80 has unnormalized
/// values which cannot be losslessly represented as f128. It should only be used when the type
/// underlying c_longdouble for the target is 80 bits.
float_c_longdouble_f80,
/// A c_longdouble value of 128 bits.
/// data is extra index to Float128.
/// This is used when a c_longdouble value is provided as any type other than an f80, since all
/// other float types can be losslessly converted to and from f128.
float_c_longdouble_f128,
/// A comptime_float value.
/// data is extra index to Float128.
float_comptime_float,
/// An extern function.
extern_func,
/// A regular function.
@ -1339,7 +1411,38 @@ pub const Float64 = struct {
pub fn get(self: Float64) f64 {
const int_bits = @as(u64, self.piece0) | (@as(u64, self.piece1) << 32);
return @bitCast(u64, int_bits);
return @bitCast(f64, int_bits);
}
fn pack(val: f64) Float64 {
const bits = @bitCast(u64, val);
return .{
.piece0 = @truncate(u32, bits),
.piece1 = @truncate(u32, bits >> 32),
};
}
};
/// A f80 value, broken up into 2 u32 parts and a u16 part zero-padded to a u32.
pub const Float80 = struct {
piece0: u32,
piece1: u32,
piece2: u32, // u16 part, top bits
pub fn get(self: Float80) f80 {
const int_bits = @as(u80, self.piece0) |
(@as(u80, self.piece1) << 32) |
(@as(u80, self.piece2) << 64);
return @bitCast(f80, int_bits);
}
fn pack(val: f80) Float80 {
const bits = @bitCast(u80, val);
return .{
.piece0 = @truncate(u32, bits),
.piece1 = @truncate(u32, bits >> 32),
.piece2 = @truncate(u16, bits >> 64),
};
}
};
@ -1357,6 +1460,16 @@ pub const Float128 = struct {
(@as(u128, self.piece3) << 96);
return @bitCast(f128, int_bits);
}
fn pack(val: f128) Float128 {
const bits = @bitCast(u128, val);
return .{
.piece0 = @truncate(u32, bits),
.piece1 = @truncate(u32, bits >> 32),
.piece2 = @truncate(u32, bits >> 64),
.piece3 = @truncate(u32, bits >> 96),
};
}
};
pub fn init(ip: *InternPool, gpa: Allocator) !void {
@ -1576,9 +1689,38 @@ pub fn indexToKey(ip: InternPool, index: Index) Key {
.int_negative => indexToKeyBigInt(ip, data, false),
.enum_tag_positive => @panic("TODO"),
.enum_tag_negative => @panic("TODO"),
.float_f32 => @panic("TODO"),
.float_f64 => @panic("TODO"),
.float_f128 => @panic("TODO"),
.float_f16 => .{ .float = .{
.ty = .f16_type,
.storage = .{ .f16 = @bitCast(f16, @intCast(u16, data)) },
} },
.float_f32 => .{ .float = .{
.ty = .f32_type,
.storage = .{ .f32 = @bitCast(f32, data) },
} },
.float_f64 => .{ .float = .{
.ty = .f64_type,
.storage = .{ .f64 = ip.extraData(Float64, data).get() },
} },
.float_f80 => .{ .float = .{
.ty = .f80_type,
.storage = .{ .f80 = ip.extraData(Float80, data).get() },
} },
.float_f128 => .{ .float = .{
.ty = .f128_type,
.storage = .{ .f128 = ip.extraData(Float128, data).get() },
} },
.float_c_longdouble_f80 => .{ .float = .{
.ty = .c_longdouble_type,
.storage = .{ .f80 = ip.extraData(Float80, data).get() },
} },
.float_c_longdouble_f128 => .{ .float = .{
.ty = .c_longdouble_type,
.storage = .{ .f128 = ip.extraData(Float128, data).get() },
} },
.float_comptime_float => .{ .float = .{
.ty = .comptime_float_type,
.storage = .{ .f128 = ip.extraData(Float128, data).get() },
} },
.extern_func => @panic("TODO"),
.func => @panic("TODO"),
.only_possible_value => {
@ -1982,6 +2124,46 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
}
},
.float => |float| {
switch (float.ty) {
.f16_type => ip.items.appendAssumeCapacity(.{
.tag = .float_f16,
.data = @bitCast(u16, float.storage.f16),
}),
.f32_type => ip.items.appendAssumeCapacity(.{
.tag = .float_f32,
.data = @bitCast(u32, float.storage.f32),
}),
.f64_type => ip.items.appendAssumeCapacity(.{
.tag = .float_f64,
.data = try ip.addExtra(gpa, Float64.pack(float.storage.f64)),
}),
.f80_type => ip.items.appendAssumeCapacity(.{
.tag = .float_f80,
.data = try ip.addExtra(gpa, Float80.pack(float.storage.f80)),
}),
.f128_type => ip.items.appendAssumeCapacity(.{
.tag = .float_f128,
.data = try ip.addExtra(gpa, Float128.pack(float.storage.f128)),
}),
.c_longdouble_type => switch (float.storage) {
.f80 => |x| ip.items.appendAssumeCapacity(.{
.tag = .float_c_longdouble_f80,
.data = try ip.addExtra(gpa, Float80.pack(x)),
}),
inline .f16, .f32, .f64, .f128 => |x| ip.items.appendAssumeCapacity(.{
.tag = .float_c_longdouble_f128,
.data = try ip.addExtra(gpa, Float128.pack(x)),
}),
},
.comptime_float_type => ip.items.appendAssumeCapacity(.{
.tag = .float_comptime_float,
.data = try ip.addExtra(gpa, Float128.pack(float.storage.f128)),
}),
else => unreachable,
}
},
.enum_tag => |enum_tag| {
const tag: Tag = if (enum_tag.tag.positive) .enum_tag_positive else .enum_tag_negative;
try addInt(ip, gpa, enum_tag.ty, tag, enum_tag.tag.limbs);
@ -2645,9 +2827,14 @@ fn dumpFallible(ip: InternPool, arena: Allocator) anyerror!void {
break :b @sizeOf(Int) + int.limbs_len * 8;
},
.float_f16 => 0,
.float_f32 => 0,
.float_f64 => @sizeOf(Float64),
.float_f80 => @sizeOf(Float80),
.float_f128 => @sizeOf(Float128),
.float_c_longdouble_f80 => @sizeOf(Float80),
.float_c_longdouble_f128 => @sizeOf(Float128),
.float_comptime_float => @sizeOf(Float128),
.extern_func => @panic("TODO"),
.func => @panic("TODO"),
.only_possible_value => 0,

View File

@ -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));
}

View File

@ -3225,7 +3225,7 @@ fn zirOpaqueDecl(
const new_namespace = mod.namespacePtr(new_namespace_index);
errdefer mod.destroyNamespace(new_namespace_index);
const opaque_ty = try mod.intern_pool.get(gpa, .{ .opaque_type = .{
const opaque_ty = try mod.intern(.{ .opaque_type = .{
.decl = new_decl_index,
.namespace = new_namespace_index,
} });
@ -5196,23 +5196,21 @@ fn zirIntBig(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
fn zirFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
_ = block;
const arena = sema.arena;
const number = sema.code.instructions.items(.data)[inst].float;
return sema.addConstant(
Type.comptime_float,
try Value.Tag.float_64.create(arena, number),
try sema.mod.floatValue(Type.comptime_float, number),
);
}
fn zirFloat128(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
_ = block;
const arena = sema.arena;
const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
const extra = sema.code.extraData(Zir.Inst.Float128, inst_data.payload_index).data;
const number = extra.get();
return sema.addConstant(
Type.comptime_float,
try Value.Tag.float_128.create(arena, number),
try sema.mod.floatValue(Type.comptime_float, number),
);
}
@ -9952,7 +9950,7 @@ fn zirFloatCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
}
if (try sema.resolveMaybeUndefVal(operand)) |operand_val| {
return sema.addConstant(dest_ty, try operand_val.floatCast(sema.arena, dest_ty, mod));
return sema.addConstant(dest_ty, try operand_val.floatCast(dest_ty, mod));
}
if (dest_is_comptime_float) {
return sema.fail(block, operand_src, "unable to cast runtime value to 'comptime_float'", .{});
@ -13302,7 +13300,7 @@ fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
if (!lhs_val.isUndef()) {
if (try lhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
const scalar_zero = switch (scalar_tag) {
.ComptimeFloat, .Float => Value.float_zero, // TODO migrate to internpool
.ComptimeFloat, .Float => try mod.floatValue(resolved_type.scalarType(mod), 0),
.ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0),
else => unreachable,
};
@ -13441,7 +13439,7 @@ fn zirDivExact(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
} else {
if (try lhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
const scalar_zero = switch (scalar_tag) {
.ComptimeFloat, .Float => Value.float_zero, // TODO migrate to internpool
.ComptimeFloat, .Float => try mod.floatValue(resolved_type.scalarType(mod), 0),
.ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0),
else => unreachable,
};
@ -13526,7 +13524,7 @@ fn zirDivExact(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const remainder = try block.addBinOp(.rem, casted_lhs, casted_rhs);
const scalar_zero = switch (scalar_tag) {
.ComptimeFloat, .Float => Value.float_zero, // TODO migrate to internpool
.ComptimeFloat, .Float => try mod.floatValue(resolved_type.scalarType(mod), 0),
.ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0),
else => unreachable,
};
@ -13616,7 +13614,7 @@ fn zirDivFloor(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
if (!lhs_val.isUndef()) {
if (try lhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
const scalar_zero = switch (scalar_tag) {
.ComptimeFloat, .Float => Value.float_zero, // TODO migrate to internpool
.ComptimeFloat, .Float => try mod.floatValue(resolved_type.scalarType(mod), 0),
.ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0),
else => unreachable,
};
@ -13737,7 +13735,7 @@ fn zirDivTrunc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
if (!lhs_val.isUndef()) {
if (try lhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
const scalar_zero = switch (scalar_tag) {
.ComptimeFloat, .Float => Value.float_zero, // TODO migrate to internpool
.ComptimeFloat, .Float => try mod.floatValue(resolved_type.scalarType(mod), 0),
.ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0),
else => unreachable,
};
@ -13895,7 +13893,10 @@ fn addDivByZeroSafety(
if (maybe_rhs_val != null) return;
const mod = sema.mod;
const scalar_zero = if (is_int) try mod.intValue(resolved_type.scalarType(mod), 0) else Value.float_zero; // TODO migrate to internpool
const scalar_zero = if (is_int)
try mod.intValue(resolved_type.scalarType(mod), 0)
else
try mod.floatValue(resolved_type.scalarType(mod), 0);
const ok = if (resolved_type.zigTypeTag(mod) == .Vector) ok: {
const zero_val = try Value.Tag.repeated.create(sema.arena, scalar_zero);
const zero = try sema.addConstant(resolved_type, zero_val);
@ -13981,7 +13982,7 @@ fn zirModRem(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
}
if (try lhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
const scalar_zero = switch (scalar_tag) {
.ComptimeFloat, .Float => Value.float_zero, // TODO migrate to internpool
.ComptimeFloat, .Float => try mod.floatValue(resolved_type.scalarType(mod), 0),
.ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0),
else => unreachable,
};
@ -14641,7 +14642,7 @@ fn analyzeArithmetic(
} else {
return sema.addConstant(
resolved_type,
try sema.floatAdd(lhs_val, rhs_val, resolved_type),
try Value.floatAdd(lhs_val, rhs_val, resolved_type, sema.arena, mod),
);
}
} else break :rs .{ .src = rhs_src, .air_tag = air_tag };
@ -14738,7 +14739,7 @@ fn analyzeArithmetic(
} else {
return sema.addConstant(
resolved_type,
try sema.floatSub(lhs_val, rhs_val, resolved_type),
try Value.floatSub(lhs_val, rhs_val, resolved_type, sema.arena, mod),
);
}
} else break :rs .{ .src = rhs_src, .air_tag = air_tag };
@ -14808,22 +14809,25 @@ fn analyzeArithmetic(
// the result is nan.
// If either of the operands are nan, the result is nan.
const scalar_zero = switch (scalar_tag) {
.ComptimeFloat, .Float => Value.float_zero, // TODO migrate to internpool
.ComptimeFloat, .Float => try mod.floatValue(resolved_type.scalarType(mod), 0),
.ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0),
else => unreachable,
};
if (maybe_lhs_val) |lhs_val| {
if (!lhs_val.isUndef()) {
if (lhs_val.isNan()) {
if (lhs_val.isNan(mod)) {
return sema.addConstant(resolved_type, lhs_val);
}
if (try lhs_val.compareAllWithZeroAdvanced(.eq, sema)) lz: {
if (maybe_rhs_val) |rhs_val| {
if (rhs_val.isNan()) {
if (rhs_val.isNan(mod)) {
return sema.addConstant(resolved_type, rhs_val);
}
if (rhs_val.isInf()) {
return sema.addConstant(resolved_type, try Value.Tag.float_32.create(sema.arena, std.math.nan_f32));
if (rhs_val.isInf(mod)) {
return sema.addConstant(
resolved_type,
try mod.floatValue(resolved_type, std.math.nan_f128),
);
}
} else if (resolved_type.isAnyFloat()) {
break :lz;
@ -14847,13 +14851,16 @@ fn analyzeArithmetic(
return sema.addConstUndef(resolved_type);
}
}
if (rhs_val.isNan()) {
if (rhs_val.isNan(mod)) {
return sema.addConstant(resolved_type, rhs_val);
}
if (try rhs_val.compareAllWithZeroAdvanced(.eq, sema)) rz: {
if (maybe_lhs_val) |lhs_val| {
if (lhs_val.isInf()) {
return sema.addConstant(resolved_type, try Value.Tag.float_32.create(sema.arena, std.math.nan_f32));
if (lhs_val.isInf(mod)) {
return sema.addConstant(
resolved_type,
try mod.floatValue(resolved_type, std.math.nan_f128),
);
}
} else if (resolved_type.isAnyFloat()) {
break :rz;
@ -14896,7 +14903,7 @@ fn analyzeArithmetic(
// If either of the operands are one, result is the other operand.
// If either of the operands are undefined, result is undefined.
const scalar_zero = switch (scalar_tag) {
.ComptimeFloat, .Float => Value.float_zero, // TODO migrate to internpool
.ComptimeFloat, .Float => try mod.floatValue(resolved_type.scalarType(mod), 0),
.ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0),
else => unreachable,
};
@ -14944,7 +14951,7 @@ fn analyzeArithmetic(
// If either of the operands are one, result is the other operand.
// If either of the operands are undefined, result is undefined.
const scalar_zero = switch (scalar_tag) {
.ComptimeFloat, .Float => Value.float_zero, // TODO migrate to internpool
.ComptimeFloat, .Float => try mod.floatValue(resolved_type.scalarType(mod), 0),
.ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0),
else => unreachable,
};
@ -19167,7 +19174,7 @@ fn zirReify(
const new_namespace = mod.namespacePtr(new_namespace_index);
errdefer mod.destroyNamespace(new_namespace_index);
const opaque_ty = try mod.intern_pool.get(gpa, .{ .opaque_type = .{
const opaque_ty = try mod.intern(.{ .opaque_type = .{
.decl = new_decl_index,
.namespace = new_namespace_index,
} });
@ -25678,7 +25685,7 @@ fn coerceExtra(
// Keep the comptime Value representation; take the new type.
return sema.addConstant(dest_ty, val);
} else {
const new_val = try mod.intern_pool.getCoerced(mod.gpa, val.ip_index, dest_ty.ip_index);
const new_val = try mod.intern_pool.getCoerced(sema.gpa, val.ip_index, dest_ty.ip_index);
return sema.addConstant(dest_ty, new_val.toValue());
}
}
@ -26032,7 +26039,7 @@ fn coerceExtra(
break :float;
};
if (val.floatHasFraction()) {
if (val.floatHasFraction(mod)) {
return sema.fail(
block,
inst_src,
@ -26081,7 +26088,7 @@ fn coerceExtra(
.Float, .ComptimeFloat => switch (inst_ty.zigTypeTag(mod)) {
.ComptimeFloat => {
const val = try sema.resolveConstValue(block, .unneeded, inst, "");
const result_val = try val.floatCast(sema.arena, dest_ty, mod);
const result_val = try val.floatCast(dest_ty, mod);
return try sema.addConstant(dest_ty, result_val);
},
.Float => {
@ -26089,7 +26096,7 @@ fn coerceExtra(
return sema.addConstUndef(dest_ty);
}
if (try sema.resolveMaybeUndefVal(inst)) |val| {
const result_val = try val.floatCast(sema.arena, dest_ty, mod);
const result_val = try val.floatCast(dest_ty, mod);
if (!val.eql(result_val, inst_ty, sema.mod)) {
return sema.fail(
block,
@ -30071,7 +30078,7 @@ fn cmpNumeric(
if (lhs_val.isUndef() or rhs_val.isUndef()) {
return sema.addConstUndef(Type.bool);
}
if (lhs_val.isNan() or rhs_val.isNan()) {
if (lhs_val.isNan(mod) or rhs_val.isNan(mod)) {
if (op == std.math.CompareOperator.neq) {
return Air.Inst.Ref.bool_true;
} else {
@ -30166,15 +30173,15 @@ fn cmpNumeric(
try sema.resolveLazyValue(lhs_val);
if (lhs_val.isUndef())
return sema.addConstUndef(Type.bool);
if (lhs_val.isNan()) switch (op) {
if (lhs_val.isNan(mod)) switch (op) {
.neq => return Air.Inst.Ref.bool_true,
else => return Air.Inst.Ref.bool_false,
};
if (lhs_val.isInf()) switch (op) {
if (lhs_val.isInf(mod)) switch (op) {
.neq => return Air.Inst.Ref.bool_true,
.eq => return Air.Inst.Ref.bool_false,
.gt, .gte => return if (lhs_val.isNegativeInf()) Air.Inst.Ref.bool_false else Air.Inst.Ref.bool_true,
.lt, .lte => return if (lhs_val.isNegativeInf()) Air.Inst.Ref.bool_true else Air.Inst.Ref.bool_false,
.gt, .gte => return if (lhs_val.isNegativeInf(mod)) Air.Inst.Ref.bool_false else Air.Inst.Ref.bool_true,
.lt, .lte => return if (lhs_val.isNegativeInf(mod)) Air.Inst.Ref.bool_true else Air.Inst.Ref.bool_false,
};
if (!rhs_is_signed) {
switch (lhs_val.orderAgainstZero(mod)) {
@ -30191,7 +30198,7 @@ fn cmpNumeric(
}
}
if (lhs_is_float) {
if (lhs_val.floatHasFraction()) {
if (lhs_val.floatHasFraction(mod)) {
switch (op) {
.eq => return Air.Inst.Ref.bool_false,
.neq => return Air.Inst.Ref.bool_true,
@ -30201,7 +30208,7 @@ fn cmpNumeric(
var bigint = try float128IntPartToBigInt(sema.gpa, lhs_val.toFloat(f128, mod));
defer bigint.deinit();
if (lhs_val.floatHasFraction()) {
if (lhs_val.floatHasFraction(mod)) {
if (lhs_is_signed) {
try bigint.addScalar(&bigint, -1);
} else {
@ -30225,15 +30232,15 @@ fn cmpNumeric(
try sema.resolveLazyValue(rhs_val);
if (rhs_val.isUndef())
return sema.addConstUndef(Type.bool);
if (rhs_val.isNan()) switch (op) {
if (rhs_val.isNan(mod)) switch (op) {
.neq => return Air.Inst.Ref.bool_true,
else => return Air.Inst.Ref.bool_false,
};
if (rhs_val.isInf()) switch (op) {
if (rhs_val.isInf(mod)) switch (op) {
.neq => return Air.Inst.Ref.bool_true,
.eq => return Air.Inst.Ref.bool_false,
.gt, .gte => return if (rhs_val.isNegativeInf()) Air.Inst.Ref.bool_true else Air.Inst.Ref.bool_false,
.lt, .lte => return if (rhs_val.isNegativeInf()) Air.Inst.Ref.bool_false else Air.Inst.Ref.bool_true,
.gt, .gte => return if (rhs_val.isNegativeInf(mod)) Air.Inst.Ref.bool_true else Air.Inst.Ref.bool_false,
.lt, .lte => return if (rhs_val.isNegativeInf(mod)) Air.Inst.Ref.bool_false else Air.Inst.Ref.bool_true,
};
if (!lhs_is_signed) {
switch (rhs_val.orderAgainstZero(mod)) {
@ -30250,7 +30257,7 @@ fn cmpNumeric(
}
}
if (rhs_is_float) {
if (rhs_val.floatHasFraction()) {
if (rhs_val.floatHasFraction(mod)) {
switch (op) {
.eq => return Air.Inst.Ref.bool_false,
.neq => return Air.Inst.Ref.bool_true,
@ -30260,7 +30267,7 @@ fn cmpNumeric(
var bigint = try float128IntPartToBigInt(sema.gpa, rhs_val.toFloat(f128, mod));
defer bigint.deinit();
if (rhs_val.floatHasFraction()) {
if (rhs_val.floatHasFraction(mod)) {
if (rhs_is_signed) {
try bigint.addScalar(&bigint, -1);
} else {
@ -31713,6 +31720,7 @@ pub fn resolveTypeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
.simple_value => unreachable,
.extern_func => unreachable,
.int => unreachable,
.float => unreachable,
.ptr => unreachable,
.opt => unreachable,
.enum_tag => unreachable,
@ -33216,6 +33224,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
.simple_value => unreachable,
.extern_func => unreachable,
.int => unreachable,
.float => unreachable,
.ptr => unreachable,
.opt => unreachable,
.enum_tag => unreachable,
@ -33777,6 +33786,7 @@ pub fn typeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
.simple_value => unreachable,
.extern_func => unreachable,
.int => unreachable,
.float => unreachable,
.ptr => unreachable,
.opt => unreachable,
.enum_tag => unreachable,
@ -33935,7 +33945,7 @@ fn numberAddWrapScalar(
}
if (ty.isAnyFloat()) {
return sema.floatAdd(lhs, rhs, ty);
return Value.floatAdd(lhs, rhs, ty, sema.arena, mod);
}
const overflow_result = try sema.intAddWithOverflow(lhs, rhs, ty);
@ -33989,127 +33999,13 @@ fn numberSubWrapScalar(
}
if (ty.isAnyFloat()) {
return sema.floatSub(lhs, rhs, ty);
return Value.floatSub(lhs, rhs, ty, sema.arena, mod);
}
const overflow_result = try sema.intSubWithOverflow(lhs, rhs, ty);
return overflow_result.wrapped_result;
}
fn floatAdd(
sema: *Sema,
lhs: Value,
rhs: Value,
float_type: Type,
) !Value {
const mod = sema.mod;
if (float_type.zigTypeTag(mod) == .Vector) {
const result_data = try sema.arena.alloc(Value, float_type.vectorLen(mod));
for (result_data, 0..) |*scalar, i| {
const lhs_elem = try lhs.elemValue(sema.mod, i);
const rhs_elem = try rhs.elemValue(sema.mod, i);
scalar.* = try sema.floatAddScalar(lhs_elem, rhs_elem, float_type.scalarType(mod));
}
return Value.Tag.aggregate.create(sema.arena, result_data);
}
return sema.floatAddScalar(lhs, rhs, float_type);
}
fn floatAddScalar(
sema: *Sema,
lhs: Value,
rhs: Value,
float_type: Type,
) !Value {
const mod = sema.mod;
const target = sema.mod.getTarget();
switch (float_type.floatBits(target)) {
16 => {
const lhs_val = lhs.toFloat(f16, mod);
const rhs_val = rhs.toFloat(f16, mod);
return Value.Tag.float_16.create(sema.arena, lhs_val + rhs_val);
},
32 => {
const lhs_val = lhs.toFloat(f32, mod);
const rhs_val = rhs.toFloat(f32, mod);
return Value.Tag.float_32.create(sema.arena, lhs_val + rhs_val);
},
64 => {
const lhs_val = lhs.toFloat(f64, mod);
const rhs_val = rhs.toFloat(f64, mod);
return Value.Tag.float_64.create(sema.arena, lhs_val + rhs_val);
},
80 => {
const lhs_val = lhs.toFloat(f80, mod);
const rhs_val = rhs.toFloat(f80, mod);
return Value.Tag.float_80.create(sema.arena, lhs_val + rhs_val);
},
128 => {
const lhs_val = lhs.toFloat(f128, mod);
const rhs_val = rhs.toFloat(f128, mod);
return Value.Tag.float_128.create(sema.arena, lhs_val + rhs_val);
},
else => unreachable,
}
}
fn floatSub(
sema: *Sema,
lhs: Value,
rhs: Value,
float_type: Type,
) !Value {
const mod = sema.mod;
if (float_type.zigTypeTag(mod) == .Vector) {
const result_data = try sema.arena.alloc(Value, float_type.vectorLen(mod));
for (result_data, 0..) |*scalar, i| {
const lhs_elem = try lhs.elemValue(sema.mod, i);
const rhs_elem = try rhs.elemValue(sema.mod, i);
scalar.* = try sema.floatSubScalar(lhs_elem, rhs_elem, float_type.scalarType(mod));
}
return Value.Tag.aggregate.create(sema.arena, result_data);
}
return sema.floatSubScalar(lhs, rhs, float_type);
}
fn floatSubScalar(
sema: *Sema,
lhs: Value,
rhs: Value,
float_type: Type,
) !Value {
const mod = sema.mod;
const target = sema.mod.getTarget();
switch (float_type.floatBits(target)) {
16 => {
const lhs_val = lhs.toFloat(f16, mod);
const rhs_val = rhs.toFloat(f16, mod);
return Value.Tag.float_16.create(sema.arena, lhs_val - rhs_val);
},
32 => {
const lhs_val = lhs.toFloat(f32, mod);
const rhs_val = rhs.toFloat(f32, mod);
return Value.Tag.float_32.create(sema.arena, lhs_val - rhs_val);
},
64 => {
const lhs_val = lhs.toFloat(f64, mod);
const rhs_val = rhs.toFloat(f64, mod);
return Value.Tag.float_64.create(sema.arena, lhs_val - rhs_val);
},
80 => {
const lhs_val = lhs.toFloat(f80, mod);
const rhs_val = rhs.toFloat(f80, mod);
return Value.Tag.float_80.create(sema.arena, lhs_val - rhs_val);
},
128 => {
const lhs_val = lhs.toFloat(f128, mod);
const rhs_val = rhs.toFloat(f128, mod);
return Value.Tag.float_128.create(sema.arena, lhs_val - rhs_val);
},
else => unreachable,
}
}
fn intSubWithOverflow(
sema: *Sema,
lhs: Value,

View File

@ -283,11 +283,6 @@ pub fn print(
}
return writer.writeAll(" }");
},
.float_16 => return writer.print("{d}", .{val.castTag(.float_16).?.data}),
.float_32 => return writer.print("{d}", .{val.castTag(.float_32).?.data}),
.float_64 => return writer.print("{d}", .{val.castTag(.float_64).?.data}),
.float_80 => return writer.print("{d}", .{@floatCast(f64, val.castTag(.float_80).?.data)}),
.float_128 => return writer.print("{d}", .{@floatCast(f64, val.castTag(.float_128).?.data)}),
.@"error" => return writer.print("error.{s}", .{val.castTag(.@"error").?.data.name}),
.eu_payload => {
val = val.castTag(.eu_payload).?.data;
@ -363,6 +358,9 @@ pub fn print(
.int => |int| switch (int.storage) {
inline .u64, .i64, .big_int => |x| return writer.print("{}", .{x}),
},
.float => |float| switch (float.storage) {
inline else => |x| return writer.print("{}", .{x}),
},
else => return writer.print("{}", .{val.ip_index}),
}
},

View File

@ -6612,7 +6612,6 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
const mod = f.object.dg.module;
const reduce = f.air.instructions.items(.data)[inst].reduce;
const target = mod.getTarget();
const scalar_ty = f.typeOfIndex(inst);
const operand = try f.resolveInst(reduce.operand);
try reap(f, inst, &.{reduce.operand});
@ -6679,16 +6678,6 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
var arena = std.heap.ArenaAllocator.init(f.object.dg.gpa);
defer arena.deinit();
const ExpectedContents = union {
f16: Value.Payload.Float_16,
f32: Value.Payload.Float_32,
f64: Value.Payload.Float_64,
f80: Value.Payload.Float_80,
f128: Value.Payload.Float_128,
};
var stack align(@alignOf(ExpectedContents)) =
std.heap.stackFallback(@sizeOf(ExpectedContents), arena.allocator());
try f.object.dg.renderValue(writer, scalar_ty, switch (reduce.operation) {
.Or, .Xor, .Add => try mod.intValue(scalar_ty, 0),
.And => switch (scalar_ty.zigTypeTag(mod)) {
@ -6701,13 +6690,13 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
.Min => switch (scalar_ty.zigTypeTag(mod)) {
.Bool => Value.one_comptime_int,
.Int => try scalar_ty.maxIntScalar(mod, scalar_ty),
.Float => try Value.floatToValue(std.math.nan(f128), stack.get(), scalar_ty, target),
.Float => try mod.floatValue(scalar_ty, std.math.nan_f128),
else => unreachable,
},
.Max => switch (scalar_ty.zigTypeTag(mod)) {
.Bool => try mod.intValue(scalar_ty, 0),
.Int => try scalar_ty.minInt(stack.get(), mod),
.Float => try Value.floatToValue(std.math.nan(f128), stack.get(), scalar_ty, target),
.Int => try scalar_ty.minInt(arena.allocator(), mod),
.Float => try mod.floatValue(scalar_ty, std.math.nan_f128),
else => unreachable,
},
.Mul => try mod.intValue(Type.comptime_int, 1),

View File

@ -9238,22 +9238,19 @@ pub const FuncGen = struct {
}) catch unreachable,
else => unreachable,
};
var init_value_payload = Value.Payload.Float_32{
.data = switch (reduce.operation) {
.Min => std.math.nan(f32),
.Max => std.math.nan(f32),
.Add => -0.0,
.Mul => 1.0,
else => unreachable,
},
};
const param_llvm_ty = try self.dg.lowerType(scalar_ty);
const param_types = [2]*llvm.Type{ param_llvm_ty, param_llvm_ty };
const libc_fn = self.getLibcFunction(fn_name, &param_types, param_llvm_ty);
const init_value = try self.dg.lowerValue(.{
.ty = scalar_ty,
.val = Value.initPayload(&init_value_payload.base),
.val = try mod.floatValue(scalar_ty, switch (reduce.operation) {
.Min => std.math.nan(f32),
.Max => std.math.nan(f32),
.Add => -0.0,
.Mul => 1.0,
else => unreachable,
}),
});
return self.buildReducedCall(libc_fn, operand, operand_ty.vectorLen(mod), init_value);
}

View File

@ -133,6 +133,7 @@ pub const Type = struct {
.un => unreachable,
.extern_func => unreachable,
.int => unreachable,
.float => unreachable,
.ptr => unreachable,
.opt => unreachable,
.enum_tag => unreachable,
@ -1434,6 +1435,7 @@ pub const Type = struct {
.simple_value => unreachable,
.extern_func => unreachable,
.int => unreachable,
.float => unreachable,
.ptr => unreachable,
.opt => unreachable,
.enum_tag => unreachable,
@ -1687,6 +1689,7 @@ pub const Type = struct {
.simple_value => unreachable,
.extern_func => unreachable,
.int => unreachable,
.float => unreachable,
.ptr => unreachable,
.opt => unreachable,
.enum_tag => unreachable,
@ -1803,6 +1806,7 @@ pub const Type = struct {
.simple_value => unreachable,
.extern_func => unreachable,
.int => unreachable,
.float => unreachable,
.ptr => unreachable,
.opt => unreachable,
.enum_tag => unreachable,
@ -2195,6 +2199,7 @@ pub const Type = struct {
.simple_value => unreachable,
.extern_func => unreachable,
.int => unreachable,
.float => unreachable,
.ptr => unreachable,
.opt => unreachable,
.enum_tag => unreachable,
@ -2612,6 +2617,7 @@ pub const Type = struct {
.simple_value => unreachable,
.extern_func => unreachable,
.int => unreachable,
.float => unreachable,
.ptr => unreachable,
.opt => unreachable,
.enum_tag => unreachable,
@ -2866,6 +2872,7 @@ pub const Type = struct {
.simple_value => unreachable,
.extern_func => unreachable,
.int => unreachable,
.float => unreachable,
.ptr => unreachable,
.opt => unreachable,
.enum_tag => unreachable,
@ -3632,6 +3639,7 @@ pub const Type = struct {
.simple_value => unreachable,
.extern_func => unreachable,
.int => unreachable,
.float => unreachable,
.ptr => unreachable,
.opt => unreachable,
.enum_tag => unreachable,
@ -3996,6 +4004,7 @@ pub const Type = struct {
.simple_value => unreachable,
.extern_func => unreachable,
.int => unreachable,
.float => unreachable,
.ptr => unreachable,
.opt => unreachable,
.enum_tag => unreachable,
@ -4157,6 +4166,7 @@ pub const Type = struct {
.simple_value => unreachable,
.extern_func => unreachable,
.int => unreachable,
.float => unreachable,
.ptr => unreachable,
.opt => unreachable,
.enum_tag => unreachable,

File diff suppressed because it is too large Load Diff