zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

modti3.zig (1380B) - Raw


      1 //! Ported from:
      2 //!
      3 //! https://github.com/llvm/llvm-project/blob/2ffb1b0413efa9a24eb3c49e710e36f92e2cb50b/compiler-rt/lib/builtins/modti3.c
      4 
      5 const std = @import("std");
      6 const builtin = @import("builtin");
      7 const udivmod = @import("udivmod.zig").udivmod;
      8 const common = @import("common.zig");
      9 
     10 pub const panic = common.panic;
     11 
     12 comptime {
     13     if (common.want_windows_v2u64_abi) {
     14         @export(&__modti3_windows_x86_64, .{ .name = "__modti3", .linkage = common.linkage, .visibility = common.visibility });
     15     } else {
     16         @export(&__modti3, .{ .name = "__modti3", .linkage = common.linkage, .visibility = common.visibility });
     17     }
     18 }
     19 
     20 pub fn __modti3(a: i128, b: i128) callconv(.c) i128 {
     21     return mod(a, b);
     22 }
     23 
     24 const v2u64 = @Vector(2, u64);
     25 
     26 fn __modti3_windows_x86_64(a: v2u64, b: v2u64) callconv(.c) v2u64 {
     27     return @bitCast(mod(@as(i128, @bitCast(a)), @as(i128, @bitCast(b))));
     28 }
     29 
     30 inline fn mod(a: i128, b: i128) i128 {
     31     const s_a = a >> (128 - 1); // s = a < 0 ? -1 : 0
     32     const s_b = b >> (128 - 1); // s = b < 0 ? -1 : 0
     33 
     34     const an = (a ^ s_a) -% s_a; // negate if s == -1
     35     const bn = (b ^ s_b) -% s_b; // negate if s == -1
     36 
     37     var r: u128 = undefined;
     38     _ = udivmod(u128, @as(u128, @bitCast(an)), @as(u128, @bitCast(bn)), &r);
     39     return (@as(i128, @bitCast(r)) ^ s_a) -% s_a; // negate if s == -1
     40 }
     41 
     42 test {
     43     _ = @import("modti3_test.zig");
     44 }