-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers_int.c
150 lines (133 loc) · 3.01 KB
/
helpers_int.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* helpers_int.c -- helper functions for the long long version of tests
*
* Copyright 2014 by Colin Benner <[email protected]>
*
* This file is part of frobenius-test.
*
* frobenius-test is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* frobenius-test is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with frobenius-test. If not, see <http://www.gnu.org/licenses/>.
*/
#include "helpers_int.h"
#include <math.h>
#include <stdlib.h>
#include <sys/time.h>
/*
* Multiply x and y, reducing the result modulo n.
*/
uint64_t mul_mod_n(uint64_t x, uint64_t y, uint64_t n)
{
#if 0
uint64_t q, r, p1, p2;
umul_ppmm(p1, p2, x, y);
udiv_qrnnd(q, r, p1, p2, n);
return r;
#endif
return (x * y) % n;
}
/*
* Given an integer 0 ≤ n < 2³², find the largest integer r such that r² ≤ n.
*/
uint64_t int_sqrt(const uint64_t n)
{
uint64_t root = (uint64_t)(sqrt((double)n));
if (n == 0)
return 0;
if (root * root > n)
root = n / root;
while ((root + 1) * (root + 1) <= n)
root++;
return root;
}
/*
* Calculate the greatest common divisor of a and b using the Euclidean
* algorithm.
*/
uint64_t gcd(uint64_t a, uint64_t b)
{
uint64_t t;
while (b != 0) {
t = b;
b = a % b;
a = t;
}
return a;
}
/*
* Test whether a given integer is a perfect square.
*/
bool is_square(const uint64_t n)
{
uint64_t sqrt = int_sqrt(n);
return sqrt * sqrt == n;
}
/*
* Calculate s, d such that n-1=2^s*d where d is odd.
*/
void split_int(uint64_t *s, uint64_t *d, const uint64_t n)
{
*s = 0;
*d = n - 1;
while (even(*d)) {
(*s)++;
*d /= 2;
}
}
/*
* This function generates a random integer between in the interval
* [low, high]. As we divide by (high - low + 1) in the process, we need
* low < high.
*/
uint64_t get_random_int(const uint64_t low, const uint64_t high)
{
return (uint64_t)rand() % (high - low + 1) + low;
}
/*
* Compute the jacobi symbol (x/y) of x over y. This is a direct translation
* of the algorithm given in Otto Forster: Algorithmische Zahlentheorie.
*/
int jacobi(uint64_t x, uint64_t y)
{
uint64_t t;
int res = 1, m8;
for (;; ) {
x %= y;
if (x == 0)
return 0;
while (even(x)) {
x /= 2;
m8 = y % 8;
if (m8 == 3 || m8 == 5)
res = -res;
}
if (x == 1)
return res;
t = x;
x = y;
y = t;
if (x % 4 == 3 && y % 4 == 3)
res = -res;
}
}
/*
* Initialises the random number generator using a (static) seed.
*/
int init_int(void)
{
unsigned seed = 123456;
/* Uncomment the following lines to use a dynamic seed. */
//struct timeval tv;
//gettimeofday(&tv, NULL);
//seed = tv.tv_usec;
srand(seed);
return 0;
}