zig

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

truncf_test.zig (10326B) - Raw


      1 const std = @import("std");
      2 const testing = std.testing;
      3 
      4 const __truncsfhf2 = @import("truncsfhf2.zig").__truncsfhf2;
      5 const __truncdfhf2 = @import("truncdfhf2.zig").__truncdfhf2;
      6 const __truncdfsf2 = @import("truncdfsf2.zig").__truncdfsf2;
      7 const __trunctfhf2 = @import("trunctfhf2.zig").__trunctfhf2;
      8 const __trunctfsf2 = @import("trunctfsf2.zig").__trunctfsf2;
      9 const __trunctfdf2 = @import("trunctfdf2.zig").__trunctfdf2;
     10 const __trunctfxf2 = @import("trunctfxf2.zig").__trunctfxf2;
     11 
     12 fn test__truncsfhf2(a: u32, expected: u16) !void {
     13     const actual: u16 = @bitCast(__truncsfhf2(@bitCast(a)));
     14 
     15     if (actual == expected) {
     16         return;
     17     }
     18 
     19     return error.TestFailure;
     20 }
     21 
     22 test "truncsfhf2" {
     23     try test__truncsfhf2(0x7fc00000, 0x7e00); // qNaN
     24     try test__truncsfhf2(0x7fe00000, 0x7f00); // sNaN
     25 
     26     try test__truncsfhf2(0, 0); // 0
     27     try test__truncsfhf2(0x80000000, 0x8000); // -0
     28 
     29     try test__truncsfhf2(0x7f800000, 0x7c00); // inf
     30     try test__truncsfhf2(0xff800000, 0xfc00); // -inf
     31 
     32     try test__truncsfhf2(0x477ff000, 0x7c00); // 65520 -> inf
     33     try test__truncsfhf2(0xc77ff000, 0xfc00); // -65520 -> -inf
     34 
     35     try test__truncsfhf2(0x71cc3892, 0x7c00); // 0x1.987124876876324p+100 -> inf
     36     try test__truncsfhf2(0xf1cc3892, 0xfc00); // -0x1.987124876876324p+100 -> -inf
     37 
     38     try test__truncsfhf2(0x38800000, 0x0400); // normal (min), 2**-14
     39     try test__truncsfhf2(0xb8800000, 0x8400); // normal (min), -2**-14
     40 
     41     try test__truncsfhf2(0x477fe000, 0x7bff); // normal (max), 65504
     42     try test__truncsfhf2(0xc77fe000, 0xfbff); // normal (max), -65504
     43 
     44     try test__truncsfhf2(0x477fe100, 0x7bff); // normal, 65505 -> 65504
     45     try test__truncsfhf2(0xc77fe100, 0xfbff); // normal, -65505 -> -65504
     46 
     47     try test__truncsfhf2(0x477fef00, 0x7bff); // normal, 65519 -> 65504
     48     try test__truncsfhf2(0xc77fef00, 0xfbff); // normal, -65519 -> -65504
     49 
     50     try test__truncsfhf2(0x3f802000, 0x3c01); // normal, 1 + 2**-10
     51     try test__truncsfhf2(0xbf802000, 0xbc01); // normal, -1 - 2**-10
     52 
     53     try test__truncsfhf2(0x3eaaa000, 0x3555); // normal, approx. 1/3
     54     try test__truncsfhf2(0xbeaaa000, 0xb555); // normal, approx. -1/3
     55 
     56     try test__truncsfhf2(0x40490fdb, 0x4248); // normal, 3.1415926535
     57     try test__truncsfhf2(0xc0490fdb, 0xc248); // normal, -3.1415926535
     58 
     59     try test__truncsfhf2(0x45cc3892, 0x6e62); // normal, 0x1.987124876876324p+12
     60 
     61     try test__truncsfhf2(0x3f800000, 0x3c00); // normal, 1
     62     try test__truncsfhf2(0x38800000, 0x0400); // normal, 0x1.0p-14
     63 
     64     try test__truncsfhf2(0x33800000, 0x0001); // denormal (min), 2**-24
     65     try test__truncsfhf2(0xb3800000, 0x8001); // denormal (min), -2**-24
     66 
     67     try test__truncsfhf2(0x387fc000, 0x03ff); // denormal (max), 2**-14 - 2**-24
     68     try test__truncsfhf2(0xb87fc000, 0x83ff); // denormal (max), -2**-14 + 2**-24
     69 
     70     try test__truncsfhf2(0x35800000, 0x0010); // denormal, 0x1.0p-20
     71     try test__truncsfhf2(0x33280000, 0x0001); // denormal, 0x1.5p-25 -> 0x1.0p-24
     72     try test__truncsfhf2(0x33000000, 0x0000); // 0x1.0p-25 -> zero
     73 }
     74 
     75 fn test__truncdfhf2(a: f64, expected: u16) void {
     76     const rep: u16 = @bitCast(__truncdfhf2(a));
     77 
     78     if (rep == expected) {
     79         return;
     80     }
     81     // test other possible NaN representation(signal NaN)
     82     else if (expected == 0x7e00) {
     83         if ((rep & 0x7c00) == 0x7c00 and (rep & 0x3ff) > 0) {
     84             return;
     85         }
     86     }
     87 
     88     @panic("__truncdfhf2 test failure");
     89 }
     90 
     91 fn test__truncdfhf2_raw(a: u64, expected: u16) void {
     92     const actual: u16 = @bitCast(__truncdfhf2(@bitCast(a)));
     93 
     94     if (actual == expected) {
     95         return;
     96     }
     97 
     98     @panic("__truncdfhf2 test failure");
     99 }
    100 
    101 test "truncdfhf2" {
    102     test__truncdfhf2_raw(0x7ff8000000000000, 0x7e00); // qNaN
    103     test__truncdfhf2_raw(0x7ff0000000008000, 0x7e00); // NaN
    104 
    105     test__truncdfhf2_raw(0x7ff0000000000000, 0x7c00); //inf
    106     test__truncdfhf2_raw(0xfff0000000000000, 0xfc00); // -inf
    107 
    108     test__truncdfhf2(0.0, 0x0); // zero
    109     test__truncdfhf2_raw(0x80000000 << 32, 0x8000); // -zero
    110 
    111     test__truncdfhf2(3.1415926535, 0x4248);
    112     test__truncdfhf2(-3.1415926535, 0xc248);
    113 
    114     test__truncdfhf2(0x1.987124876876324p+1000, 0x7c00);
    115     test__truncdfhf2(0x1.987124876876324p+12, 0x6e62);
    116     test__truncdfhf2(0x1.0p+0, 0x3c00);
    117     test__truncdfhf2(0x1.0p-14, 0x0400);
    118 
    119     // denormal
    120     test__truncdfhf2(0x1.0p-20, 0x0010);
    121     test__truncdfhf2(0x1.0p-24, 0x0001);
    122     test__truncdfhf2(-0x1.0p-24, 0x8001);
    123     test__truncdfhf2(0x1.5p-25, 0x0001);
    124 
    125     // and back to zero
    126     test__truncdfhf2(0x1.0p-25, 0x0000);
    127     test__truncdfhf2(-0x1.0p-25, 0x8000);
    128 
    129     // max (precise)
    130     test__truncdfhf2(65504.0, 0x7bff);
    131 
    132     // max (rounded)
    133     test__truncdfhf2(65519.0, 0x7bff);
    134 
    135     // max (to +inf)
    136     test__truncdfhf2(65520.0, 0x7c00);
    137     test__truncdfhf2(-65520.0, 0xfc00);
    138     test__truncdfhf2(65536.0, 0x7c00);
    139 }
    140 
    141 fn test__trunctfsf2(a: f128, expected: u32) void {
    142     const x = __trunctfsf2(a);
    143 
    144     const rep: u32 = @bitCast(x);
    145     if (rep == expected) {
    146         return;
    147     }
    148     // test other possible NaN representation(signal NaN)
    149     else if (expected == 0x7fc00000) {
    150         if ((rep & 0x7f800000) == 0x7f800000 and (rep & 0x7fffff) > 0) {
    151             return;
    152         }
    153     }
    154 
    155     @panic("__trunctfsf2 test failure");
    156 }
    157 
    158 test "trunctfsf2" {
    159     // qnan
    160     test__trunctfsf2(@bitCast(@as(u128, 0x7fff800000000000 << 64)), 0x7fc00000);
    161     // nan
    162     test__trunctfsf2(@bitCast(@as(u128, (0x7fff000000000000 | (0x810000000000 & 0xffffffffffff)) << 64)), 0x7fc08000);
    163     // inf
    164     test__trunctfsf2(@bitCast(@as(u128, 0x7fff000000000000 << 64)), 0x7f800000);
    165     // zero
    166     test__trunctfsf2(0.0, 0x0);
    167 
    168     test__trunctfsf2(0x1.23a2abb4a2ddee355f36789abcdep+5, 0x4211d156);
    169     test__trunctfsf2(0x1.e3d3c45bd3abfd98b76a54cc321fp-9, 0x3b71e9e2);
    170     test__trunctfsf2(0x1.234eebb5faa678f4488693abcdefp+4534, 0x7f800000);
    171     test__trunctfsf2(0x1.edcba9bb8c76a5a43dd21f334634p-435, 0x0);
    172 }
    173 
    174 fn test__trunctfdf2(a: f128, expected: u64) void {
    175     const x = __trunctfdf2(a);
    176 
    177     const rep: u64 = @bitCast(x);
    178     if (rep == expected) {
    179         return;
    180     }
    181     // test other possible NaN representation(signal NaN)
    182     else if (expected == 0x7ff8000000000000) {
    183         if ((rep & 0x7ff0000000000000) == 0x7ff0000000000000 and (rep & 0xfffffffffffff) > 0) {
    184             return;
    185         }
    186     }
    187 
    188     @panic("__trunctfsf2 test failure");
    189 }
    190 
    191 test "trunctfdf2" {
    192     // qnan
    193     test__trunctfdf2(@bitCast(@as(u128, 0x7fff800000000000 << 64)), 0x7ff8000000000000);
    194     // nan
    195     test__trunctfdf2(@bitCast(@as(u128, (0x7fff000000000000 | (0x810000000000 & 0xffffffffffff)) << 64)), 0x7ff8100000000000);
    196     // inf
    197     test__trunctfdf2(@bitCast(@as(u128, 0x7fff000000000000 << 64)), 0x7ff0000000000000);
    198     // zero
    199     test__trunctfdf2(0.0, 0x0);
    200 
    201     test__trunctfdf2(0x1.af23456789bbaaab347645365cdep+5, 0x404af23456789bbb);
    202     test__trunctfdf2(0x1.dedafcff354b6ae9758763545432p-9, 0x3f6dedafcff354b7);
    203     test__trunctfdf2(0x1.2f34dd5f437e849b4baab754cdefp+4534, 0x7ff0000000000000);
    204     test__trunctfdf2(0x1.edcbff8ad76ab5bf46463233214fp-435, 0x24cedcbff8ad76ab);
    205 }
    206 
    207 fn test__truncdfsf2(a: f64, expected: u32) void {
    208     const x = __truncdfsf2(a);
    209 
    210     const rep: u32 = @bitCast(x);
    211     if (rep == expected) {
    212         return;
    213     }
    214     // test other possible NaN representation(signal NaN)
    215     else if (expected == 0x7fc00000) {
    216         if ((rep & 0x7f800000) == 0x7f800000 and (rep & 0x7fffff) > 0) {
    217             return;
    218         }
    219     }
    220 
    221     std.debug.print("got 0x{x} wanted 0x{x}\n", .{ rep, expected });
    222 
    223     @panic("__trunctfsf2 test failure");
    224 }
    225 
    226 test "truncdfsf2" {
    227     // nan & qnan
    228     test__truncdfsf2(@bitCast(@as(u64, 0x7ff8000000000000)), 0x7fc00000);
    229     test__truncdfsf2(@bitCast(@as(u64, 0x7ff0000000000001)), 0x7fc00000);
    230     // inf
    231     test__truncdfsf2(@bitCast(@as(u64, 0x7ff0000000000000)), 0x7f800000);
    232     test__truncdfsf2(@bitCast(@as(u64, 0xfff0000000000000)), 0xff800000);
    233 
    234     test__truncdfsf2(0.0, 0x0);
    235     test__truncdfsf2(1.0, 0x3f800000);
    236     test__truncdfsf2(-1.0, 0xbf800000);
    237 
    238     // huge number becomes inf
    239     test__truncdfsf2(340282366920938463463374607431768211456.0, 0x7f800000);
    240 }
    241 
    242 fn test__trunctfhf2(a: f128, expected: u16) void {
    243     const x = __trunctfhf2(a);
    244 
    245     const rep: u16 = @bitCast(x);
    246     if (rep == expected) {
    247         return;
    248     }
    249 
    250     std.debug.print("got 0x{x} wanted 0x{x}\n", .{ rep, expected });
    251 
    252     @panic("__trunctfhf2 test failure");
    253 }
    254 
    255 test "trunctfhf2" {
    256     // qNaN
    257     test__trunctfhf2(@bitCast(@as(u128, 0x7fff8000000000000000000000000000)), 0x7e00);
    258     // NaN
    259     test__trunctfhf2(@bitCast(@as(u128, 0x7fff0000000000000000000000000001)), 0x7e00);
    260     // inf
    261     test__trunctfhf2(@bitCast(@as(u128, 0x7fff0000000000000000000000000000)), 0x7c00);
    262     test__trunctfhf2(-@as(f128, @bitCast(@as(u128, 0x7fff0000000000000000000000000000))), 0xfc00);
    263     // zero
    264     test__trunctfhf2(0.0, 0x0);
    265     test__trunctfhf2(-0.0, 0x8000);
    266 
    267     test__trunctfhf2(3.1415926535, 0x4248);
    268     test__trunctfhf2(-3.1415926535, 0xc248);
    269     test__trunctfhf2(0x1.987124876876324p+100, 0x7c00);
    270     test__trunctfhf2(0x1.987124876876324p+12, 0x6e62);
    271     test__trunctfhf2(0x1.0p+0, 0x3c00);
    272     test__trunctfhf2(0x1.0p-14, 0x0400);
    273     // denormal
    274     test__trunctfhf2(0x1.0p-20, 0x0010);
    275     test__trunctfhf2(0x1.0p-24, 0x0001);
    276     test__trunctfhf2(-0x1.0p-24, 0x8001);
    277     test__trunctfhf2(0x1.5p-25, 0x0001);
    278     // and back to zero
    279     test__trunctfhf2(0x1.0p-25, 0x0000);
    280     test__trunctfhf2(-0x1.0p-25, 0x8000);
    281     // max (precise)
    282     test__trunctfhf2(65504.0, 0x7bff);
    283     // max (rounded)
    284     test__trunctfhf2(65519.0, 0x7bff);
    285     // max (to +inf)
    286     test__trunctfhf2(65520.0, 0x7c00);
    287     test__trunctfhf2(65536.0, 0x7c00);
    288     test__trunctfhf2(-65520.0, 0xfc00);
    289 
    290     test__trunctfhf2(0x1.23a2abb4a2ddee355f36789abcdep+5, 0x508f);
    291     test__trunctfhf2(0x1.e3d3c45bd3abfd98b76a54cc321fp-9, 0x1b8f);
    292     test__trunctfhf2(0x1.234eebb5faa678f4488693abcdefp+453, 0x7c00);
    293     test__trunctfhf2(0x1.edcba9bb8c76a5a43dd21f334634p-43, 0x0);
    294 }
    295 
    296 test "trunctfxf2" {
    297     try test__trunctfxf2(1.5, 1.5);
    298     try test__trunctfxf2(2.5, 2.5);
    299     try test__trunctfxf2(-2.5, -2.5);
    300     try test__trunctfxf2(0.0, 0.0);
    301 }
    302 
    303 fn test__trunctfxf2(a: f128, expected: f80) !void {
    304     const x = __trunctfxf2(a);
    305     try testing.expect(x == expected);
    306 }