Debian Codegraph @

libgcobol/gmath.cc

raw
1 /*
2 * Copyright (c) 2021-2025 Symas Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of the Symas Corporation nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <fcntl.h>
32 #include <unistd.h>
33
34 #include <cctype>
35 #include <cerrno>
36 #include <cmath>
37 #include <cfenv>
38 #include <cstdio>
39 #include <cstdlib>
40 #include <cstring>
41 #include <ctime>
42
43 #include <algorithm>
44 #include <vector>
45
46 #include "config.h"
47 #include "libgcobol-fp.h"
48
49 #include "ec.h"
50 #include "common-defs.h"
51 #include "io.h"
52 #include "gcobolio.h"
53 #include "libgcobol.h"
54 #include "gmath.h"
55 #include "gcobolio.h"
56
57 #include <sys/mman.h>
58 #include <sys/stat.h>
59 #include <sys/types.h>
60
61 #define MAX_INTERMEDIATE_BITS 126
62 #define MAX_INTERMEDIATE_DECIMALS 16
63
64 static int
65 conditional_stash( cblc_field_t *destination,
66 size_t destination_o,
67 size_t destination_s,
68 bool on_error_flag,
69 __int128 value,
70 int rdigits,
71 cbl_round_t rounded)
72 {
73 int retval = compute_error_none;
74 if( !on_error_flag )
75 {
76 // It's an uncomplicated assignment, because there was no
77 // ON SIZE ERROR phrase
78 __gg__int128_to_qualified_field(destination,
79 destination_o,
80 destination_s,
81 value,
82 rdigits,
83 rounded,
84 &retval);
85 }
86 else
87 {
88 // This is slightly more complex, because in the event of a
89 // SIZE ERROR. we need to leave the original value untouched
90
91 unsigned char *stash = static_cast<unsigned char *>(malloc(destination_s));
92 massert(stash);
93 memcpy(stash, destination->data+destination_o, destination_s);
94
95 __gg__int128_to_qualified_field(destination,
96 destination_o,
97 destination_s,
98 value,
99 rdigits,
100 rounded,
101 &retval);
102 if( retval )
103 {
104 // Because there was a size error, we will report that
105 // upon return, and we need to put back the original value:
106 memcpy(destination->data+destination_o, stash, destination_s);
107 }
108 free(stash);
109 }
110 return retval;
111 }
112
113 static int
114 conditional_stash( cblc_field_t *destination,
115 size_t destination_o,
116 size_t destination_s,
117 bool on_error_flag,
118 GCOB_FP128 value,
119 cbl_round_t rounded)
120 {
121 int retval = compute_error_none;
122 if( !on_error_flag )
123 {
124 // It's an uncomplicated assignment, because there was no
125 // ON SIZE ERROR phrase
126 __gg__float128_to_qualified_field(destination,
127 destination_o,
128 value,
129 rounded,
130 &retval);
131 }
132 else
133 {
134 // This is slightly more complex, because in the event of a
135 // SIZE ERROR. we need to leave the original value untouched
136 assert(destination_s);
137 unsigned char *stash = static_cast<unsigned char *>(malloc(destination_s));
138 massert(stash);
139 memcpy(stash, destination->data+destination_o, destination_s);
140 __gg__float128_to_qualified_field(destination,
141 destination_o,
142 value,
143 rounded,
144 &retval);
145 if( retval )
146 {
147 // Because there was a size error, we will report that
148 // upon return, and we need to put back the original value:
149 memcpy(destination->data+destination_o, stash, destination_s);
150 }
151 free(stash);
152 }
153 return retval;
154 }
155
156 static
157 GCOB_FP128
158 divide_helper_float(GCOB_FP128 a_value,
159 GCOB_FP128 b_value,
160 int *compute_error)
161 {
162 if( b_value == 0 )
163 {
164 // Can't divide by zero
165 *compute_error |= compute_error_divide_by_zero;
166 return a_value;
167 }
168
169 // Do the actual division, giving us 0.399999999999999999999999999999999971
170 a_value /= b_value;
171
172 if( __builtin_isinf(a_value) )
173 {
174 *compute_error |= compute_error_overflow;
175 return 0;
176 }
177
178 if( __builtin_isnan(a_value) )
179 {
180 *compute_error |= compute_error_underflow;
181 return 0;
182 }
183
184 return a_value;
185 }
186
187 static
188 GCOB_FP128
189 multiply_helper_float(GCOB_FP128 a_value,
190 GCOB_FP128 b_value,
191 int *compute_error)
192 {
193 a_value *= b_value;
194
195 if( __builtin_isinf(a_value) )
196 {
197 *compute_error |= compute_error_overflow;
198 return 0;
199 }
200
201 if( __builtin_isnan(a_value) )
202 {
203 *compute_error |= compute_error_underflow;
204 return 0;
205 }
206
207 return a_value;
208 }
209
210 static
211 GCOB_FP128
212 addition_helper_float(GCOB_FP128 a_value,
213 GCOB_FP128 b_value,
214 int *compute_error)
215 {
216 a_value += b_value;
217
218 if( __builtin_isinf(a_value) )
219 {
220 *compute_error |= compute_error_overflow;
221 return 0;
222 }
223
224 if( __builtin_isnan(a_value) )
225 {
226 *compute_error |= compute_error_underflow;
227 return 0;
228 }
229
230 return a_value;
231 }
232
233 static
234 GCOB_FP128
235 subtraction_helper_float(GCOB_FP128 a_value,
236 GCOB_FP128 b_value,
237 int *compute_error)
238 {
239 a_value -= b_value;
240
241 if( __builtin_isinf(a_value) )
242 {
243 *compute_error |= compute_error_overflow;
244 return 0;
245 }
246
247 if( __builtin_isnan(a_value) )
248 {
249 *compute_error |= compute_error_underflow;
250 return 0;
251 }
252
253 return a_value;
254 }
255
256 extern "C"
257 void
258 __gg__pow( cbl_arith_format_t,
259 size_t,
260 size_t,
261 size_t,
262 const cbl_round_t *rounded,
263 int on_error_flag,
264 int *compute_error
265 )
266 {
267 cblc_field_t **A = __gg__treeplet_1f;
268 const size_t *A_o = __gg__treeplet_1o;
269 const size_t *A_s = __gg__treeplet_1s;
270 cblc_field_t **B = __gg__treeplet_2f;
271 const size_t *B_o = __gg__treeplet_2o;
272 const size_t *B_s = __gg__treeplet_2s;
273 cblc_field_t **C = __gg__treeplet_3f;
274 const size_t *C_o = __gg__treeplet_3o;
275 const size_t *C_s = __gg__treeplet_3s;
276
277 GCOB_FP128 avalue = __gg__float128_from_qualified_field(A[0], A_o[0], A_s[0]);
278 GCOB_FP128 bvalue = __gg__float128_from_qualified_field(B[0], B_o[0], B_s[0]);
279 GCOB_FP128 tgt_value;
280
281 if( avalue == 0 && bvalue == 0 )
282 {
283 *compute_error |= compute_error_exp_zero_by_zero;
284 tgt_value = 1;
285 }
286 else if(avalue == 0 && bvalue < 0 )
287 {
288 *compute_error |= compute_error_exp_zero_by_minus;
289 tgt_value = 0;
290 }
291 else
292 {
293 // Calculate our answer, in floating point:
294 errno = 0;
295 feclearexcept(FE_ALL_EXCEPT);
296 tgt_value = FP128_FUNC(pow)(avalue, bvalue);
297 if( errno || fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW) )
298 {
299 // One of a large number of errors took place. See math_error(7) and
300 // pow(3). Let's just use this last error as a grab-bag; I didn't
301 // care to go down the rabbit hole of figuring out if a floating point
302 // number did or did not have a fractional part. That way lies
303 // madness.
304 *compute_error |= compute_error_exp_minus_by_frac;
305 // This kind of error doesn't overwrite the target, so the returned
306 // value is not relevant. Make it zero to avoid overheating the
307 // converstion routine
308 tgt_value = 0;
309 }
310 }
311 if( !(*compute_error & compute_error_exp_minus_by_frac) )
312 {
313 *compute_error |= conditional_stash(C[0],
314 C_o[0],
315 C_s[0],
316 (on_error_flag & ON_SIZE_ERROR),
317 tgt_value,
318 *rounded);
319 }
320 }
321
322 extern "C"
323 void
324 __gg__process_compute_error(int compute_error)
325 {
326 // This routine gets called after a series of parser_op operations is
327 // complete (see parser_assign()) when the source code didn't specify
328 // an ON SIZE ERROR clause.
329 if( compute_error & compute_error_divide_by_zero)
330 {
331 exception_raise(ec_size_zero_divide_e);
332 }
333 else if( compute_error & compute_error_truncate )
334 {
335 exception_raise(ec_size_truncation_e);
336 }
337 else if( compute_error & ( compute_error_exp_zero_by_zero
338 | compute_error_exp_zero_by_minus
339 | compute_error_exp_minus_by_frac ) )
340 {
341 exception_raise(ec_size_exponentiation_e);
342 }
343 else if( compute_error & compute_error_overflow )
344 {
345 exception_raise(ec_size_overflow_e);
346 }
347 else if( compute_error & compute_error_underflow )
348 {
349 exception_raise(ec_size_underflow_e);
350 }
351 }
352
353 typedef unsigned __int128 uint128;
354 typedef struct int256
355 {
356 union
357 {
358 unsigned char data[32];
359 uint64_t i64 [4];
360 uint128 i128[2];
361 };
362 }int256;
363
364 static int
365 multiply_int256_by_int64(int256 &product, const uint64_t multiplier)
366 {
367 // Typical use of this routine is multiplying a temporary value by
368 // a factor of ten. This is effectively left-shifting by decimal
369 // digits. See scale_int256_by_digits
370 uint64_t overflows[5] = {};
371 for(int i=0; i<4; i++)
372 {
373 uint128 temp = (uint128)product.i64[i] * multiplier;
374 product.i64[i] = *PTRCAST(uint64_t, &temp);
375 overflows[i+1] = *PTRCAST(uint64_t, PTRCAST(uint8_t, &temp) + 8);
376 }
377
378 for(int i=1; i<4; i++)
379 {
380 product.i64[i] += overflows[i];
381 if(product.i64[i] < overflows[i])
382 {
383 overflows[i+1] += 1;
384 }
385 }
386 // Indicate that an overflow took place. This is not useful unless the int256
387 // is known to be positive.
388 return overflows[4];
389 }
390
391 static int
392 add_int256_to_int256(int256 &sum, const int256 &addend)
393 {
394 uint128 overflows[3] = {};
395 for(int i=0; i<2; i++)
396 {
397 sum.i128[i] += addend.i128[i];
398 if( sum.i128[i] < addend.i128[i] )
399 {
400 overflows[i+1] = 1;
401 }
402 }
403 if( overflows[1] )
404 {
405 sum.i128[1] += overflows[1];
406 if( sum.i128[1] == 0 )
407 {
408 overflows[2] = 1;
409 }
410 }
411 // Indicate that an overflow took place. This is not useful unless the two
412 // values are known to be positive.
413 return (int)overflows[2];
414 }
415
416 static void
417 negate_int256(int256 &val)
418 {
419 val.i128[0] = ~val.i128[0];
420 val.i128[1] = ~val.i128[1];
421 val.i128[0] += 1;
422 if( !val.i128[0] )
423 {
424 val.i128[1] += 1;
425 }
426 }
427
428 static int
429 subtract_int256_from_int256(int256 &difference, int256 subtrahend)
430 {
431 negate_int256(subtrahend);
432 return add_int256_to_int256(difference, subtrahend);
433 }
434
435 static void
436 scale_int256_by_digits(int256 &val, int digits)
437 {
438 uint64_t pot;
439 while(digits > 17)
440 {
441 pot = (uint64_t)__gg__power_of_ten(17);
442 multiply_int256_by_int64(val, pot);
443 digits -= 17;
444 }
445 pot = (uint64_t)__gg__power_of_ten(digits);
446 multiply_int256_by_int64(val, pot);
447 }
448
449 static void
450 divide_int256_by_int64(int256 &val, uint64_t divisor)
451 {
452 // val needs to be a positive number
453 uint128 temp = 0;
454 for( int i=3; i>=0; i-- )
455 {
456 // Left shift temp 64 bits:
457 *PTRCAST(uint64_t, ((PTRCAST(uint8_t, &temp))+8))
458 = *PTRCAST(uint64_t, ((PTRCAST(uint8_t, &temp))+0));
459
460 // Put the high digit of val into the bottom of temp
461 *PTRCAST(uint64_t, ((PTRCAST(uint8_t, &temp))+0)) = val.i64[i];
462
463 // Divide that combinary by divisor to get the new digits
464 val.i64[i] = temp / divisor;
465
466 // And the new temp is that combination modulo divisor
467 temp = temp % divisor;
468 }
469 }
470
471 static int
472 squeeze_int256(int256 &val, int &rdigits)
473 {
474 int overflow = 0;
475 // It has been decreed that at this juncture the result must fit into
476 // MAX_FIXED_POINT_DIGITS. If the result does not, we have an OVERFLOW
477 // error.
478
479 int is_negative = val.data[31] & 0x80;
480 if( is_negative )
481 {
482 negate_int256(val);
483 }
484
485 // As long as there are some decimal places left, we hold our nose and
486 // right-shift a too-large value rightward by decimal digits. In other
487 // words, we truncate the fractional part to make room for the integer part:
488 while(rdigits > 0 && val.i128[1] )
489 {
490 divide_int256_by_int64(val, 10UL);
491 rdigits -= 1;
492 }
493
494 // At this point, to be useful, val has to have fewer than 128 bits:
495 if( val.i128[1] )
496 {
497 overflow = compute_error_overflow;
498 }
499 else
500 {
501 // We know that it has fewer than 128 bits. But the remaining 128 bits need
502 // to be less than 10^MAX_FIXED_POINT_DIGITS. This gets a bit nasty here,
503 // since at this writing the gcc compiler doesn't understand 128-bit
504 // constants. So, we are forced into some annoying compiler gymnastics.
505 #if MAX_FIXED_POINT_DIGITS != 37
506 #error MAX_FIXED_POINT_DIGITS needs to be 37
507 #endif
508
509 // These sixteen bytes comprise the binary value of 10^38
510 static const uint8_t C1038[] = {0x00, 0x00, 0x00, 0x00, 0x40, 0x22, 0x8a, 0x09,
511 0x7a, 0xc4, 0x86, 0x5a, 0xa8, 0x4c, 0x3b, 0x4b};
512 static const uint128 biggest = *reinterpret_cast<const uint128 *>(C1038);
513
514 // If we still have some rdigits to throw away, we can keep shrinking
515 // the value:
516
517 while(rdigits > 0 && val.i128[0] >= biggest )
518 {
519 divide_int256_by_int64(val, 10UL);
520 rdigits -= 1;
521 }
522
523 // And we have to make sure that rdigits isn't too big
524
525 while(rdigits > MAX_FIXED_POINT_DIGITS)
526 {
527 divide_int256_by_int64(val, 10UL);
528 rdigits -= 1;
529 }
530
531 if( val.i128[0] >= biggest )
532 {
533 overflow = compute_error_overflow;
534 }
535 }
536
537 if( is_negative )
538 {
539 negate_int256(val);
540 }
541
542 return overflow;
543 }
544
545 static void
546 get_int256_from_qualified_field(int256 &var,
547 int &rdigits,
548 const cblc_field_t *field,
549 size_t field_o,
550 size_t field_s)
551 {
552 var.i128[0] = (uint128)__gg__binary_value_from_qualified_field( &rdigits,
553 field,
554 field_o,
555 field_s);
556 if( var.data[15] & 0x80 )
557 {
558 // This value is negative, so extend the sign bit:
559 memset(var.data + 16, 0xFF, 16);
560 }
561 else
562 {
563 // This value is positive
564 memset(var.data + 16, 0x00, 16);
565 }
566 }
567
568 static int256 phase1_result;
569 static int phase1_rdigits;
570
571 static GCOB_FP128 phase1_result_float;
572
573 extern "C"
574 void
575 __gg__add_fixed_phase1( cbl_arith_format_t ,
576 size_t nA,
577 size_t ,
578 size_t ,
579 const cbl_round_t *,
580 int ,
581 int *compute_error
582 )
583 {
584 // Our job is to add together the nA fixed-point values in the A[] array
585
586 // The result goes into the temporary phase1_result.
587
588 cblc_field_t **A = __gg__treeplet_1f;
589 const size_t *A_o = __gg__treeplet_1o;
590 const size_t *A_s = __gg__treeplet_1s;
591
592 // Let us prime the pump with the first value of A[]
593 get_int256_from_qualified_field(phase1_result, phase1_rdigits, A[0], A_o[0], A_s[0]);
594
595 // We now go into a loop adding each of the A[] values to phase1_result:
596
597 for( size_t i=1; i<nA; i++ )
598 {
599 int temp_rdigits;
600 int256 temp = {};
601 get_int256_from_qualified_field(temp, temp_rdigits, A[i], A_o[i], A_s[i]);
602
603 // We have to scale the one with fewer rdigits to match the one with greater
604 // rdigits:
605 if( phase1_rdigits > temp_rdigits )
606 {
607 scale_int256_by_digits(temp, phase1_rdigits - temp_rdigits);
608 }
609 else if( phase1_rdigits < temp_rdigits )
610 {
611 scale_int256_by_digits(phase1_result, temp_rdigits - phase1_rdigits);
612 phase1_rdigits = temp_rdigits;
613 }
614
615 // The two numbers have the same number of rdigits. It's now safe to add
616 // them.
617 add_int256_to_int256(phase1_result, temp);
618 }
619
620 // phase1_result/phase1_rdigits now reflect the sum of all A[]
621
622 int overflow = squeeze_int256(phase1_result, phase1_rdigits);
623 if( overflow )
624 {
625 *compute_error |= compute_error_overflow;
626 }
627 }
628
629 extern "C"
630 void
631 __gg__addf1_fixed_phase2( cbl_arith_format_t ,
632 size_t ,
633 size_t ,
634 size_t ,
635 const cbl_round_t *rounded,
636 int on_error_flag,
637 int *compute_error
638 )
639 {
640 cblc_field_t **C = __gg__treeplet_3f;
641 const size_t *C_o = __gg__treeplet_3o;
642 const size_t *C_s = __gg__treeplet_3s;
643
644 // This is the assignment phase of an ADD Format 1
645
646 // We take phase1_result and accumulate it into C
647 bool on_size_error = !!(on_error_flag & ON_SIZE_ERROR);
648
649 if( C[0]->type == FldFloat)
650 {
651 // The target we need to accumulate into is a floating-point number, so we
652 // need to convert our fixed-point intermediate into floating point and
653 // proceed accordingly.
654
655 // Convert the intermediate
656 GCOB_FP128 value_a = (GCOB_FP128)phase1_result.i128[0];
657 value_a /= __gg__power_of_ten(phase1_rdigits);
658
659 // Pick up the target
660 GCOB_FP128 value_b = __gg__float128_from_qualified_field(C[0], C_o[0], C_s[0]);
661
662 value_a += value_b;
663
664 // At this point, we assign running_sum to *C.
665 *compute_error |= conditional_stash(C[0], C_o[0], C_s[0],
666 on_size_error,
667 value_a,
668 *rounded++);
669 }
670 else
671 {
672 // We have a fixed-point intermediate, and we are accumulating intoi a
673 // fixed point target.
674 int256 value_a = phase1_result;
675 int rdigits_a = phase1_rdigits;
676
677 int256 value_b = {};
678 int rdigits_b;
679
680 get_int256_from_qualified_field(value_b, rdigits_b, C[0], C_o[0], C_s[0]);
681
682 // We have to scale the one with fewer rdigits to match the one with greater
683 // rdigits:
684 if( rdigits_a > rdigits_b )
685 {
686 scale_int256_by_digits(value_b, rdigits_a - rdigits_b);
687 }
688 else if( rdigits_a < rdigits_b )
689 {
690 scale_int256_by_digits(value_a, rdigits_b - rdigits_a);
691 rdigits_a = rdigits_b;
692 }
693
694 // The two numbers have the same number of rdigits. It's now safe to add
695 // them.
696 add_int256_to_int256(value_a, value_b);
697
698 int overflow = squeeze_int256(value_a, rdigits_a);
699 if( overflow )
700 {
701 *compute_error |= compute_error_overflow;
702 }
703
704 // At this point, we assign running_sum to *C.
705 *compute_error |= conditional_stash(C[0], C_o[0], C_s[0],
706 on_size_error,
707 value_a.i128[0],
708 rdigits_a,
709 *rounded++);
710 }
711 }
712
713 extern "C"
714 void
715 __gg__fixed_phase2_assign_to_c( cbl_arith_format_t ,
716 size_t ,
717 size_t ,
718 size_t ,
719 const cbl_round_t *rounded,
720 int on_error_flag,
721 int *compute_error
722 )
723 {
724 // This is the assignment phase of an ADD Format 2
725
726 cblc_field_t **C = __gg__treeplet_3f;
727 const size_t *C_o = __gg__treeplet_3o;
728 const size_t *C_s = __gg__treeplet_3s;
729
730
731 // We take phase1_result and put it into C
732 bool on_size_error = !!(on_error_flag & ON_SIZE_ERROR);
733
734 if( C[0]->type == FldFloat)
735 {
736 // The target we need to accumulate into is a floating-point number, so we
737 // need to convert our fixed-point intermediate into floating point and
738 // proceed accordingly.
739
740 // Convert the intermediate
741 GCOB_FP128 value_a = (GCOB_FP128)phase1_result.i128[0];
742 value_a /= __gg__power_of_ten(phase1_rdigits);
743
744 *compute_error |= conditional_stash(C[0], C_o[0], C_s[0],
745 on_size_error,
746 value_a,
747 *rounded++);
748 }
749 else
750 {
751 // We have a fixed-point intermediate, and we are accumulating intoi a
752 // fixed point target.
753 int256 value_a = phase1_result;
754 int rdigits_a = phase1_rdigits;
755
756 int overflow = squeeze_int256(value_a, rdigits_a);
757 if( overflow )
758 {
759 *compute_error |= compute_error_overflow;
760 }
761
762 // At this point, we assign that value to *C.
763 *compute_error |= conditional_stash(C[0], C_o[0], C_s[0],
764 on_size_error,
765 value_a.i128[0],
766 rdigits_a,
767 *rounded++);
768 }
769 }
770
771 extern "C"
772 void
773 __gg__add_float_phase1( cbl_arith_format_t ,
774 size_t nA,
775 size_t ,
776 size_t ,
777 const cbl_round_t *,
778 int ,
779 int *compute_error
780 )
781 {
782 // Our job is to add together the nA floating-point values in the A[] array
783
784 // The result goes into the temporary phase1_result_ffloat.
785
786 cblc_field_t **A = __gg__treeplet_1f;
787 const size_t *A_o = __gg__treeplet_1o;
788 const size_t *A_s = __gg__treeplet_1s;
789
790 // Let us prime the pump with the first value of A[]
791 phase1_result_float = __gg__float128_from_qualified_field(A[0], A_o[0], A_s[0]);
792
793 // We now go into a loop adding each of the A[] values to phase1_result_flt:
794
795 for( size_t i=1; i<nA; i++ )
796 {
797 GCOB_FP128 temp = __gg__float128_from_qualified_field(A[i], A_o[i], A_s[i]);
798 phase1_result_float = addition_helper_float(phase1_result_float,
799 temp,
800 compute_error);
801 }
802 }
803
804 extern "C"
805 void
806 __gg__addf1_float_phase2( cbl_arith_format_t ,
807 size_t ,
808 size_t ,
809 size_t ,
810 const cbl_round_t *rounded,
811 int on_error_flag,
812 int *compute_error
813 )
814 {
815 cblc_field_t **C = __gg__treeplet_3f;
816 const size_t *C_o = __gg__treeplet_3o;
817 const size_t *C_s = __gg__treeplet_3s;
818
819 bool on_size_error = !!(on_error_flag & ON_SIZE_ERROR);
820 // This is the assignment phase of an ADD Format 2
821 // We take phase1_result and accumulate it into C
822
823 GCOB_FP128 temp = __gg__float128_from_qualified_field(C[0], C_o[0], C_s[0]);
824 temp = addition_helper_float(temp, phase1_result_float, compute_error);
825 *compute_error |= conditional_stash(C[0], C_o[0], C_s[0],
826 on_size_error,
827 temp,
828 *rounded++);
829 }
830
831 extern "C"
832 void
833 __gg__float_phase2_assign_to_c( cbl_arith_format_t ,
834 size_t ,
835 size_t ,
836 size_t ,
837 const cbl_round_t *rounded,
838 int on_error_flag,
839 int *compute_error
840 )
841 {
842 cblc_field_t **C = __gg__treeplet_3f;
843 const size_t *C_o = __gg__treeplet_3o;
844 const size_t *C_s = __gg__treeplet_3s;
845
846 bool on_size_error = !!(on_error_flag & ON_SIZE_ERROR);
847 // This is the assignment phase of an ADD Format 2
848 // We take phase1_result and put it into C
849
850 *compute_error |= conditional_stash(C[0], C_o[0], C_s[0],
851 on_size_error,
852 phase1_result_float,
853 *rounded++);
854 }
855
856 extern "C"
857 void
858 __gg__addf3(cbl_arith_format_t ,
859 size_t nA,
860 size_t ,
861 size_t ,
862 const cbl_round_t *rounded,
863 int on_error_flag,
864 int *compute_error
865 )
866 {
867 // This is an ADD Format 3. Each A[i] gets accumulated into each C[i]. When
868 // both are fixed, we do fixed arithmetic. When either is a FldFloat, we
869 // do floating-point arithmetic.
870 cblc_field_t **A = __gg__treeplet_1f;
871 const size_t *A_o = __gg__treeplet_1o;
872 const size_t *A_s = __gg__treeplet_1s;
873
874 cblc_field_t **C = __gg__treeplet_3f;
875 const size_t *C_o = __gg__treeplet_3o;
876 const size_t *C_s = __gg__treeplet_3s;
877
878 bool on_size_error = !!(on_error_flag & ON_SIZE_ERROR);
879
880 for(size_t i=0; i<nA; i++)
881 {
882 if( A[i]->type == FldFloat || C[i]->type == FldFloat )
883 {
884 GCOB_FP128 value_a = __gg__float128_from_qualified_field(A[i], A_o[i], A_s[i]);
885 GCOB_FP128 value_b = __gg__float128_from_qualified_field(C[i], C_o[i], C_s[i]);
886
887 value_a = addition_helper_float(value_a, value_b, compute_error);
888
889 // At this point, we assign the sum to *C.
890 *compute_error |= conditional_stash(C[i], C_o[i], C_s[i],
891 on_size_error,
892 value_a,
893 *rounded++);
894 }
895 else
896 {
897 // We have are doing fixed-point arithmetic.
898 int256 value_a;
899 int rdigits_a;
900
901 int256 value_b;
902 int rdigits_b;
903
904 get_int256_from_qualified_field(value_a, rdigits_a, A[i], A_o[i], A_s[i]);
905 get_int256_from_qualified_field(value_b, rdigits_b, C[i], C_o[i], C_s[i]);
906
907 // We have to scale the one with fewer rdigits to match the one with greater
908 // rdigits:
909 if( rdigits_a > rdigits_b )
910 {
911 scale_int256_by_digits(value_b, rdigits_a - rdigits_b);
912 }
913 else if( rdigits_a < rdigits_b )
914 {
915 scale_int256_by_digits(value_a, rdigits_b - rdigits_a);
916 rdigits_a = rdigits_b;
917 }
918
919 // The two numbers have the same number of rdigits. It's now safe to add
920 // them.
921 add_int256_to_int256(value_a, value_b);
922
923 int overflow = squeeze_int256(value_a, rdigits_a);
924 if( overflow )
925 {
926 *compute_error |= compute_error_overflow;
927 }
928
929 // At this point, we assign the sum to *C.
930 *compute_error |= conditional_stash(C[i], C_o[i], C_s[i],
931 on_size_error,
932 value_a.i128[0],
933 rdigits_a,
934 *rounded++);
935 }
936 }
937 }
938
939 extern "C"
940 void
941 __gg__subtractf1_fixed_phase2(cbl_arith_format_t ,
942 size_t ,
943 size_t ,
944 size_t ,
945 const cbl_round_t *rounded,
946 int on_error_flag,
947 int *compute_error
948 )
949 {
950 cblc_field_t **C = __gg__treeplet_3f;
951 const size_t *C_o = __gg__treeplet_3o;
952 const size_t *C_s = __gg__treeplet_3s;
953
954 // This is the assignment phase of an ADD Format 1
955
956 // We take phase1_result and subtrace it from C
957 bool on_size_error = !!(on_error_flag & ON_SIZE_ERROR);
958
959 if( C[0]->type == FldFloat)
960 {
961 // The target we need to accumulate into is a floating-point number, so we
962 // need to convert our fixed-point intermediate into floating point and
963 // proceed accordingly.
964
965 // Convert the intermediate
966 GCOB_FP128 value_a = (GCOB_FP128)phase1_result.i128[0];
967 value_a /= __gg__power_of_ten(phase1_rdigits);
968
969 // Pick up the target
970 GCOB_FP128 value_b = __gg__float128_from_qualified_field(C[0], C_o[0], C_s[0]);
971
972 value_b -= value_a;
973
974 // At this point, we assign the difference to *C.
975 *compute_error |= conditional_stash(C[0], C_o[0], C_s[0],
976 on_size_error,
977 value_b,
978 *rounded++);
979 }
980 else
981 {
982 // We have a fixed-point intermediate, and we are accumulating intoi a
983 // fixed point target.
984 int256 value_a = phase1_result;
985 int rdigits_a = phase1_rdigits;
986
987 int256 value_b = {};
988 int rdigits_b;
989
990 get_int256_from_qualified_field(value_b, rdigits_b, C[0], C_o[0], C_s[0]);
991
992 // We have to scale the one with fewer rdigits to match the one with greater
993 // rdigits:
994 if( rdigits_a > rdigits_b )
995 {
996 scale_int256_by_digits(value_b, rdigits_a - rdigits_b);
997 rdigits_b = rdigits_a;
998 }
999 else if( rdigits_a < rdigits_b )
1000 {
1001 scale_int256_by_digits(value_a, rdigits_b - rdigits_a);
1002 }
1003
1004 // The two numbers have the same number of rdigits. It's now safe to add
1005 // them.
1006 subtract_int256_from_int256(value_b, value_a);
1007
1008 int overflow = squeeze_int256(value_b, rdigits_b);
1009 if( overflow )
1010 {
1011 *compute_error |= compute_error_overflow;
1012 }
1013
1014 // At this point, we assign running_sum to *C.
1015 *compute_error |= conditional_stash(C[0], C_o[0], C_s[0],
1016 on_size_error,
1017 value_b.i128[0],
1018 rdigits_b,
1019 *rounded++);
1020 }
1021 }
1022
1023 extern "C"
1024 void
1025 __gg__subtractf2_fixed_phase1(cbl_arith_format_t ,
1026 size_t nA,
1027 size_t ,
1028 size_t ,
1029 const cbl_round_t *rounded,
1030 int on_error_flag,
1031 int *compute_error
1032 )
1033 {
1034 // This is the calculation phase of a fixed-point SUBTRACT Format 2
1035
1036 cblc_field_t **B = __gg__treeplet_2f;
1037 const size_t *B_o = __gg__treeplet_2o;
1038 const size_t *B_s = __gg__treeplet_2s;
1039
1040 // Add up all the A values
1041 __gg__add_fixed_phase1( not_expected_e ,
1042 nA,
1043 0,
1044 0,
1045 rounded,
1046 on_error_flag,
1047 compute_error);
1048
1049 // Subtract that from the B value:
1050
1051 int256 value_a = phase1_result;
1052 int rdigits_a = phase1_rdigits;
1053
1054 int256 value_b = {};
1055 int rdigits_b;
1056
1057 get_int256_from_qualified_field(value_b, rdigits_b, B[0], B_o[0], B_s[0]);
1058
1059 // We have to scale the one with fewer rdigits to match the one with greater
1060 // rdigits:
1061 if( rdigits_a > rdigits_b )
1062 {
1063 scale_int256_by_digits(value_b, rdigits_a - rdigits_b);
1064 rdigits_b = rdigits_a;
1065 }
1066 else if( rdigits_a < rdigits_b )
1067 {
1068 scale_int256_by_digits(value_a, rdigits_b - rdigits_a);
1069 }
1070
1071 // The two numbers have the same number of rdigits. It's now safe to add
1072 // them.
1073 subtract_int256_from_int256(value_b, value_a);
1074
1075 int overflow = squeeze_int256(value_b, rdigits_b);
1076 if( overflow )
1077 {
1078 *compute_error |= compute_error_overflow;
1079 }
1080 phase1_result = value_b;
1081 phase1_rdigits = rdigits_b;
1082 }
1083
1084 extern "C"
1085 void
1086 __gg__subtractf1_float_phase2(cbl_arith_format_t ,
1087 size_t ,
1088 size_t ,
1089 size_t ,
1090 const cbl_round_t *rounded,
1091 int on_error_flag,
1092 int *compute_error
1093 )
1094 {
1095 cblc_field_t **C = __gg__treeplet_3f;
1096 const size_t *C_o = __gg__treeplet_3o;
1097 const size_t *C_s = __gg__treeplet_3s;
1098
1099 bool on_size_error = !!(on_error_flag & ON_SIZE_ERROR);
1100 // This is the assignment phase of an ADD Format 2
1101 // We take phase1_result and subtract it from C
1102
1103 GCOB_FP128 temp = __gg__float128_from_qualified_field(C[0], C_o[0], C_s[0]);
1104 temp = subtraction_helper_float(temp, phase1_result_float, compute_error);
1105 *compute_error |= conditional_stash(C[0], C_o[0], C_s[0],
1106 on_size_error,
1107 temp,
1108 *rounded++);
1109 }
1110
1111 extern "C"
1112 void
1113 __gg__subtractf2_float_phase1(cbl_arith_format_t ,
1114 size_t nA,
1115 size_t ,
1116 size_t ,
1117 const cbl_round_t *rounded,
1118 int on_error_flag,
1119 int *compute_error
1120 )
1121 {
1122 // This is the calculation phase of a fixed-point SUBTRACT Format 2
1123
1124 cblc_field_t **B = __gg__treeplet_2f;
1125 const size_t *B_o = __gg__treeplet_2o;
1126 const size_t *B_s = __gg__treeplet_2s;
1127
1128 // Add up all the A values
1129 __gg__add_float_phase1( not_expected_e ,
1130 nA,
1131 0,
1132 0,
1133 rounded,
1134 on_error_flag,
1135 compute_error
1136 );
1137
1138 // Subtract that from the B value:
1139 GCOB_FP128 value_b = __gg__float128_from_qualified_field(B[0], B_o[0], B_s[0]);
1140
1141 // The two numbers have the same number of rdigits. It's now safe to add
1142 // them.
1143 phase1_result_float = subtraction_helper_float(value_b, phase1_result_float, compute_error);
1144 }
1145
1146 extern "C"
1147 void
1148 __gg__subtractf3( cbl_arith_format_t ,
1149 size_t nA,
1150 size_t ,
1151 size_t ,
1152 const cbl_round_t *rounded,
1153 int on_error_flag,
1154 int *compute_error
1155 )
1156 {
1157 // This is an ADD Format 3. Each A[i] gets accumulated into each C[i]. Each
1158 // SUBTRACTION is treated separately.
1159
1160 cblc_field_t **A = __gg__treeplet_1f;
1161 const size_t *A_o = __gg__treeplet_1o;
1162 const size_t *A_s = __gg__treeplet_1s;
1163 cblc_field_t **C = __gg__treeplet_3f;
1164 const size_t *C_o = __gg__treeplet_3o;
1165 const size_t *C_s = __gg__treeplet_3s;
1166
1167 bool on_size_error = !!(on_error_flag & ON_SIZE_ERROR);
1168
1169 for(size_t i=0; i<nA; i++)
1170 {
1171 if( A[i]->type == FldFloat || C[i]->type == FldFloat)
1172 {
1173 GCOB_FP128 value_a = __gg__float128_from_qualified_field(A[i], A_o[i], A_s[i]);
1174 GCOB_FP128 value_b = __gg__float128_from_qualified_field(C[i], C_o[i], C_s[i]);
1175
1176 value_b = subtraction_helper_float(value_b, value_a, compute_error);
1177
1178 // At this point, we assign the sum to *C.
1179 *compute_error |= conditional_stash(C[i], C_o[i], C_s[i],
1180 on_size_error,
1181 value_b,
1182 *rounded++);
1183 }
1184 else
1185 {
1186 // We are doing fixed-point subtraction.
1187 int256 value_a;
1188 int rdigits_a;
1189
1190 int256 value_b;
1191 int rdigits_b;
1192
1193 get_int256_from_qualified_field(value_a, rdigits_a, A[i], A_o[i], A_s[i]);
1194 get_int256_from_qualified_field(value_b, rdigits_b, C[i], C_o[i], C_s[i]);
1195
1196 // We have to scale the one with fewer rdigits to match the one with greater
1197 // rdigits:
1198 if( rdigits_a > rdigits_b )
1199 {
1200 scale_int256_by_digits(value_b, rdigits_a - rdigits_b);
1201 rdigits_b = rdigits_a;
1202 }
1203 else if( rdigits_a < rdigits_b )
1204 {
1205 scale_int256_by_digits(value_a, rdigits_b - rdigits_a);
1206 }
1207
1208 // The two numbers have the same number of rdigits. It's now safe to add
1209 // them.
1210 subtract_int256_from_int256(value_b, value_a);
1211
1212 int overflow = squeeze_int256(value_b, rdigits_b);
1213
1214 if( overflow )
1215 {
1216 *compute_error |= compute_error_overflow;
1217 }
1218
1219 // At this point, we assign the sum to *C.
1220 *compute_error |= conditional_stash(C[i], C_o[i], C_s[i],
1221 on_size_error,
1222 value_b.i128[0],
1223 rdigits_b,
1224 *rounded++);
1225 }
1226 }
1227 }
1228
1229 static bool multiply_intermediate_is_float;
1230 static GCOB_FP128 multiply_intermediate_float;
1231 static __int128 multiply_intermediate_int128;
1232 static int multiply_intermediate_rdigits;
1233
1234 extern "C"
1235 void
1236 __gg__multiplyf1_phase1(cbl_arith_format_t ,
1237 size_t ,
1238 size_t ,
1239 size_t ,
1240 const cbl_round_t *,
1241 int ,
1242 int *)
1243 {
1244 // We are getting just the one value, which we are converting to the necessary
1245 // intermediate form
1246
1247 cblc_field_t **A = __gg__treeplet_1f;
1248 const size_t *A_o = __gg__treeplet_1o;
1249 const size_t *A_s = __gg__treeplet_1s;
1250
1251 if( A[0]->type == FldFloat )
1252 {
1253 multiply_intermediate_is_float = true;
1254 multiply_intermediate_float = __gg__float128_from_qualified_field(A[0],
1255 A_o[0],
1256 A_s[0]);
1257 }
1258 else
1259 {
1260 multiply_intermediate_is_float = false;
1261 multiply_intermediate_int128 =
1262 __gg__binary_value_from_qualified_field(&multiply_intermediate_rdigits,
1263 A[0],
1264 A_o[0],
1265 A_s[0]);
1266 }
1267 }
1268
1269 static
1270 void multiply_int128_by_int128(int256 &ABCD,
1271 __int128 ab_value,
1272 __int128 cd_value)
1273 {
1274 int is_negative = ( (PTRCAST(uint8_t, (&ab_value)))[15]
1275 ^(PTRCAST(uint8_t, (&cd_value)))[15]) & 0x80;
1276 if( ab_value < 0 )
1277 {
1278 ab_value = -ab_value;
1279 }
1280 if( cd_value < 0 )
1281 {
1282 cd_value = -cd_value;
1283 }
1284
1285 uint128 AC00;
1286 uint128 AD0;
1287 uint128 BC0;
1288 uint128 BD;
1289
1290 // Let's extract the digits.
1291 uint64_t a = *PTRCAST(uint64_t, (PTRCAST(unsigned char, (&ab_value))+8));
1292 uint64_t b = *PTRCAST(uint64_t, (PTRCAST(unsigned char, (&ab_value))+0));
1293 uint64_t c = *PTRCAST(uint64_t, (PTRCAST(unsigned char, (&cd_value))+8));
1294 uint64_t d = *PTRCAST(uint64_t, (PTRCAST(unsigned char, (&cd_value))+0));
1295
1296 // multiply (a0 + b) * (c0 + d)
1297
1298 AC00 = (uint128)a * c;
1299 AD0 = (uint128)a * d;
1300 BC0 = (uint128)b * c;
1301 BD = (uint128)b * d;
1302
1303 // ABCD is the sum of those four pieces
1304 int256 temp;
1305
1306 ABCD.i128[1] = 0;
1307 ABCD.i128[0] = BD;
1308
1309 temp.i64[3] = 0;
1310 memcpy(&temp.i64[1], &BC0, 16);
1311 temp.i64[0] = 0;
1312 add_int256_to_int256(ABCD, temp);
1313
1314 memcpy(&temp.i64[1], &AD0, 16);
1315 add_int256_to_int256(ABCD, temp);
1316
1317 temp.i64[1] = 0;
1318 memcpy(&temp.i64[2], &AC00, 16);
1319 add_int256_to_int256(ABCD, temp);
1320
1321 // ABCD is now a 256-bit integer with rdigits decimal places
1322 if(is_negative)
1323 {
1324 negate_int256(ABCD);
1325 }
1326 }
1327
1328
1329 extern "C"
1330 void
1331 __gg__multiplyf1_phase2(cbl_arith_format_t ,
1332 size_t ,
1333 size_t ,
1334 size_t ,
1335 const cbl_round_t *rounded,
1336 int on_error_flag,
1337 int *compute_error
1338 )
1339 {
1340 cblc_field_t **C = __gg__treeplet_3f;
1341 const size_t *C_o = __gg__treeplet_3o;
1342 const size_t *C_s = __gg__treeplet_3s;
1343
1344 bool on_size_error = !!(on_error_flag & ON_SIZE_ERROR);
1345 int error_this_time=0;
1346
1347 GCOB_FP128 a_value;
1348 GCOB_FP128 b_value;
1349
1350 if( multiply_intermediate_is_float )
1351 {
1352 a_value = multiply_intermediate_float;
1353 if( C[0]->type == FldFloat )
1354 {
1355 b_value = __gg__float128_from_qualified_field(C[0], C_o[0], C_s[0]);
1356 goto float_float;
1357 }
1358 else
1359 {
1360 // float times fixed
1361 b_value = __gg__float128_from_qualified_field(C[0], C_o[0], C_s[0]);
1362 goto float_float;
1363 }
1364 }
1365 else
1366 {
1367 if( C[0]->type == FldFloat )
1368 {
1369 // gixed * float
1370 a_value = (GCOB_FP128) multiply_intermediate_int128;
1371 if( multiply_intermediate_rdigits )
1372 {
1373 a_value /= (GCOB_FP128)__gg__power_of_ten(multiply_intermediate_rdigits);
1374 }
1375 b_value = __gg__float128_from_qualified_field(C[0], C_o[0], C_s[0]);
1376 goto float_float;
1377 }
1378 else
1379 {
1380 // fixed times fixed
1381
1382 // We have two 128-bit numbers. Call them AB and CD, where A, B, C, D are
1383 // 64-bit "digits". We need to multiply them to create a 256-bit result
1384
1385 int cd_rdigits;
1386 __int128 ab_value = multiply_intermediate_int128;
1387 __int128 cd_value = __gg__binary_value_from_qualified_field(&cd_rdigits, C[0], C_o[0], C_s[0]);
1388
1389 int256 ABCD;
1390 int rdigits = multiply_intermediate_rdigits + cd_rdigits;
1391
1392 multiply_int128_by_int128(ABCD, ab_value, cd_value);
1393
1394 int overflow = squeeze_int256(ABCD, rdigits);
1395 if( overflow )
1396 {
1397 *compute_error |= compute_error_overflow;
1398 }
1399 // At this point, we assign running_sum to *C.
1400 *compute_error |= conditional_stash(C[0], C_o[0], C_s[0],
1401 on_size_error,
1402 ABCD.i128[0],
1403 rdigits,
1404 *rounded++);
1405
1406 goto done;
1407 }
1408 }
1409 float_float:
1410
1411 a_value = multiply_helper_float(a_value, b_value, &error_this_time);
1412
1413 if( error_this_time && on_size_error)
1414 {
1415 *compute_error |= error_this_time;
1416 }
1417 else
1418 {
1419 *compute_error |= conditional_stash(C[0], C_o[0], C_s[0],
1420 on_size_error,
1421 a_value,
1422 *rounded);
1423 }
1424 done:
1425 return;
1426 }
1427
1428 extern "C"
1429 void
1430 __gg__multiplyf2( cbl_arith_format_t ,
1431 size_t ,
1432 size_t ,
1433 size_t nC,
1434 const cbl_round_t *rounded,
1435 int on_error_flag,
1436 int *compute_error
1437 )
1438 {
1439 cblc_field_t **A = __gg__treeplet_1f;
1440 const size_t *A_o = __gg__treeplet_1o;
1441 const size_t *A_s = __gg__treeplet_1s;
1442 cblc_field_t **B = __gg__treeplet_2f;
1443 const size_t *B_o = __gg__treeplet_2o;
1444 const size_t *B_s = __gg__treeplet_2s;
1445 cblc_field_t **C = __gg__treeplet_3f;
1446 const size_t *C_o = __gg__treeplet_3o;
1447 const size_t *C_s = __gg__treeplet_3s;
1448
1449 bool on_size_error = !!(on_error_flag & ON_SIZE_ERROR);
1450
1451 bool got_float = false;
1452 GCOB_FP128 product_float;
1453 int256 product_fix;
1454 int product_fix_digits;
1455
1456 if( A[0]->type == FldFloat || B[0]->type == FldFloat )
1457 {
1458 GCOB_FP128 a_value = __gg__float128_from_qualified_field(A[0], A_o[0], A_s[0]);
1459 GCOB_FP128 b_value = __gg__float128_from_qualified_field(B[0], B_o[0], B_s[0]);
1460 product_float = multiply_helper_float(a_value, b_value, compute_error);
1461 got_float = true;
1462 }
1463 else
1464 {
1465 int a_rdigits;
1466 int b_rdigits;
1467 __int128 a_value = __gg__binary_value_from_qualified_field(&a_rdigits, A[0], A_o[0], A_s[0]);
1468 __int128 b_value = __gg__binary_value_from_qualified_field(&b_rdigits, B[0], B_o[0], B_s[0]);
1469 product_fix_digits = a_rdigits + b_rdigits;
1470 multiply_int128_by_int128(product_fix, a_value, b_value);
1471 int overflow = squeeze_int256(product_fix, product_fix_digits);
1472 if( overflow )
1473 {
1474 *compute_error |= compute_error_overflow;
1475 }
1476 }
1477
1478 for(size_t i=0; i<nC; i++)
1479 {
1480 if( got_float )
1481 {
1482 *compute_error |= conditional_stash(C[i], C_o[i], C_s[i],
1483 on_size_error,
1484 product_float,
1485 *rounded++);
1486 }
1487 else
1488 {
1489 *compute_error |= conditional_stash(C[i], C_o[i], C_s[i],
1490 on_size_error,
1491 product_fix.i128[0],
1492 product_fix_digits,
1493 *rounded++);
1494 }
1495 }
1496 }
1497
1498 static void
1499 shift_in_place128(uint8_t *buf, int size, int bits)
1500 {
1501 // Assume that size in bytes is some multiple of sixteen. That is, the
1502 // buffer we are shifting is made up of either two 128-bit bit values (for
1503 // an int256) or three 128-bit values (an int384)
1504
1505 // "bits" is a value from zero to 127
1506
1507 if( !bits )
1508 {
1509 return;
1510 }
1511
1512 size_t places = size/16; // This is either two or three
1513
1514 uint128 temp;
1515 uint128 overflow = 0;
1516
1517 uint128 *as128 = PTRCAST(uint128, buf);
1518
1519 for( size_t i=0; i<places; i++ )
1520 {
1521 temp = as128[i] >> (128 - bits);
1522 as128[i] <<= bits;
1523 as128[i] += overflow;
1524 overflow = temp;
1525 }
1526 }
1527
1528 #pragma GCC diagnostic push
1529 #pragma GCC diagnostic ignored "-Wunused-function"
1530
1531 static char *
1532 int256_as_decimal(int256 val)
1533 {
1534 char ach[120];
1535 memset(ach, 0, sizeof(ach));
1536 strcpy(ach, "0");
1537 int index = 0;
1538 while(val.i128[0] || val.i128[1])
1539 {
1540 int256 before;
1541 int256 after;
1542 before = val;
1543 after = val;
1544 divide_int256_by_int64(after, 10);
1545 multiply_int256_by_int64(after, 10);
1546 uint64_t digit = before.i64[0] - after.i64[0];
1547 ach[index++] = digit + '0';
1548 divide_int256_by_int64(val, 10);
1549 }
1550 if( !index )
1551 {
1552 index = 1;
1553 }
1554 index -= 1;
1555 int r = 0;
1556 static char retval[120];
1557 while(index >= 0)
1558 {
1559 retval[r++] = ach[index--];
1560 }
1561 retval[r++] = '\0';
1562 return retval;
1563 }
1564 #pragma GCC diagnostic pop
1565
1566
1567 static void
1568 divide_int128_by_int128(int256 &quotient,
1569 int &quotient_rdigits,
1570 __int128 dividend,
1571 int dividend_rdigits,
1572 __int128 divisor,
1573 int divisor_rdigits,
1574 int *compute_error)
1575 {
1576 if( divisor == 0 )
1577 {
1578 *compute_error |= compute_error_divide_by_zero;
1579 memset(&quotient, 0, sizeof(quotient));
1580 memcpy(&quotient, &dividend, 8);
1581 quotient_rdigits = dividend_rdigits;
1582 return;
1583 }
1584
1585 bool is_negative = false;
1586 if(dividend < 0)
1587 {
1588 is_negative = !is_negative;
1589 dividend = -dividend;
1590 }
1591 if(divisor < 0)
1592 {
1593 is_negative = !is_negative;
1594 divisor = -divisor;
1595 }
1596
1597 // We are going to be referencing the 64-bit pices of the 128-bit divisor:
1598 uint64_t *divisor64 = PTRCAST(uint64_t, &divisor);
1599
1600 quotient.i128[1] = 0;
1601 quotient.i128[0] = dividend;
1602
1603 // In order to get 0.3333333.... from 1 / 3, we are going to scale up the
1604 // numerator so that it has 37 rdigits:
1605
1606 int scale = MAX_FIXED_POINT_DIGITS;
1607 scale_int256_by_digits(quotient, scale);
1608 quotient_rdigits = scale + dividend_rdigits - divisor_rdigits;
1609
1610 // Now, let's see if we can do a simple divide-by-single-place calculation:
1611
1612 if( divisor64[1] == 0 )
1613 {
1614 // Yes! The divisor fits into 64 bits:
1615 divide_int256_by_int64(quotient, (uint64_t)divisor);
1616 }
1617 else
1618 {
1619 // We have to do long division, and that means Knuth's Algorithm D. Let's
1620 // review where we are.
1621 //
1622 // We are using 64-bit places to divide one 128-bit number by another. We
1623 // know that both are positive. So, we are dividing
1624 //
1625 // AB by CD, where we know C is non-zero.
1626 //
1627 // Because we want fractional digits to the right, we multiplied by 10**37,
1628 // which is smaller than 2**64, so we have one additional place:
1629 //
1630 // ABx by CD
1631 //
1632 // Algorithm D requires that we left-shift ABX and CD by enough bits to make
1633 // turn on high-order by of CD. This will be a value between 1 and 63
1634 // shifts, resulting in
1635 //
1636 // ABxy by CD.
1637 //
1638 // We can know that the quotient will have at most two places. We can see
1639 // this in the decimal analogy. The worst case scenario is dividing
1640 //
1641 // 99 by 10
1642 //
1643 // We multiply the top by 10 to give us one fractional decimal place in the
1644 // result:
1645 //
1646 // 990 by 10
1647 //
1648 // To satisfy Algorithm D's requirement that C be >= b/2, we multiply both
1649 // dividend and divisor by 5:
1650 //
1651 // 4950 by 50
1652 //
1653 // And then we do the work that gives us the two-place answer of 99.
1654 //
1655 //
1656 // Here is our four-place 256-bit "numerator"
1657 int256 numerator;
1658
1659 // Copy the three-place ABx value into the numerator area
1660 memcpy(&numerator, &quotient, sizeof(quotient));
1661
1662 // Calculate how many bits we need to shift CD to make the high-order bit
1663 // turn on:
1664
1665 int bits_to_shift = 0;
1666 int i=15;
1667 while( (PTRCAST(uint8_t, &divisor))[i] == 0 )
1668 {
1669 i -= 1;
1670 bits_to_shift += 8;
1671 } uint8_t tail = ( PTRCAST(uint8_t, &divisor) )[i];
1672 while( !(tail & 0x80) )
1673 {
1674 bits_to_shift += 1;
1675 tail <<= 1;
1676 }
1677
1678 // Shift both the numerator and the divisor that number of bits
1679
1680 shift_in_place128( PTRCAST(uint8_t, &numerator), sizeof(numerator), bits_to_shift);
1681 shift_in_place128( PTRCAST(uint8_t, &divisor), sizeof(divisor), bits_to_shift);
1682
1683 // We are now ready to do the guess-multiply-subtract loop. We know that
1684 // the result will have two places, so we know we are going to go through
1685 // that loop two times. We will build the quotient from the high-order
1686 // place down:
1687
1688 // Let's prime the pump by loading remnant with the A value of ABxyz
1689 int q_place = 1;
1690
1691 memset(&quotient, 0, sizeof(quotient));
1692
1693 while( q_place >= 0 )
1694 {
1695 // We develop our guess for a quotient by dividing the top two places of
1696 // the numerator area by C
1697 uint128 temp;
1698 uint64_t *temp64 = PTRCAST(uint64_t, &temp);
1699
1700 temp64[1] = numerator.i64[q_place+2];
1701 temp64[0] = numerator.i64[q_place+1];
1702
1703 quotient.i64[q_place] = temp / divisor64[1];
1704
1705 // Now we multiply our 64-bit guess by the 128-bit CD to get the
1706 // three-place value we are going to subtract from the numerator area.
1707
1708 uint64_t subber[3];
1709 subber[2] = 0;
1710
1711 // Start with the bottom 128 bits of the "subber"
1712 *PTRCAST(uint128, subber) = (uint128) divisor64[0] * quotient.i64[q_place];
1713
1714 // Get the next 128 bits of subber
1715 temp = (uint128) divisor64[1] * quotient.i64[q_place];
1716
1717 // Add the top of the first product to the bottom of the second:
1718 subber[1] += temp64[0];
1719
1720 // See if there was an overflow:
1721 if( subber[1] < temp64[0] )
1722 {
1723 // There was an overflow
1724 subber[2] = 1;
1725 }
1726 // And now put the top of the second product into place:
1727 subber[2] += temp64[1];
1728
1729 // "subber" is now the three-place product of the two-place divisor times
1730 // the one-place guess
1731
1732 // We now subtract the three-place subber from the appropriate place of
1733 // the numerator:
1734
1735 uint64_t borrow = 0;
1736 for(size_t j=0; j<3; j++)
1737 {
1738 if( numerator.i64[q_place + j] == 0 && borrow )
1739 {
1740 // We are subtracting from zero and we have a borrow. Leave the
1741 // borrow on and just do the subtraction:
1742 numerator.i64[q_place + j] -= subber[j];
1743 }
1744 else
1745 {
1746 uint64_t stash = numerator.i64[q_place + j];
1747 numerator.i64[q_place + j] -= borrow;
1748 numerator.i64[q_place + j] -= subber[j];
1749 if( numerator.i64[q_place + j] > stash )
1750 {
1751 // After subtracting, the value got bigger, which means we have
1752 // to borrow from the next value to the left
1753 borrow = 1;
1754 }
1755 else
1756 {
1757 borrow = 0;
1758 }
1759 }
1760 }
1761 // The three-place subber has been removed from the numerator.
1762
1763 // Now Algorithm D comes back into play. Knuth has proved that the guess
1764 // is usually correct, but sometimes can be one too large, which means
1765 // that the numerator area goes negative. On rare occasions, the guess can
1766 // be two too large. So, we have to make sure that the numerator area is
1767 // actually positive by adding subber back in.
1768
1769 while( numerator.i64[q_place+2] & 0x8000000000000000UL )
1770 {
1771 // We need to add subber back into the numerator area
1772 uint64_t carry = 0;
1773 for(size_t ii=0; ii<3; ii++)
1774 {
1775 if( numerator.i64[q_place + ii] == 0xFFFFFFFFFFFFFFFFUL && carry )
1776 {
1777 // We are at the top and have a carry. Just leave the carry on
1778 // and do the addition:
1779 numerator.i64[q_place + ii] += subber[ii];
1780 }
1781 else
1782 {
1783 // We are not at the top.
1784 uint64_t stash = numerator.i64[q_place + ii];
1785 numerator.i64[q_place + ii] += carry;
1786 numerator.i64[q_place + ii] += subber[ii];
1787 if( numerator.i64[q_place + ii] < stash )
1788 {
1789 // The addition caused the result to get smaller, meaning that
1790 // we wrapped around:
1791 carry = 1;
1792 }
1793 else
1794 {
1795 carry = 0;
1796 }
1797 }
1798 }
1799 }
1800 q_place -= 1;
1801 }
1802 }
1803 if( is_negative )
1804 {
1805 negate_int256(quotient);
1806 }
1807 }
1808
1809 extern "C"
1810 void
1811 __gg__dividef1_phase2(cbl_arith_format_t ,
1812 size_t ,
1813 size_t ,
1814 size_t ,
1815 const cbl_round_t *rounded,
1816 int on_error_flag,
1817 int *compute_error
1818 )
1819 {
1820 cblc_field_t **C = __gg__treeplet_3f;
1821 const size_t *C_o = __gg__treeplet_3o;
1822 const size_t *C_s = __gg__treeplet_3s;
1823
1824 bool on_size_error = !!(on_error_flag & ON_SIZE_ERROR);
1825 int error_this_time=0;
1826
1827 GCOB_FP128 a_value;
1828 GCOB_FP128 b_value;
1829
1830 if( multiply_intermediate_is_float )
1831 {
1832 a_value = multiply_intermediate_float;
1833 if( C[0]->type == FldFloat )
1834 {
1835 b_value = __gg__float128_from_qualified_field(C[0], C_o[0], C_s[0]);
1836 goto float_float;
1837 }
1838 else
1839 {
1840 // float times fixed
1841 b_value = __gg__float128_from_qualified_field(C[0], C_o[0], C_s[0]);
1842 goto float_float;
1843 }
1844 }
1845 else
1846 {
1847 if( C[0]->type == FldFloat )
1848 {
1849 // gixed * float
1850 a_value = (GCOB_FP128) multiply_intermediate_int128;
1851 if( multiply_intermediate_rdigits )
1852 {
1853 a_value /= (GCOB_FP128)__gg__power_of_ten(multiply_intermediate_rdigits);
1854 }
1855 b_value = __gg__float128_from_qualified_field(C[0], C_o[0], C_s[0]);
1856 goto float_float;
1857 }
1858 else
1859 {
1860 // fixed times fixed
1861
1862 // We have two 128-bit numbers. Call them AB and CD, where A, B, C, D are
1863 // 64-bit "digits". We need to multiply them to create a 256-bit result
1864
1865 int dividend_rdigits;
1866 __int128 dividend = __gg__binary_value_from_qualified_field(&dividend_rdigits, C[0], C_o[0], C_s[0]);
1867
1868 int quotient_rdigits;
1869 int256 quotient;
1870
1871 divide_int128_by_int128(quotient,
1872 quotient_rdigits,
1873 dividend,
1874 dividend_rdigits,
1875 multiply_intermediate_int128,
1876 multiply_intermediate_rdigits,
1877 compute_error);
1878
1879 int overflow = squeeze_int256(quotient, quotient_rdigits);
1880 if( overflow )
1881 {
1882 *compute_error |= compute_error_overflow;
1883 }
1884 // At this point, we assign the quotient to *C.
1885 *compute_error |= conditional_stash(C[0], C_o[0], C_s[0],
1886 on_size_error,
1887 quotient.i128[0],
1888 quotient_rdigits,
1889 *rounded++);
1890
1891 goto done;
1892 }
1893 }
1894 float_float:
1895
1896 b_value = divide_helper_float(b_value, a_value, &error_this_time);
1897
1898 *compute_error |= error_this_time;
1899
1900 if( error_this_time && on_size_error)
1901 {
1902 }
1903 else
1904 {
1905 *compute_error |= conditional_stash(C[0], C_o[0], C_s[0],
1906 on_size_error,
1907 b_value,
1908 *rounded);
1909 }
1910 done:
1911 return;
1912 }
1913
1914 extern "C"
1915 void
1916 __gg__dividef23(cbl_arith_format_t ,
1917 size_t ,
1918 size_t ,
1919 size_t nC,
1920 const cbl_round_t *rounded,
1921 int on_error_flag,
1922 int *compute_error
1923 )
1924 {
1925 cblc_field_t **A = __gg__treeplet_1f;
1926 const size_t *A_o = __gg__treeplet_1o;
1927 const size_t *A_s = __gg__treeplet_1s;
1928 cblc_field_t **B = __gg__treeplet_2f;
1929 const size_t *B_o = __gg__treeplet_2o;
1930 const size_t *B_s = __gg__treeplet_2s;
1931 cblc_field_t **C = __gg__treeplet_3f;
1932 const size_t *C_o = __gg__treeplet_3o;
1933 const size_t *C_s = __gg__treeplet_3s;
1934
1935 bool on_size_error = !!(on_error_flag & ON_SIZE_ERROR);
1936 int error_this_time=0;
1937
1938 if( A[0]->type == FldFloat || B[0]->type == FldFloat )
1939 {
1940 GCOB_FP128 a_value;
1941 GCOB_FP128 b_value;
1942 GCOB_FP128 c_value;
1943 a_value = __gg__float128_from_qualified_field(A[0], A_o[0], A_s[0]);
1944 b_value = __gg__float128_from_qualified_field(B[0], B_o[0], B_s[0]);
1945 c_value = divide_helper_float(a_value, b_value, &error_this_time);
1946
1947 *compute_error |= error_this_time;
1948 if( !error_this_time )
1949 {
1950 for(size_t i=0; i<nC; i++)
1951 {
1952 *compute_error |= conditional_stash(C[i], C_o[i], C_s[i],
1953 on_size_error,
1954 c_value,
1955 *rounded++);
1956 }
1957 }
1958 }
1959 else
1960 {
1961 // fixed divided by fixed
1962 int dividend_rdigits;
1963 __int128 dividend = __gg__binary_value_from_qualified_field(&dividend_rdigits, A[0], A_o[0], A_s[0]);
1964
1965 int divisor_rdigits;
1966 __int128 divisor = __gg__binary_value_from_qualified_field(&divisor_rdigits, B[0], B_o[0], B_s[0]);
1967
1968 int quotient_rdigits;
1969 int256 quotient;
1970
1971 divide_int128_by_int128(quotient,
1972 quotient_rdigits,
1973 dividend,
1974 dividend_rdigits,
1975 divisor,
1976 divisor_rdigits,
1977 compute_error);
1978
1979 *compute_error |= squeeze_int256(quotient, quotient_rdigits);
1980 if( !*compute_error )
1981 {
1982 // At this point, we assign the quotient to *C.
1983 for(size_t i=0; i<nC; i++)
1984 {
1985 *compute_error |= conditional_stash(C[i], C_o[i], C_s[i],
1986 on_size_error,
1987 quotient.i128[0],
1988 quotient_rdigits,
1989 *rounded++);
1990 }
1991 }
1992 }
1993 }
1994
1995 extern "C"
1996 void
1997 __gg__dividef45(cbl_arith_format_t ,
1998 size_t ,
1999 size_t ,
2000 size_t ,
2001 cbl_round_t *rounded_p,
2002 int on_error_flag,
2003 int *compute_error
2004 )
2005 {
2006 cblc_field_t **A = __gg__treeplet_1f; // Numerator
2007 const size_t *A_o = __gg__treeplet_1o;
2008 const size_t *A_s = __gg__treeplet_1s;
2009 cblc_field_t **B = __gg__treeplet_2f; // Denominator
2010 const size_t *B_o = __gg__treeplet_2o;
2011 const size_t *B_s = __gg__treeplet_2s;
2012 cblc_field_t **C = __gg__treeplet_3f; // Has remainder, then quotient
2013 const size_t *C_o = __gg__treeplet_3o;
2014 const size_t *C_s = __gg__treeplet_3s;
2015
2016 bool on_size_error = !!(on_error_flag & ON_SIZE_ERROR);
2017 int error_this_time=0;
2018
2019 if( A[0]->type == FldFloat || B[0]->type == FldFloat )
2020 {
2021 GCOB_FP128 a_value;
2022 GCOB_FP128 b_value;
2023 GCOB_FP128 c_value;
2024 a_value = __gg__float128_from_qualified_field(A[0], A_o[0], A_s[0]);
2025 b_value = __gg__float128_from_qualified_field(B[0], B_o[0], B_s[0]);
2026 c_value = divide_helper_float(a_value, b_value, &error_this_time);
2027
2028 *compute_error |= error_this_time;
2029
2030 if( !error_this_time )
2031 {
2032 *compute_error |= conditional_stash(C[1], C_o[1], C_s[1],
2033 on_size_error,
2034 c_value,
2035 *rounded_p++);
2036 // This is floating point, and there is a remainder, and we don't know
2037 // what that means. Set the remainder to zero
2038 if( !*compute_error )
2039 {
2040 c_value = 0;
2041 *compute_error |= conditional_stash(C[0], C_o[0], C_s[0],
2042 on_size_error,
2043 c_value,
2044 *rounded_p++);
2045 }
2046 }
2047 }
2048 else
2049 {
2050 // fixed divided by fixed
2051 int dividend_rdigits;
2052 __int128 dividend = __gg__binary_value_from_qualified_field(&dividend_rdigits, A[0], A_o[0], A_s[0]);
2053
2054 int divisor_rdigits;
2055 __int128 divisor = __gg__binary_value_from_qualified_field(&divisor_rdigits, B[0], B_o[0], B_s[0]);
2056
2057 int quotient_rdigits;
2058 int256 quotient;
2059
2060 divide_int128_by_int128(quotient,
2061 quotient_rdigits,
2062 dividend,
2063 dividend_rdigits,
2064 divisor,
2065 divisor_rdigits,
2066 compute_error);
2067
2068 *compute_error |= squeeze_int256(quotient, quotient_rdigits);
2069 if( !*compute_error )
2070 {
2071 // We are going to need the unrounded quotient to calculate the remainder
2072 __int128 unrounded_quotient = 0;
2073 int unrounded_quotient_digits;
2074 rounded_p += 1;// Skip the rounded value for the remainder
2075 cbl_round_t rounded = *rounded_p;
2076 switch(rounded)
2077 {
2078 case truncation_e:
2079 {
2080 *compute_error |= conditional_stash(C[1], C_o[1], C_s[1],
2081 on_size_error,
2082 quotient.i128[0],
2083 quotient_rdigits,
2084 *rounded_p++);
2085 unrounded_quotient = __gg__binary_value_from_qualified_field(
2086 &unrounded_quotient_digits,
2087 C[1], C_o[1], C_s[1]);
2088 break;
2089 }
2090 default:
2091 {
2092 conditional_stash(C[1], C_o[1], C_s[1],
2093 false,
2094 quotient.i128[0],
2095 quotient_rdigits,
2096 truncation_e);
2097 unrounded_quotient = __gg__binary_value_from_qualified_field(
2098 &unrounded_quotient_digits,
2099 C[1], C_o[1], C_s[1]);
2100 // At this point, we assign the rounded quotient to *C.
2101 *compute_error |= conditional_stash(C[1], C_o[1], C_s[1],
2102 on_size_error,
2103 quotient.i128[0],
2104 quotient_rdigits,
2105 *rounded_p++);
2106 break;
2107 }
2108 }
2109 if( !*compute_error )
2110 {
2111 // We need to calculate the remainder
2112
2113 // Remainders in COBOL are seriously weird. The NIST suite
2114 // has an example where 174 is divided by 16. The quotient
2115 // is a 999.9, and the remainder is a 9999
2116
2117 // So, here goes: 174 by 16 is 10.875. The unrounded
2118 // assignment to Q is thus 10.8
2119 // You then multiply 10.8 by 16, giving 172.8
2120 // That gets subtracted from 174, giving 1.2
2121 // That gets assigned to the 9999 remainder, which is
2122 // thus 1
2123
2124 // Any mathematician would walk away, slowly, shaking their head.
2125
2126 // We need to multiply the unrounded quotient by the divisor.
2127 int256 temp;
2128 int temp_rdigits;
2129 // Step 1: Multiply the unrounded quotient by the divisor
2130 multiply_int128_by_int128(temp, unrounded_quotient, divisor);
2131 temp_rdigits = unrounded_quotient_digits + divisor_rdigits;
2132
2133 int256 odividend = {};
2134 memcpy(&odividend, &dividend, sizeof(dividend));
2135
2136 // We need to line up the rdigits so that we can subtract temp from
2137 // odividend:
2138
2139 if( temp_rdigits < dividend_rdigits )
2140 {
2141 scale_int256_by_digits(temp, dividend_rdigits-temp_rdigits);
2142 temp_rdigits = dividend_rdigits;
2143 }
2144 else if(temp_rdigits > dividend_rdigits)
2145 {
2146 scale_int256_by_digits(odividend, temp_rdigits-dividend_rdigits);
2147 }
2148
2149 subtract_int256_from_int256(odividend, temp);
2150
2151 *compute_error |= squeeze_int256(odividend, temp_rdigits);
2152
2153 if( !*compute_error )
2154 {
2155 *compute_error |= conditional_stash(C[0], C_o[0], C_s[0],
2156 on_size_error,
2157 odividend.i128[0],
2158 temp_rdigits,
2159 truncation_e);
2160 }
2161 }
2162 }
2163 }
2164 }
2165