-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdouble.c
479 lines (384 loc) · 13.8 KB
/
double.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
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
/*******************************************************************
** m a t h 6 4 . c
** Forth Inspired Command Language - 64 bit math support routines
** Authors: Michael A. Gauland ([email protected])
** Larry Hastings ([email protected])
** John Sadler ([email protected])
** Created: 25 January 1998
** Rev 2.03: Support for 128 bit DP math. This file really ouught to
** be renamed!
** $Id$
*******************************************************************/
/*
** Copyright (c) 1997-2001 John Sadler ([email protected])
** All rights reserved.
**
** Get the latest Ficl release at http://ficl.sourceforge.net
**
** I am interested in hearing from anyone who uses Ficl. If you have
** a problem, a success story, a defect, an enhancement request, or
** if you would like to contribute to the Ficl release, please
** contact me by email at the address above.
**
** L I C E N S E and D I S C L A I M E R
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE.
*/
#include <stdint.h>
#include "ficl.h"
#if FICL_PLATFORM_HAS_2INTEGER
ficl2UnsignedQR ficl2UnsignedDivide(ficl2Unsigned q, ficlUnsigned y)
{
ficl2UnsignedQR result;
result.quotient = q / y;
/*
** Once we have the quotient, it's cheaper to calculate the
** remainder this way than with % (mod). --lch
*/
result.remainder = (ficlInteger)(q - (result.quotient * y));
return result;
}
#else /* FICL_PLATFORM_HAS_2INTEGER */
#define FICL_CELL_HIGH_BIT ((uintmax_t)1 << (FICL_BITS_PER_CELL-1))
#define UMOD_SHIFT (FICL_BITS_PER_CELL / 2)
#define UMOD_MASK ((1L << (FICL_BITS_PER_CELL / 2)) - 1)
/**************************************************************************
ficl2IntegerIsNegative
** Returns TRUE if the specified ficl2Unsigned has its sign bit set.
**************************************************************************/
int ficl2IntegerIsNegative(ficl2Integer x)
{
return (x.high < 0);
}
/**************************************************************************
ficl2IntegerNegate
** Negates an ficl2Unsigned by complementing and incrementing.
**************************************************************************/
ficl2Integer ficl2IntegerNegate(ficl2Integer x)
{
x.high = ~x.high;
x.low = ~x.low;
x.low ++;
if (x.low == 0)
x.high++;
return x;
}
/**************************************************************************
ficl2UnsignedMultiplyAccumulate
** Mixed precision multiply and accumulate primitive for number building.
** Multiplies ficl2Unsigned u by ficlUnsigned mul and adds ficlUnsigned add. Mul is typically
** the numeric base, and add represents a digit to be appended to the
** growing number.
** Returns the result of the operation
**************************************************************************/
ficl2Unsigned ficl2UnsignedMultiplyAccumulate(ficl2Unsigned u, ficlUnsigned mul, ficlUnsigned add)
{
ficl2Unsigned resultLo = ficl2UnsignedMultiply(u.low, mul);
ficl2Unsigned resultHi = ficl2UnsignedMultiply(u.high, mul);
resultLo.high += resultHi.low;
resultHi.low = resultLo.low + add;
if (resultHi.low < resultLo.low)
resultLo.high++;
resultLo.low = resultHi.low;
return resultLo;
}
/**************************************************************************
ficl2IntegerMultiply
** Multiplies a pair of ficlIntegers and returns an ficl2Integer result.
**************************************************************************/
ficl2Integer ficl2IntegerMultiply(ficlInteger x, ficlInteger y)
{
ficl2Unsigned prod;
int sign = 1;
if (x < 0)
{
sign = -sign;
x = -x;
}
if (y < 0)
{
sign = -sign;
y = -y;
}
prod = ficl2UnsignedMultiply(x, y);
if (sign > 0)
return FICL_2UNSIGNED_TO_2INTEGER(prod);
else
return ficl2IntegerNegate(FICL_2UNSIGNED_TO_2INTEGER(prod));
}
ficl2Integer ficl2IntegerDecrement(ficl2Integer x)
{
if (x.low == INT_MIN)
x.high--;
x.low--;
return x;
}
ficl2Unsigned ficl2UnsignedAdd(ficl2Unsigned x, ficl2Unsigned y)
{
ficl2Unsigned result;
int carry;
result.high = x.high + y.high;
result.low = x.low + y.low;
carry = ((x.low | y.low) & FICL_CELL_HIGH_BIT) && !(result.low & FICL_CELL_HIGH_BIT);
carry |= ((x.low & y.low) & FICL_CELL_HIGH_BIT);
if (carry)
{
result.high++;
}
return result;
}
/**************************************************************************
ficl2UnsignedMultiply
** Contributed by:
** Michael A. Gauland [email protected]
**************************************************************************/
ficl2Unsigned ficl2UnsignedMultiply(ficlUnsigned x, ficlUnsigned y)
{
ficl2Unsigned result = { 0, 0 };
ficl2Unsigned addend;
addend.low = y;
addend.high = 0; /* No sign extension--arguments are unsigned */
while (x != 0)
{
if ( x & 1)
{
result = ficl2UnsignedAdd(result, addend);
}
x >>= 1;
addend = ficl2UnsignedArithmeticShiftLeft(addend);
}
return result;
}
/**************************************************************************
ficl2UnsignedSubtract
**
**************************************************************************/
ficl2Unsigned ficl2UnsignedSubtract(ficl2Unsigned x, ficl2Unsigned y)
{
ficl2Unsigned result;
result.high = x.high - y.high;
result.low = x.low - y.low;
if (x.low < y.low)
{
result.high--;
}
return result;
}
/**************************************************************************
ficl2UnsignedArithmeticShiftLeft
** 64 bit left shift
**************************************************************************/
ficl2Unsigned ficl2UnsignedArithmeticShiftLeft( ficl2Unsigned x )
{
ficl2Unsigned result;
result.high = x.high << 1;
if (x.low & FICL_CELL_HIGH_BIT)
{
result.high++;
}
result.low = x.low << 1;
return result;
}
/**************************************************************************
ficl2UnsignedArithmeticShiftRight
** 64 bit right shift (unsigned - no sign extend)
**************************************************************************/
ficl2Unsigned ficl2UnsignedArithmeticShiftRight( ficl2Unsigned x )
{
ficl2Unsigned result;
result.low = x.low >> 1;
if (x.high & 1)
{
result.low |= FICL_CELL_HIGH_BIT;
}
result.high = x.high >> 1;
return result;
}
/**************************************************************************
ficl2UnsignedOr
** 64 bit bitwise OR
**************************************************************************/
ficl2Unsigned ficl2UnsignedOr( ficl2Unsigned x, ficl2Unsigned y )
{
ficl2Unsigned result;
result.high = x.high | y.high;
result.low = x.low | y.low;
return result;
}
/**************************************************************************
ficl2UnsignedCompare
** Return -1 if x < y; 0 if x==y, and 1 if x > y.
**************************************************************************/
int ficl2UnsignedCompare(ficl2Unsigned x, ficl2Unsigned y)
{
if (x.high > y.high)
return 1;
if (x.high < y.high)
return -1;
/* High parts are equal */
if (x.low > y.low)
return 1;
else if (x.low < y.low)
return -1;
return 0;
}
/**************************************************************************
ficl2UnsignedDivide
** Portable versions of ficl2Multiply and ficl2Divide in C
** Contributed by:
** Michael A. Gauland [email protected]
**************************************************************************/
ficl2UnsignedQR ficl2UnsignedDivide(ficl2Unsigned q, ficlUnsigned y)
{
ficl2UnsignedQR result;
ficl2Unsigned quotient;
ficl2Unsigned subtrahend;
ficl2Unsigned mask;
quotient.low = 0;
quotient.high = 0;
subtrahend.low = y;
subtrahend.high = 0;
mask.low = 1;
mask.high = 0;
while ((ficl2UnsignedCompare(subtrahend, q) < 0) &&
(subtrahend.high & FICL_CELL_HIGH_BIT) == 0)
{
mask = ficl2UnsignedArithmeticShiftLeft(mask);
subtrahend = ficl2UnsignedArithmeticShiftLeft(subtrahend);
}
while (mask.low != 0 || mask.high != 0)
{
if (ficl2UnsignedCompare(subtrahend, q) <= 0)
{
q = ficl2UnsignedSubtract( q, subtrahend);
quotient = ficl2UnsignedOr(quotient, mask);
}
mask = ficl2UnsignedArithmeticShiftRight(mask);
subtrahend = ficl2UnsignedArithmeticShiftRight(subtrahend);
}
result.quotient = quotient;
result.remainder = q.low;
return result;
}
#endif /* !FICL_PLATFORM_HAS_2INTEGER */
/**************************************************************************
ficl2IntegerAbsoluteValue
** Returns the absolute value of an ficl2Unsigned
**************************************************************************/
ficl2Integer ficl2IntegerAbsoluteValue(ficl2Integer x)
{
if (ficl2IntegerIsNegative(x))
return ficl2IntegerNegate(x);
return x;
}
/**************************************************************************
ficl2IntegerDivideFloored
**
** FROM THE FORTH ANS...
** Floored division is integer division in which the remainder carries
** the sign of the divisor or is zero, and the quotient is rounded to
** its arithmetic floor. Symmetric division is integer division in which
** the remainder carries the sign of the dividend or is zero and the
** quotient is the mathematical quotient rounded towards zero or
** truncated. Examples of each are shown in tables 3.3 and 3.4.
**
** Table 3.3 - Floored Division Example
** Dividend Divisor Remainder Quotient
** -------- ------- --------- --------
** 10 7 3 1
** -10 7 4 -2
** 10 -7 -4 -2
** -10 -7 -3 1
**
**
** Table 3.4 - Symmetric Division Example
** Dividend Divisor Remainder Quotient
** -------- ------- --------- --------
** 10 7 3 1
** -10 7 -3 -1
** 10 -7 3 -1
** -10 -7 -3 1
**************************************************************************/
ficl2IntegerQR ficl2IntegerDivideFloored(ficl2Integer num, ficlInteger den)
{
ficl2IntegerQR qr;
ficl2UnsignedQR uqr;
int signRem = 1;
int signQuot = 1;
if (ficl2IntegerIsNegative(num))
{
num = ficl2IntegerNegate(num);
signQuot = -signQuot;
}
if (den < 0)
{
den = -den;
signRem = -signRem;
signQuot = -signQuot;
}
uqr = ficl2UnsignedDivide(FICL_2INTEGER_TO_2UNSIGNED(num), (ficlUnsigned)den);
qr = FICL_2UNSIGNEDQR_TO_2INTEGERQR(uqr);
if (signQuot < 0)
{
qr.quotient = ficl2IntegerNegate(qr.quotient);
if (qr.remainder != 0)
{
qr.quotient = ficl2IntegerDecrement(qr.quotient);
qr.remainder = den - qr.remainder;
}
}
if (signRem < 0)
qr.remainder = -qr.remainder;
return qr;
}
/**************************************************************************
ficl2IntegerDivideSymmetric
** Divide an ficl2Unsigned by a ficlInteger and return a ficlInteger quotient and a
** ficlInteger remainder. The absolute values of quotient and remainder are not
** affected by the signs of the numerator and denominator (the operation
** is symmetric on the number line)
**************************************************************************/
ficl2IntegerQR ficl2IntegerDivideSymmetric(ficl2Integer num, ficlInteger den)
{
ficl2IntegerQR qr;
ficl2UnsignedQR uqr;
int signRem = 1;
int signQuot = 1;
if (ficl2IntegerIsNegative(num))
{
num = ficl2IntegerNegate(num);
signRem = -signRem;
signQuot = -signQuot;
}
if (den < 0)
{
den = -den;
signQuot = -signQuot;
}
uqr = ficl2UnsignedDivide(FICL_2INTEGER_TO_2UNSIGNED(num), (ficlUnsigned)den);
qr = FICL_2UNSIGNEDQR_TO_2INTEGERQR(uqr);
if (signRem < 0)
qr.remainder = -qr.remainder;
if (signQuot < 0)
qr.quotient = ficl2IntegerNegate(qr.quotient);
return qr;
}