-
Notifications
You must be signed in to change notification settings - Fork 0
/
alup.c
2367 lines (1863 loc) · 46.2 KB
/
alup.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
#include <alu/alup.h>
size_t alu_man_dig( size_t bits )
{
size_t max_exp = bits - LDBL_MANT_DIG;
return EITHER
(
bits < 6
, 3
, EITHER
(
bits < bitsof(float)
, 5
, EITHER
(
bits < bitsof(double)
, FLT_MANT_DIG
, EITHER
(
bits < bitsof(long double)
, DBL_MANT_DIG
, bits - max_exp
)
)
)
);
}
void alup__print
(
char *file
, uint_t line
, const char *func
, char *pfx
, alup_t const * const _PTR
, bool print_info
, bool print_value
)
{
alup_t _EXP, _MAN;
alup_init_exponent( _PTR, _EXP );
alup_init_mantissa( _PTR, _MAN );
if ( print_info )
{
alu__printf
(
"%s: part = %p, bits = %zu, upto = %zu, from = %zu"
", signed = %c, mdig = %zu"
", _EXP.bits = %zu, _MAN.bits = %zu"
, file
, line
, func
, pfx
, _PTR->data
, _PTR->bits
, _PTR->upto
, _PTR->from
, '0' + alup___signed( _PTR )
, _PTR->mdig
, _EXP.bits
, _MAN.bits
);
}
if ( print_value )
{
alub_t p = alup_until_bit( _PTR );
size_t bit = 0, bits = _PTR->bits, sep_at = 4;
char sec_sep = ' ', dig_sep = '\'';
fprintf( aluout, "%s:%u: %s() %s = ", file, line, func, pfx );
#define alup_print_bit() \
do \
{ \
alub_dec( &p ); \
fputc( '0' + !!(*(p.ptr) & p.mask), aluout ); \
++bit; \
--bits; \
} \
while (0)
if ( alup___signed( _PTR ) )
{
alup_print_bit();
(void)fputc( sec_sep, aluout );
}
if ( alup_floating( _PTR ) )
{
ssize_t exp = alup_get_exponent( _PTR );
char str[bitsof(size_t)] = {0};
if ( exp )
exp -= alup_get_exponent_bias( _PTR );
sprintf( str, "%+05zd%c", (ssize_t)exp, sec_sep );
fputs( str, aluout );
while ( p.bit > _EXP.from )
{
if ( bit >= sep_at )
{
//fputc( dig_sep, aluout );
bit = 0;
}
alup_print_bit();
}
(void)fputc( sec_sep, aluout );
}
while ( p.bit > _PTR->from )
{
if ( bit >= sep_at )
{
fputc( dig_sep, aluout );
bit = 0;
}
alup_print_bit();
}
fputc( '\n', aluout );
#undef alup_print_bit
}
}
bool_t alup_below0( alup_t const * const _SRC )
{
if ( _SRC && _SRC->data && alup___signed(_SRC) )
{
alub_t s = alup_final_bit( _SRC );
return !!(*(s.ptr) & s.mask);
}
return false;
}
alub_t alup_first_bit_with_val( alup_t const * const _SRC, bool val )
{
alub_t s = {0};
if ( _SRC && _SRC->data )
{
/* Ensure is only 1 or 0 contained if maps to integer */
val = !!val;
s = alup_first_bit( _SRC );
while ( s.bit < _SRC->last )
{
if ( !!( *(s.ptr) & s.mask ) == val )
break;
alub_inc(&s);
}
}
return s;
}
alub_t alup_final_bit_with_val( alup_t const * const _SRC, bool val )
{
alub_t s = {0};
if ( _SRC && _SRC->data )
{
/* Ensure is only 1 or 0 contained if maps to integer */
val = !!val;
s = alup_final_bit( _SRC );
while ( s.bit > _SRC->from )
{
if ( !!( *(s.ptr) & s.mask ) == val )
break;
alub_dec(&s);
}
}
return s;
}
size_t alup_get_exponent( alup_t const * const _SRC )
{
int_t ret;
size_t dst = 0;
alup_t _EXP, _DST;
alup_init_unsigned( _DST, &dst, bitsof(size_t) );
alup_init_exponent( _SRC, _EXP );
ret = alup_mov_int2int( &_DST, &_EXP );
if ( ret )
alu_error( ret );
return dst;
}
int_t alup_set_exponent( alup_t const * const _DST, size_t src )
{
alup_t _EXP, _SRC;
alup_init_unsigned( _SRC, &src, bitsof(size_t) );
alup_init_exponent( _DST, _EXP );
return alup_mov_int2int( &_EXP, &_SRC );
}
size_t alup_get_exponent_bias( alup_t const * const _SRC )
{
alup_t _EXP;
size_t bias = UNIC_SIZE_C(~0);
alup_init_exponent( _SRC, _EXP );
bias <<= _EXP.bits - 1;
return ~bias;
}
int_t alup_set( alup_t const * const _DST, bool fillwith )
{
if ( _DST->data )
{
alub_t n;
size_t upto = alup_until_pos( _DST );
/* Force to either 1 or 0 */
fillwith = !!fillwith;
for ( n = alub( _DST->data, _DST->from ); n.bit < upto; alub_inc(&n) )
{
*(n.ptr) &= ~(n.mask);
*(n.ptr) |= IFTRUE( fillwith, n.mask );
}
return 0;
}
return alu_err_null_ptr("_DST->data");
}
int_t alup_set_intmax( alup_t const * const _DST )
{
int_t ret = alup_set( _DST, 1 );
if ( ret == 0 )
{
if ( alup___signed( _DST ) )
{
alup_set_sign( _DST, 0 );
}
return 0;
}
return ret;
}
int_t alup_set_intmin( alup_t const * const _DST )
{
int_t ret = alup_set( _DST, 0 );
if ( ret == 0 )
{
if ( alup___signed( _DST ) )
{
alup_set_sign( _DST, 1 );
}
return 0;
}
return ret;
}
int_t alup_set_fltinf( alup_t const * const _DST, bool_t neg )
{
alup_t _MAN;
size_t bias = alup_get_exponent_bias( _DST );
alup_set_sign( _DST, neg );
alup_set_exponent( _DST, (bias << 1) | 1 );
alup_init_mantissa( _DST, _MAN );
return alup_set( &_MAN, 0 );
}
int_t alup_set_max( alup_t const * const _DST )
{
if ( alup_floating( _DST ) )
{
return alup_set_fltinf( _DST, 0 );
}
return alup_set_intmax( _DST );
}
int_t alup_set_min( alup_t const * const _DST )
{
if ( alup_floating( _DST ) )
{
return alup_set_fltinf( _DST, 1 );
}
return alup_set_intmin( _DST );
}
int_t alup_set_inf( alup_t const * const _DST, bool_t neg )
{
if ( alup_floating( _DST ) )
{
return alup_set_fltinf( _DST, neg );
}
/* Integers do not support infinity, min/max is closest we can get */
if ( neg )
{
return alup_set_intmin( _DST );
}
return alup_set_intmax( _DST );
}
int_t alup_mov_int2int( alup_t const * const _DST, alup_t const * const _SRC )
{
if ( _DST->data )
{
if ( _SRC->data )
{
alub_t
d = alub( _DST->data, _DST->from )
, s = alub( _SRC->data, _SRC->from );
size_t upto = _DST->from + LOWEST( _DST->bits, _SRC->bits );
bool neg = alup_below0( _SRC );
for ( ; d.bit < upto; alub_inc(&d), alub_inc(&s) )
{
*(d.ptr) &= ~(d.mask);
*(d.ptr) |= IFTRUE( *(s.ptr) & s.mask, d.mask );
}
upto = _DST->from + _DST->bits;
for ( ; d.bit < upto; alub_inc(&d) )
{
*(d.ptr) &= ~(d.mask);
*(d.ptr) |= IFTRUE( neg, d.mask );
}
return IFTRUE( d.bit < (_DST->from + _SRC->bits), ERANGE );
}
return alup_set( _DST, 0 );
}
return alu_err_null_ptr("_DST->data");
}
int_t alup_mov_int2flt( alup_t const * const _DST, alup_t const * const _SRC )
{
alub_t s;
ssize_t exp, bias;
bool neg = alup_below0( _SRC ), is0, val;
if ( neg )
{
alup_neg( _SRC );
}
s = alup_final_bit_with_val( _SRC, 1 );
val = !!(*(s.ptr) & s.mask);
exp = s.bit - _SRC->from;
is0 = !(neg || exp || val != neg);
if ( is0 )
{
if ( neg )
{
alup_neg( _SRC );
}
(void)alup_set( _DST, 0 );
return ENODATA;
}
bias = alup_get_exponent_bias( _DST );
if ( exp >= bias )
{
if ( neg )
{
alup_neg( _SRC );
}
(void)alup_set_inf( _DST, neg );
return ERANGE;
}
else
{
alup_t _MAN, _REF;
ssize_t upto = LOWEST( exp + 1, (ssize_t)(_DST->mdig) );
alup_init_mantissa( _DST, _MAN );
alup_set( &_MAN, 0 );
_REF = *_SRC;
_REF.bits = upto - 1;
_REF.from = IFTRUE( s.bit, s.bit - _REF.bits );
_MAN.from += ( _DST->mdig - upto );
(void)alup_mov_int2int( &_MAN, &_REF );
if ( neg )
{
alup_neg( _SRC );
}
/* Round to nearest */
if ( _SRC->from < _REF.from )
{
s = alub( _REF.data, _REF.from - 1 );
if ( *(s.ptr) & s.mask )
alup_inc( &_MAN );
}
(void)alup_set_exponent( _DST, exp + bias );
alup_set_sign( _DST, neg );
return 0;
}
}
int_t alup_mov_flt2int( alup_t const * const _DST, alup_t const * const _SRC )
{
int_t ret;
alup_t _MAN;
alub_t s;
ssize_t exp = alup_get_exponent( _SRC )
, bias = alup_get_exponent_bias(_SRC)
, inf = (bias << 1) | 1;
bool Inf, neg;
alup_init_mantissa( _SRC, _MAN );
neg = alup_below0( _SRC );
Inf = (exp == inf);
if ( !exp )
{
return alup_set( _DST, 0 );
}
exp -= bias;
if ( Inf || exp >= (ssize_t)(_DST->bits) )
{
s = alup_final_bit_with_val( &_MAN, true );
/* NaN cannot be recorded by an integer, use 0 instead */
if ( Inf && *(s.ptr) & s.mask )
{
alup_set( _DST, 0 );
return 0;
}
return alup_set_inf( _DST, neg );
}
if ( exp < 0 )
{
return alup_set( _DST, 0 );
}
_MAN.from += _MAN.bits - LOWEST( exp, (ssize_t)(_MAN.bits) );
(void)alup_mov_int2int( _DST, &_MAN );
ret = alup__shl_int2int( _DST, exp );
/* Put in the assumed bit */
alub_set( _DST->data, _DST->from + exp, 1 );
if ( neg )
{
alup_neg( _DST );
}
return ret;
}
int_t alup_mov_flt2flt( alup_t const * const _DST, alup_t const * const _SRC )
{
alup_t _DMAN, _SMAN;
ssize_t exp, dbias, sbias, dinf, sinf;
bool neg = alup_below0( _SRC );
//#define INC_MANTISSA
#ifdef INC_MANTISSA
bool inc = false;
#endif
int_t ret = 0;
exp = alup_get_exponent( _SRC );
if ( !exp )
{
alup_set( _DST, 0 );
/* Must not convert -0 to +0 */
alup_set_sign( _DST, neg );
return 0;
}
dbias = alup_get_exponent_bias( _DST );
sbias = alup_get_exponent_bias( _SRC );
dinf = (dbias << 1) | 1;
sinf = (sbias << 1) | 1;
alup_init_mantissa( _DST, _DMAN );
alup_init_mantissa( _SRC, _SMAN );
/* Clear this before we fiddle with boundaries */
alup_set( &_DMAN, 0 );
/* Make sure we refer to upper bits of both mantissa's if they're of
* different lengths */
if ( _DST->mdig > _SRC->mdig )
{
alup_set( &_DMAN, 0 );
_DMAN.from += _DST->mdig - _SRC->mdig;
_DMAN.bits = _DMAN.upto - _DMAN.from;
}
else if ( _SRC->mdig > _DST->mdig )
{
alup_t _MAN = _SMAN;
#ifdef INC_MANTISSA
alub_t final;
#endif
_SMAN.bits = _DST->mdig;
_SMAN.from += _SRC->mdig - _DST->mdig;
_SMAN.bits = _SMAN.upto - _SMAN.from;
_MAN.upto = _SMAN.from;
_MAN.last = _SMAN.from - 1;
_MAN.bits = _MAN.upto - _MAN.from;
#ifdef INC_MANTISSA
final = alup_final_bit_with_val( &_MAN, 1 );
inc = !!(*(final.ptr) & final.mask);
#endif
ret = ERANGE;
}
exp = EITHER( exp == sinf, dinf, exp - sbias );
ret = EITHER( exp < -dbias, ERANGE, EITHER( exp > dbias, ERANGE, ret ) );
exp = EITHER( exp < -dbias, 0, EITHER( exp > dbias, dinf, exp + dbias ) );
alup_set_sign( _DST, neg );
(void)alup_set_exponent( _DST, exp );
/* Apparently NaN is treated as inifinity when transfering from large to
* small, mimic that behaviour for consistancy */
if ( exp < dinf )
{
(void)alup_mov_int2int( &_DMAN, &_SMAN );
#ifdef INC_MANTISSA
if ( inc )
(void)alup__inc_int( &_DMAN );
#undef INC_MANTISSA
#endif
}
return ret;
}
int_t alup_mov( alup_t const * const _DST, alup_t const * const _SRC )
{
if ( alup_floating( _DST ) )
{
if ( alup_floating( _SRC ) )
return alup_mov_flt2flt( _DST, _SRC );
return alup_mov_int2flt( _DST, _SRC );
}
else
{
if ( alup_floating( _SRC ) )
return alup_mov_flt2int( _DST, _SRC );
return alup_mov_int2int( _DST, _SRC );
}
}
int_t alup_not( alup_t const * const _DST )
{
if ( _DST->data )
{
alub_t d, e;
size_t mask, mask_init, mask_last;
d = alup_first_bit( _DST );
e = alup_final_bit( _DST );
mask = 0;
mask_init = mask_last = ~mask;
mask_init <<= d.pos;
mask_last <<= e.pos + 1;
mask_last = ~mask_last;
mask = EITHER( e.seg > d.seg, mask_last, mask_last & mask_init );
mask = EITHER( mask, mask, mask_init );
*(d.ptr) ^= IFTRUE( e.seg > d.seg, mask_init );
for ( ++(d.seg); d.seg < e.seg; ++(d.seg) )
{
++(d.ptr);
d.ptr[d.seg] = ~(d.ptr[d.seg]);
}
*(e.ptr) ^= mask;
return 0;
}
return alu_err_null_ptr("_DST->data");
}
int_t alup_cmp_int2int( alup_t const * const _NUM, alup_t const * const _VAL )
{
int a = !!(_NUM && _NUM->data && _NUM->upto > _NUM->from)
, b = !!(_VAL && _VAL->data && _VAL->upto > _VAL->from)
, r = a - b;
if ( r )
return r;
a = alup_below0( _NUM );
b = alup_below0( _VAL );
r = a - b;
if ( r )
return -r;
else
{
alub_t n = alup_until_bit( _NUM ), v = alup_until_bit( _VAL );
size_t nlen = n.bit - _NUM->from, vlen = v.bit - _VAL->from;
while ( nlen > vlen )
{
--nlen;
alub_dec(&n);
a = !!(*(n.ptr) & n.mask);
r = a - b;
if ( r )
return r;
}
while ( vlen > nlen )
{
--vlen;
alub_dec(&v);
b = !!(*(v.ptr) & v.mask);
r = a - b;
if ( r )
return r;
}
while ( nlen )
{
--nlen;
alub_dec(&n);
alub_dec(&v);
a = !!(*(n.ptr) & n.mask);
b = !!(*(v.ptr) & v.mask);
r = a - b;
if ( r )
return r;
}
}
return 0;
}
int_t alup_cmp( alup_t const * const _NUM, alup_t const * const _VAL )
{
alup_t _VMAN;
ssize_t nexp, vexp, vbias;
bool_t nneg = alup_below0( _NUM ), vneg = alup_below0( _VAL ), vhad = 0;
int_t ret = (nneg < vneg) - (nneg > vneg);
if ( ret )
return ret;
if ( alup_floating( _NUM ) )
{
alup_t _NMAN;
bool_t nhad;
ssize_t nbias = alup_get_exponent_bias( _NUM );
alup_init_mantissa( _NUM, _NMAN );
nexp = alup_get_exponent( _NUM );
nhad = !!nexp;
nexp -= IFTRUE( nhad, nbias );
if ( alup_floating( _VAL ) )
{
vbias = alup_get_exponent_bias( _VAL );
vexp = alup_get_exponent( _VAL );
vhad = !!vexp;
vexp -= IFTRUE( vhad, vbias );
ret = (nexp > vexp) - (nexp < vexp);
if ( ret == 0 )
{
alup_init_mantissa( _VAL, _VMAN );
alub_set( _NUM->data, alup_until_pos(&_NMAN), 1 );
alub_set( _VAL->data, alup_until_pos(&_VMAN), 1 );
_NMAN.bits++;
_VMAN.bits++;
// FIXME: Doesn't account for different size mantissas
ret = alup_cmp_int2int( &_NMAN, &_VMAN );
alup_set_exponent( _NUM, nexp + IFTRUE( nhad, nbias ) );
alup_set_exponent( _VAL, vexp + IFTRUE( vhad, vbias ) );
}
}
return ret;
}
else if ( alup_floating( _VAL ) )
{
alub_t n;
if ( nneg )
alup_neg( _NUM );
n = alup_final_bit_with_val( _NUM, true );
vbias = alup_get_exponent_bias( _VAL );
nexp = n.bit - _NUM->from;
vexp = alup_get_exponent( _VAL );
vhad = !!vexp;
vexp -= IFTRUE( vhad, vbias );
ret = (nexp > vexp) - (nexp < vexp);
if ( ret == 0 )
{
alup_init_mantissa( _VAL, _VMAN );
alub_set( _VAL->data, alup_until_pos( &_VMAN ), 1 );
_VMAN.bits++;
_VMAN.from += IFTRUE
(
vexp >= 0 && vexp <= (ssize_t)(_VMAN.bits)
, _VMAN.bits - vexp
);
ret = alup_cmp_int2int( _NUM, &_VMAN );
alup_set_exponent( _VAL, vexp + IFTRUE( vhad, vbias ) );
}
if ( nneg )
alup_neg( _NUM );
return ret;
}
return alup_cmp_int2int( _NUM, _VAL );
}
int_t alup__inc_int( alup_t const * const _SRC )
{
alub_t n;
bool_t is1 = true;
size_t upto = alup_until_pos( _SRC );
for
(
n = alub( _SRC->data, _SRC->from )
; n.bit < upto
; alub_inc(&n)
)
{
is1 = !!(*(n.ptr) & n.mask);
*(n.ptr) &= ~(n.mask);
*(n.ptr) |= IFTRUE( !is1, n.mask );
if ( !is1 ) return 0;
}
return EOVERFLOW;
}
int_t alup_inc( alup_t const * const _SRC )
{
if ( alup_floating( _SRC ) )
{
bool_t neg = alup_get_sign( _SRC );
ssize_t exp = alup_get_exponent( _SRC )
, bias = alup_get_exponent_bias( _SRC ), mdig = _SRC->mdig;
if ( exp )
{
exp -= bias;
if ( exp >= 0 && exp <= mdig )
{
alup_t _EXP, _MAN;
alub_t e;
alup_init_exponent( _SRC, _EXP );
_EXP.upto = _SRC->upto;
alup_set( &_EXP, 0 );
alup_init_mantissa( _SRC, _MAN );
e = alub( _MAN.data, _MAN.upto );
*(e.ptr) |= e.mask;
_MAN.from = _MAN.upto - exp;
_MAN.upto++;
alup__inc_int( &_MAN );
/* Normalise */
if ( !(*(e.ptr) & e.mask) )
{
if ( neg )
{
--exp;
alup__shl_int2int( &_MAN, 1 );
neg = (exp == 0);
}
else
{
++exp;
alup__shr_int2int( &_MAN, 1 );
}
}
alup_set_sign( _SRC, neg );
return alup_set_exponent( _SRC, exp + bias );
}
return ERANGE;
}
alup_set_sign( _SRC, false );
return alup_set_exponent( _SRC, bias );
}
return alup__inc_int( _SRC );
}
int_t alup__dec_int( alup_t const * const _SRC )
{
alub_t n;
bool_t is0 = true;
size_t upto = alup_until_pos( _SRC );
for
(
n = alub( _SRC->data, _SRC->from )
; n.bit < upto
; alub_inc(&n)
)
{
is0 = !(*(n.ptr) & n.mask);
*(n.ptr) &= ~(n.mask);
if ( !is0 ) return 0;
*(n.ptr) |= n.mask;
}
return EOVERFLOW;
}
int_t alup_dec( alup_t const * const _SRC )
{
if ( alup_floating( _SRC ) )
{
bool_t neg = alup_get_sign( _SRC );
ssize_t exp = alup_get_exponent( _SRC )
, bias = alup_get_exponent_bias( _SRC ), mdig = _SRC->mdig;
if ( exp )
{
exp -= bias;
if ( exp >= 0 && exp <= mdig )
{
int ret;
alup_t _MAN;
alub_t e;
alup_init_mantissa( _SRC, _MAN );
e = alub( _MAN.data, _MAN.upto );
*(e.ptr) |= e.mask;
_MAN.from = _MAN.upto - exp;
_MAN.upto++;
ret = alup__dec_int( &_MAN );
if ( ret == EOVERFLOW )
{
++exp;
alup_set_sign( _SRC, true );
alup__shr_int2int( &_MAN, 1 );
}
else if ( !( *(e.ptr) & e.mask ) )
{
if ( neg )
{
++exp;
alup__shr_int2int( &_MAN, 1 );
}
else if ( exp == 0 )
{
_MAN.upto--;
_MAN.from = _SRC->from;
e = alup_final_bit_with_val( &_MAN, true );
exp = !!(*(e.ptr) & e.mask) * (_MAN.upto - e.bit);
exp = -exp;
}
else
{
--exp;
alup__shl_int2int( &_MAN, 1 );
}
}
neg = !!((*(e.ptr) & e.mask) || exp);
return alup_set_exponent( _SRC, neg * (bias + exp) );
}
return ERANGE;
}
alup_set_sign( _SRC, true );
return alup_set_exponent( _SRC, bias );
}
return alup__dec_int( _SRC );
}
int_t alup_match_exponents( void *_num, void *_val, size_t bits )
{
int_t ret;
alup_t _NUM, _VAL, _DST;
size_t exp = 0, nexp, vexp, diff = 0;
bool truncated = false;
alup_init_floating( _NUM, _num, bits );
alup_init_floating( _VAL, _val, bits );
nexp = alup_get_exponent( &_NUM );
vexp = alup_get_exponent( &_VAL );
if ( nexp > vexp )
{
exp = nexp;
diff = nexp - vexp;
_DST = _VAL;
}
else if ( vexp > nexp )
{
exp = vexp;
diff = vexp - nexp;
_DST = _NUM;
}
if ( diff )
{
alup_t _MAN;