-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathround_ulp.c
424 lines (331 loc) · 8.67 KB
/
round_ulp.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
Compilation:
gcc -std=c99 -O3 -W -Wall -pedantic -c round_ulp.c
Author:
laurent.deniau@cern.ch, 2012
*/
#include <math.h>
#include <assert.h>
#include "round_ulp.h"
// union type
typedef union {
double fval;
unsigned long long ival;
} value_t;
// compile-time assertion
enum {
invalid_size_of_ulonglong_vs_double = 1/(sizeof(double) == sizeof(unsigned long long))
};
#if defined(TEST) || defined(SEARCH)
// forward declaration
static void print(value_t x);
#endif
// ---- round_ulp -------------------------------------------------------------
// round2ulp: portable rounding function to ±2 ulps (discading the 2 lsbs).
// thread safe after first call.
void
round2ulp(double *x_)
{
enum { ulps = 2 };
static unsigned long long mask = 0;
static int done = 0;
assert(x_);
value_t x = { *x_ };
// do not process NaN and Inf
if (isfinite(x.fval)) {
// setup the mask to truncate log2(ulps)+1 lsbs
if (!done) {
value_t fmask = { 0.0 };
for (int i=1; i < 2*ulps; i++)
fmask.fval = nextafter(fmask.fval, 1);
mask = ~fmask.ival;
done = 0;
}
// add ulps-1 times 1 ulp
double y = 2*ulps*x.fval;
for (int i=1; i < ulps; i++)
x.fval = nextafter(x.fval, y);
#if defined(TEST) && !defined(SEARCH)
print(x);
#endif
// truncate lsbs
x.ival &= mask;
*x_ = x.fval;
}
}
// round4ulp: portable rounding function to ±4 ulps (discading the 3 lsbs).
// thread safe after first call.
void
round4ulp(double *x_)
{
enum { ulps = 4 };
static unsigned long long mask = 0;
static int done = 0;
assert(x_);
value_t x = { *x_ };
// do not process NaN and Inf
if (isfinite(x.fval)) {
// setup the mask to truncate log2(ulps)+1 lsbs
if (!done) {
value_t fmask = { 0.0 };
for (int i=1; i < 2*ulps; i++)
fmask.fval = nextafter(fmask.fval, 1);
mask = ~fmask.ival;
done = 0;
}
// add ulps-1 times 1 ulp
double y = 2*ulps*x.fval;
for (int i=1; i < ulps; i++)
x.fval = nextafter(x.fval, y);
#if defined(TEST) && !defined(SEARCH)
print(x);
#endif
// truncate lsbs
x.ival &= mask;
*x_ = x.fval;
}
}
// round8ulp: portable rounding function to ±8 ulps (discading the 4 lsbs).
// thread safe after first call.
void
round8ulp(double *x_)
{
enum { ulps = 8 };
static unsigned long long mask = 0;
static int done = 0;
assert(x_);
value_t x = { *x_ };
// do not process NaN and Inf
if (isfinite(x.fval)) {
// setup the mask to truncate log2(ulps)+1 lsbs
if (!done) {
value_t fmask = { 0.0 };
for (int i=1; i < 2*ulps; i++)
fmask.fval = nextafter(fmask.fval, 1);
mask = ~fmask.ival;
done = 0;
}
// add ulps-1 times 1 ulp
double y = 2*ulps*x.fval;
for (int i=1; i < ulps; i++)
x.fval = nextafter(x.fval, y);
#if defined(TEST) && !defined(SEARCH)
print(x);
#endif
// truncate lsbs
x.ival &= mask;
*x_ = x.fval;
}
}
// roundnulp: portable rounding function to ±n ulps
// not thread safe
int
roundnulp(double *x_, int *n_)
{
static unsigned long long mask = 0;
static int ulps = 0;
assert(x_ && n_);
value_t x = { *x_ };
// do not process NaN and Inf
if (isfinite(x.fval)) {
int n = *n_;
// setup the mask to truncate log2(ulps)+1 lsbs
if (n != ulps) {
value_t fmask = { 0.0 };
if (n < 2 || (n & (n-1))) return -1; // must be a positive power of two
for (int i=1; i < 2*n; i++)
fmask.fval = nextafter(fmask.fval, 1);
mask = ~fmask.ival;
ulps = n;
}
// add ulps-1 times 1 ulp
double y = 2*ulps*x.fval;
for (int i=1; i < ulps; i++)
x.fval = nextafter(x.fval, y);
#if defined(TEST) && !defined(SEARCH)
print(x);
#endif
// truncate lsbs
x.ival &= mask;
*x_ = x.fval;
}
return 0;
}
// ---- testsuite -------------------------------------------------------------
/*
Testsuite compilation:
gcc -std=c99 -O3 -W -Wall -pedantic round_ulp.c -o round_ulp -lm
testing for special cases: add -DTEST=n where n=2,4,8
looking for invalid cases: add -DSEARCH=n where n=2,4,8
parallel search : add -fopenmp
32-bit architecture : add -m32
*/
#if defined(TEST) || defined(SEARCH)
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <time.h>
#ifdef TEST
#define round_ulp round_ulp_name(TEST)
#else
#define round_ulp round_ulp_name(SEARCH)
#endif
#define round_ulp_name( a) round_ulp_name_(a)
#define round_ulp_name_(a) round ## a ## ulp
static void
print(value_t x)
{
printf("%+#.13A [%016llX]", x.fval, x.ival);
}
static void
check_rounding(double a)
{
for (int i = 0; i <= 20; i++) {
printf("round%02d: ", i);
value_t x = { a+(nextafter(a,2*a)-a)*i };
value_t y = x;
round_ulp(&y.fval);
printf(" -> "); print(x); printf(" -> "); print(y); printf("\n");
}
}
#ifdef SEARCH
static void
invalid_case(int i, double x, double e)
{
printf("** invalid case detected for i=%d, x=%.17e, e=%.17e\n", i, x, e);
check_rounding(x);
exit(EXIT_FAILURE);
}
static void
check_case(double x, int loops)
{
double y=0, z;
for (int i = 0; i < loops; i++) {
z = x; round_ulp(&z);
#ifdef XCHECK
int n = SEARCH;
double z2 = x;
assert(!roundnulp(&z2, &n));
if (z != z2) invalid_case(i, x, x-z);
#endif
if (i % (SEARCH*2) == 0) y = z;
if (y != z) invalid_case(i, x, x-y);
x = nextafter(x,2*x);
}
}
static double
init_case(double x)
{
double y = x*(SEARCH*2*2);
round_ulp(&x);
for (int i = 0; i < SEARCH+1; i++)
x = nextafter(x,y);
return x;
}
static unsigned
next_seed(unsigned a)
{
// group generator for any \frac{\setN}{2^k\setN}, k=1..32
// must start with a=1
return a * 2621124293u + 1;
}
#endif
int main(void)
{
static const double pi = 3.141592653589793238462643383279502884197169399;
static const double e = 2.718281828459045235360287471352662497757247094;
static const double sn = 0X1.ffffffffffff0P-1022; // close to max subnormal
#ifdef TEST
// denormalized positive ulps
printf("** denormalized positive ulps at +0.0\n");
check_rounding(nextafter(+0.0,1));
// denormalized negative ulps
printf("** denormalized negative ulps at -0.0\n");
check_rounding(nextafter(-0.0,-1));
// denormalized positive ulps
printf("** denormalized maximum positive at +sn\n");
check_rounding(+sn);
// denormalized negative ulps
printf("** denormalized maximum negative at -sn\n");
check_rounding(-sn);
// normalized positive ulps
printf("** normalized positive ulps at +1.0\n");
check_rounding(+1.0);
// normalized negative ulps
printf("** normalized negative ulps at -1.0\n");
check_rounding(-1.0);
// normalized positive ulps
printf("** normalized positive ulps at +pi\n");
check_rounding(+pi);
// normalized negative ulps
printf("** normalized negative ulps at -pi\n");
check_rounding(-pi);
// normalized positive ulps
printf("** normalized positive ulps at +1/pi\n");
check_rounding(+1.0/pi);
// normalized negative ulps
printf("** normalized negative ulps at -1/pi\n");
check_rounding(-1.0/pi);
// normalized positive ulps
printf("** normalized positive ulps at +e\n");
check_rounding(+e);
// normalized negative ulps
printf("** normalized negative ulps at -e\n");
check_rounding(-e);
// normalized positive ulps
printf("** normalized positive ulps at +1/e\n");
check_rounding(+1.0/e);
// normalized negative ulps
printf("** normalized negative ulps at -1/e\n");
check_rounding(-1.0/e);
#endif
#ifdef SEARCH
// search for invalid cases (>1 million)
enum { loops = 10000000 };
#pragma omp parallel
#pragma omp sections
{
{
printf("** searching for invalid cases at sn\n");
check_case(init_case(sn), loops);
}
#pragma omp section
{
printf("** searching for invalid cases at pi\n");
check_case(init_case(pi), loops);
}
#pragma omp section
{
printf("** searching for invalid cases at 1/pi\n");
check_case(init_case(1/pi), loops);
}
#pragma omp section
{
printf("** searching for invalid cases at e\n");
check_case(init_case(e), loops);
}
#pragma omp section
{
printf("** searching for invalid cases at 1/e\n");
check_case(init_case(1/e), loops);
}
}
{
enum { nperiods = 10, inloops = nperiods*SEARCH*2 };
srand(time(0));
int l = ((1<<24)-1) & rand();
unsigned s = 1;
while (l--) s = next_seed(s);
printf("** searching for invalid cases at %d random positions checking %d periods\n",
loops, nperiods);
#pragma omp parallel for shared(s)
for (int i = 0; i < loops; i++) {
double x = (s = next_seed(s));
check_case(init_case(x), inloops);
double y = 1/x;
check_case(init_case(y), inloops);
}
}
#endif
}
#endif