master
  1const std = @import("std");
  2const builtin = @import("builtin");
  3const native_endian = builtin.cpu.arch.endian();
  4const ofmt_c = builtin.object_format == .c;
  5
  6/// For now, we prefer weak linkage because some of the routines we implement here may also be
  7/// provided by system/dynamic libc. Eventually we should be more disciplined about this on a
  8/// per-symbol, per-target basis: https://github.com/ziglang/zig/issues/11883
  9pub const linkage: std.builtin.GlobalLinkage = if (builtin.is_test)
 10    .internal
 11else if (ofmt_c)
 12    .strong
 13else
 14    .weak;
 15
 16/// Determines the symbol's visibility to other objects.
 17/// For WebAssembly this allows the symbol to be resolved to other modules, but will not
 18/// export it to the host runtime.
 19pub const visibility: std.builtin.SymbolVisibility = if (linkage == .internal or builtin.link_mode == .dynamic)
 20    .default
 21else
 22    .hidden;
 23
 24pub const PreferredLoadStoreElement = element: {
 25    if (std.simd.suggestVectorLength(u8)) |vec_size| {
 26        const Vec = @Vector(vec_size, u8);
 27
 28        if (@sizeOf(Vec) == vec_size and std.math.isPowerOfTwo(vec_size)) {
 29            break :element Vec;
 30        }
 31    }
 32    break :element usize;
 33};
 34
 35pub const want_aeabi = switch (builtin.abi) {
 36    .eabi,
 37    .eabihf,
 38    .musleabi,
 39    .musleabihf,
 40    .gnueabi,
 41    .gnueabihf,
 42    .android,
 43    .androideabi,
 44    => switch (builtin.cpu.arch) {
 45        .arm, .armeb, .thumb, .thumbeb => true,
 46        else => false,
 47    },
 48    else => false,
 49};
 50
 51/// These functions are required on Windows on ARM. They are provided by MSVC libc, but in libc-less
 52/// builds or when linking MinGW libc they are our responsibility.
 53/// Temporarily used for thumb-uefi until https://github.com/ziglang/zig/issues/21630 is addressed.
 54pub const want_windows_arm_abi = e: {
 55    if (!builtin.cpu.arch.isArm()) break :e false;
 56    switch (builtin.os.tag) {
 57        .windows, .uefi => {},
 58        else => break :e false,
 59    }
 60    // The ABI is needed, but it's only our reponsibility if libc won't provide it.
 61    break :e builtin.abi.isGnu() or !builtin.link_libc;
 62};
 63
 64/// These functions are required by on Windows on x86 on some ABIs. They are provided by MSVC libc,
 65/// but in libc-less builds they are our responsibility.
 66pub const want_windows_x86_msvc_abi = e: {
 67    if (builtin.cpu.arch != .x86) break :e false;
 68    if (builtin.os.tag != .windows) break :e false;
 69    switch (builtin.abi) {
 70        .none, .msvc, .itanium => {},
 71        else => break :e false,
 72    }
 73    // The ABI is needed, but it's only our responsibility if libc won't provide it.
 74    break :e !builtin.link_libc;
 75};
 76
 77pub const want_ppc_abi = builtin.cpu.arch.isPowerPC();
 78
 79pub const want_float_exceptions = !builtin.cpu.arch.isWasm();
 80
 81// Libcalls that involve u128 on Windows x86-64 are expected by LLVM to use the
 82// calling convention of @Vector(2, u64), rather than what's standard.
 83pub const want_windows_v2u64_abi = builtin.os.tag == .windows and builtin.cpu.arch == .x86_64 and !ofmt_c;
 84
 85/// This governs whether to use these symbol names for f16/f32 conversions
 86/// rather than the standard names:
 87/// * __gnu_f2h_ieee
 88/// * __gnu_h2f_ieee
 89/// Known correct configurations:
 90///   x86_64-freestanding-none => true
 91///   x86_64-linux-none => true
 92///   x86_64-linux-gnu => true
 93///   x86_64-linux-musl => true
 94///   x86_64-linux-eabi => true
 95///   arm-linux-musleabihf => true
 96///   arm-linux-gnueabihf => true
 97///   arm-linux-eabihf => false
 98///   wasm32-wasi-musl => false
 99///   wasm32-freestanding-none => false
100///   x86_64-windows-gnu => true
101///   x86_64-windows-msvc => true
102///   any-macos-any => false
103pub const gnu_f16_abi = switch (builtin.cpu.arch) {
104    .wasm32,
105    .wasm64,
106    .riscv64,
107    .riscv64be,
108    .riscv32,
109    .riscv32be,
110    => false,
111
112    .x86, .x86_64 => true,
113
114    .arm, .armeb, .thumb, .thumbeb => switch (builtin.abi) {
115        .eabi, .eabihf => false,
116        else => true,
117    },
118
119    else => !builtin.os.tag.isDarwin(),
120};
121
122pub const want_sparc_abi = builtin.cpu.arch.isSPARC();
123
124pub const test_safety = switch (builtin.zig_backend) {
125    .stage2_aarch64 => false,
126    else => builtin.is_test,
127};
128
129// Avoid dragging in the runtime safety mechanisms into this .o file, unless
130// we're trying to test compiler-rt.
131pub const panic = if (test_safety) std.debug.FullPanic(std.debug.defaultPanic) else std.debug.no_panic;
132
133/// This seems to mostly correspond to `clang::TargetInfo::HasFloat16`.
134pub fn F16T(comptime OtherType: type) type {
135    return switch (builtin.cpu.arch) {
136        .amdgcn,
137        .arm,
138        .armeb,
139        .thumb,
140        .thumbeb,
141        .aarch64,
142        .aarch64_be,
143        .hexagon,
144        .loongarch32,
145        .loongarch64,
146        .nvptx,
147        .nvptx64,
148        .riscv32,
149        .riscv32be,
150        .riscv64,
151        .riscv64be,
152        .s390x,
153        .spirv32,
154        .spirv64,
155        => f16,
156        .x86, .x86_64 => if (builtin.target.os.tag.isDarwin()) switch (OtherType) {
157            // Starting with LLVM 16, Darwin uses different abi for f16
158            // depending on the type of the other return/argument..???
159            f32, f64 => u16,
160            f80, f128 => f16,
161            else => unreachable,
162        } else f16,
163        else => u16,
164    };
165}
166
167pub fn wideMultiply(comptime Z: type, a: Z, b: Z, hi: *Z, lo: *Z) void {
168    switch (Z) {
169        u16 => {
170            // 16x16 --> 32 bit multiply
171            const product = @as(u32, a) * @as(u32, b);
172            hi.* = @intCast(product >> 16);
173            lo.* = @truncate(product);
174        },
175        u32 => {
176            // 32x32 --> 64 bit multiply
177            const product = @as(u64, a) * @as(u64, b);
178            hi.* = @truncate(product >> 32);
179            lo.* = @truncate(product);
180        },
181        u64 => {
182            const S = struct {
183                fn loWord(x: u64) u64 {
184                    return @as(u32, @truncate(x));
185                }
186                fn hiWord(x: u64) u64 {
187                    return @as(u32, @truncate(x >> 32));
188                }
189            };
190            // 64x64 -> 128 wide multiply for platforms that don't have such an operation;
191            // many 64-bit platforms have this operation, but they tend to have hardware
192            // floating-point, so we don't bother with a special case for them here.
193            // Each of the component 32x32 -> 64 products
194            const plolo: u64 = S.loWord(a) * S.loWord(b);
195            const plohi: u64 = S.loWord(a) * S.hiWord(b);
196            const philo: u64 = S.hiWord(a) * S.loWord(b);
197            const phihi: u64 = S.hiWord(a) * S.hiWord(b);
198            // Sum terms that contribute to lo in a way that allows us to get the carry
199            const r0: u64 = S.loWord(plolo);
200            const r1: u64 = S.hiWord(plolo) +% S.loWord(plohi) +% S.loWord(philo);
201            lo.* = r0 +% (r1 << 32);
202            // Sum terms contributing to hi with the carry from lo
203            hi.* = S.hiWord(plohi) +% S.hiWord(philo) +% S.hiWord(r1) +% phihi;
204        },
205        u128 => {
206            const Word_LoMask: u64 = 0x00000000ffffffff;
207            const Word_HiMask: u64 = 0xffffffff00000000;
208            const Word_FullMask: u64 = 0xffffffffffffffff;
209            const S = struct {
210                fn Word_1(x: u128) u64 {
211                    return @as(u32, @truncate(x >> 96));
212                }
213                fn Word_2(x: u128) u64 {
214                    return @as(u32, @truncate(x >> 64));
215                }
216                fn Word_3(x: u128) u64 {
217                    return @as(u32, @truncate(x >> 32));
218                }
219                fn Word_4(x: u128) u64 {
220                    return @as(u32, @truncate(x));
221                }
222            };
223            // 128x128 -> 256 wide multiply for platforms that don't have such an operation;
224            // many 64-bit platforms have this operation, but they tend to have hardware
225            // floating-point, so we don't bother with a special case for them here.
226
227            const product11: u64 = S.Word_1(a) * S.Word_1(b);
228            const product12: u64 = S.Word_1(a) * S.Word_2(b);
229            const product13: u64 = S.Word_1(a) * S.Word_3(b);
230            const product14: u64 = S.Word_1(a) * S.Word_4(b);
231            const product21: u64 = S.Word_2(a) * S.Word_1(b);
232            const product22: u64 = S.Word_2(a) * S.Word_2(b);
233            const product23: u64 = S.Word_2(a) * S.Word_3(b);
234            const product24: u64 = S.Word_2(a) * S.Word_4(b);
235            const product31: u64 = S.Word_3(a) * S.Word_1(b);
236            const product32: u64 = S.Word_3(a) * S.Word_2(b);
237            const product33: u64 = S.Word_3(a) * S.Word_3(b);
238            const product34: u64 = S.Word_3(a) * S.Word_4(b);
239            const product41: u64 = S.Word_4(a) * S.Word_1(b);
240            const product42: u64 = S.Word_4(a) * S.Word_2(b);
241            const product43: u64 = S.Word_4(a) * S.Word_3(b);
242            const product44: u64 = S.Word_4(a) * S.Word_4(b);
243
244            const sum0: u128 = @as(u128, product44);
245            const sum1: u128 = @as(u128, product34) +%
246                @as(u128, product43);
247            const sum2: u128 = @as(u128, product24) +%
248                @as(u128, product33) +%
249                @as(u128, product42);
250            const sum3: u128 = @as(u128, product14) +%
251                @as(u128, product23) +%
252                @as(u128, product32) +%
253                @as(u128, product41);
254            const sum4: u128 = @as(u128, product13) +%
255                @as(u128, product22) +%
256                @as(u128, product31);
257            const sum5: u128 = @as(u128, product12) +%
258                @as(u128, product21);
259            const sum6: u128 = @as(u128, product11);
260
261            const r0: u128 = (sum0 & Word_FullMask) +%
262                ((sum1 & Word_LoMask) << 32);
263            const r1: u128 = (sum0 >> 64) +%
264                ((sum1 >> 32) & Word_FullMask) +%
265                (sum2 & Word_FullMask) +%
266                ((sum3 << 32) & Word_HiMask);
267
268            lo.* = r0 +% (r1 << 64);
269            hi.* = (r1 >> 64) +%
270                (sum1 >> 96) +%
271                (sum2 >> 64) +%
272                (sum3 >> 32) +%
273                sum4 +%
274                (sum5 << 32) +%
275                (sum6 << 64);
276        },
277        else => @compileError("unsupported"),
278    }
279}
280
281pub fn normalize(comptime T: type, significand: *std.meta.Int(.unsigned, @typeInfo(T).float.bits)) i32 {
282    const Z = std.meta.Int(.unsigned, @typeInfo(T).float.bits);
283    const integerBit = @as(Z, 1) << std.math.floatFractionalBits(T);
284
285    const shift = @clz(significand.*) - @clz(integerBit);
286    significand.* <<= @as(std.math.Log2Int(Z), @intCast(shift));
287    return @as(i32, 1) - shift;
288}
289
290pub inline fn fneg(a: anytype) @TypeOf(a) {
291    const F = @TypeOf(a);
292    const bits = @typeInfo(F).float.bits;
293    const U = @Int(.unsigned, bits);
294    const sign_bit_mask = @as(U, 1) << (bits - 1);
295    const negated = @as(U, @bitCast(a)) ^ sign_bit_mask;
296    return @bitCast(negated);
297}
298
299/// Allows to access underlying bits as two equally sized lower and higher
300/// signed or unsigned integers.
301pub fn HalveInt(comptime T: type, comptime signed_half: bool) type {
302    return extern union {
303        pub const bits = @divExact(@typeInfo(T).int.bits, 2);
304        pub const HalfTU = std.meta.Int(.unsigned, bits);
305        pub const HalfTS = std.meta.Int(.signed, bits);
306        pub const HalfT = if (signed_half) HalfTS else HalfTU;
307
308        all: T,
309        s: if (native_endian == .little)
310            extern struct { low: HalfT, high: HalfT }
311        else
312            extern struct { high: HalfT, low: HalfT },
313    };
314}