mirror of
https://github.com/ziglang/zig.git
synced 2026-02-13 04:48:20 +00:00
parent
7a8d9af4a9
commit
8b58dab78b
@ -17499,7 +17499,7 @@ fn zirIntToFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
|
||||
|
||||
if (try sema.resolveMaybeUndefVal(block, operand_src, operand)) |val| {
|
||||
const target = sema.mod.getTarget();
|
||||
const result_val = try val.intToFloat(sema.arena, operand_ty, dest_ty, target);
|
||||
const result_val = try val.intToFloatAdvanced(sema.arena, operand_ty, dest_ty, target, sema.kit(block, operand_src));
|
||||
return sema.addConstant(dest_ty, result_val);
|
||||
} else if (dest_ty.zigTypeTag() == .ComptimeFloat) {
|
||||
return sema.failWithNeededComptime(block, operand_src, "value being casted to 'comptime_float' must be comptime known");
|
||||
@ -22998,7 +22998,7 @@ fn coerceExtra(
|
||||
}
|
||||
break :int;
|
||||
};
|
||||
const result_val = try val.intToFloat(sema.arena, inst_ty, dest_ty, target);
|
||||
const result_val = try val.intToFloatAdvanced(sema.arena, inst_ty, dest_ty, target, sema.kit(block, inst_src));
|
||||
// TODO implement this compile error
|
||||
//const int_again_val = try result_val.floatToInt(sema.arena, inst_ty);
|
||||
//if (!int_again_val.eql(val, inst_ty, mod)) {
|
||||
|
||||
@ -2940,17 +2940,24 @@ pub const Value = extern union {
|
||||
}
|
||||
|
||||
pub fn intToFloat(val: Value, arena: Allocator, int_ty: Type, float_ty: Type, target: Target) !Value {
|
||||
return intToFloatAdvanced(val, arena, int_ty, float_ty, target, null) catch |err| switch (err) {
|
||||
error.OutOfMemory => return error.OutOfMemory,
|
||||
else => unreachable,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn intToFloatAdvanced(val: Value, arena: Allocator, int_ty: Type, float_ty: Type, target: Target, sema_kit: ?Module.WipAnalysis) !Value {
|
||||
if (int_ty.zigTypeTag() == .Vector) {
|
||||
const result_data = try arena.alloc(Value, int_ty.vectorLen());
|
||||
for (result_data) |*scalar, i| {
|
||||
scalar.* = try intToFloatScalar(val.indexVectorlike(i), arena, float_ty.scalarType(), target);
|
||||
scalar.* = try intToFloatScalar(val.indexVectorlike(i), arena, float_ty.scalarType(), target, sema_kit);
|
||||
}
|
||||
return Value.Tag.aggregate.create(arena, result_data);
|
||||
}
|
||||
return intToFloatScalar(val, arena, float_ty, target);
|
||||
return intToFloatScalar(val, arena, float_ty, target, sema_kit);
|
||||
}
|
||||
|
||||
pub fn intToFloatScalar(val: Value, arena: Allocator, float_ty: Type, target: Target) !Value {
|
||||
pub fn intToFloatScalar(val: Value, arena: Allocator, float_ty: Type, target: Target, sema_kit: ?Module.WipAnalysis) !Value {
|
||||
switch (val.tag()) {
|
||||
.undef, .zero, .one => return val,
|
||||
.the_only_possible_value => return Value.initTag(.zero), // for i0, u0
|
||||
@ -2970,6 +2977,22 @@ pub const Value = extern union {
|
||||
const float = bigIntToFloat(limbs, false);
|
||||
return floatToValue(float, arena, float_ty, target);
|
||||
},
|
||||
.lazy_align => {
|
||||
const ty = val.castTag(.lazy_align).?.data;
|
||||
if (sema_kit) |sk| {
|
||||
return intToFloatInner((try ty.abiAlignmentAdvanced(target, .{ .sema_kit = sk })).scalar, arena, float_ty, target);
|
||||
} else {
|
||||
return intToFloatInner(ty.abiAlignment(target), arena, float_ty, target);
|
||||
}
|
||||
},
|
||||
.lazy_size => {
|
||||
const ty = val.castTag(.lazy_size).?.data;
|
||||
if (sema_kit) |sk| {
|
||||
return intToFloatInner((try ty.abiSizeAdvanced(target, .{ .sema_kit = sk })).scalar, arena, float_ty, target);
|
||||
} else {
|
||||
return intToFloatInner(ty.abiSize(target), arena, float_ty, target);
|
||||
}
|
||||
},
|
||||
else => unreachable,
|
||||
}
|
||||
}
|
||||
|
||||
@ -301,3 +301,14 @@ test "array access of generic param in typeof expression" {
|
||||
try expect(S.first("a") == 'a');
|
||||
comptime try expect(S.first("a") == 'a');
|
||||
}
|
||||
|
||||
test "lazy size cast to float" {
|
||||
{
|
||||
const S = struct { a: u8 };
|
||||
try expect(@intToFloat(f32, @sizeOf(S)) == 1.0);
|
||||
}
|
||||
{
|
||||
const S = struct { a: u8 };
|
||||
try expect(@as(f32, @sizeOf(S)) == 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user