|
| 1 | +/* |
| 2 | + Copyright (C) 2024, 2026 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 3 of the License, or |
| 9 | + (at your option) any later version. See <https://www.gnu.org/licenses/>. |
| 10 | +*/ |
| 11 | + |
| 12 | +#include "gr_vec.h" |
| 13 | +#include "gr_poly.h" |
| 14 | + |
| 15 | +PUSH_OPTIONS |
| 16 | +OPTIMIZE_OSIZE |
| 17 | + |
| 18 | +void _gr_poly_test_mulmid(gr_method_poly_binary_trunc2_op mulmid_impl, gr_method_poly_binary_trunc2_op mulmid_ref, |
| 19 | + flint_rand_t state, slong iters, slong maxn, gr_ctx_t ctx) |
| 20 | +{ |
| 21 | + slong iter; |
| 22 | + gr_ctx_ptr given_ctx = ctx; |
| 23 | + |
| 24 | + if (mulmid_ref == NULL) |
| 25 | + mulmid_ref = (gr_method_poly_binary_trunc2_op) _gr_poly_mulmid_generic; |
| 26 | + |
| 27 | + for (iter = 0; iter < iters; iter++) |
| 28 | + { |
| 29 | + gr_ptr A, B, C, D; |
| 30 | + slong n1, n2, nhi, nlo; |
| 31 | + int status = GR_SUCCESS; |
| 32 | + gr_ctx_t my_ctx; |
| 33 | + gr_ctx_struct * ctx; |
| 34 | + int squaring; |
| 35 | + |
| 36 | + if (given_ctx == NULL) |
| 37 | + { |
| 38 | + gr_ctx_init_random(my_ctx, state); |
| 39 | + ctx = my_ctx; |
| 40 | + } |
| 41 | + else |
| 42 | + ctx = given_ctx; |
| 43 | + |
| 44 | + squaring = n_randint(state, 2); |
| 45 | + |
| 46 | + n1 = 1 + n_randint(state, 1 + maxn); |
| 47 | + n2 = squaring ? n1 : 1 + n_randint(state, 1 + maxn); |
| 48 | + nhi = 1 + n_randint(state, 1 + maxn); |
| 49 | + nhi = FLINT_MIN(nhi, n1 + n2 - 1); |
| 50 | + nlo = n_randint(state, nhi); |
| 51 | + |
| 52 | + A = gr_heap_init_vec(n1, ctx); |
| 53 | + B = squaring ? A : gr_heap_init_vec(n2, ctx); |
| 54 | + C = gr_heap_init_vec(nhi - nlo, ctx); |
| 55 | + D = gr_heap_init_vec(nhi - nlo, ctx); |
| 56 | + |
| 57 | + GR_MUST_SUCCEED(_gr_vec_randtest(A, state, n1, ctx)); |
| 58 | + if (!squaring) |
| 59 | + GR_MUST_SUCCEED(_gr_vec_randtest(B, state, n2, ctx)); |
| 60 | + GR_MUST_SUCCEED(_gr_vec_randtest(C, state, nhi - nlo, ctx)); |
| 61 | + |
| 62 | +#if 0 |
| 63 | + /* Useful for debugging */ |
| 64 | + slong i; |
| 65 | + for (i = 0; i < n1; i++) |
| 66 | + GR_IGNORE(gr_set_ui(GR_ENTRY(A, i, ctx->sizeof_elem), i + 1, ctx)); |
| 67 | + for (i = 0; i < n2; i++) |
| 68 | + GR_IGNORE(gr_set_ui(GR_ENTRY(B, i, ctx->sizeof_elem), i + 1, ctx)); |
| 69 | +#endif |
| 70 | + |
| 71 | + status |= mulmid_impl(C, A, n1, B, n2, nlo, nhi, ctx); |
| 72 | + |
| 73 | + if (status == GR_SUCCESS) |
| 74 | + status |= mulmid_ref(D, A, n1, B, n2, nlo, nhi, ctx); |
| 75 | + |
| 76 | + if (status == GR_SUCCESS && _gr_vec_equal(C, D, nhi - nlo, ctx) == T_FALSE) |
| 77 | + { |
| 78 | + flint_printf("FAIL: mulmid\n"); |
| 79 | + gr_ctx_println(ctx); |
| 80 | + flint_printf("len1 = %wd, len2 = %wd, nlo = %wd, ni = %wd, squaring = %d\n\n", n1, n2, nlo, nhi, squaring); |
| 81 | + flint_printf("A:\n"); _gr_vec_print(A, n1, ctx); flint_printf("\n\n"); |
| 82 | + flint_printf("B:\n"); _gr_vec_print(B, n2, ctx); flint_printf("\n\n"); |
| 83 | + flint_printf("C:\n"); _gr_vec_print(C, nhi - nlo, ctx); flint_printf("\n\n"); |
| 84 | + flint_printf("D:\n"); _gr_vec_print(D, nhi - nlo, ctx); flint_printf("\n\n"); |
| 85 | + flint_abort(); |
| 86 | + } |
| 87 | + |
| 88 | + gr_heap_clear_vec(A, n1, ctx); |
| 89 | + if (!squaring) |
| 90 | + gr_heap_clear_vec(B, n2, ctx); |
| 91 | + gr_heap_clear_vec(C, nhi - nlo, ctx); |
| 92 | + gr_heap_clear_vec(D, nhi - nlo, ctx); |
| 93 | + |
| 94 | + if (given_ctx == NULL) |
| 95 | + gr_ctx_clear(ctx); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +POP_OPTIONS |
0 commit comments