1 /*
2    support.c:  various support routines for test, profiling and tuning code
3 
4    Copyright (C) 2007, 2008, David Harvey
5 
6    This file is part of the zn_poly library (version 0.9).
7 
8    This program is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation, either version 2 of the License, or
11    (at your option) version 3 of the License.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #include "support.h"
24 
25 
26 unsigned test_bitsizes[] = {2, 3, 8,
27                             ULONG_BITS/2 - 1, ULONG_BITS/2, ULONG_BITS/2 + 1,
28                             ULONG_BITS - 2, ULONG_BITS - 1, ULONG_BITS};
29 
30 unsigned num_test_bitsizes = sizeof (test_bitsizes) / sizeof (unsigned);
31 
32 
33 gmp_randstate_t randstate;
34 
35 
36 void
mpz_to_mpn(mp_limb_t * res,size_t n,const mpz_t op)37 mpz_to_mpn (mp_limb_t* res, size_t n, const mpz_t op)
38 {
39    ZNP_ASSERT (mpz_size (op) <= n);
40    size_t count_p;
41 
42    mpz_export (res, &count_p, -1, sizeof (mp_limb_t), 0, GMP_NAIL_BITS, op);
43    // zero-pad remaining buffer
44    for (; count_p < n; count_p++)
45       res[count_p] = 0;
46 }
47 
48 
49 void
mpn_to_mpz(mpz_t res,const mp_limb_t * op,size_t n)50 mpn_to_mpz (mpz_t res, const mp_limb_t* op, size_t n)
51 {
52    ZNP_ASSERT (n >= 1);
53    mpz_import (res, n, -1, sizeof (mp_limb_t), 0, GMP_NAIL_BITS, op);
54 }
55 
56 
57 ulong
random_ulong(ulong max)58 random_ulong (ulong max)
59 {
60    return gmp_urandomm_ui (randstate, max);
61 }
62 
63 
64 ulong
random_ulong_bits(unsigned b)65 random_ulong_bits (unsigned b)
66 {
67    return gmp_urandomb_ui (randstate, b);
68 }
69 
70 
71 ulong
random_modulus(unsigned b,int require_odd)72 random_modulus (unsigned b, int require_odd)
73 {
74    ZNP_ASSERT(b >= 2 && b <= ULONG_BITS);
75 
76    if (require_odd)
77    {
78       if (b == 2)
79          return 3;
80       return (1UL << (b - 1)) + 2 * random_ulong_bits (b - 2) + 1;
81    }
82    else
83       return (1UL << (b - 1)) + random_ulong_bits (b - 1);
84 }
85 
86 
87 void
zn_array_print(const ulong * x,size_t n)88 zn_array_print (const ulong* x, size_t n)
89 {
90    size_t i;
91    printf ("[");
92    for (i = 0; i < n; i++)
93    {
94       if (i)
95          printf (", ");
96       printf ("%lu", x[i]);
97    }
98    printf ("]");
99 }
100 
101 
102 void
ZNP_mpn_random2(mp_limb_t * res,size_t n)103 ZNP_mpn_random2 (mp_limb_t* res, size_t n)
104 {
105    size_t i;
106 
107    mpn_random2 (res, n);
108 
109    if (random_ulong (2))
110       for (i = 0; i < n; i++)
111          res[i] ^= GMP_NUMB_MASK;
112 }
113 
114 
115 // end of file ****************************************************************
116