Skip to content

Commit

Permalink
Value: handle int -> complex float conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
ehaas committed Apr 1, 2024
1 parent aed02e7 commit 576f880
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/aro/Value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,19 @@ pub fn floatToInt(v: *Value, dest_ty: Type, comp: *Compilation) !FloatToIntChang
/// `.none` value remains unchanged.
pub fn intToFloat(v: *Value, dest_ty: Type, comp: *Compilation) !void {
if (v.opt_ref == .none) return;

if (dest_ty.isComplex()) {
const bits = dest_ty.bitSizeof(comp).?;
const cf: Interner.Key.Complex = switch (bits) {
64 => .{ .cf32 = .{ v.toFloat(f32, comp), 0 } },
128 => .{ .cf64 = .{ v.toFloat(f64, comp), 0 } },
160 => .{ .cf80 = .{ v.toFloat(f80, comp), 0 } },
256 => .{ .cf128 = .{ v.toFloat(f128, comp), 0 } },
else => unreachable,
};
v.* = try intern(comp, .{ .complex = cf });
return;
}
const bits = dest_ty.bitSizeof(comp).?;
return switch (comp.interner.get(v.ref()).int) {
inline .u64, .i64 => |data| {
Expand Down
2 changes: 2 additions & 0 deletions test/cases/complex values.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ _Complex double a = (_Complex double) {1.0, 2.0,};
_Complex double b = (_Complex double) {1.0, 2.0,,,,,,};
_Complex double c = (_Complex double) {1.0, 2.0,3.0};

_Static_assert(3 + 4.0il == 3 + 4.0il, "");
_Static_assert(5ll + 4.0il == 5ll + 4.0il, "");

#define EXPECTED_ERRORS "complex values.c:31:49: error: expected expression" \
"complex values.c:32:49: warning: excess elements in scalar initializer [-Wexcess-initializers]" \
Expand Down

0 comments on commit 576f880

Please sign in to comment.