-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpdecimal.c
7935 lines (6978 loc) · 185 KB
/
mpdecimal.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
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2008-2010 Stefan Krah. All rights reserved.
*
* 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 "mpdecimal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include "basearith.h"
#include "bits.h"
#include "convolute.h"
#include "crt.h"
#include "errno.h"
#include "memory.h"
#include "typearith.h"
#include "umodarith.h"
#include "mptest.h"
#include "mptypes.h"
#ifdef PPRO
#if defined(_MSC_VER)
#include <float.h>
#pragma fenv_access(on)
#elif !defined(__OpenBSD__) && !defined(__NetBSD__)
/* C99 */
#include <fenv.h>
#pragma STDC FENV_ACCESS ON
#endif
#endif
#if defined(__x86_64__) && defined(__GLIBC__) && !defined(__INTEL_COMPILER)
#define USE_80BIT_LONG_DOUBLE
#endif
#if defined(_MSC_VER)
#define ALWAYS_INLINE __forceinline
#elif defined(LEGACY_COMPILER)
#define ALWAYS_INLINE
#undef inline
#define inline
#else
#ifdef TEST_COVERAGE
#define ALWAYS_INLINE
#else
#define ALWAYS_INLINE inline __attribute__ ((always_inline))
#endif
#endif
#define MPD_NEWTONDIV_CUTOFF 1024L
#define MPD_NEW_STATIC(name, flags, exp, digits, len) \
mpd_uint_t name##_data[MPD_MINALLOC_MAX]; \
mpd_t name = {flags|MPD_STATIC|MPD_STATIC_DATA, exp, digits, \
len, MPD_MINALLOC_MAX, name##_data}
#define MPD_NEW_CONST(name, flags, exp, digits, len, alloc, initval) \
mpd_uint_t name##_data[alloc] = {initval}; \
mpd_t name = {flags|MPD_STATIC|MPD_CONST_DATA, exp, digits, \
len, alloc, name##_data}
#define MPD_NEW_SHARED(name, a) \
mpd_t name = {(a->flags&~MPD_DATAFLAGS)|MPD_STATIC|MPD_SHARED_DATA, \
a->exp, a->digits, a->len, a->alloc, a->data}
static mpd_uint_t data_one[1] = {1};
static mpd_uint_t data_zero[1] = {0};
static const mpd_t one = {MPD_STATIC|MPD_CONST_DATA, 0, 1, 1, 1, data_one};
static const mpd_t minus_one = {MPD_NEG|MPD_STATIC|MPD_CONST_DATA, 0, 1, 1, 1,
data_one};
static const mpd_t zero = {MPD_STATIC|MPD_CONST_DATA, 0, 1, 1, 1, data_zero};
static inline void _mpd_check_exp(mpd_t *dec, const mpd_context_t *ctx,
uint32_t *status);
static void _settriple(mpd_t *result, uint8_t sign, mpd_uint_t a,
mpd_ssize_t exp);
static inline mpd_ssize_t _mpd_real_size(mpd_uint_t *data, mpd_ssize_t size);
static void _mpd_qadd(mpd_t *result, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status);
static inline void _mpd_qmul(mpd_t *result, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status);
static void _mpd_qbarrett_divmod(mpd_t *q, mpd_t *r, const mpd_t *a,
const mpd_t *b, uint32_t *status);
static inline void _mpd_qpow_uint(mpd_t *result, mpd_t *base, mpd_uint_t exp,
uint8_t resultsign, const mpd_context_t *ctx, uint32_t *status);
mpd_uint_t mpd_qsshiftr(mpd_t *result, const mpd_t *a, mpd_ssize_t n);
/******************************************************************************/
/* Performance critical inline functions */
/******************************************************************************/
#ifdef CONFIG_64
/* Digits in a word, primarily useful for the most significant word. */
ALWAYS_INLINE int
mpd_word_digits(mpd_uint_t word)
{
if (word < mpd_pow10[9]) {
if (word < mpd_pow10[4]) {
if (word < mpd_pow10[2]) {
return (word < mpd_pow10[1]) ? 1 : 2;
}
return (word < mpd_pow10[3]) ? 3 : 4;
}
if (word < mpd_pow10[6]) {
return (word < mpd_pow10[5]) ? 5 : 6;
}
if (word < mpd_pow10[8]) {
return (word < mpd_pow10[7]) ? 7 : 8;
}
return 9;
}
if (word < mpd_pow10[14]) {
if (word < mpd_pow10[11]) {
return (word < mpd_pow10[10]) ? 10 : 11;
}
if (word < mpd_pow10[13]) {
return (word < mpd_pow10[12]) ? 12 : 13;
}
return 14;
}
if (word < mpd_pow10[17]) {
if (word < mpd_pow10[16]) {
return (word < mpd_pow10[15]) ? 15 : 16;
}
return 17;
}
return (word < mpd_pow10[18]) ? 18 : 19;
}
#else
ALWAYS_INLINE int
mpd_word_digits(mpd_uint_t word)
{
if (word < mpd_pow10[4]) {
if (word < mpd_pow10[2]) {
return (word < mpd_pow10[1]) ? 1 : 2;
}
return (word < mpd_pow10[3]) ? 3 : 4;
}
if (word < mpd_pow10[6]) {
return (word < mpd_pow10[5]) ? 5 : 6;
}
if (word < mpd_pow10[8]) {
return (word < mpd_pow10[7]) ? 7 : 8;
}
return (word < mpd_pow10[9]) ? 9 : 10;
}
#endif
/* Adjusted exponent */
ALWAYS_INLINE mpd_ssize_t
mpd_adjexp(const mpd_t *dec)
{
return (dec->exp + dec->digits) - 1;
}
/* Etiny */
ALWAYS_INLINE mpd_ssize_t
mpd_etiny(const mpd_context_t *ctx)
{
return ctx->emin - (ctx->prec - 1);
}
/* Etop: used for folding down in IEEE clamping */
ALWAYS_INLINE mpd_ssize_t
mpd_etop(const mpd_context_t *ctx)
{
return ctx->emax - (ctx->prec - 1);
}
/* Most significant word */
ALWAYS_INLINE mpd_uint_t
mpd_msword(const mpd_t *dec)
{
assert(dec->len > 0);
return dec->data[dec->len-1];
}
/* Most significant digit of a word */
inline mpd_uint_t
mpd_msd(mpd_uint_t word)
{
int n;
n = mpd_word_digits(word);
return word / mpd_pow10[n-1];
}
/* Least significant digit of a word */
ALWAYS_INLINE mpd_uint_t
mpd_lsd(mpd_uint_t word)
{
return word % 10;
}
/* Coefficient size needed to store 'digits' */
ALWAYS_INLINE mpd_ssize_t
mpd_digits_to_size(mpd_ssize_t digits)
{
mpd_ssize_t q, r;
_mpd_idiv_word(&q, &r, digits, MPD_RDIGITS);
return (r == 0) ? q : q+1;
}
/* Number of digits in the exponent. Not defined for MPD_SSIZE_MIN. */
inline int
mpd_exp_digits(mpd_ssize_t exp)
{
exp = (exp < 0) ? -exp : exp;
return mpd_word_digits(exp);
}
/* Canonical */
ALWAYS_INLINE int
mpd_iscanonical(const mpd_t *dec UNUSED)
{
return 1;
}
/* Finite */
ALWAYS_INLINE int
mpd_isfinite(const mpd_t *dec)
{
return !(dec->flags & MPD_SPECIAL);
}
/* Infinite */
ALWAYS_INLINE int
mpd_isinfinite(const mpd_t *dec)
{
return dec->flags & MPD_INF;
}
/* NaN */
ALWAYS_INLINE int
mpd_isnan(const mpd_t *dec)
{
return dec->flags & (MPD_NAN|MPD_SNAN);
}
/* Negative */
ALWAYS_INLINE int
mpd_isnegative(const mpd_t *dec)
{
return dec->flags & MPD_NEG;
}
/* Positive */
ALWAYS_INLINE int
mpd_ispositive(const mpd_t *dec)
{
return !(dec->flags & MPD_NEG);
}
/* qNaN */
ALWAYS_INLINE int
mpd_isqnan(const mpd_t *dec)
{
return dec->flags & MPD_NAN;
}
/* Signed */
ALWAYS_INLINE int
mpd_issigned(const mpd_t *dec)
{
return dec->flags & MPD_NEG;
}
/* sNaN */
ALWAYS_INLINE int
mpd_issnan(const mpd_t *dec)
{
return dec->flags & MPD_SNAN;
}
/* Special */
ALWAYS_INLINE int
mpd_isspecial(const mpd_t *dec)
{
return dec->flags & MPD_SPECIAL;
}
/* Zero */
ALWAYS_INLINE int
mpd_iszero(const mpd_t *dec)
{
return !mpd_isspecial(dec) && mpd_msword(dec) == 0;
}
/* Test for zero when specials have been ruled out already */
ALWAYS_INLINE int
mpd_iszerocoeff(const mpd_t *dec)
{
return mpd_msword(dec) == 0;
}
/* Normal */
inline int
mpd_isnormal(const mpd_t *dec, const mpd_context_t *ctx)
{
if (mpd_isspecial(dec)) return 0;
if (mpd_iszerocoeff(dec)) return 0;
return mpd_adjexp(dec) >= ctx->emin;
}
/* Subnormal */
inline int
mpd_issubnormal(const mpd_t *dec, const mpd_context_t *ctx)
{
if (mpd_isspecial(dec)) return 0;
if (mpd_iszerocoeff(dec)) return 0;
return mpd_adjexp(dec) < ctx->emin;
}
/* Odd word */
ALWAYS_INLINE int
mpd_isoddword(mpd_uint_t word)
{
return word & 1;
}
/* Odd coefficient */
ALWAYS_INLINE int
mpd_isoddcoeff(const mpd_t *dec)
{
return mpd_isoddword(dec->data[0]);
}
/* 0 if dec is positive, 1 if dec is negative */
ALWAYS_INLINE uint8_t
mpd_sign(const mpd_t *dec)
{
return dec->flags & MPD_NEG;
}
/* 1 if dec is positive, -1 if dec is negative */
ALWAYS_INLINE int
mpd_arith_sign(const mpd_t *dec)
{
return 1 - 2 * mpd_isnegative(dec);
}
/* Radix */
ALWAYS_INLINE long
mpd_radix(void)
{
return 10;
}
/* Dynamic decimal */
ALWAYS_INLINE int
mpd_isdynamic(mpd_t *dec)
{
return !(dec->flags & MPD_STATIC);
}
/* Static decimal */
ALWAYS_INLINE int
mpd_isstatic(mpd_t *dec)
{
return dec->flags & MPD_STATIC;
}
/* Data of decimal is dynamic */
ALWAYS_INLINE int
mpd_isdynamic_data(mpd_t *dec)
{
return !(dec->flags & MPD_DATAFLAGS);
}
/* Data of decimal is static */
ALWAYS_INLINE int
mpd_isstatic_data(mpd_t *dec)
{
return dec->flags & MPD_STATIC_DATA;
}
/* Data of decimal is shared */
ALWAYS_INLINE int
mpd_isshared_data(mpd_t *dec)
{
return dec->flags & MPD_SHARED_DATA;
}
/* Data of decimal is const */
ALWAYS_INLINE int
mpd_isconst_data(mpd_t *dec)
{
return dec->flags & MPD_CONST_DATA;
}
/******************************************************************************/
/* Inline memory handling */
/******************************************************************************/
/* Fill destination with zeros */
ALWAYS_INLINE void
mpd_uint_zero(mpd_uint_t *dest, mpd_size_t len)
{
mpd_size_t i;
for (i = 0; i < len; i++) {
dest[i] = 0;
}
}
/* Free a decimal */
ALWAYS_INLINE void
mpd_del(mpd_t *dec)
{
if (mpd_isdynamic_data(dec)) {
mpd_free(dec->data);
}
if (mpd_isdynamic(dec)) {
mpd_free(dec);
}
}
/*
* Update the memory size for the coefficient. Existing data up to size is
* left untouched.
*
* Error handling: When relloc fails, result->data will still be a valid pointer
* to the old memory area of size result->len. If the requested size is less than
* result->len, we can continue normally, so we treat the failure as a soft error.
* If the requested size is greater than the old area, MPD_Malloc_error is
* set and the result will be a NaN.
*/
ALWAYS_INLINE int
mpd_qresize(mpd_t *result, mpd_ssize_t size, uint32_t *status)
{
assert(!mpd_isconst_data(result)); /* illegal operation for a const */
assert(!mpd_isshared_data(result)); /* illegal operation for a shared */
if (mpd_isstatic_data(result)) {
if (size > result->alloc) {
return mpd_switch_to_dyn(result, size, status);
}
}
else if (size != result->alloc && size >= MPD_MINALLOC) {
return mpd_realloc_dyn(result, size, status);
}
return 1;
}
/* Same as mpd_qresize, but the complete coefficient (including the old
* memory area!) is initialized to zero. */
ALWAYS_INLINE int
mpd_qresize_zero(mpd_t *result, mpd_ssize_t size, uint32_t *status)
{
assert(!mpd_isconst_data(result)); /* illegal operation for a const */
assert(!mpd_isshared_data(result)); /* illegal operation for a shared */
if (mpd_isstatic_data(result)) {
if (size > result->alloc) {
return mpd_switch_to_dyn_zero(result, size, status);
}
}
else if (size != result->alloc && size >= MPD_MINALLOC) {
if (!mpd_realloc_dyn(result, size, status)) {
return 0;
}
}
mpd_uint_zero(result->data, size);
return 1;
}
/*
* Reduce memory size for the coefficient to MPD_MINALLOC. In theory,
* realloc may fail even when reducing the memory size. But in that case
* the old memory area is always big enough, so checking for MPD_Malloc_error
* is not imperative.
*/
ALWAYS_INLINE void
mpd_minalloc(mpd_t *result)
{
assert(!mpd_isconst_data(result)); /* illegal operation for a const */
assert(!mpd_isshared_data(result)); /* illegal operation for a shared */
if (!mpd_isstatic_data(result) && result->alloc > MPD_MINALLOC) {
uint8_t err = 0;
result->data = mpd_realloc(result->data, MPD_MINALLOC,
sizeof *result->data, &err);
if (!err) {
result->alloc = MPD_MINALLOC;
}
}
}
int
mpd_resize(mpd_t *result, mpd_ssize_t size, mpd_context_t *ctx)
{
uint32_t status = 0;
if (!mpd_qresize(result, size, &status)) {
mpd_addstatus_raise(ctx, status);
return 0;
}
return 1;
}
int
mpd_resize_zero(mpd_t *result, mpd_ssize_t size, mpd_context_t *ctx)
{
uint32_t status = 0;
if (!mpd_qresize_zero(result, size, &status)) {
mpd_addstatus_raise(ctx, status);
return 0;
}
return 1;
}
/******************************************************************************/
/* Set attributes of a decimal */
/******************************************************************************/
/* Set digits. result->len is assumed to be correct. */
inline void
mpd_setdigits(mpd_t *result)
{
mpd_ssize_t wdigits = mpd_word_digits(mpd_msword(result));
result->digits = wdigits + (result->len-1) * MPD_RDIGITS;
}
/* Set sign */
ALWAYS_INLINE void
mpd_set_sign(mpd_t *result, uint8_t sign)
{
result->flags &= ~MPD_NEG;
result->flags |= sign;
}
/* Copy sign from another decimal */
ALWAYS_INLINE void
mpd_signcpy(mpd_t *result, mpd_t *a)
{
uint8_t sign = a->flags&MPD_NEG;
result->flags &= ~MPD_NEG;
result->flags |= sign;
}
/* Set infinity */
ALWAYS_INLINE void
mpd_set_infinity(mpd_t *result)
{
result->flags &= ~MPD_SPECIAL;
result->flags |= MPD_INF;
}
/* Set qNaN */
ALWAYS_INLINE void
mpd_set_qnan(mpd_t *result)
{
result->flags &= ~MPD_SPECIAL;
result->flags |= MPD_NAN;
}
/* Set sNaN */
ALWAYS_INLINE void
mpd_set_snan(mpd_t *result)
{
result->flags &= ~MPD_SPECIAL;
result->flags |= MPD_SNAN;
}
/* Set to negative */
ALWAYS_INLINE void
mpd_set_negative(mpd_t *result)
{
result->flags |= MPD_NEG;
}
/* Set to positive */
ALWAYS_INLINE void
mpd_set_positive(mpd_t *result)
{
result->flags &= ~MPD_NEG;
}
/* Set to dynamic */
ALWAYS_INLINE void
mpd_set_dynamic(mpd_t *result)
{
result->flags &= ~MPD_STATIC;
}
/* Set to static */
ALWAYS_INLINE void
mpd_set_static(mpd_t *result)
{
result->flags |= MPD_STATIC;
}
/* Set data to dynamic */
ALWAYS_INLINE void
mpd_set_dynamic_data(mpd_t *result)
{
result->flags &= ~MPD_DATAFLAGS;
}
/* Set data to static */
ALWAYS_INLINE void
mpd_set_static_data(mpd_t *result)
{
result->flags &= ~MPD_DATAFLAGS;
result->flags |= MPD_STATIC_DATA;
}
/* Set data to shared */
ALWAYS_INLINE void
mpd_set_shared_data(mpd_t *result)
{
result->flags &= ~MPD_DATAFLAGS;
result->flags |= MPD_SHARED_DATA;
}
/* Set data to const */
ALWAYS_INLINE void
mpd_set_const_data(mpd_t *result)
{
result->flags &= ~MPD_DATAFLAGS;
result->flags |= MPD_CONST_DATA;
}
/* Clear flags, preserving memory attributes. */
ALWAYS_INLINE void
mpd_clear_flags(mpd_t *result)
{
result->flags &= (MPD_STATIC|MPD_DATAFLAGS);
}
/* Set flags, preserving memory attributes. */
ALWAYS_INLINE void
mpd_set_flags(mpd_t *result, uint8_t flags)
{
result->flags &= (MPD_STATIC|MPD_DATAFLAGS);
result->flags |= flags;
}
/* Copy flags, preserving memory attributes of result. */
ALWAYS_INLINE void
mpd_copy_flags(mpd_t *result, const mpd_t *a)
{
uint8_t aflags = a->flags;
result->flags &= (MPD_STATIC|MPD_DATAFLAGS);
result->flags |= (aflags & ~(MPD_STATIC|MPD_DATAFLAGS));
}
/* Make a work context */
static inline void
mpd_workcontext(mpd_context_t *workctx, const mpd_context_t *ctx)
{
workctx->prec = ctx->prec;
workctx->emax = ctx->emax;
workctx->emin = ctx->emin;
workctx->round = ctx->round;
workctx->traps = 0;
workctx->status= 0;
workctx->newtrap= 0;
workctx->clamp = ctx->clamp;
workctx->allcr = ctx->allcr;
}
/******************************************************************************/
/* Getting and setting parts of decimals */
/******************************************************************************/
/* Flip the sign of a decimal */
static inline void
_mpd_negate(mpd_t *dec)
{
dec->flags ^= MPD_NEG;
}
/* Set coefficient to zero */
void
mpd_zerocoeff(mpd_t *result)
{
mpd_minalloc(result);
result->digits = 1;
result->len = 1;
result->data[0] = 0;
}
/* Set the coefficient to all nines. */
void
mpd_qmaxcoeff(mpd_t *result, const mpd_context_t *ctx, uint32_t *status)
{
mpd_ssize_t len, r;
_mpd_idiv_word(&len, &r, ctx->prec, MPD_RDIGITS);
len = (r == 0) ? len : len+1;
if (!mpd_qresize(result, len, status)) {
return;
}
result->len = len;
result->digits = ctx->prec;
--len;
if (r > 0) {
result->data[len--] = mpd_pow10[r]-1;
}
for (; len >= 0; --len) {
result->data[len] = MPD_RADIX-1;
}
}
/*
* Cut off the most significant digits so that the rest fits in ctx->prec.
* Cannot fail.
*/
static void
_mpd_cap(mpd_t *result, const mpd_context_t *ctx)
{
uint32_t dummy;
mpd_ssize_t len, r;
if (result->len > 0 && result->digits > ctx->prec) {
_mpd_idiv_word(&len, &r, ctx->prec, MPD_RDIGITS);
len = (r == 0) ? len : len+1;
if (r != 0) {
result->data[len-1] %= mpd_pow10[r];
}
len = _mpd_real_size(result->data, len);
/* resize to fewer words cannot fail */
mpd_qresize(result, len, &dummy);
result->len = len;
mpd_setdigits(result);
}
if (mpd_iszero(result)) {
_settriple(result, mpd_sign(result), 0, result->exp);
}
}
/*
* Cut off the most significant digits of a NaN payload so that the rest
* fits in ctx->prec - ctx->clamp. Cannot fail.
*/
static void
_mpd_fix_nan(mpd_t *result, const mpd_context_t *ctx)
{
uint32_t dummy;
mpd_ssize_t prec;
mpd_ssize_t len, r;
prec = ctx->prec - ctx->clamp;
if (result->len > 0 && result->digits > prec) {
if (prec == 0) {
mpd_minalloc(result);
result->len = result->digits = 0;
}
else {
_mpd_idiv_word(&len, &r, prec, MPD_RDIGITS);
len = (r == 0) ? len : len+1;
if (r != 0) {
result->data[len-1] %= mpd_pow10[r];
}
len = _mpd_real_size(result->data, len);
/* resize to fewer words cannot fail */
mpd_qresize(result, len, &dummy);
result->len = len;
mpd_setdigits(result);
if (mpd_iszerocoeff(result)) {
/* NaN0 is not a valid representation */
result->len = result->digits = 0;
}
}
}
}
/*
* Get n most significant digits from a decimal, where 0 < n <= MPD_UINT_DIGITS.
* Assumes MPD_UINT_DIGITS == MPD_RDIGITS+1, which is true for 32 and 64 bit
* machines.
*
* The result of the operation will be in lo. If the operation is impossible,
* hi will be nonzero. This is used to indicate an error.
*/
static inline void
_mpd_get_msdigits(mpd_uint_t *hi, mpd_uint_t *lo, const mpd_t *dec,
unsigned int n)
{
mpd_uint_t r, tmp;
assert(0 < n && n <= MPD_RDIGITS+1);
_mpd_div_word(&tmp, &r, dec->digits, MPD_RDIGITS);
r = (r == 0) ? MPD_RDIGITS : r; /* digits in the most significant word */
*hi = 0;
*lo = dec->data[dec->len-1];
if (n <= r) {
*lo /= mpd_pow10[r-n];
}
else if (dec->len > 1) {
/* at this point 1 <= r < n <= MPD_RDIGITS+1 */
_mpd_mul_words(hi, lo, *lo, mpd_pow10[n-r]);
tmp = dec->data[dec->len-2] / mpd_pow10[MPD_RDIGITS-(n-r)];
*lo = *lo + tmp;
if (*lo < tmp) (*hi)++;
}
}
/******************************************************************************/
/* Gathering information about a decimal */
/******************************************************************************/
/* The real size of the coefficient without leading zero words. */
static inline mpd_ssize_t
_mpd_real_size(mpd_uint_t *data, mpd_ssize_t size)
{
while (size > 1 && data[size-1] == 0) {
size--;
}
return size;
}
/* Return number of trailing zeros. No errors are possible. */
mpd_ssize_t
mpd_trail_zeros(const mpd_t *dec)
{
mpd_uint_t word;
mpd_ssize_t i, tz = 0;
for (i=0; i < dec->len; ++i) {
if (dec->data[i] != 0) {
word = dec->data[i];
tz = i * MPD_RDIGITS;
while (word % 10 == 0) {
word /= 10;
tz++;
}
break;
}
}
return tz;
}
/* Integer: Undefined for specials */
static int
_mpd_isint(const mpd_t *dec)
{
mpd_ssize_t tz;
if (mpd_iszerocoeff(dec)) {
return 1;
}
tz = mpd_trail_zeros(dec);
return (dec->exp + tz >= 0);
}
/* Integer */
int
mpd_isinteger(const mpd_t *dec)
{
if (mpd_isspecial(dec)) {
return 0;
}
return _mpd_isint(dec);
}
/* Word is a power of 10 */
static int
mpd_word_ispow10(mpd_uint_t word)
{
int n;
n = mpd_word_digits(word);
if (word == mpd_pow10[n-1]) {
return 1;
}
return 0;
}
/* Coefficient is a power of 10 */
static int
mpd_coeff_ispow10(const mpd_t *dec)
{
if (mpd_word_ispow10(mpd_msword(dec))) {
if (_mpd_isallzero(dec->data, dec->len-1)) {
return 1;
}
}
return 0;
}
/* All digits of a word are nines */
static int
mpd_word_isallnine(mpd_uint_t word)
{
int n;
n = mpd_word_digits(word);
if (word == mpd_pow10[n]-1) {
return 1;
}
return 0;
}
/* All digits of the coefficient are nines */
static int
mpd_coeff_isallnine(const mpd_t *dec)
{
if (mpd_word_isallnine(mpd_msword(dec))) {
if (_mpd_isallnine(dec->data, dec->len-1)) {
return 1;
}
}
return 0;
}
/* Odd decimal: Undefined for non-integers! */
int
mpd_isodd(const mpd_t *dec)
{
mpd_uint_t q, r;
assert(mpd_isinteger(dec));
if (mpd_iszerocoeff(dec)) return 0;
if (dec->exp < 0) {
_mpd_div_word(&q, &r, -dec->exp, MPD_RDIGITS);
q = dec->data[q] / mpd_pow10[r];
return mpd_isoddword(q);
}
return dec->exp == 0 && mpd_isoddword(dec->data[0]);
}
/* Even: Undefined for non-integers! */
int
mpd_iseven(const mpd_t *dec)
{
return !mpd_isodd(dec);
}
/******************************************************************************/
/* Getting and setting decimals */
/******************************************************************************/
/* Internal function: Set a static decimal from a triple, no error checking. */
static void
_ssettriple(mpd_t *result, uint8_t sign, mpd_uint_t a, mpd_ssize_t exp)
{
mpd_set_flags(result, sign);
result->exp = exp;
_mpd_div_word(&result->data[1], &result->data[0], a, MPD_RADIX);
result->len = (result->data[1] == 0) ? 1 : 2;