forked from fernandotcl/TinyEMU
-
Notifications
You must be signed in to change notification settings - Fork 24
/
softfp_template.h
1129 lines (1036 loc) · 30.1 KB
/
softfp_template.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
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
/*
* SoftFP Library
*
* Copyright (c) 2016 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#if F_SIZE == 32
#define F_UINT uint32_t
#define F_ULONG uint64_t
#define MANT_SIZE 23
#define EXP_SIZE 8
#elif F_SIZE == 64
#define F_UHALF uint32_t
#define F_UINT uint64_t
#ifdef HAVE_INT128
#define F_ULONG uint128_t
#endif
#define MANT_SIZE 52
#define EXP_SIZE 11
#elif F_SIZE == 128
#define F_UHALF uint64_t
#define F_UINT uint128_t
#define MANT_SIZE 112
#define EXP_SIZE 15
#else
#error unsupported F_SIZE
#endif
#define EXP_MASK ((1 << EXP_SIZE) - 1)
#define MANT_MASK (((F_UINT)1 << MANT_SIZE) - 1)
#define SIGN_MASK ((F_UINT)1 << (F_SIZE - 1))
#define IMANT_SIZE (F_SIZE - 2) /* internal mantissa size */
#define RND_SIZE (IMANT_SIZE - MANT_SIZE)
#define QNAN_MASK ((F_UINT)1 << (MANT_SIZE - 1))
/* quiet NaN */
#define F_QNAN glue(F_QNAN, F_SIZE)
#define clz glue(clz, F_SIZE)
#define pack_sf glue(pack_sf, F_SIZE)
#define unpack_sf glue(unpack_sf, F_SIZE)
#define rshift_rnd glue(rshift_rnd, F_SIZE)
#define round_pack_sf glue(roundpack_sf, F_SIZE)
#define normalize_sf glue(normalize_sf, F_SIZE)
#define normalize2_sf glue(normalize2_sf, F_SIZE)
#define issignan_sf glue(issignan_sf, F_SIZE)
#define isnan_sf glue(isnan_sf, F_SIZE)
#define add_sf glue(add_sf, F_SIZE)
#define mul_sf glue(mul_sf, F_SIZE)
#define fma_sf glue(fma_sf, F_SIZE)
#define div_sf glue(div_sf, F_SIZE)
#define sqrt_sf glue(sqrt_sf, F_SIZE)
#define normalize_subnormal_sf glue(normalize_subnormal_sf, F_SIZE)
#define divrem_u glue(divrem_u, F_SIZE)
#define sqrtrem_u glue(sqrtrem_u, F_SIZE)
#define mul_u glue(mul_u, F_SIZE)
#define cvt_sf32_sf glue(cvt_sf32_sf, F_SIZE)
#define cvt_sf64_sf glue(cvt_sf64_sf, F_SIZE)
static const F_UINT F_QNAN = (((F_UINT)EXP_MASK << MANT_SIZE) | ((F_UINT)1 << (MANT_SIZE - 1)));
static inline F_UINT pack_sf(uint32_t a_sign, uint32_t a_exp, F_UINT a_mant)
{
return ((F_UINT)a_sign << (F_SIZE - 1)) |
((F_UINT)a_exp << MANT_SIZE) |
(a_mant & MANT_MASK);
}
static inline F_UINT unpack_sf(uint32_t *pa_sign, int32_t *pa_exp,
F_UINT a)
{
*pa_sign = a >> (F_SIZE - 1);
*pa_exp = (a >> MANT_SIZE) & EXP_MASK;
return a & MANT_MASK;
}
static F_UINT rshift_rnd(F_UINT a, int d)
{
F_UINT mask;
if (d != 0) {
if (d >= F_SIZE) {
a = (a != 0);
} else {
mask = ((F_UINT)1 << d) - 1;
a = (a >> d) | ((a & mask) != 0);
}
}
return a;
}
/* a_mant is considered to have its MSB at F_SIZE - 2 bits */
static F_UINT round_pack_sf(uint32_t a_sign, int a_exp, F_UINT a_mant,
RoundingModeEnum rm, uint32_t *pfflags)
{
int diff;
uint32_t addend, rnd_bits;
switch(rm) {
case RM_RNE:
case RM_RMM:
addend = (1 << (RND_SIZE - 1));
break;
case RM_RTZ:
addend = 0;
break;
default:
case RM_RDN:
case RM_RUP:
// printf("s=%d rm=%d m=%x\n", a_sign, rm, a_mant);
if (a_sign ^ (rm & 1))
addend = (1 << RND_SIZE) - 1;
else
addend = 0;
break;
}
/* potentially subnormal */
if (a_exp <= 0) {
bool is_subnormal;
/* Note: we set the underflow flag if the rounded result
is subnormal and inexact */
is_subnormal = (a_exp < 0 ||
(a_mant + addend) < ((F_UINT)1 << (F_SIZE - 1)));
diff = 1 - a_exp;
a_mant = rshift_rnd(a_mant, diff);
rnd_bits = a_mant & ((1 << RND_SIZE ) - 1);
if (is_subnormal && rnd_bits != 0) {
*pfflags |= FFLAG_UNDERFLOW;
}
a_exp = 1;
} else {
rnd_bits = a_mant & ((1 << RND_SIZE ) - 1);
}
if (rnd_bits != 0)
*pfflags |= FFLAG_INEXACT;
a_mant = (a_mant + addend) >> RND_SIZE;
/* half way: select even result */
if (rm == RM_RNE && rnd_bits == (1 << (RND_SIZE - 1)))
a_mant &= ~1;
/* Note the rounding adds at least 1, so this is the maximum
value */
a_exp += a_mant >> (MANT_SIZE + 1);
if (a_mant <= MANT_MASK) {
/* denormalized or zero */
a_exp = 0;
} else if (a_exp >= EXP_MASK) {
/* overflow */
if (addend == 0) {
a_exp = EXP_MASK - 1;
a_mant = MANT_MASK;
} else {
/* infinity */
a_exp = EXP_MASK;
a_mant = 0;
}
*pfflags |= FFLAG_OVERFLOW | FFLAG_INEXACT;
}
return pack_sf(a_sign, a_exp, a_mant);
}
/* a_mant is considered to have at most F_SIZE - 1 bits */
static F_UINT normalize_sf(uint32_t a_sign, int a_exp, F_UINT a_mant,
RoundingModeEnum rm, uint32_t *pfflags)
{
int shift;
shift = clz(a_mant) - (F_SIZE - 1 - IMANT_SIZE);
assert(shift >= 0);
a_exp -= shift;
a_mant <<= shift;
return round_pack_sf(a_sign, a_exp, a_mant, rm, pfflags);
}
/* same as normalize_sf() but with a double word mantissa. a_mant1 is
considered to have at most F_SIZE - 1 bits */
static F_UINT normalize2_sf(uint32_t a_sign, int a_exp, F_UINT a_mant1, F_UINT a_mant0,
RoundingModeEnum rm, uint32_t *pfflags)
{
int l, shift;
if (a_mant1 == 0) {
l = F_SIZE + clz(a_mant0);
} else {
l = clz(a_mant1);
}
shift = l - (F_SIZE - 1 - IMANT_SIZE);
assert(shift >= 0);
a_exp -= shift;
if (shift == 0) {
a_mant1 |= (a_mant0 != 0);
} else if (shift < F_SIZE) {
a_mant1 = (a_mant1 << shift) | (a_mant0 >> (F_SIZE - shift));
a_mant0 <<= shift;
a_mant1 |= (a_mant0 != 0);
} else {
a_mant1 = a_mant0 << (shift - F_SIZE);
}
return round_pack_sf(a_sign, a_exp, a_mant1, rm, pfflags);
}
bool issignan_sf(F_UINT a)
{
uint32_t a_exp1;
F_UINT a_mant;
a_exp1 = (a >> (MANT_SIZE - 1)) & ((1 << (EXP_SIZE + 1)) - 1);
a_mant = a & MANT_MASK;
return (a_exp1 == (2 * EXP_MASK) && a_mant != 0);
}
bool isnan_sf(F_UINT a)
{
uint32_t a_exp;
F_UINT a_mant;
a_exp = (a >> MANT_SIZE) & EXP_MASK;
a_mant = a & MANT_MASK;
return (a_exp == EXP_MASK && a_mant != 0);
}
F_UINT add_sf(F_UINT a, F_UINT b, RoundingModeEnum rm,
uint32_t *pfflags)
{
uint32_t a_sign, b_sign, a_exp, b_exp;
F_UINT tmp, a_mant, b_mant;
/* swap so that abs(a) >= abs(b) */
if ((a & ~SIGN_MASK) < (b & ~SIGN_MASK)) {
tmp = a;
a = b;
b = tmp;
}
a_sign = a >> (F_SIZE - 1);
b_sign = b >> (F_SIZE - 1);
a_exp = (a >> MANT_SIZE) & EXP_MASK;
b_exp = (b >> MANT_SIZE) & EXP_MASK;
a_mant = (a & MANT_MASK) << 3;
b_mant = (b & MANT_MASK) << 3;
if (unlikely(a_exp == EXP_MASK)) {
if (a_mant != 0) {
/* NaN result */
if (!(a_mant & (QNAN_MASK << 3)) || issignan_sf(b))
*pfflags |= FFLAG_INVALID_OP;
return F_QNAN;
} else if (b_exp == EXP_MASK && a_sign != b_sign) {
*pfflags |= FFLAG_INVALID_OP;
return F_QNAN;
} else {
/* infinity */
return a;
}
}
if (a_exp == 0) {
a_exp = 1;
} else {
a_mant |= (F_UINT)1 << (MANT_SIZE + 3);
}
if (b_exp == 0) {
b_exp = 1;
} else {
b_mant |= (F_UINT)1 << (MANT_SIZE + 3);
}
b_mant = rshift_rnd(b_mant, a_exp - b_exp);
if (a_sign == b_sign) {
/* same signs : add the absolute values */
a_mant += b_mant;
} else {
/* different signs : subtract the absolute values */
a_mant -= b_mant;
if (a_mant == 0) {
/* zero result : the sign needs a specific handling */
a_sign = (rm == RM_RDN);
}
}
a_exp += (RND_SIZE - 3);
return normalize_sf(a_sign, a_exp, a_mant, rm, pfflags);
}
F_UINT glue(sub_sf, F_SIZE)(F_UINT a, F_UINT b, RoundingModeEnum rm,
uint32_t *pfflags)
{
return add_sf(a, b ^ SIGN_MASK, rm, pfflags);
}
static inline F_UINT normalize_subnormal_sf(int32_t *pa_exp, F_UINT a_mant)
{
int shift;
shift = MANT_SIZE - ((F_SIZE - 1 - clz(a_mant)));
*pa_exp = 1 - shift;
return a_mant << shift;
}
#ifdef F_ULONG
static F_UINT mul_u(F_UINT *plow, F_UINT a, F_UINT b)
{
F_ULONG r;
r = (F_ULONG)a * (F_ULONG)b;
*plow = r;
return r >> F_SIZE;
}
#else
#define FH_SIZE (F_SIZE / 2)
static F_UINT mul_u(F_UINT *plow, F_UINT a, F_UINT b)
{
F_UHALF a0, a1, b0, b1, r0, r1, r2, r3;
F_UINT r00, r01, r10, r11, c;
a0 = a;
a1 = a >> FH_SIZE;
b0 = b;
b1 = b >> FH_SIZE;
r00 = (F_UINT)a0 * (F_UINT)b0;
r01 = (F_UINT)a0 * (F_UINT)b1;
r10 = (F_UINT)a1 * (F_UINT)b0;
r11 = (F_UINT)a1 * (F_UINT)b1;
r0 = r00;
c = (r00 >> FH_SIZE) + (F_UHALF)r01 + (F_UHALF)r10;
r1 = c;
c = (c >> FH_SIZE) + (r01 >> FH_SIZE) + (r10 >> FH_SIZE) + (F_UHALF)r11;
r2 = c;
r3 = (c >> FH_SIZE) + (r11 >> FH_SIZE);
*plow = ((F_UINT)r1 << FH_SIZE) | r0;
return ((F_UINT)r3 << FH_SIZE) | r2;
}
#undef FH_SIZE
#endif
F_UINT mul_sf(F_UINT a, F_UINT b, RoundingModeEnum rm,
uint32_t *pfflags)
{
uint32_t a_sign, b_sign, r_sign;
int32_t a_exp, b_exp, r_exp;
F_UINT a_mant, b_mant, r_mant, r_mant_low;
a_sign = a >> (F_SIZE - 1);
b_sign = b >> (F_SIZE - 1);
r_sign = a_sign ^ b_sign;
a_exp = (a >> MANT_SIZE) & EXP_MASK;
b_exp = (b >> MANT_SIZE) & EXP_MASK;
a_mant = a & MANT_MASK;
b_mant = b & MANT_MASK;
if (a_exp == EXP_MASK || b_exp == EXP_MASK) {
if (isnan_sf(a) || isnan_sf(b)) {
if (issignan_sf(a) || issignan_sf(b)) {
*pfflags |= FFLAG_INVALID_OP;
}
return F_QNAN;
} else {
/* infinity */
if ((a_exp == EXP_MASK && (b_exp == 0 && b_mant == 0)) ||
(b_exp == EXP_MASK && (a_exp == 0 && a_mant == 0))) {
*pfflags |= FFLAG_INVALID_OP;
return F_QNAN;
} else {
return pack_sf(r_sign, EXP_MASK, 0);
}
}
}
if (a_exp == 0) {
if (a_mant == 0)
return pack_sf(r_sign, 0, 0); /* zero */
a_mant = normalize_subnormal_sf(&a_exp, a_mant);
} else {
a_mant |= (F_UINT)1 << MANT_SIZE;
}
if (b_exp == 0) {
if (b_mant == 0)
return pack_sf(r_sign, 0, 0); /* zero */
b_mant = normalize_subnormal_sf(&b_exp, b_mant);
} else {
b_mant |= (F_UINT)1 << MANT_SIZE;
}
r_exp = a_exp + b_exp - (1 << (EXP_SIZE - 1)) + 2;
r_mant = mul_u(&r_mant_low,a_mant << RND_SIZE, b_mant << (RND_SIZE + 1));
r_mant |= (r_mant_low != 0);
return normalize_sf(r_sign, r_exp, r_mant, rm, pfflags);
}
/* fused multiply and add */
F_UINT fma_sf(F_UINT a, F_UINT b, F_UINT c, RoundingModeEnum rm,
uint32_t *pfflags)
{
uint32_t a_sign, b_sign, c_sign, r_sign;
int32_t a_exp, b_exp, c_exp, r_exp, shift;
F_UINT a_mant, b_mant, c_mant, r_mant1, r_mant0, c_mant1, c_mant0, mask;
a_sign = a >> (F_SIZE - 1);
b_sign = b >> (F_SIZE - 1);
c_sign = c >> (F_SIZE - 1);
r_sign = a_sign ^ b_sign;
a_exp = (a >> MANT_SIZE) & EXP_MASK;
b_exp = (b >> MANT_SIZE) & EXP_MASK;
c_exp = (c >> MANT_SIZE) & EXP_MASK;
a_mant = a & MANT_MASK;
b_mant = b & MANT_MASK;
c_mant = c & MANT_MASK;
if (a_exp == EXP_MASK || b_exp == EXP_MASK || c_exp == EXP_MASK) {
if (isnan_sf(a) || isnan_sf(b) || isnan_sf(c)) {
if (issignan_sf(a) || issignan_sf(b) || issignan_sf(c)) {
*pfflags |= FFLAG_INVALID_OP;
}
return F_QNAN;
} else {
/* infinities */
if ((a_exp == EXP_MASK && (b_exp == 0 && b_mant == 0)) ||
(b_exp == EXP_MASK && (a_exp == 0 && a_mant == 0)) ||
((a_exp == EXP_MASK || b_exp == EXP_MASK) &&
(c_exp == EXP_MASK && r_sign != c_sign))) {
*pfflags |= FFLAG_INVALID_OP;
return F_QNAN;
} else if (c_exp == EXP_MASK) {
return pack_sf(c_sign, EXP_MASK, 0);
} else {
return pack_sf(r_sign, EXP_MASK, 0);
}
}
}
if (a_exp == 0) {
if (a_mant == 0)
goto mul_zero;
a_mant = normalize_subnormal_sf(&a_exp, a_mant);
} else {
a_mant |= (F_UINT)1 << MANT_SIZE;
}
if (b_exp == 0) {
if (b_mant == 0) {
mul_zero:
if (c_exp == 0 && c_mant == 0) {
if (c_sign != r_sign)
r_sign = (rm == RM_RDN);
return pack_sf(r_sign, 0, 0);
} else {
return c;
}
}
b_mant = normalize_subnormal_sf(&b_exp, b_mant);
} else {
b_mant |= (F_UINT)1 << MANT_SIZE;
}
/* multiply */
r_exp = a_exp + b_exp - (1 << (EXP_SIZE - 1)) + 3;
r_mant1 = mul_u(&r_mant0, a_mant << RND_SIZE, b_mant << RND_SIZE);
/* normalize to F_SIZE - 3 */
if (r_mant1 < ((F_UINT)1 << (F_SIZE - 3))) {
r_mant1 = (r_mant1 << 1) | (r_mant0 >> (F_SIZE - 1));
r_mant0 <<= 1;
r_exp--;
}
/* add */
if (c_exp == 0) {
if (c_mant == 0) {
/* add zero */
r_mant1 |= (r_mant0 != 0);
return normalize_sf(r_sign, r_exp, r_mant1, rm, pfflags);
}
c_mant = normalize_subnormal_sf(&c_exp, c_mant);
} else {
c_mant |= (F_UINT)1 << MANT_SIZE;
}
c_exp++;
c_mant1 = c_mant << (RND_SIZE - 1);
c_mant0 = 0;
// printf("r_s=%d r_exp=%d r_mant=%08x %08x\n", r_sign, r_exp, (uint32_t)r_mant1, (uint32_t)r_mant0);
// printf("c_s=%d c_exp=%d c_mant=%08x %08x\n", c_sign, c_exp, (uint32_t)c_mant1, (uint32_t)c_mant0);
/* ensure that abs(r) >= abs(c) */
if (!(r_exp > c_exp || (r_exp == c_exp && r_mant1 >= c_mant1))) {
F_UINT tmp;
int32_t c_tmp;
/* swap */
tmp = r_mant1; r_mant1 = c_mant1; c_mant1 = tmp;
tmp = r_mant0; r_mant0 = c_mant0; c_mant0 = tmp;
c_tmp = r_exp; r_exp = c_exp; c_exp = c_tmp;
c_tmp = r_sign; r_sign = c_sign; c_sign = c_tmp;
}
/* right shift c_mant */
shift = r_exp - c_exp;
if (shift >= 2 * F_SIZE) {
c_mant0 = (c_mant0 | c_mant1) != 0;
c_mant1 = 0;
} else if (shift >= F_SIZE + 1) {
c_mant0 = rshift_rnd(c_mant1, shift - F_SIZE);
c_mant1 = 0;
} else if (shift == F_SIZE) {
c_mant0 = c_mant1 | (c_mant0 != 0);
c_mant1 = 0;
} else if (shift != 0) {
mask = ((F_UINT)1 << shift) - 1;
c_mant0 = (c_mant1 << (F_SIZE - shift)) | (c_mant0 >> shift) | ((c_mant0 & mask) != 0);
c_mant1 = c_mant1 >> shift;
}
// printf(" r_mant=%08x %08x\n", (uint32_t)r_mant1, (uint32_t)r_mant0);
// printf(" c_mant=%08x %08x\n", (uint32_t)c_mant1, (uint32_t)c_mant0);
/* add or subtract */
if (r_sign == c_sign) {
r_mant0 += c_mant0;
r_mant1 += c_mant1 + (r_mant0 < c_mant0);
} else {
F_UINT tmp;
tmp = r_mant0;
r_mant0 -= c_mant0;
r_mant1 = r_mant1 - c_mant1 - (r_mant0 > tmp);
if ((r_mant0 | r_mant1) == 0) {
/* zero result : the sign needs a specific handling */
r_sign = (rm == RM_RDN);
}
}
#if 0
// printf(" r1_mant=%08x %08x\n", (uint32_t)r_mant1, (uint32_t)r_mant0);
/* normalize */
if (r_mant1 == 0) {
r_mant1 = r_mant0;
r_exp -= F_SIZE;
} else {
shift = clz(r_mant1) - (F_SIZE - 1 - IMANT_SIZE);
if (shift != 0) {
r_mant1 = (r_mant1 << shift) | (r_mant0 >> (F_SIZE - shift));
r_mant0 <<= shift;
r_exp -= shift;
}
r_mant1 |= (r_mant0 != 0);
}
return normalize_sf(r_sign, r_exp, r_mant1, rm, pfflags);
#else
return normalize2_sf(r_sign, r_exp, r_mant1, r_mant0, rm, pfflags);
#endif
}
#ifdef F_ULONG
static F_UINT divrem_u(F_UINT *pr, F_UINT ah, F_UINT al, F_UINT b)
{
F_ULONG a;
a = ((F_ULONG)ah << F_SIZE) | al;
*pr = a % b;
return a / b;
}
#else
/* XXX: optimize */
static F_UINT divrem_u(F_UINT *pr, F_UINT a1, F_UINT a0, F_UINT b)
{
int i, qb, ab;
assert(a1 < b);
for(i = 0; i < F_SIZE; i++) {
ab = a1 >> (F_SIZE - 1);
a1 = (a1 << 1) | (a0 >> (F_SIZE - 1));
if (ab || a1 >= b) {
a1 -= b;
qb = 1;
} else {
qb = 0;
}
a0 = (a0 << 1) | qb;
}
*pr = a1;
return a0;
}
#endif
F_UINT div_sf(F_UINT a, F_UINT b, RoundingModeEnum rm,
uint32_t *pfflags)
{
uint32_t a_sign, b_sign, r_sign;
int32_t a_exp, b_exp, r_exp;
F_UINT a_mant, b_mant, r_mant, r;
a_sign = a >> (F_SIZE - 1);
b_sign = b >> (F_SIZE - 1);
r_sign = a_sign ^ b_sign;
a_exp = (a >> MANT_SIZE) & EXP_MASK;
b_exp = (b >> MANT_SIZE) & EXP_MASK;
a_mant = a & MANT_MASK;
b_mant = b & MANT_MASK;
if (a_exp == EXP_MASK) {
if (a_mant != 0 || isnan_sf(b)) {
if (issignan_sf(a) || issignan_sf(b)) {
*pfflags |= FFLAG_INVALID_OP;
}
return F_QNAN;
} else if (b_exp == EXP_MASK) {
*pfflags |= FFLAG_INVALID_OP;
return F_QNAN;
} else {
return pack_sf(r_sign, EXP_MASK, 0);
}
} else if (b_exp == EXP_MASK) {
if (b_mant != 0) {
if (issignan_sf(a) || issignan_sf(b)) {
*pfflags |= FFLAG_INVALID_OP;
}
return F_QNAN;
} else {
return pack_sf(r_sign, 0, 0);
}
}
if (b_exp == 0) {
if (b_mant == 0) {
/* zero */
if (a_exp == 0 && a_mant == 0) {
*pfflags |= FFLAG_INVALID_OP;
return F_QNAN;
} else {
*pfflags |= FFLAG_DIVIDE_ZERO;
return pack_sf(r_sign, EXP_MASK, 0);
}
}
b_mant = normalize_subnormal_sf(&b_exp, b_mant);
} else {
b_mant |= (F_UINT)1 << MANT_SIZE;
}
if (a_exp == 0) {
if (a_mant == 0)
return pack_sf(r_sign, 0, 0); /* zero */
a_mant = normalize_subnormal_sf(&a_exp, a_mant);
} else {
a_mant |= (F_UINT)1 << MANT_SIZE;
}
r_exp = a_exp - b_exp + (1 << (EXP_SIZE - 1)) - 1;
r_mant = divrem_u(&r, a_mant, 0, b_mant << 2);
if (r != 0)
r_mant |= 1;
return normalize_sf(r_sign, r_exp, r_mant, rm, pfflags);
}
#ifdef F_ULONG
/* compute sqrt(a) with a = ah*2^F_SIZE+al and a < 2^(F_SIZE - 2)
return true if not exact square. */
static int sqrtrem_u(F_UINT *pr, F_UINT ah, F_UINT al)
{
F_ULONG a, u, s;
int l, inexact;
/* 2^l >= a */
if (ah != 0) {
l = 2 * F_SIZE - clz(ah - 1);
} else {
if (al == 0) {
*pr = 0;
return 0;
}
l = F_SIZE - clz(al - 1);
}
a = ((F_ULONG)ah << F_SIZE) | al;
u = (F_ULONG)1 << ((l + 1) / 2);
for(;;) {
s = u;
u = ((a / s) + s) / 2;
if (u >= s)
break;
}
inexact = (a - s * s) != 0;
*pr = s;
return inexact;
}
#else
static int sqrtrem_u(F_UINT *pr, F_UINT a1, F_UINT a0)
{
int l, inexact;
F_UINT u, s, r, q, sq0, sq1;
/* 2^l >= a */
if (a1 != 0) {
l = 2 * F_SIZE - clz(a1 - 1);
} else {
if (a0 == 0) {
*pr = 0;
return 0;
}
l = F_SIZE - clz(a0 - 1);
}
u = (F_UINT)1 << ((l + 1) / 2);
for(;;) {
s = u;
q = divrem_u(&r, a1, a0, s);
u = (q + s) / 2;
if (u >= s)
break;
}
sq1 = mul_u(&sq0, s, s);
inexact = (sq0 != a0 || sq1 != a1);
*pr = s;
return inexact;
}
#endif
F_UINT sqrt_sf(F_UINT a, RoundingModeEnum rm,
uint32_t *pfflags)
{
uint32_t a_sign;
int32_t a_exp;
F_UINT a_mant;
a_sign = a >> (F_SIZE - 1);
a_exp = (a >> MANT_SIZE) & EXP_MASK;
a_mant = a & MANT_MASK;
if (a_exp == EXP_MASK) {
if (a_mant != 0) {
if (issignan_sf(a)) {
*pfflags |= FFLAG_INVALID_OP;
}
return F_QNAN;
} else if (a_sign) {
goto neg_error;
} else {
return a; /* +infinity */
}
}
if (a_sign) {
if (a_exp == 0 && a_mant == 0)
return a; /* -zero */
neg_error:
*pfflags |= FFLAG_INVALID_OP;
return F_QNAN;
}
if (a_exp == 0) {
if (a_mant == 0)
return pack_sf(0, 0, 0); /* zero */
a_mant = normalize_subnormal_sf(&a_exp, a_mant);
} else {
a_mant |= (F_UINT)1 << MANT_SIZE;
}
a_exp -= EXP_MASK / 2;
/* simpler to handle an even exponent */
if (a_exp & 1) {
a_exp--;
a_mant <<= 1;
}
a_exp = (a_exp >> 1) + EXP_MASK / 2;
a_mant <<= (F_SIZE - 4 - MANT_SIZE);
if (sqrtrem_u(&a_mant, a_mant, 0))
a_mant |= 1;
return normalize_sf(a_sign, a_exp, a_mant, rm, pfflags);
}
/* comparisons */
static F_UINT glue(min_max_nan_sf, F_SIZE)(F_UINT a, F_UINT b, uint32_t *pfflags, SoftFPMinMaxTypeEnum minmax_type)
{
if (issignan_sf(a) || issignan_sf(b)) {
*pfflags |= FFLAG_INVALID_OP;
if (minmax_type == FMINMAX_IEEE754_2008)
return F_QNAN;
}
if (minmax_type == FMINMAX_PROP) {
return F_QNAN;
} else {
if (isnan_sf(a)) {
if (isnan_sf(b))
return F_QNAN;
else
return b;
} else {
return a;
}
}
}
F_UINT glue(min_sf, F_SIZE)(F_UINT a, F_UINT b, uint32_t *pfflags,
SoftFPMinMaxTypeEnum minmax_type)
{
uint32_t a_sign, b_sign;
if (isnan_sf(a) || isnan_sf(b)) {
return glue(min_max_nan_sf, F_SIZE)(a, b, pfflags, minmax_type);
}
a_sign = a >> (F_SIZE - 1);
b_sign = b >> (F_SIZE - 1);
if (a_sign != b_sign) {
if (a_sign)
return a;
else
return b;
} else {
if ((a < b) ^ a_sign)
return a;
else
return b;
}
}
F_UINT glue(max_sf, F_SIZE)(F_UINT a, F_UINT b, uint32_t *pfflags,
SoftFPMinMaxTypeEnum minmax_type)
{
uint32_t a_sign, b_sign;
if (isnan_sf(a) || isnan_sf(b)) {
return glue(min_max_nan_sf, F_SIZE)(a, b, pfflags, minmax_type);
}
a_sign = a >> (F_SIZE - 1);
b_sign = b >> (F_SIZE - 1);
if (a_sign != b_sign) {
if (a_sign)
return b;
else
return a;
} else {
if ((a < b) ^ a_sign)
return b;
else
return a;
}
}
int glue(eq_quiet_sf, F_SIZE)(F_UINT a, F_UINT b, uint32_t *pfflags)
{
if (isnan_sf(a) || isnan_sf(b)) {
if (issignan_sf(a) || issignan_sf(b)) {
*pfflags |= FFLAG_INVALID_OP;
}
return 0;
}
if ((F_UINT)((a | b) << 1) == 0)
return 1; /* zero case */
return (a == b);
}
int glue(le_sf, F_SIZE)(F_UINT a, F_UINT b, uint32_t *pfflags)
{
uint32_t a_sign, b_sign;
if (isnan_sf(a) || isnan_sf(b)) {
*pfflags |= FFLAG_INVALID_OP;
return 0;
}
a_sign = a >> (F_SIZE - 1);
b_sign = b >> (F_SIZE - 1);
if (a_sign != b_sign) {
return (a_sign || ((F_UINT)((a | b) << 1) == 0));
} else {
if (a_sign) {
return (a >= b);
} else {
return (a <= b);
}
}
}
int glue(lt_sf, F_SIZE)(F_UINT a, F_UINT b, uint32_t *pfflags)
{
uint32_t a_sign, b_sign;
if (isnan_sf(a) || isnan_sf(b)) {
*pfflags |= FFLAG_INVALID_OP;
return 0;
}
a_sign = a >> (F_SIZE - 1);
b_sign = b >> (F_SIZE - 1);
if (a_sign != b_sign) {
return (a_sign && ((F_UINT)((a | b) << 1) != 0));
} else {
if (a_sign) {
return (a > b);
} else {
return (a < b);
}
}
}
uint32_t glue(fclass_sf, F_SIZE)(F_UINT a)
{
uint32_t a_sign;
int32_t a_exp;
F_UINT a_mant;
uint32_t ret;
a_sign = a >> (F_SIZE - 1);
a_exp = (a >> MANT_SIZE) & EXP_MASK;
a_mant = a & MANT_MASK;
if (a_exp == EXP_MASK) {
if (a_mant != 0) {
if (a_mant & QNAN_MASK)
ret = FCLASS_QNAN;
else
ret = FCLASS_SNAN;
} else {
if (a_sign)
ret = FCLASS_NINF;
else
ret = FCLASS_PINF;
}
} else if (a_exp == 0) {
if (a_mant == 0) {
if (a_sign)
ret = FCLASS_NZERO;
else
ret = FCLASS_PZERO;
} else {
if (a_sign)
ret = FCLASS_NSUBNORMAL;
else
ret = FCLASS_PSUBNORMAL;
}
} else {
if (a_sign)
ret = FCLASS_NNORMAL;
else
ret = FCLASS_PNORMAL;
}
return ret;
}
/* conversions between floats */
#if F_SIZE >= 64
F_UINT cvt_sf32_sf(uint32_t a, uint32_t *pfflags)
{
uint32_t a_sign;
int32_t a_exp;
F_UINT a_mant;
a_mant = unpack_sf32(&a_sign, &a_exp, a);
if (a_exp == 0xff) {
if (a_mant != 0) {
/* NaN */
if (issignan_sf32(a)) {
*pfflags |= FFLAG_INVALID_OP;
}
return F_QNAN;
} else {
/* infinity */
return pack_sf(a_sign, EXP_MASK, 0);
}
}
if (a_exp == 0) {
if (a_mant == 0)
return pack_sf(a_sign, 0, 0); /* zero */
a_mant = normalize_subnormal_sf32(&a_exp, a_mant);
}
/* convert the exponent value */
a_exp = a_exp - 0x7f + (EXP_MASK / 2);
/* shift the mantissa */
a_mant <<= (MANT_SIZE - 23);
/* We assume the target float is large enough to that no
normalization is necessary */
return pack_sf(a_sign, a_exp, a_mant);
}
uint32_t glue(glue(cvt_sf, F_SIZE), _sf32)(F_UINT a, RoundingModeEnum rm,
uint32_t *pfflags)
{
uint32_t a_sign;
int32_t a_exp;
F_UINT a_mant;
a_mant = unpack_sf(&a_sign, &a_exp, a);
if (a_exp == EXP_MASK) {
if (a_mant != 0) {
/* NaN */
if (issignan_sf(a)) {
*pfflags |= FFLAG_INVALID_OP;
}
return F_QNAN32;
} else {
/* infinity */
return pack_sf32(a_sign, 0xff, 0);
}
}
if (a_exp == 0) {
if (a_mant == 0)
return pack_sf32(a_sign, 0, 0); /* zero */