1 1.1 mrg /* Test mpn_scan0 and mpn_scan1. 2 1.1 mrg 3 1.1 mrg Copyright 2002 Free Software Foundation, Inc. 4 1.1 mrg 5 1.1.1.2 mrg This file is part of the GNU MP Library test suite. 6 1.1 mrg 7 1.1.1.2 mrg The GNU MP Library test suite is free software; you can redistribute it 8 1.1.1.2 mrg and/or modify it under the terms of the GNU General Public License as 9 1.1.1.2 mrg published by the Free Software Foundation; either version 3 of the License, 10 1.1.1.2 mrg or (at your option) any later version. 11 1.1.1.2 mrg 12 1.1.1.2 mrg The GNU MP Library test suite is distributed in the hope that it will be 13 1.1.1.2 mrg useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1.1.2 mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 15 1.1.1.2 mrg Public License for more details. 16 1.1 mrg 17 1.1.1.2 mrg You should have received a copy of the GNU General Public License along with 18 1.1.1.3 mrg the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */ 19 1.1 mrg 20 1.1 mrg #include <stdio.h> 21 1.1 mrg #include <stdlib.h> 22 1.1 mrg 23 1.1 mrg #include "gmp-impl.h" 24 1.1 mrg 25 1.1 mrg #include "tests.h" 26 1.1 mrg 27 1.1 mrg 28 1.1 mrg #define SIZE ((mp_size_t) 3) 29 1.1 mrg mp_limb_t x[SIZE+1]; 30 1.1 mrg 31 1.1 mrg void 32 1.1 mrg check (void) 33 1.1 mrg { 34 1.1 mrg unsigned long i, got, want; 35 1.1 mrg 36 1.1 mrg x[SIZE] = 1; 37 1.1 mrg for (i = 0; i < SIZE*GMP_NUMB_BITS; i++) 38 1.1 mrg { 39 1.1 mrg got = refmpn_scan1 (x, i); 40 1.1 mrg want = mpn_scan1 (x, i); 41 1.1 mrg if (got != want) 42 1.1 mrg { 43 1.1 mrg printf ("mpn_scan1\n"); 44 1.1 mrg printf (" i %lu\n", i); 45 1.1 mrg printf (" got %lu\n", got); 46 1.1 mrg printf (" want %lu\n", want); 47 1.1 mrg mpn_trace (" x ", x, SIZE); 48 1.1 mrg abort (); 49 1.1 mrg } 50 1.1 mrg } 51 1.1 mrg 52 1.1 mrg x[SIZE] = 0; 53 1.1 mrg for (i = 0; i < SIZE*GMP_NUMB_BITS; i++) 54 1.1 mrg { 55 1.1 mrg got = refmpn_scan0 (x, i); 56 1.1 mrg want = mpn_scan0 (x, i); 57 1.1 mrg if (got != want) 58 1.1 mrg { 59 1.1 mrg printf ("mpn_scan0\n"); 60 1.1 mrg printf (" i %lu\n", i); 61 1.1 mrg printf (" got %lu\n", got); 62 1.1 mrg printf (" want %lu\n", want); 63 1.1 mrg mpn_trace (" x ", x, SIZE); 64 1.1 mrg abort (); 65 1.1 mrg } 66 1.1 mrg } 67 1.1 mrg } 68 1.1 mrg 69 1.1 mrg void 70 1.1 mrg check_twobits (void) 71 1.1 mrg { 72 1.1 mrg #define TWOBITS(a, b) \ 73 1.1 mrg ((CNST_LIMB(1) << (a)) | (CNST_LIMB(1) << (b))) 74 1.1 mrg 75 1.1 mrg refmpn_zero (x, SIZE); 76 1.1 mrg x[0] = TWOBITS (1, 0); 77 1.1 mrg check (); 78 1.1 mrg 79 1.1 mrg refmpn_zero (x, SIZE); 80 1.1 mrg x[0] = TWOBITS (GMP_NUMB_BITS-1, 1); 81 1.1 mrg check (); 82 1.1 mrg 83 1.1 mrg refmpn_zero (x, SIZE); 84 1.1 mrg x[0] = CNST_LIMB(1); 85 1.1 mrg x[1] = CNST_LIMB(1); 86 1.1 mrg check (); 87 1.1 mrg 88 1.1 mrg refmpn_zero (x, SIZE); 89 1.1 mrg x[0] = CNST_LIMB(1) << (GMP_NUMB_BITS-1); 90 1.1 mrg x[1] = CNST_LIMB(1); 91 1.1 mrg check (); 92 1.1 mrg 93 1.1 mrg refmpn_zero (x, SIZE); 94 1.1 mrg x[1] = TWOBITS (1, 0); 95 1.1 mrg check (); 96 1.1 mrg 97 1.1 mrg refmpn_zero (x, SIZE); 98 1.1 mrg x[1] = CNST_LIMB(1); 99 1.1 mrg x[2] = CNST_LIMB(1); 100 1.1 mrg check (); 101 1.1 mrg } 102 1.1 mrg 103 1.1 mrg /* This is unused, it takes too long, especially on 64-bit systems. */ 104 1.1 mrg void 105 1.1 mrg check_twobits_exhaustive (void) 106 1.1 mrg { 107 1.1 mrg unsigned long i, j; 108 1.1 mrg 109 1.1 mrg for (i = 0; i < GMP_NUMB_BITS * SIZE; i++) 110 1.1 mrg { 111 1.1 mrg for (j = 0; j < GMP_NUMB_BITS * SIZE; j++) 112 1.1 mrg { 113 1.1 mrg refmpn_zero (x, SIZE); 114 1.1 mrg refmpn_setbit (x, i); 115 1.1 mrg refmpn_setbit (x, j); 116 1.1 mrg check (); 117 1.1 mrg } 118 1.1 mrg } 119 1.1 mrg } 120 1.1 mrg 121 1.1 mrg void 122 1.1 mrg check_rand (void) 123 1.1 mrg { 124 1.1 mrg int i; 125 1.1 mrg 126 1.1 mrg for (i = 0; i < 100; i++) 127 1.1 mrg { 128 1.1 mrg refmpn_random2 (x, SIZE); 129 1.1 mrg check (); 130 1.1 mrg } 131 1.1 mrg } 132 1.1 mrg 133 1.1 mrg int 134 1.1 mrg main (void) 135 1.1 mrg { 136 1.1 mrg mp_trace_base = -16; 137 1.1 mrg tests_start (); 138 1.1 mrg 139 1.1 mrg check_twobits (); 140 1.1 mrg check_rand (); 141 1.1 mrg 142 1.1 mrg tests_end (); 143 1.1 mrg exit (0); 144 1.1 mrg } 145