-
Notifications
You must be signed in to change notification settings - Fork 0
/
r128.h
521 lines (421 loc) · 11.5 KB
/
r128.h
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
r128.h: 128-bit (64.64) signed fixed-point arithmetic. Version 1.6.0
Modified by tcsullivan to be used as (4.124) fixed-point.
Unused operators also removed.
COMPILATION
-----------
Drop this header file somewhere in your project and include it wherever it is
needed. There is no separate .c file for this library. To get the code, in ONE
file in your project, put:
#define R128_IMPLEMENTATION
before you include this file. You may also provide a definition for R128_ASSERT
to force the library to use a custom assert macro.
COMPILER/LIBRARY SUPPORT
------------------------
This library requires a C89 compiler with support for 64-bit integers. If your
compiler does not support the long long data type, the R128_U64, etc. macros
must be set appropriately. On x86 and x64 targets, Intel intrinsics are used
for speed. If your compiler does not support these intrinsics, you can add
#define R128_STDC_ONLY
in your implementation file before including r128.h.
The only C runtime library functionality used by this library is <assert.h>.
This can be avoided by defining an R128_ASSERT macro in your implementation
file. Since this library uses 64-bit arithmetic, this may implicitly add a
runtime library dependency on 32-bit platforms.
C++ SUPPORT
-----------
Operator overloads are supplied for C++ files that include this file. Since all
C++ functions are declared inline (or static inline), the R128_IMPLEMENTATION
file can be either C++ or C.
LICENSE
-------
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef H_R128_H
#define H_R128_H
#include <stddef.h>
#include <stdint.h>
#define R128_S32 int32_t
#define R128_U32 uint32_t
#define R128_S64 int64_t
#define R128_U64 uint64_t
#ifdef __cplusplus
extern "C" {
#endif
typedef struct R128 {
R128_U64 lo;
R128_U64 hi;
#ifdef __cplusplus
R128();
R128(double);
R128(int);
R128(R128_S64);
R128(R128_U64 low, R128_U64 high);
operator double() const;
R128 operator-() const;
R128 &operator+=(const R128 &rhs);
R128 &operator-=(const R128 &rhs);
R128 &operator*=(const R128 &rhs);
#endif //__cplusplus
} __attribute__ ((packed)) R128;
// Type conversion
extern void r128FromInt(R128 *dst, R128_S64 v);
extern void r128FromFloat(R128 *dst, double v);
extern double r128ToFloat(const R128 *v);
// Copy
extern void r128Copy(R128 *dst, const R128 *src);
// Sign manipulation
extern void r128Neg(R128 *dst, const R128 *v); // -v
// Bitwise operations
extern void r128Shl(R128 *dst, const R128 *src, int amount); // shift left by amount mod 128
extern void r128Shr(R128 *dst, const R128 *src, int amount); // shift right logical by amount mod 128
// Arithmetic
extern void r128Add(R128 *dst, const R128 *a, const R128 *b); // a + b
extern void r128Sub(R128 *dst, const R128 *a, const R128 *b); // a - b
extern void r128Mul(R128 *dst, const R128 *a, const R128 *b); // a * b
// Comparison
extern int r128Cmp(const R128 *a, const R128 *b); // sign of a-b
extern int r128IsNeg(const R128 *v); // quick check for < 0
// Constants
extern const R128 R128_min; // minimum (most negative) value
extern const R128 R128_max; // maximum (most positive) value
extern const R128 R128_smallest; // smallest positive value
extern const R128 R128_zero; // zero
extern const R128 R128_one; // 1.0
extern char R128_decimal; // decimal point character used by r128From/ToString. defaults to '.'
#ifdef __cplusplus
}
inline R128::R128() {}
inline R128::R128(double v)
{
r128FromFloat(this, v);
}
inline R128::R128(int v)
{
r128FromInt(this, v);
}
inline R128::R128(R128_S64 v)
{
r128FromInt(this, v);
}
inline R128::R128(R128_U64 low, R128_U64 high)
{
lo = low;
hi = high;
}
inline R128::operator double() const
{
return r128ToFloat(this);
}
inline R128 R128::operator-() const
{
R128 r;
r128Neg(&r, this);
return r;
}
inline R128 &R128::operator+=(const R128 &rhs)
{
r128Add(this, this, &rhs);
return *this;
}
inline R128 &R128::operator-=(const R128 &rhs)
{
r128Sub(this, this, &rhs);
return *this;
}
inline R128 &R128::operator*=(const R128 &rhs)
{
r128Mul(this, this, &rhs);
return *this;
}
static inline R128 operator+(const R128 &lhs, const R128 &rhs)
{
R128 r(lhs);
return r += rhs;
}
static inline R128 operator-(const R128 &lhs, const R128 &rhs)
{
R128 r(lhs);
return r -= rhs;
}
static inline R128 operator*(const R128 &lhs, const R128 &rhs)
{
R128 r(lhs);
return r *= rhs;
}
static inline bool operator<(const R128 &lhs, const R128 &rhs)
{
return r128Cmp(&lhs, &rhs) < 0;
}
static inline bool operator>(const R128 &lhs, const R128 &rhs)
{
return r128Cmp(&lhs, &rhs) > 0;
}
static inline bool operator<=(const R128 &lhs, const R128 &rhs)
{
return r128Cmp(&lhs, &rhs) <= 0;
}
static inline bool operator>=(const R128 &lhs, const R128 &rhs)
{
return r128Cmp(&lhs, &rhs) >= 0;
}
static inline bool operator==(const R128 &lhs, const R128 &rhs)
{
return lhs.lo == rhs.lo && lhs.hi == rhs.hi;
}
static inline bool operator!=(const R128 &lhs, const R128 &rhs)
{
return lhs.lo != rhs.lo || lhs.hi != rhs.hi;
}
#endif //__cplusplus
#endif //H_R128_H
#ifdef R128_IMPLEMENTATION
#define R128_SET2(x, l, h) { (x)->lo = (R128_U64)(l); (x)->hi = (R128_U64)(h); }
#include <stdlib.h> // for NULL
const R128 R128_min = { 0, 0x8000000000000000 };
const R128 R128_max = { 0xffffffffffffffff, 0x7fffffffffffffff };
const R128 R128_smallest = { 1, 0 };
const R128 R128_zero = { 0, 0 };
const R128 R128_one = { 0, 1 };
char R128_decimal = '.';
static void r128__neg(R128 *dst, const R128 *src)
{
if (src->lo) {
dst->lo = ~src->lo + 1;
dst->hi = ~src->hi;
} else {
dst->lo = 0;
dst->hi = ~src->hi + 1;
}
}
// 64*64->128
static void r128__umul128(R128 *dst, R128_U64 a, R128_U64 b)
{
R128_U32 alo = (R128_U32)a;
R128_U32 ahi = (R128_U32)(a >> 32);
R128_U32 blo = (R128_U32)b;
R128_U32 bhi = (R128_U32)(b >> 32);
R128_U64 p0, p1, p2, p3;
p0 = alo * (uint64_t)blo;
p1 = alo * (uint64_t)bhi;
p2 = ahi * (uint64_t)blo;
p3 = ahi * (uint64_t)bhi;
{
R128_U64 carry, lo, hi;
carry = ((R128_U64)(R128_U32)p1 + (R128_U64)(R128_U32)p2 + (p0 >> 32)) >> 32;
lo = p0 + ((p1 + p2) << 32);
hi = p3 + ((R128_U32)(p1 >> 32) + (R128_U32)(p2 >> 32)) + carry;
R128_SET2(dst, lo, hi);
}
}
static int r128__ucmp(const R128 *a, const R128 *b)
{
if (a->hi != b->hi) {
if (a->hi > b->hi) {
return 1;
} else {
return -1;
}
} else {
if (a->lo == b->lo) {
return 0;
} else if (a->lo > b->lo) {
return 1;
} else {
return -1;
}
}
}
static void r128__umul(R128 *dst, const R128 *a, const R128 *b)
{
R128 albl, ahbl, albh, ahbh;
r128__umul128(&albl, a->lo, b->lo);
r128__umul128(&ahbl, a->hi, b->lo);
r128__umul128(&albh, a->lo, b->hi);
r128__umul128(&ahbh, a->hi, b->hi);
R128 sum;
r128Shr(&ahbh, &ahbh, 60);
sum.lo = 0;
sum.hi = ahbh.lo;
albl.lo = albl.hi;
albl.hi = 0;
r128Add(&sum, &sum, &albl);
r128Add(&sum, &sum, &ahbl);
r128Add(&sum, &sum, &albh);
R128_SET2(dst, sum.lo, sum.hi);
}
void r128FromInt(R128 *dst, R128_S64 v)
{
dst->lo = 0;
dst->hi = (R128_U64)v << 60;
}
void r128FromFloat(R128 *dst, double v)
{
if (v < -9223372036854775808.0) {
r128Copy(dst, &R128_min);
} else if (v >= 9223372036854775808.0) {
r128Copy(dst, &R128_max);
} else {
R128 r;
int sign = 0;
if (v < 0) {
v = -v;
sign = 1;
}
r.hi = ((R128_U64)(R128_S32)v) << 60;
v -= (R128_S32)v;
v *= (double)((R128_U64)1 << 60);
r.hi = (r.hi & 0xF000000000000000) + (R128_U64)v;
r.lo = (R128_U64)(v * 18446744073709551616.0);
if (sign) {
r128__neg(&r, &r);
}
r128Copy(dst, &r);
}
}
R128_S64 r128ToInt(const R128 *v)
{
if ((R128_S64)v->hi < 0) {
return (R128_S64)v->hi + (v->lo != 0);
} else {
return (R128_S64)v->hi;
}
}
double r128ToFloat(const R128 *v)
{
R128 tmp;
int sign = 0;
double d;
R128_SET2(&tmp, v->lo, v->hi);
if (r128IsNeg(&tmp)) {
r128__neg(&tmp, &tmp);
sign = 1;
}
d = (tmp.hi >> 60);
d += (tmp.hi & 0xFFFFFFFFFFFFFF) / (double)((uint64_t)1 << 60);
d += tmp.lo / (double)((uint64_t)1 << 60) / 18446744073709551616.0;
if (sign) {
d = -d;
}
return d;
}
void r128Copy(R128 *dst, const R128 *src)
{
dst->lo = src->lo;
dst->hi = src->hi;
}
void r128Neg(R128 *dst, const R128 *v)
{
r128__neg(dst, v);
}
void r128Shl(R128 *dst, const R128 *src, int amount)
{
R128_U64 r[4];
r[0] = src->lo;
r[1] = src->hi;
amount &= 127;
if (amount >= 64) {
r[1] = r[0] << (amount - 64);
r[0] = 0;
} else if (amount) {
r[1] = (r[1] << amount) | (r[0] >> (64 - amount));
r[0] = r[0] << amount;
}
dst->lo = r[0];
dst->hi = r[1];
}
void r128Shr(R128 *dst, const R128 *src, int amount)
{
R128_U64 r[4];
r[2] = src->lo;
r[3] = src->hi;
amount &= 127;
if (amount >= 64) {
r[2] = r[3] >> (amount - 64);
r[3] = 0;
} else if (amount) {
r[2] = (r[2] >> amount) | (r[3] << (64 - amount));
r[3] = r[3] >> amount;
}
dst->lo = r[2];
dst->hi = r[3];
}
void r128Add(R128 *dst, const R128 *a, const R128 *b)
{
unsigned char carry = 0;
{
R128_U64 r = a->lo + b->lo;
carry = r < a->lo;
dst->lo = r;
dst->hi = a->hi + b->hi + carry;
}
}
void r128Sub(R128 *dst, const R128 *a, const R128 *b)
{
unsigned char borrow = 0;
{
R128_U64 r = a->lo - b->lo;
borrow = r > a->lo;
dst->lo = r;
dst->hi = a->hi - b->hi - borrow;
}
}
void r128Mul(R128 *dst, const R128 *a, const R128 *b)
{
int sign = 0;
R128 ta, tb, tc;
R128_SET2(&ta, a->lo, a->hi);
R128_SET2(&tb, b->lo, b->hi);
if (r128IsNeg(&ta)) {
r128__neg(&ta, &ta);
sign = !sign;
}
if (r128IsNeg(&tb)) {
r128__neg(&tb, &tb);
sign = !sign;
}
r128__umul(&tc, &ta, &tb);
if (sign) {
r128__neg(&tc, &tc);
}
r128Copy(dst, &tc);
}
int r128Cmp(const R128 *a, const R128 *b)
{
if (a->hi == b->hi) {
if (a->lo == b->lo) {
return 0;
} else if (a->lo > b->lo) {
return 1;
} else {
return -1;
}
} else if ((R128_S64)a->hi > (R128_S64)b->hi) {
return 1;
} else {
return -1;
}
}
int r128IsNeg(const R128 *v)
{
return (R128_S64)v->hi < 0;
}
#endif //R128_IMPLEMENTATION