|
| 1 | +/* |
| 2 | + Copyright (C) 2024 Fredrik Johansson |
| 3 | +
|
| 4 | + This file is part of FLINT. |
| 5 | +
|
| 6 | + FLINT is free software: you can redistribute it and/or modify it under |
| 7 | + the terms of the GNU Lesser General Public License (LGPL) as published |
| 8 | + by the Free Software Foundation; either version 2.1 of the License, or |
| 9 | + (at your option) any later version. See <https://www.gnu.org/licenses/>. |
| 10 | +*/ |
| 11 | + |
| 12 | +#include "test_helpers.h" |
| 13 | +#include <float.h> |
| 14 | +#include "ulong_extras.h" |
| 15 | +#include "double_extras.h" |
| 16 | +#include "d_vec.h" |
| 17 | + |
| 18 | +TEST_FUNCTION_START(d_mul_2exp, state) |
| 19 | +{ |
| 20 | + double x, res1, res2; |
| 21 | + int e, ok; |
| 22 | + slong iter; |
| 23 | + |
| 24 | + for (iter = 0; iter < 10000 * flint_test_multiplier(); iter++) |
| 25 | + { |
| 26 | + x = d_randtest_special(state, -1024, 1024); |
| 27 | + e = n_randint(state, 3000) - 1500; |
| 28 | + |
| 29 | + res1 = d_mul_2exp(x, e); |
| 30 | + res2 = ldexp(x, e); |
| 31 | + |
| 32 | + if (d_is_nan(res2)) |
| 33 | + ok = d_is_nan(res1); |
| 34 | + else |
| 35 | + ok = res1 == res2; |
| 36 | + |
| 37 | + if (!ok) |
| 38 | + TEST_FUNCTION_FAIL("x = %.20g\n res1 = %.20g\n res2 = %.20g\n", x, res1, res2); |
| 39 | + } |
| 40 | + |
| 41 | + for (iter = 0; iter < 10000 * flint_test_multiplier(); iter++) |
| 42 | + { |
| 43 | + do { |
| 44 | + x = d_randtest_signed(state, -256, 256); |
| 45 | + } while (x == 0.0); |
| 46 | + e = n_randint(state, 512) - 256; |
| 47 | + |
| 48 | + res1 = d_mul_2exp_inrange2(x, e); |
| 49 | + res2 = ldexp(x, e); |
| 50 | + |
| 51 | + ok = res1 == res2; |
| 52 | + |
| 53 | + if (!ok) |
| 54 | + TEST_FUNCTION_FAIL("x = %.20g\n res1 = %.20g\n res2 = %.20g\n", x, res1, res2); |
| 55 | + } |
| 56 | + |
| 57 | + for (iter = 0; iter < 10000 * flint_test_multiplier(); iter++) |
| 58 | + { |
| 59 | + x = d_randtest_special(state, -1024, 1024); |
| 60 | + e = n_randint(state, D_MAX_NORMAL_EXPONENT - D_MIN_NORMAL_EXPONENT + 1) + D_MIN_NORMAL_EXPONENT; |
| 61 | + |
| 62 | + res1 = d_mul_2exp_inrange(x, e); |
| 63 | + res2 = ldexp(x, e); |
| 64 | + |
| 65 | + if (d_is_nan(res2)) |
| 66 | + ok = d_is_nan(res1); |
| 67 | + else |
| 68 | + ok = res1 == res2; |
| 69 | + |
| 70 | + if (!ok) |
| 71 | + TEST_FUNCTION_FAIL("x = %.20g\n res1 = %.20g\n res2 = %.20g\n", x, res1, res2); |
| 72 | + } |
| 73 | + |
| 74 | + for (iter = 0; iter < 1000 * flint_test_multiplier(); iter++) |
| 75 | + { |
| 76 | + double v[5] = { 0, 0, 0, 0, 0 }; |
| 77 | + double r[10] = { 0, 0, 0, 0, 0 }; |
| 78 | + slong i, n = n_randint(state, 5); |
| 79 | + |
| 80 | + for (i = 0; i < n; i++) |
| 81 | + x = d_randtest_special(state, -1024, 1024); |
| 82 | + |
| 83 | + e = n_randint(state, 3000) - 1500; |
| 84 | + |
| 85 | + _d_vec_mul_2exp(r, v, n, e); |
| 86 | + |
| 87 | + for (i = 0; i < n; i++) |
| 88 | + { |
| 89 | + res1 = r[i]; |
| 90 | + res2 = ldexp(v[i], e); |
| 91 | + |
| 92 | + if (d_is_nan(res2)) |
| 93 | + ok = d_is_nan(res1); |
| 94 | + else |
| 95 | + ok = res1 == res2; |
| 96 | + |
| 97 | + if (!ok) |
| 98 | + TEST_FUNCTION_FAIL("x = %.20g\n res1 = %.20g\n res2 = %.20g\n", x, res1, res2); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + TEST_FUNCTION_END(state); |
| 103 | +} |
0 commit comments