| author | |
| committer | |
| log | 6b056d1fb93bba68c03bda5039994d4df338ef13 |
| tree | a55fe4549eb562f3bb9e66e1c0a6878120af7482 |
| parent | 7d94e712f125e10f234338e800953aa4837206ef |
5 files changed, 227 insertions(+), 379 deletions(-)
lib/std/special/compiler_rt.zig+26-26| ... | ... | @@ -16,42 +16,42 @@ comptime { |
| 16 | 16 | else => {}, |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | @export(@import("compiler_rt/comparesf2.zig").__lesf2, .{ .name = "__lesf2", .linkage = linkage }); | |
| 20 | @export(@import("compiler_rt/comparedf2.zig").__ledf2, .{ .name = "__ledf2", .linkage = linkage }); | |
| 21 | @export(@import("compiler_rt/comparetf2.zig").__letf2, .{ .name = "__letf2", .linkage = linkage }); | |
| 19 | @export(@import("compiler_rt/compareXf2.zig").__lesf2, .{ .name = "__lesf2", .linkage = linkage }); | |
| 20 | @export(@import("compiler_rt/compareXf2.zig").__ledf2, .{ .name = "__ledf2", .linkage = linkage }); | |
| 21 | @export(@import("compiler_rt/compareXf2.zig").__letf2, .{ .name = "__letf2", .linkage = linkage }); | |
| 22 | 22 | |
| 23 | @export(@import("compiler_rt/comparesf2.zig").__gesf2, .{ .name = "__gesf2", .linkage = linkage }); | |
| 24 | @export(@import("compiler_rt/comparedf2.zig").__gedf2, .{ .name = "__gedf2", .linkage = linkage }); | |
| 25 | @export(@import("compiler_rt/comparetf2.zig").__getf2, .{ .name = "__getf2", .linkage = linkage }); | |
| 23 | @export(@import("compiler_rt/compareXf2.zig").__gesf2, .{ .name = "__gesf2", .linkage = linkage }); | |
| 24 | @export(@import("compiler_rt/compareXf2.zig").__gedf2, .{ .name = "__gedf2", .linkage = linkage }); | |
| 25 | @export(@import("compiler_rt/compareXf2.zig").__getf2, .{ .name = "__getf2", .linkage = linkage }); | |
| 26 | 26 | |
| 27 | 27 | if (!is_test) { |
| 28 | @export(@import("compiler_rt/comparesf2.zig").__lesf2, .{ .name = "__cmpsf2", .linkage = linkage }); | |
| 29 | @export(@import("compiler_rt/comparedf2.zig").__ledf2, .{ .name = "__cmpdf2", .linkage = linkage }); | |
| 30 | @export(@import("compiler_rt/comparetf2.zig").__letf2, .{ .name = "__cmptf2", .linkage = linkage }); | |
| 28 | @export(@import("compiler_rt/compareXf2.zig").__lesf2, .{ .name = "__cmpsf2", .linkage = linkage }); | |
| 29 | @export(@import("compiler_rt/compareXf2.zig").__ledf2, .{ .name = "__cmpdf2", .linkage = linkage }); | |
| 30 | @export(@import("compiler_rt/compareXf2.zig").__letf2, .{ .name = "__cmptf2", .linkage = linkage }); | |
| 31 | 31 | |
| 32 | @export(@import("compiler_rt/comparesf2.zig").__eqsf2, .{ .name = "__eqsf2", .linkage = linkage }); | |
| 33 | @export(@import("compiler_rt/comparedf2.zig").__eqdf2, .{ .name = "__eqdf2", .linkage = linkage }); | |
| 34 | @export(@import("compiler_rt/comparetf2.zig").__letf2, .{ .name = "__eqtf2", .linkage = linkage }); | |
| 32 | @export(@import("compiler_rt/compareXf2.zig").__eqsf2, .{ .name = "__eqsf2", .linkage = linkage }); | |
| 33 | @export(@import("compiler_rt/compareXf2.zig").__eqdf2, .{ .name = "__eqdf2", .linkage = linkage }); | |
| 34 | @export(@import("compiler_rt/compareXf2.zig").__letf2, .{ .name = "__eqtf2", .linkage = linkage }); | |
| 35 | 35 | |
| 36 | @export(@import("compiler_rt/comparesf2.zig").__ltsf2, .{ .name = "__ltsf2", .linkage = linkage }); | |
| 37 | @export(@import("compiler_rt/comparedf2.zig").__ltdf2, .{ .name = "__ltdf2", .linkage = linkage }); | |
| 38 | @export(@import("compiler_rt/comparetf2.zig").__letf2, .{ .name = "__lttf2", .linkage = linkage }); | |
| 36 | @export(@import("compiler_rt/compareXf2.zig").__ltsf2, .{ .name = "__ltsf2", .linkage = linkage }); | |
| 37 | @export(@import("compiler_rt/compareXf2.zig").__ltdf2, .{ .name = "__ltdf2", .linkage = linkage }); | |
| 38 | @export(@import("compiler_rt/compareXf2.zig").__letf2, .{ .name = "__lttf2", .linkage = linkage }); | |
| 39 | 39 | |
| 40 | @export(@import("compiler_rt/comparesf2.zig").__nesf2, .{ .name = "__nesf2", .linkage = linkage }); | |
| 41 | @export(@import("compiler_rt/comparedf2.zig").__nedf2, .{ .name = "__nedf2", .linkage = linkage }); | |
| 42 | @export(@import("compiler_rt/comparetf2.zig").__letf2, .{ .name = "__netf2", .linkage = linkage }); | |
| 40 | @export(@import("compiler_rt/compareXf2.zig").__nesf2, .{ .name = "__nesf2", .linkage = linkage }); | |
| 41 | @export(@import("compiler_rt/compareXf2.zig").__nedf2, .{ .name = "__nedf2", .linkage = linkage }); | |
| 42 | @export(@import("compiler_rt/compareXf2.zig").__letf2, .{ .name = "__netf2", .linkage = linkage }); | |
| 43 | 43 | |
| 44 | @export(@import("compiler_rt/comparesf2.zig").__gtsf2, .{ .name = "__gtsf2", .linkage = linkage }); | |
| 45 | @export(@import("compiler_rt/comparedf2.zig").__gtdf2, .{ .name = "__gtdf2", .linkage = linkage }); | |
| 46 | @export(@import("compiler_rt/comparetf2.zig").__getf2, .{ .name = "__gttf2", .linkage = linkage }); | |
| 44 | @export(@import("compiler_rt/compareXf2.zig").__gtsf2, .{ .name = "__gtsf2", .linkage = linkage }); | |
| 45 | @export(@import("compiler_rt/compareXf2.zig").__gtdf2, .{ .name = "__gtdf2", .linkage = linkage }); | |
| 46 | @export(@import("compiler_rt/compareXf2.zig").__getf2, .{ .name = "__gttf2", .linkage = linkage }); | |
| 47 | 47 | |
| 48 | 48 | @export(@import("compiler_rt/extendXfYf2.zig").__extendhfsf2, .{ .name = "__gnu_h2f_ieee", .linkage = linkage }); |
| 49 | 49 | @export(@import("compiler_rt/truncXfYf2.zig").__truncsfhf2, .{ .name = "__gnu_f2h_ieee", .linkage = linkage }); |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | @export(@import("compiler_rt/comparesf2.zig").__unordsf2, .{ .name = "__unordsf2", .linkage = linkage }); | |
| 53 | @export(@import("compiler_rt/comparedf2.zig").__unorddf2, .{ .name = "__unorddf2", .linkage = linkage }); | |
| 54 | @export(@import("compiler_rt/comparetf2.zig").__unordtf2, .{ .name = "__unordtf2", .linkage = linkage }); | |
| 52 | @export(@import("compiler_rt/compareXf2.zig").__unordsf2, .{ .name = "__unordsf2", .linkage = linkage }); | |
| 53 | @export(@import("compiler_rt/compareXf2.zig").__unorddf2, .{ .name = "__unorddf2", .linkage = linkage }); | |
| 54 | @export(@import("compiler_rt/compareXf2.zig").__unordtf2, .{ .name = "__unordtf2", .linkage = linkage }); | |
| 55 | 55 | |
| 56 | 56 | @export(@import("compiler_rt/addXf3.zig").__addsf3, .{ .name = "__addsf3", .linkage = linkage }); |
| 57 | 57 | @export(@import("compiler_rt/addXf3.zig").__adddf3, .{ .name = "__adddf3", .linkage = linkage }); |
| ... | ... | @@ -231,14 +231,14 @@ comptime { |
| 231 | 231 | @export(@import("compiler_rt/arm/aeabi_fcmp.zig").__aeabi_fcmple, .{ .name = "__aeabi_fcmple", .linkage = linkage }); |
| 232 | 232 | @export(@import("compiler_rt/arm/aeabi_fcmp.zig").__aeabi_fcmpge, .{ .name = "__aeabi_fcmpge", .linkage = linkage }); |
| 233 | 233 | @export(@import("compiler_rt/arm/aeabi_fcmp.zig").__aeabi_fcmpgt, .{ .name = "__aeabi_fcmpgt", .linkage = linkage }); |
| 234 | @export(@import("compiler_rt/comparesf2.zig").__aeabi_fcmpun, .{ .name = "__aeabi_fcmpun", .linkage = linkage }); | |
| 234 | @export(@import("compiler_rt/compareXf2.zig").__aeabi_fcmpun, .{ .name = "__aeabi_fcmpun", .linkage = linkage }); | |
| 235 | 235 | |
| 236 | 236 | @export(@import("compiler_rt/arm/aeabi_dcmp.zig").__aeabi_dcmpeq, .{ .name = "__aeabi_dcmpeq", .linkage = linkage }); |
| 237 | 237 | @export(@import("compiler_rt/arm/aeabi_dcmp.zig").__aeabi_dcmplt, .{ .name = "__aeabi_dcmplt", .linkage = linkage }); |
| 238 | 238 | @export(@import("compiler_rt/arm/aeabi_dcmp.zig").__aeabi_dcmple, .{ .name = "__aeabi_dcmple", .linkage = linkage }); |
| 239 | 239 | @export(@import("compiler_rt/arm/aeabi_dcmp.zig").__aeabi_dcmpge, .{ .name = "__aeabi_dcmpge", .linkage = linkage }); |
| 240 | 240 | @export(@import("compiler_rt/arm/aeabi_dcmp.zig").__aeabi_dcmpgt, .{ .name = "__aeabi_dcmpgt", .linkage = linkage }); |
| 241 | @export(@import("compiler_rt/comparedf2.zig").__aeabi_dcmpun, .{ .name = "__aeabi_dcmpun", .linkage = linkage }); | |
| 241 | @export(@import("compiler_rt/compareXf2.zig").__aeabi_dcmpun, .{ .name = "__aeabi_dcmpun", .linkage = linkage }); | |
| 242 | 242 | } |
| 243 | 243 | if (builtin.os == .windows) { |
| 244 | 244 | // Default stack-probe functions emitted by LLVM |
lib/std/special/compiler_rt/compareXf2.zig created+201| ... | ... | @@ -0,0 +1,201 @@ |
| 1 | // Ported from: | |
| 2 | // | |
| 3 | // https://github.com/llvm/llvm-project/commit/d674d96bc56c0f377879d01c9d8dfdaaa7859cdb/compiler-rt/lib/builtins/comparesf2.c | |
| 4 | ||
| 5 | const std = @import("std"); | |
| 6 | const builtin = @import("builtin"); | |
| 7 | ||
| 8 | const LE = extern enum(i32) { | |
| 9 | Less = -1, | |
| 10 | Equal = 0, | |
| 11 | Greater = 1, | |
| 12 | Unordered = 1, | |
| 13 | }; | |
| 14 | ||
| 15 | const GE = extern enum(i32) { | |
| 16 | Less = -1, | |
| 17 | Equal = 0, | |
| 18 | Greater = 1, | |
| 19 | Unordered = -1, | |
| 20 | }; | |
| 21 | ||
| 22 | pub fn cmp(comptime T: type, comptime RT: type, a: T, b: T) RT { | |
| 23 | @setRuntimeSafety(builtin.is_test); | |
| 24 | ||
| 25 | const srep_t = @IntType(true, T.bit_count); | |
| 26 | const rep_t = @IntType(false, T.bit_count); | |
| 27 | ||
| 28 | const significandBits = std.math.floatMantissaBits(T); | |
| 29 | const exponentBits = std.math.floatExponentBits(T); | |
| 30 | const signBit = (@as(rep_t, 1) << (significandBits + exponentBits)); | |
| 31 | const absMask = signBit - 1; | |
| 32 | const infRep = @bitCast(rep_t, std.math.inf(T)); | |
| 33 | ||
| 34 | const aInt = @bitCast(srep_t, a); | |
| 35 | const bInt = @bitCast(srep_t, b); | |
| 36 | const aAbs = @bitCast(rep_t, aInt) & absMask; | |
| 37 | const bAbs = @bitCast(rep_t, bInt) & absMask; | |
| 38 | ||
| 39 | // If either a or b is NaN, they are unordered. | |
| 40 | if (aAbs > infRep or bAbs > infRep) return .Unordered; | |
| 41 | ||
| 42 | // If a and b are both zeros, they are equal. | |
| 43 | if ((aAbs | bAbs) == 0) return .Equal; | |
| 44 | ||
| 45 | // If at least one of a and b is positive, we get the same result comparing | |
| 46 | // a and b as signed integers as we would with a fp_ting-point compare. | |
| 47 | if ((aInt & bInt) >= 0) { | |
| 48 | if (aInt < bInt) { | |
| 49 | return .Less; | |
| 50 | } else if (aInt == bInt) { | |
| 51 | return .Equal; | |
| 52 | } else return .Greater; | |
| 53 | } | |
| 54 | ||
| 55 | // Otherwise, both are negative, so we need to flip the sense of the | |
| 56 | // comparison to get the correct result. (This assumes a twos- or ones- | |
| 57 | // complement integer representation; if integers are represented in a | |
| 58 | // sign-magnitude representation, then this flip is incorrect). | |
| 59 | else { | |
| 60 | if (aInt > bInt) { | |
| 61 | return .Less; | |
| 62 | } else if (aInt == bInt) { | |
| 63 | return .Equal; | |
| 64 | } else return .Greater; | |
| 65 | } | |
| 66 | } | |
| 67 | ||
| 68 | pub fn unordcmp(comptime T: type, a: T, b: T) i32 { | |
| 69 | @setRuntimeSafety(builtin.is_test); | |
| 70 | ||
| 71 | const rep_t = @IntType(false, T.bit_count); | |
| 72 | ||
| 73 | const significandBits = std.math.floatMantissaBits(T); | |
| 74 | const exponentBits = std.math.floatExponentBits(T); | |
| 75 | const signBit = (@as(rep_t, 1) << (significandBits + exponentBits)); | |
| 76 | const absMask = signBit - 1; | |
| 77 | const infRep = @bitCast(rep_t, std.math.inf(T)); | |
| 78 | ||
| 79 | const aAbs: rep_t = @bitCast(rep_t, a) & absMask; | |
| 80 | const bAbs: rep_t = @bitCast(rep_t, b) & absMask; | |
| 81 | ||
| 82 | return @boolToInt(aAbs > infRep or bAbs > infRep); | |
| 83 | } | |
| 84 | ||
| 85 | // Comparison between f32 | |
| 86 | ||
| 87 | pub fn __lesf2(a: f32, b: f32) callconv(.C) i32 { | |
| 88 | @setRuntimeSafety(builtin.is_test); | |
| 89 | return @bitCast(i32, @call(.{ .modifier = .always_inline }, cmp, .{ f32, LE, a, b })); | |
| 90 | } | |
| 91 | ||
| 92 | pub fn __gesf2(a: f32, b: f32) callconv(.C) i32 { | |
| 93 | @setRuntimeSafety(builtin.is_test); | |
| 94 | return @bitCast(i32, @call(.{ .modifier = .always_inline }, cmp, .{ f32, GE, a, b })); | |
| 95 | } | |
| 96 | ||
| 97 | pub fn __eqsf2(a: f32, b: f32) callconv(.C) i32 { | |
| 98 | return __lesf2(a, b); | |
| 99 | } | |
| 100 | ||
| 101 | pub fn __ltsf2(a: f32, b: f32) callconv(.C) i32 { | |
| 102 | return __lesf2(a, b); | |
| 103 | } | |
| 104 | ||
| 105 | pub fn __nesf2(a: f32, b: f32) callconv(.C) i32 { | |
| 106 | return __lesf2(a, b); | |
| 107 | } | |
| 108 | ||
| 109 | pub fn __gtsf2(a: f32, b: f32) callconv(.C) i32 { | |
| 110 | return __gesf2(a, b); | |
| 111 | } | |
| 112 | ||
| 113 | // Comparison between f64 | |
| 114 | ||
| 115 | pub fn __ledf2(a: f64, b: f64) callconv(.C) i32 { | |
| 116 | @setRuntimeSafety(builtin.is_test); | |
| 117 | return @bitCast(i32, @call(.{ .modifier = .always_inline }, cmp, .{ f64, LE, a, b })); | |
| 118 | } | |
| 119 | ||
| 120 | pub fn __gedf2(a: f64, b: f64) callconv(.C) i32 { | |
| 121 | @setRuntimeSafety(builtin.is_test); | |
| 122 | return @bitCast(i32, @call(.{ .modifier = .always_inline }, cmp, .{ f64, GE, a, b })); | |
| 123 | } | |
| 124 | ||
| 125 | pub fn __eqdf2(a: f64, b: f64) callconv(.C) i32 { | |
| 126 | return __ledf2(a, b); | |
| 127 | } | |
| 128 | ||
| 129 | pub fn __ltdf2(a: f64, b: f64) callconv(.C) i32 { | |
| 130 | return __ledf2(a, b); | |
| 131 | } | |
| 132 | ||
| 133 | pub fn __nedf2(a: f64, b: f64) callconv(.C) i32 { | |
| 134 | return __ledf2(a, b); | |
| 135 | } | |
| 136 | ||
| 137 | pub fn __gtdf2(a: f64, b: f64) callconv(.C) i32 { | |
| 138 | return __gedf2(a, b); | |
| 139 | } | |
| 140 | ||
| 141 | // Comparison between f128 | |
| 142 | ||
| 143 | pub fn __letf2(a: f128, b: f128) callconv(.C) i32 { | |
| 144 | @setRuntimeSafety(builtin.is_test); | |
| 145 | return @bitCast(i32, @call(.{ .modifier = .always_inline }, cmp, .{ f128, LE, a, b })); | |
| 146 | } | |
| 147 | ||
| 148 | pub fn __getf2(a: f128, b: f128) callconv(.C) i32 { | |
| 149 | @setRuntimeSafety(builtin.is_test); | |
| 150 | return @bitCast(i32, @call(.{ .modifier = .always_inline }, cmp, .{ f128, GE, a, b })); | |
| 151 | } | |
| 152 | ||
| 153 | pub fn __eqtf2(a: f128, b: f128) callconv(.C) i32 { | |
| 154 | return __letf2(a, b); | |
| 155 | } | |
| 156 | ||
| 157 | pub fn __lttf2(a: f128, b: f128) callconv(.C) i32 { | |
| 158 | return __letf2(a, b); | |
| 159 | } | |
| 160 | ||
| 161 | pub fn __netf2(a: f128, b: f128) callconv(.C) i32 { | |
| 162 | return __letf2(a, b); | |
| 163 | } | |
| 164 | ||
| 165 | pub fn __gttf2(a: f128, b: f128) callconv(.C) i32 { | |
| 166 | return __getf2(a, b); | |
| 167 | } | |
| 168 | ||
| 169 | // Unordered comparison between f32/f64/f128 | |
| 170 | ||
| 171 | pub fn __unordsf2(a: f32, b: f32) callconv(.C) i32 { | |
| 172 | @setRuntimeSafety(builtin.is_test); | |
| 173 | return @call(.{ .modifier = .always_inline }, unordcmp, .{ f32, a, b }); | |
| 174 | } | |
| 175 | ||
| 176 | pub fn __unorddf2(a: f64, b: f64) callconv(.C) i32 { | |
| 177 | @setRuntimeSafety(builtin.is_test); | |
| 178 | return @call(.{ .modifier = .always_inline }, unordcmp, .{ f64, a, b }); | |
| 179 | } | |
| 180 | ||
| 181 | pub fn __unordtf2(a: f128, b: f128) callconv(.C) i32 { | |
| 182 | @setRuntimeSafety(builtin.is_test); | |
| 183 | return @call(.{ .modifier = .always_inline }, unordcmp, .{ f128, a, b }); | |
| 184 | } | |
| 185 | ||
| 186 | pub fn __aeabi_fcmpun(a: f32, b: f32) callconv(.AAPCS) i32 { | |
| 187 | @setRuntimeSafety(false); | |
| 188 | return @call(.{ .modifier = .always_inline }, __unordsf2, .{ a, b }); | |
| 189 | } | |
| 190 | ||
| 191 | pub fn __aeabi_dcmpun(a: f64, b: f64) callconv(.AAPCS) i32 { | |
| 192 | @setRuntimeSafety(false); | |
| 193 | return @call(.{ .modifier = .always_inline }, __unorddf2, .{ a, b }); | |
| 194 | } | |
| 195 | ||
| 196 | test "comparesf2" { | |
| 197 | _ = @import("comparesf2_test.zig"); | |
| 198 | } | |
| 199 | test "comparedf2" { | |
| 200 | _ = @import("comparedf2_test.zig"); | |
| 201 | } |
lib/std/special/compiler_rt/comparedf2.zig deleted-127| ... | ... | @@ -1,127 +0,0 @@ |
| 1 | // Ported from: | |
| 2 | // | |
| 3 | // https://github.com/llvm/llvm-project/commit/d674d96bc56c0f377879d01c9d8dfdaaa7859cdb/compiler-rt/lib/builtins/comparedf2.c | |
| 4 | ||
| 5 | const std = @import("std"); | |
| 6 | const builtin = @import("builtin"); | |
| 7 | const is_test = builtin.is_test; | |
| 8 | ||
| 9 | const fp_t = f64; | |
| 10 | const rep_t = u64; | |
| 11 | const srep_t = i64; | |
| 12 | ||
| 13 | const typeWidth = rep_t.bit_count; | |
| 14 | const significandBits = std.math.floatMantissaBits(fp_t); | |
| 15 | const exponentBits = std.math.floatExponentBits(fp_t); | |
| 16 | const signBit = (@as(rep_t, 1) << (significandBits + exponentBits)); | |
| 17 | const absMask = signBit - 1; | |
| 18 | const implicitBit = @as(rep_t, 1) << significandBits; | |
| 19 | const significandMask = implicitBit - 1; | |
| 20 | const exponentMask = absMask ^ significandMask; | |
| 21 | const infRep = @bitCast(rep_t, std.math.inf(fp_t)); | |
| 22 | ||
| 23 | // TODO https://github.com/ziglang/zig/issues/641 | |
| 24 | // and then make the return types of some of these functions the enum instead of c_int | |
| 25 | const LE_LESS = @as(c_int, -1); | |
| 26 | const LE_EQUAL = @as(c_int, 0); | |
| 27 | const LE_GREATER = @as(c_int, 1); | |
| 28 | const LE_UNORDERED = @as(c_int, 1); | |
| 29 | ||
| 30 | pub fn __ledf2(a: fp_t, b: fp_t) callconv(.C) c_int { | |
| 31 | @setRuntimeSafety(is_test); | |
| 32 | const aInt: srep_t = @bitCast(srep_t, a); | |
| 33 | const bInt: srep_t = @bitCast(srep_t, b); | |
| 34 | const aAbs: rep_t = @bitCast(rep_t, aInt) & absMask; | |
| 35 | const bAbs: rep_t = @bitCast(rep_t, bInt) & absMask; | |
| 36 | ||
| 37 | // If either a or b is NaN, they are unordered. | |
| 38 | if (aAbs > infRep or bAbs > infRep) return LE_UNORDERED; | |
| 39 | ||
| 40 | // If a and b are both zeros, they are equal. | |
| 41 | if ((aAbs | bAbs) == 0) return LE_EQUAL; | |
| 42 | ||
| 43 | // If at least one of a and b is positive, we get the same result comparing | |
| 44 | // a and b as signed integers as we would with a fp_ting-point compare. | |
| 45 | if ((aInt & bInt) >= 0) { | |
| 46 | if (aInt < bInt) { | |
| 47 | return LE_LESS; | |
| 48 | } else if (aInt == bInt) { | |
| 49 | return LE_EQUAL; | |
| 50 | } else return LE_GREATER; | |
| 51 | } | |
| 52 | ||
| 53 | // Otherwise, both are negative, so we need to flip the sense of the | |
| 54 | // comparison to get the correct result. (This assumes a twos- or ones- | |
| 55 | // complement integer representation; if integers are represented in a | |
| 56 | // sign-magnitude representation, then this flip is incorrect). | |
| 57 | else { | |
| 58 | if (aInt > bInt) { | |
| 59 | return LE_LESS; | |
| 60 | } else if (aInt == bInt) { | |
| 61 | return LE_EQUAL; | |
| 62 | } else return LE_GREATER; | |
| 63 | } | |
| 64 | } | |
| 65 | ||
| 66 | // TODO https://github.com/ziglang/zig/issues/641 | |
| 67 | // and then make the return types of some of these functions the enum instead of c_int | |
| 68 | const GE_LESS = @as(c_int, -1); | |
| 69 | const GE_EQUAL = @as(c_int, 0); | |
| 70 | const GE_GREATER = @as(c_int, 1); | |
| 71 | const GE_UNORDERED = @as(c_int, -1); // Note: different from LE_UNORDERED | |
| 72 | ||
| 73 | pub fn __gedf2(a: fp_t, b: fp_t) callconv(.C) c_int { | |
| 74 | @setRuntimeSafety(is_test); | |
| 75 | const aInt: srep_t = @bitCast(srep_t, a); | |
| 76 | const bInt: srep_t = @bitCast(srep_t, b); | |
| 77 | const aAbs: rep_t = @bitCast(rep_t, aInt) & absMask; | |
| 78 | const bAbs: rep_t = @bitCast(rep_t, bInt) & absMask; | |
| 79 | ||
| 80 | if (aAbs > infRep or bAbs > infRep) return GE_UNORDERED; | |
| 81 | if ((aAbs | bAbs) == 0) return GE_EQUAL; | |
| 82 | if ((aInt & bInt) >= 0) { | |
| 83 | if (aInt < bInt) { | |
| 84 | return GE_LESS; | |
| 85 | } else if (aInt == bInt) { | |
| 86 | return GE_EQUAL; | |
| 87 | } else return GE_GREATER; | |
| 88 | } else { | |
| 89 | if (aInt > bInt) { | |
| 90 | return GE_LESS; | |
| 91 | } else if (aInt == bInt) { | |
| 92 | return GE_EQUAL; | |
| 93 | } else return GE_GREATER; | |
| 94 | } | |
| 95 | } | |
| 96 | ||
| 97 | pub fn __unorddf2(a: fp_t, b: fp_t) callconv(.C) c_int { | |
| 98 | @setRuntimeSafety(is_test); | |
| 99 | const aAbs: rep_t = @bitCast(rep_t, a) & absMask; | |
| 100 | const bAbs: rep_t = @bitCast(rep_t, b) & absMask; | |
| 101 | return @boolToInt(aAbs > infRep or bAbs > infRep); | |
| 102 | } | |
| 103 | ||
| 104 | pub fn __eqdf2(a: fp_t, b: fp_t) callconv(.C) c_int { | |
| 105 | return __ledf2(a, b); | |
| 106 | } | |
| 107 | ||
| 108 | pub fn __ltdf2(a: fp_t, b: fp_t) callconv(.C) c_int { | |
| 109 | return __ledf2(a, b); | |
| 110 | } | |
| 111 | ||
| 112 | pub fn __nedf2(a: fp_t, b: fp_t) callconv(.C) c_int { | |
| 113 | return __ledf2(a, b); | |
| 114 | } | |
| 115 | ||
| 116 | pub fn __gtdf2(a: fp_t, b: fp_t) callconv(.C) c_int { | |
| 117 | return __gedf2(a, b); | |
| 118 | } | |
| 119 | ||
| 120 | pub fn __aeabi_dcmpun(a: fp_t, b: fp_t) callconv(.AAPCS) c_int { | |
| 121 | @setRuntimeSafety(false); | |
| 122 | return @call(.{ .modifier = .always_inline }, __unorddf2, .{ a, b }); | |
| 123 | } | |
| 124 | ||
| 125 | test "import comparedf2" { | |
| 126 | _ = @import("comparedf2_test.zig"); | |
| 127 | } |
lib/std/special/compiler_rt/comparesf2.zig deleted-127| ... | ... | @@ -1,127 +0,0 @@ |
| 1 | // Ported from: | |
| 2 | // | |
| 3 | // https://github.com/llvm/llvm-project/commit/d674d96bc56c0f377879d01c9d8dfdaaa7859cdb/compiler-rt/lib/builtins/comparesf2.c | |
| 4 | ||
| 5 | const std = @import("std"); | |
| 6 | const builtin = @import("builtin"); | |
| 7 | const is_test = builtin.is_test; | |
| 8 | ||
| 9 | const fp_t = f32; | |
| 10 | const rep_t = u32; | |
| 11 | const srep_t = i32; | |
| 12 | ||
| 13 | const typeWidth = rep_t.bit_count; | |
| 14 | const significandBits = std.math.floatMantissaBits(fp_t); | |
| 15 | const exponentBits = std.math.floatExponentBits(fp_t); | |
| 16 | const signBit = (@as(rep_t, 1) << (significandBits + exponentBits)); | |
| 17 | const absMask = signBit - 1; | |
| 18 | const implicitBit = @as(rep_t, 1) << significandBits; | |
| 19 | const significandMask = implicitBit - 1; | |
| 20 | const exponentMask = absMask ^ significandMask; | |
| 21 | const infRep = @bitCast(rep_t, std.math.inf(fp_t)); | |
| 22 | ||
| 23 | // TODO https://github.com/ziglang/zig/issues/641 | |
| 24 | // and then make the return types of some of these functions the enum instead of c_int | |
| 25 | const LE_LESS = @as(c_int, -1); | |
| 26 | const LE_EQUAL = @as(c_int, 0); | |
| 27 | const LE_GREATER = @as(c_int, 1); | |
| 28 | const LE_UNORDERED = @as(c_int, 1); | |
| 29 | ||
| 30 | pub fn __lesf2(a: fp_t, b: fp_t) callconv(.C) c_int { | |
| 31 | @setRuntimeSafety(is_test); | |
| 32 | const aInt: srep_t = @bitCast(srep_t, a); | |
| 33 | const bInt: srep_t = @bitCast(srep_t, b); | |
| 34 | const aAbs: rep_t = @bitCast(rep_t, aInt) & absMask; | |
| 35 | const bAbs: rep_t = @bitCast(rep_t, bInt) & absMask; | |
| 36 | ||
| 37 | // If either a or b is NaN, they are unordered. | |
| 38 | if (aAbs > infRep or bAbs > infRep) return LE_UNORDERED; | |
| 39 | ||
| 40 | // If a and b are both zeros, they are equal. | |
| 41 | if ((aAbs | bAbs) == 0) return LE_EQUAL; | |
| 42 | ||
| 43 | // If at least one of a and b is positive, we get the same result comparing | |
| 44 | // a and b as signed integers as we would with a fp_ting-point compare. | |
| 45 | if ((aInt & bInt) >= 0) { | |
| 46 | if (aInt < bInt) { | |
| 47 | return LE_LESS; | |
| 48 | } else if (aInt == bInt) { | |
| 49 | return LE_EQUAL; | |
| 50 | } else return LE_GREATER; | |
| 51 | } | |
| 52 | ||
| 53 | // Otherwise, both are negative, so we need to flip the sense of the | |
| 54 | // comparison to get the correct result. (This assumes a twos- or ones- | |
| 55 | // complement integer representation; if integers are represented in a | |
| 56 | // sign-magnitude representation, then this flip is incorrect). | |
| 57 | else { | |
| 58 | if (aInt > bInt) { | |
| 59 | return LE_LESS; | |
| 60 | } else if (aInt == bInt) { | |
| 61 | return LE_EQUAL; | |
| 62 | } else return LE_GREATER; | |
| 63 | } | |
| 64 | } | |
| 65 | ||
| 66 | // TODO https://github.com/ziglang/zig/issues/641 | |
| 67 | // and then make the return types of some of these functions the enum instead of c_int | |
| 68 | const GE_LESS = @as(c_int, -1); | |
| 69 | const GE_EQUAL = @as(c_int, 0); | |
| 70 | const GE_GREATER = @as(c_int, 1); | |
| 71 | const GE_UNORDERED = @as(c_int, -1); // Note: different from LE_UNORDERED | |
| 72 | ||
| 73 | pub fn __gesf2(a: fp_t, b: fp_t) callconv(.C) c_int { | |
| 74 | @setRuntimeSafety(is_test); | |
| 75 | const aInt: srep_t = @bitCast(srep_t, a); | |
| 76 | const bInt: srep_t = @bitCast(srep_t, b); | |
| 77 | const aAbs: rep_t = @bitCast(rep_t, aInt) & absMask; | |
| 78 | const bAbs: rep_t = @bitCast(rep_t, bInt) & absMask; | |
| 79 | ||
| 80 | if (aAbs > infRep or bAbs > infRep) return GE_UNORDERED; | |
| 81 | if ((aAbs | bAbs) == 0) return GE_EQUAL; | |
| 82 | if ((aInt & bInt) >= 0) { | |
| 83 | if (aInt < bInt) { | |
| 84 | return GE_LESS; | |
| 85 | } else if (aInt == bInt) { | |
| 86 | return GE_EQUAL; | |
| 87 | } else return GE_GREATER; | |
| 88 | } else { | |
| 89 | if (aInt > bInt) { | |
| 90 | return GE_LESS; | |
| 91 | } else if (aInt == bInt) { | |
| 92 | return GE_EQUAL; | |
| 93 | } else return GE_GREATER; | |
| 94 | } | |
| 95 | } | |
| 96 | ||
| 97 | pub fn __unordsf2(a: fp_t, b: fp_t) callconv(.C) c_int { | |
| 98 | @setRuntimeSafety(is_test); | |
| 99 | const aAbs: rep_t = @bitCast(rep_t, a) & absMask; | |
| 100 | const bAbs: rep_t = @bitCast(rep_t, b) & absMask; | |
| 101 | return @boolToInt(aAbs > infRep or bAbs > infRep); | |
| 102 | } | |
| 103 | ||
| 104 | pub fn __eqsf2(a: fp_t, b: fp_t) callconv(.C) c_int { | |
| 105 | return __lesf2(a, b); | |
| 106 | } | |
| 107 | ||
| 108 | pub fn __ltsf2(a: fp_t, b: fp_t) callconv(.C) c_int { | |
| 109 | return __lesf2(a, b); | |
| 110 | } | |
| 111 | ||
| 112 | pub fn __nesf2(a: fp_t, b: fp_t) callconv(.C) c_int { | |
| 113 | return __lesf2(a, b); | |
| 114 | } | |
| 115 | ||
| 116 | pub fn __gtsf2(a: fp_t, b: fp_t) callconv(.C) c_int { | |
| 117 | return __gesf2(a, b); | |
| 118 | } | |
| 119 | ||
| 120 | pub fn __aeabi_fcmpun(a: fp_t, b: fp_t) callconv(.AAPCS) c_int { | |
| 121 | @setRuntimeSafety(false); | |
| 122 | return @call(.{ .modifier = .always_inline }, __unordsf2, .{ a, b }); | |
| 123 | } | |
| 124 | ||
| 125 | test "import comparesf2" { | |
| 126 | _ = @import("comparesf2_test.zig"); | |
| 127 | } |
lib/std/special/compiler_rt/comparetf2.zig deleted-99| ... | ... | @@ -1,99 +0,0 @@ |
| 1 | // TODO https://github.com/ziglang/zig/issues/641 | |
| 2 | // and then make the return types of some of these functions the enum instead of c_int | |
| 3 | const LE_LESS = @as(c_int, -1); | |
| 4 | const LE_EQUAL = @as(c_int, 0); | |
| 5 | const LE_GREATER = @as(c_int, 1); | |
| 6 | const LE_UNORDERED = @as(c_int, 1); | |
| 7 | ||
| 8 | const rep_t = u128; | |
| 9 | const srep_t = i128; | |
| 10 | ||
| 11 | const typeWidth = rep_t.bit_count; | |
| 12 | const significandBits = 112; | |
| 13 | const exponentBits = (typeWidth - significandBits - 1); | |
| 14 | const signBit = (@as(rep_t, 1) << (significandBits + exponentBits)); | |
| 15 | const absMask = signBit - 1; | |
| 16 | const implicitBit = @as(rep_t, 1) << significandBits; | |
| 17 | const significandMask = implicitBit - 1; | |
| 18 | const exponentMask = absMask ^ significandMask; | |
| 19 | const infRep = exponentMask; | |
| 20 | ||
| 21 | const builtin = @import("builtin"); | |
| 22 | const is_test = builtin.is_test; | |
| 23 | ||
| 24 | pub fn __letf2(a: f128, b: f128) callconv(.C) c_int { | |
| 25 | @setRuntimeSafety(is_test); | |
| 26 | ||
| 27 | const aInt = @bitCast(rep_t, a); | |
| 28 | const bInt = @bitCast(rep_t, b); | |
| 29 | ||
| 30 | const aAbs: rep_t = aInt & absMask; | |
| 31 | const bAbs: rep_t = bInt & absMask; | |
| 32 | ||
| 33 | // If either a or b is NaN, they are unordered. | |
| 34 | if (aAbs > infRep or bAbs > infRep) return LE_UNORDERED; | |
| 35 | ||
| 36 | // If a and b are both zeros, they are equal. | |
| 37 | if ((aAbs | bAbs) == 0) return LE_EQUAL; | |
| 38 | ||
| 39 | // If at least one of a and b is positive, we get the same result comparing | |
| 40 | // a and b as signed integers as we would with a floating-point compare. | |
| 41 | return if ((aInt & bInt) >= 0) | |
| 42 | if (aInt < bInt) | |
| 43 | LE_LESS | |
| 44 | else if (aInt == bInt) | |
| 45 | LE_EQUAL | |
| 46 | else | |
| 47 | LE_GREATER | |
| 48 | else | |
| 49 | // Otherwise, both are negative, so we need to flip the sense of the | |
| 50 | // comparison to get the correct result. (This assumes a twos- or ones- | |
| 51 | // complement integer representation; if integers are represented in a | |
| 52 | // sign-magnitude representation, then this flip is incorrect). | |
| 53 | if (aInt > bInt) | |
| 54 | LE_LESS | |
| 55 | else if (aInt == bInt) | |
| 56 | LE_EQUAL | |
| 57 | else | |
| 58 | LE_GREATER; | |
| 59 | } | |
| 60 | ||
| 61 | // TODO https://github.com/ziglang/zig/issues/641 | |
| 62 | // and then make the return types of some of these functions the enum instead of c_int | |
| 63 | const GE_LESS = @as(c_int, -1); | |
| 64 | const GE_EQUAL = @as(c_int, 0); | |
| 65 | const GE_GREATER = @as(c_int, 1); | |
| 66 | const GE_UNORDERED = @as(c_int, -1); // Note: different from LE_UNORDERED | |
| 67 | ||
| 68 | pub fn __getf2(a: f128, b: f128) callconv(.C) c_int { | |
| 69 | @setRuntimeSafety(is_test); | |
| 70 | ||
| 71 | const aInt = @bitCast(srep_t, a); | |
| 72 | const bInt = @bitCast(srep_t, b); | |
| 73 | const aAbs = @bitCast(rep_t, aInt) & absMask; | |
| 74 | const bAbs = @bitCast(rep_t, bInt) & absMask; | |
| 75 | ||
| 76 | if (aAbs > infRep or bAbs > infRep) return GE_UNORDERED; | |
| 77 | if ((aAbs | bAbs) == 0) return GE_EQUAL; | |
| 78 | return if ((aInt & bInt) >= 0) | |
| 79 | if (aInt < bInt) | |
| 80 | GE_LESS | |
| 81 | else if (aInt == bInt) | |
| 82 | GE_EQUAL | |
| 83 | else | |
| 84 | GE_GREATER | |
| 85 | else if (aInt > bInt) | |
| 86 | GE_LESS | |
| 87 | else if (aInt == bInt) | |
| 88 | GE_EQUAL | |
| 89 | else | |
| 90 | GE_GREATER; | |
| 91 | } | |
| 92 | ||
| 93 | pub fn __unordtf2(a: f128, b: f128) callconv(.C) c_int { | |
| 94 | @setRuntimeSafety(is_test); | |
| 95 | ||
| 96 | const aAbs = @bitCast(rep_t, a) & absMask; | |
| 97 | const bAbs = @bitCast(rep_t, b) & absMask; | |
| 98 | return @boolToInt(aAbs > infRep or bAbs > infRep); | |
| 99 | } |