compiler_rt: attempt to fix f16 abi on mac with llvm 16

This commit is contained in:
Jacob Young 2023-03-27 15:52:15 -04:00 committed by Andrew Kelley
parent a4d7b7edc1
commit 243848167b
3 changed files with 107 additions and 90 deletions

View File

@ -84,7 +84,7 @@ pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, _: ?
pub const F16T = switch (builtin.cpu.arch) {
.aarch64, .aarch64_be, .aarch64_32 => f16,
.riscv64 => if (builtin.zig_backend == .stage1) u16 else f16,
.x86, .x86_64 => f16,
.x86, .x86_64 => if (builtin.target.isDarwin()) u16 else f16,
else => u16,
};

157
lib/zig.h
View File

@ -502,6 +502,15 @@ typedef ptrdiff_t intptr_t;
#endif
#define zig_make_small_i8(val) INT8_C(val)
#define zig_make_small_u8(val) UINT8_C(val)
#define zig_make_small_i16(val) INT16_C(val)
#define zig_make_small_u16(val) UINT16_C(val)
#define zig_make_small_i32(val) INT32_C(val)
#define zig_make_small_u32(val) UINT32_C(val)
#define zig_make_small_i64(val) INT64_C(val)
#define zig_make_small_u64(val) UINT64_C(val)
#define zig_minInt_i8 INT8_MIN
#define zig_maxInt_i8 INT8_MAX
#define zig_minInt_u8 UINT8_C(0)
@ -585,7 +594,7 @@ typedef ptrdiff_t intptr_t;
} \
\
static inline int##w##_t zig_shlw_i##w(int##w##_t lhs, uint8_t rhs, uint8_t bits) { \
return zig_wrap_i##w((int##w##_t)zig_shl_u##w((uint##w##_t)lhs, (uint##w##_t)rhs), bits); \
return zig_wrap_i##w((int##w##_t)zig_shl_u##w((uint##w##_t)lhs, rhs), bits); \
} \
\
static inline uint##w##_t zig_addw_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \
@ -1294,6 +1303,9 @@ typedef struct { zig_align(16) int64_t hi; uint64_t lo; } zig_i128;
#endif /* zig_has_int128 */
#define zig_make_small_u128(val) zig_make_u128(0, val)
#define zig_make_small_i128(val) zig_make_i128((val) < 0 ? -INT64_C(1) : INT64_C(0), val)
#define zig_minInt_u128 zig_make_u128(zig_minInt_u64, zig_minInt_u64)
#define zig_maxInt_u128 zig_make_u128(zig_maxInt_u64, zig_maxInt_u64)
#define zig_minInt_i128 zig_make_i128(zig_minInt_i64, zig_minInt_u64)
@ -2828,7 +2840,7 @@ static inline uint16_t zig_popcount_big(const void *val, bool is_signed, uint16_
#define zig_has_f16 1
#define zig_bitSizeOf_f16 16
typedef int16_t zig_repr_f16;
typedef uint16_t zig_repr_f16;
#define zig_libc_name_f16(name) __##name##h
#define zig_init_special_f16(sign, name, arg, repr) zig_make_special_f16(sign, name, arg, repr)
#if FLT_MANT_DIG == 11
@ -2854,7 +2866,7 @@ typedef __fp16 zig_f16;
#undef zig_has_f16
#define zig_has_f16 0
#define zig_bitSizeOf_repr_f16 16
typedef int16_t zig_f16;
typedef zig_repr_f16 zig_f16;
#define zig_make_f16(fp, repr) repr
#undef zig_make_special_f16
#define zig_make_special_f16(sign, name, arg, repr) repr
@ -2864,7 +2876,7 @@ typedef int16_t zig_f16;
#define zig_has_f32 1
#define zig_bitSizeOf_f32 32
typedef int32_t zig_repr_f32;
typedef uint32_t zig_repr_f32;
#define zig_libc_name_f32(name) name##f
#if _MSC_VER
#define zig_init_special_f32(sign, name, arg, repr) sign zig_make_f32(zig_msvc_flt_##name, )
@ -2891,7 +2903,7 @@ typedef _Float32 zig_f32;
#undef zig_has_f32
#define zig_has_f32 0
#define zig_bitSizeOf_repr_f32 32
typedef int32_t zig_f32;
typedef zig_repr_f32 zig_f32;
#define zig_make_f32(fp, repr) repr
#undef zig_make_special_f32
#define zig_make_special_f32(sign, name, arg, repr) repr
@ -2901,7 +2913,7 @@ typedef int32_t zig_f32;
#define zig_has_f64 1
#define zig_bitSizeOf_f64 64
typedef int64_t zig_repr_f64;
typedef uint64_t zig_repr_f64;
#define zig_libc_name_f64(name) name
#if _MSC_VER
#ifdef ZIG_TARGET_ABI_MSVC
@ -2937,7 +2949,7 @@ typedef _Float32x zig_f64;
#undef zig_has_f64
#define zig_has_f64 0
#define zig_bitSizeOf_repr_f64 64
typedef int64_t zig_f64;
typedef zig_repr_f64 zig_f64;
#define zig_make_f64(fp, repr) repr
#undef zig_make_special_f64
#define zig_make_special_f64(sign, name, arg, repr) repr
@ -2947,7 +2959,7 @@ typedef int64_t zig_f64;
#define zig_has_f80 1
#define zig_bitSizeOf_f80 80
typedef zig_i128 zig_repr_f80;
typedef zig_u128 zig_repr_f80;
#define zig_libc_name_f80(name) __##name##x
#define zig_init_special_f80(sign, name, arg, repr) zig_make_special_f80(sign, name, arg, repr)
#if FLT_MANT_DIG == 64
@ -2976,7 +2988,7 @@ typedef __float80 zig_f80;
#undef zig_has_f80
#define zig_has_f80 0
#define zig_bitSizeOf_repr_f80 128
typedef zig_i128 zig_f80;
typedef zig_repr_f80 zig_f80;
#define zig_make_f80(fp, repr) repr
#undef zig_make_special_f80
#define zig_make_special_f80(sign, name, arg, repr) repr
@ -2986,7 +2998,7 @@ typedef zig_i128 zig_f80;
#define zig_has_f128 1
#define zig_bitSizeOf_f128 128
typedef zig_i128 zig_repr_f128;
typedef zig_u128 zig_repr_f128;
#define zig_libc_name_f128(name) name##q
#define zig_init_special_f128(sign, name, arg, repr) zig_make_special_f128(sign, name, arg, repr)
#if FLT_MANT_DIG == 113
@ -3017,7 +3029,7 @@ typedef __float128 zig_f128;
#undef zig_has_f128
#define zig_has_f128 0
#define zig_bitSizeOf_repr_f128 128
typedef zig_i128 zig_f128;
typedef zig_repr_f128 zig_f128;
#define zig_make_f128(fp, repr) repr
#undef zig_make_special_f128
#define zig_make_special_f128(sign, name, arg, repr) repr
@ -3097,15 +3109,18 @@ zig_convert_builtin(zig_f128, extend, zig_f32, 2)
zig_convert_builtin(zig_f128, extend, zig_f64, 2)
zig_convert_builtin(zig_f128, extend, zig_f80, 2)
#define zig_float_negate_builtin_0(Type) \
static inline zig_##Type zig_neg_##Type(zig_##Type arg) { \
return zig_expand_concat(zig_xor_i, zig_bitSizeOf_repr_##Type)( \
#define zig_float_negate_builtin_0(w) \
static inline zig_f##w zig_neg_f##w(zig_f##w arg) { \
return zig_expand_concat(zig_xor_u, zig_bitSizeOf_repr_f##w)( \
arg, \
zig_minInt_i(zig_bitSizeOf_repr_##Type, zig_bitSizeOf_##Type) \
zig_expand_concat(zig_shl_u, zig_bitSizeOf_repr_f##w)( \
zig_expand_concat(zig_make_small_u, zig_bitSizeOf_repr_f##w)(1), \
UINT8_C(w - 1) \
) \
); \
}
#define zig_float_negate_builtin_1(Type) \
static inline zig_##Type zig_neg_##Type(zig_##Type arg) { \
#define zig_float_negate_builtin_1(w) \
static inline zig_f##w zig_neg_f##w(zig_f##w arg) { \
return -arg; \
}
@ -3138,66 +3153,66 @@ zig_convert_builtin(zig_f128, extend, zig_f80, 2)
return lhs operator rhs; \
}
#define zig_float_builtins(Type) \
zig_convert_builtin( int32_t, fix, zig_##Type, ) \
zig_convert_builtin(uint32_t, fixuns, zig_##Type, ) \
zig_convert_builtin( int64_t, fix, zig_##Type, ) \
zig_convert_builtin(uint64_t, fixuns, zig_##Type, ) \
zig_convert_builtin(zig_i128, fix, zig_##Type, ) \
zig_convert_builtin(zig_u128, fixuns, zig_##Type, ) \
zig_convert_builtin(zig_##Type, float, int32_t, ) \
zig_convert_builtin(zig_##Type, floatun, uint32_t, ) \
zig_convert_builtin(zig_##Type, float, int64_t, ) \
zig_convert_builtin(zig_##Type, floatun, uint64_t, ) \
zig_convert_builtin(zig_##Type, float, zig_i128, ) \
zig_convert_builtin(zig_##Type, floatun, zig_u128, ) \
zig_expand_concat(zig_float_negate_builtin_, zig_has_##Type)(Type) \
zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, cmp) \
zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, ne) \
zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, eq) \
zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, lt) \
zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, le) \
zig_expand_concat(zig_float_greater_builtin_, zig_has_##Type)(Type, gt) \
zig_expand_concat(zig_float_greater_builtin_, zig_has_##Type)(Type, ge) \
zig_expand_concat(zig_float_binary_builtin_, zig_has_##Type)(Type, add, +) \
zig_expand_concat(zig_float_binary_builtin_, zig_has_##Type)(Type, sub, -) \
zig_expand_concat(zig_float_binary_builtin_, zig_has_##Type)(Type, mul, *) \
zig_expand_concat(zig_float_binary_builtin_, zig_has_##Type)(Type, div, /) \
zig_extern zig_##Type zig_libc_name_##Type(sqrt)(zig_##Type); \
zig_extern zig_##Type zig_libc_name_##Type(sin)(zig_##Type); \
zig_extern zig_##Type zig_libc_name_##Type(cos)(zig_##Type); \
zig_extern zig_##Type zig_libc_name_##Type(tan)(zig_##Type); \
zig_extern zig_##Type zig_libc_name_##Type(exp)(zig_##Type); \
zig_extern zig_##Type zig_libc_name_##Type(exp2)(zig_##Type); \
zig_extern zig_##Type zig_libc_name_##Type(log)(zig_##Type); \
zig_extern zig_##Type zig_libc_name_##Type(log2)(zig_##Type); \
zig_extern zig_##Type zig_libc_name_##Type(log10)(zig_##Type); \
zig_extern zig_##Type zig_libc_name_##Type(fabs)(zig_##Type); \
zig_extern zig_##Type zig_libc_name_##Type(floor)(zig_##Type); \
zig_extern zig_##Type zig_libc_name_##Type(ceil)(zig_##Type); \
zig_extern zig_##Type zig_libc_name_##Type(round)(zig_##Type); \
zig_extern zig_##Type zig_libc_name_##Type(trunc)(zig_##Type); \
zig_extern zig_##Type zig_libc_name_##Type(fmod)(zig_##Type, zig_##Type); \
zig_extern zig_##Type zig_libc_name_##Type(fmin)(zig_##Type, zig_##Type); \
zig_extern zig_##Type zig_libc_name_##Type(fmax)(zig_##Type, zig_##Type); \
zig_extern zig_##Type zig_libc_name_##Type(fma)(zig_##Type, zig_##Type, zig_##Type); \
#define zig_float_builtins(w) \
zig_convert_builtin( int32_t, fix, zig_f##w, ) \
zig_convert_builtin(uint32_t, fixuns, zig_f##w, ) \
zig_convert_builtin( int64_t, fix, zig_f##w, ) \
zig_convert_builtin(uint64_t, fixuns, zig_f##w, ) \
zig_convert_builtin(zig_i128, fix, zig_f##w, ) \
zig_convert_builtin(zig_u128, fixuns, zig_f##w, ) \
zig_convert_builtin(zig_f##w, float, int32_t, ) \
zig_convert_builtin(zig_f##w, floatun, uint32_t, ) \
zig_convert_builtin(zig_f##w, float, int64_t, ) \
zig_convert_builtin(zig_f##w, floatun, uint64_t, ) \
zig_convert_builtin(zig_f##w, float, zig_i128, ) \
zig_convert_builtin(zig_f##w, floatun, zig_u128, ) \
zig_expand_concat(zig_float_negate_builtin_, zig_has_f##w)(w) \
zig_expand_concat(zig_float_less_builtin_, zig_has_f##w)(f##w, cmp) \
zig_expand_concat(zig_float_less_builtin_, zig_has_f##w)(f##w, ne) \
zig_expand_concat(zig_float_less_builtin_, zig_has_f##w)(f##w, eq) \
zig_expand_concat(zig_float_less_builtin_, zig_has_f##w)(f##w, lt) \
zig_expand_concat(zig_float_less_builtin_, zig_has_f##w)(f##w, le) \
zig_expand_concat(zig_float_greater_builtin_, zig_has_f##w)(f##w, gt) \
zig_expand_concat(zig_float_greater_builtin_, zig_has_f##w)(f##w, ge) \
zig_expand_concat(zig_float_binary_builtin_, zig_has_f##w)(f##w, add, +) \
zig_expand_concat(zig_float_binary_builtin_, zig_has_f##w)(f##w, sub, -) \
zig_expand_concat(zig_float_binary_builtin_, zig_has_f##w)(f##w, mul, *) \
zig_expand_concat(zig_float_binary_builtin_, zig_has_f##w)(f##w, div, /) \
zig_extern zig_f##w zig_libc_name_f##w(sqrt)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(sin)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(cos)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(tan)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(exp)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(exp2)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(log)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(log2)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(log10)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(fabs)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(floor)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(ceil)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(round)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(trunc)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(fmod)(zig_f##w, zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(fmin)(zig_f##w, zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(fmax)(zig_f##w, zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(fma)(zig_f##w, zig_f##w, zig_f##w); \
\
static inline zig_##Type zig_div_trunc_##Type(zig_##Type lhs, zig_##Type rhs) { \
return zig_libc_name_##Type(trunc)(zig_div_##Type(lhs, rhs)); \
static inline zig_f##w zig_div_trunc_f##w(zig_f##w lhs, zig_f##w rhs) { \
return zig_libc_name_f##w(trunc)(zig_div_f##w(lhs, rhs)); \
} \
\
static inline zig_##Type zig_div_floor_##Type(zig_##Type lhs, zig_##Type rhs) { \
return zig_libc_name_##Type(floor)(zig_div_##Type(lhs, rhs)); \
static inline zig_f##w zig_div_floor_f##w(zig_f##w lhs, zig_f##w rhs) { \
return zig_libc_name_f##w(floor)(zig_div_f##w(lhs, rhs)); \
} \
\
static inline zig_##Type zig_mod_##Type(zig_##Type lhs, zig_##Type rhs) { \
return zig_sub_##Type(lhs, zig_mul_##Type(zig_div_floor_##Type(lhs, rhs), rhs)); \
static inline zig_f##w zig_mod_f##w(zig_f##w lhs, zig_f##w rhs) { \
return zig_sub_f##w(lhs, zig_mul_f##w(zig_div_floor_f##w(lhs, rhs), rhs)); \
}
zig_float_builtins(f16)
zig_float_builtins(f32)
zig_float_builtins(f64)
zig_float_builtins(f80)
zig_float_builtins(f128)
zig_float_builtins(16)
zig_float_builtins(32)
zig_float_builtins(64)
zig_float_builtins(80)
zig_float_builtins(128)
#if _MSC_VER && (_M_IX86 || _M_X64)

View File

@ -751,8 +751,8 @@ pub const DeclGen = struct {
.Int, .Enum, .ErrorSet => return writer.print("{x}", .{try dg.fmtIntLiteral(ty, val, location)}),
.Float => {
const bits = ty.floatBits(target);
var int_pl = Type.Payload.Bits{ .base = .{ .tag = .int_signed }, .data = bits };
const int_ty = Type.initPayload(&int_pl.base);
var repr_pl = Type.Payload.Bits{ .base = .{ .tag = .int_unsigned }, .data = bits };
const repr_ty = Type.initPayload(&repr_pl.base);
try writer.writeAll("zig_cast_");
try dg.renderTypeForBuiltinFnName(writer, ty);
@ -768,7 +768,7 @@ pub const DeclGen = struct {
else => unreachable,
}
try writer.writeAll(", ");
try dg.renderValue(writer, int_ty, Value.undef, .FunctionArgument);
try dg.renderValue(writer, repr_ty, Value.undef, .FunctionArgument);
return writer.writeByte(')');
},
.Pointer => if (ty.isSlice()) {
@ -935,31 +935,33 @@ pub const DeclGen = struct {
const bits = ty.floatBits(target);
const f128_val = val.toFloat(f128);
var int_ty_pl = Type.Payload.Bits{ .base = .{ .tag = .int_signed }, .data = bits };
const int_ty = Type.initPayload(&int_ty_pl.base);
var repr_ty_pl = Type.Payload.Bits{ .base = .{ .tag = .int_unsigned }, .data = bits };
const repr_ty = Type.initPayload(&repr_ty_pl.base);
assert(bits <= 128);
var int_val_limbs: [BigInt.calcTwosCompLimbCount(128)]BigIntLimb = undefined;
var int_val_big = BigInt.Mutable{
.limbs = &int_val_limbs,
var repr_val_limbs: [BigInt.calcTwosCompLimbCount(128)]BigIntLimb = undefined;
var repr_val_big = BigInt.Mutable{
.limbs = &repr_val_limbs,
.len = undefined,
.positive = undefined,
};
switch (bits) {
16 => int_val_big.set(@bitCast(i16, val.toFloat(f16))),
32 => int_val_big.set(@bitCast(i32, val.toFloat(f32))),
64 => int_val_big.set(@bitCast(i64, val.toFloat(f64))),
80 => int_val_big.set(@bitCast(i80, val.toFloat(f80))),
128 => int_val_big.set(@bitCast(i128, f128_val)),
16 => repr_val_big.set(@bitCast(u16, val.toFloat(f16))),
32 => repr_val_big.set(@bitCast(u32, val.toFloat(f32))),
64 => repr_val_big.set(@bitCast(u64, val.toFloat(f64))),
80 => repr_val_big.set(@bitCast(u80, val.toFloat(f80))),
128 => repr_val_big.set(@bitCast(u128, f128_val)),
else => unreachable,
}
var int_val_pl = Value.Payload.BigInt{
.base = .{ .tag = if (int_val_big.positive) .int_big_positive else .int_big_negative },
.data = int_val_big.limbs[0..int_val_big.len],
var repr_val_pl = Value.Payload.BigInt{
.base = .{
.tag = if (repr_val_big.positive) .int_big_positive else .int_big_negative,
},
.data = repr_val_big.limbs[0..repr_val_big.len],
};
const int_val = Value.initPayload(&int_val_pl.base);
const repr_val = Value.initPayload(&repr_val_pl.base);
try writer.writeAll("zig_cast_");
try dg.renderTypeForBuiltinFnName(writer, ty);
@ -1023,7 +1025,7 @@ pub const DeclGen = struct {
try writer.writeAll(", ");
empty = false;
}
try writer.print("{x}", .{try dg.fmtIntLiteral(int_ty, int_val, location)});
try writer.print("{x}", .{try dg.fmtIntLiteral(repr_ty, repr_val, location)});
if (!empty) try writer.writeByte(')');
return;
},