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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
|
/*
** emfloat.c
** Source for emulated floating-point routines.
** BYTEmark (tm)
** BYTE's Native Mode Benchmarks
** Rick Grehan, BYTE Magazine.
**
** Created:
** Last update: 3/95
**
** DISCLAIMER
** The source, executable, and documentation files that comprise
** the BYTEmark benchmarks are made available on an "as is" basis.
** This means that we at BYTE Magazine have made every reasonable
** effort to verify that the there are no errors in the source and
** executable code. We cannot, however, guarantee that the programs
** are error-free. Consequently, McGraw-HIll and BYTE Magazine make
** no claims in regard to the fitness of the source code, executable
** code, and documentation of the BYTEmark.
** Furthermore, BYTE Magazine, McGraw-Hill, and all employees
** of McGraw-Hill cannot be held responsible for any damages resulting
** from the use of this code or the results obtained from using
** this code.
*/
#include <stdio.h>
#include <string.h>
#include "cleanbench.h"
#include "randnum.h"
#include "emfloat.h"
static void SetInternalFPFZero(InternalFPF *dest,
unsigned char sign);
static void SetInternalFPFInfinity(InternalFPF *dest,
unsigned char sign);
static void SetInternalFPFNaN(InternalFPF *dest);
static int IsMantissaZero(uint16_t *mant);
static void Add16Bits(uint16_t *carry,uint16_t *a,uint16_t b,uint16_t c);
static void Sub16Bits(uint16_t *borrow,uint16_t *a,uint16_t b,uint16_t c);
static void ShiftMantLeft1(uint16_t *carry,uint16_t *mantissa);
static void ShiftMantRight1(uint16_t *carry,uint16_t *mantissa);
static void StickyShiftRightMant(InternalFPF *ptr,int amount);
static void RoundInternalFPF(InternalFPF *ptr);
static void normalize(InternalFPF *ptr);
static void denormalize(InternalFPF *ptr,int minimum_exponent);
static void choose_nan(InternalFPF *x,InternalFPF *y,InternalFPF *z,
int intel_flag);
/*
** Floating-point emulator.
** These routines are only "sort of" IEEE-compliant. All work is
** done using an internal representation. Also, the routines do
** not check for many of the exceptions that might occur.
** Still, the external formats produced are IEEE-compatible,
** with the restriction that they presume a low-endian machine
** (though the endianism will not effect the performance).
**
** Some code here was based on work done by Steve Snelgrove of
** Orem, UT. Other code comes from routines presented in
** the long-ago book: "Microprocessor Programming for
** Computer Hobbyists" by Neill Graham.
*/
/***********************
** SetInternalFPFZero **
************************
** Set an internal floating-point-format number to zero.
** sign determines the sign of the zero.
*/
static void SetInternalFPFZero(InternalFPF *dest,
unsigned char sign)
{
int i; /* Index */
dest->type=IFPF_IS_ZERO;
dest->sign=sign;
dest->exp=MIN_EXP;
for(i=0;i<INTERNAL_FPF_PRECISION;i++)
dest->mantissa[i]=0;
return;
}
/***************************
** SetInternalFPFInfinity **
****************************
** Set an internal floating-point-format number to infinity.
** This can happen if the exponent exceeds MAX_EXP.
** As above, sign picks the sign of infinity.
*/
static void SetInternalFPFInfinity(InternalFPF *dest,
unsigned char sign)
{
int i; /* Index */
dest->type=IFPF_IS_INFINITY;
dest->sign=sign;
dest->exp=MIN_EXP;
for(i=0;i<INTERNAL_FPF_PRECISION;i++)
dest->mantissa[i]=0;
return;
}
/**********************
** SetInternalFPFNaN **
***********************
** Set an internal floating-point-format number to Nan
** (not a number). Note that we "emulate" an 80x87 as far
** as the mantissa bits go.
*/
static void SetInternalFPFNaN(InternalFPF *dest)
{
int i; /* Index */
dest->type=IFPF_IS_NAN;
dest->exp=MAX_EXP;
dest->sign=1;
dest->mantissa[0]=0x4000;
for(i=1;i<INTERNAL_FPF_PRECISION;i++)
dest->mantissa[i]=0;
return;
}
/*******************
** IsMantissaZero **
********************
** Pass this routine a pointer to an internal floating point format
** number's mantissa. It checks for an all-zero mantissa.
** Returns 0 if it is NOT all zeros, !=0 otherwise.
*/
static int IsMantissaZero(uint16_t *mant)
{
int i; /* Index */
int n; /* Return value */
n=0;
for(i=0;i<INTERNAL_FPF_PRECISION;i++)
n|=mant[i];
return(!n);
}
/**************
** Add16Bits **
***************
** Add b, c, and carry. Retult in a. New carry in carry.
*/
static void Add16Bits(uint16_t *carry,
uint16_t *a,
uint16_t b,
uint16_t c)
{
uint32_t accum; /* Accumulator */
/*
** Do the work in the 32-bit accumulator so we can return
** the carry.
*/
accum=(uint32_t)b;
accum+=(uint32_t)c;
accum+=(uint32_t)*carry;
*carry=(uint16_t)((accum & 0x00010000) ? 1 : 0); /* New carry */
*a=(uint16_t)(accum & 0xFFFF); /* Result is lo 16 bits */
return;
}
/**************
** Sub16Bits **
***************
** Additive inverse of above.
*/
static void Sub16Bits(uint16_t *borrow,
uint16_t *a,
uint16_t b,
uint16_t c)
{
uint32_t accum; /* Accumulator */
accum=(uint32_t)b;
accum-=(uint32_t)c;
accum-=(uint32_t)*borrow;
*borrow=(accum & 0x00010000) ? 1 : 0; /* New borrow */
*a=(uint16_t)(accum & 0xFFFF);
return;
}
/*******************
** ShiftMantLeft1 **
********************
** Shift a vector of 16-bit numbers left 1 bit. Also provides
** a carry bit, which is shifted in at the beginning, and
** shifted out at the end.
*/
static void ShiftMantLeft1(uint16_t *carry,
uint16_t *mantissa)
{
int i; /* Index */
int new_carry;
uint16_t accum; /* Temporary holding placed */
for(i=INTERNAL_FPF_PRECISION-1;i>=0;i--)
{ accum=mantissa[i];
new_carry=accum & 0x8000; /* Get new carry */
accum=accum<<1; /* Do the shift */
if(*carry)
accum|=1; /* Insert previous carry */
*carry=new_carry;
mantissa[i]=accum; /* Return shifted value */
}
return;
}
/********************
** ShiftMantRight1 **
*********************
** Shift a mantissa right by 1 bit. Provides carry, as
** above
*/
static void ShiftMantRight1(uint16_t *carry,
uint16_t *mantissa)
{
int i; /* Index */
int new_carry;
uint16_t accum;
for(i=0;i<INTERNAL_FPF_PRECISION;i++)
{ accum=mantissa[i];
new_carry=accum & 1; /* Get new carry */
accum=accum>>1;
if(*carry)
accum|=0x8000;
*carry=new_carry;
mantissa[i]=accum;
}
return;
}
/*****************************
** StickyShiftMantRight **
******************************
** This is a shift right of the mantissa with a "sticky bit".
** I.E., if a carry of 1 is shifted out of the least significant
** bit, the least significant bit is set to 1.
*/
static void StickyShiftRightMant(InternalFPF *ptr,
int amount)
{
int i; /* Index */
uint16_t carry; /* Self-explanatory */
uint16_t *mantissa;
mantissa=ptr->mantissa;
if(ptr->type!=IFPF_IS_ZERO) /* Don't bother shifting a zero */
{
/*
** If the amount of shifting will shift everyting
** out of existence, then just clear the whole mantissa
** and set the lowmost bit to 1.
*/
if(amount>=INTERNAL_FPF_PRECISION * 16)
{
for(i=0;i<INTERNAL_FPF_PRECISION-1;i++)
mantissa[i]=0;
mantissa[INTERNAL_FPF_PRECISION-1]=1;
}
else
for(i=0;i<amount;i++)
{
carry=0;
ShiftMantRight1(&carry,mantissa);
if(carry)
mantissa[INTERNAL_FPF_PRECISION-1] |= 1;
}
}
return;
}
/**************************************************
** POST ARITHMETIC PROCESSING **
** (NORMALIZE, ROUND, OVERFLOW, AND UNDERFLOW) **
**************************************************/
/**************
** normalize **
***************
** Normalize an internal-representation number. Normalization
** discards empty most-significant bits.
*/
static void normalize(InternalFPF *ptr)
{
uint16_t carry;
/*
** As long as there's a highmost 0 bit, shift the significand
** left 1 bit. Each time you do this, though, you've
** gotta decrement the exponent.
*/
while ((ptr->mantissa[0] & 0x8000) == 0)
{
carry = 0;
ShiftMantLeft1(&carry, ptr->mantissa);
ptr->exp--;
}
return;
}
/****************
** denormalize **
*****************
** Denormalize an internal-representation number. This means
** shifting it right until its exponent is equivalent to
** minimum_exponent. (You have to do this often in order
** to perform additions and subtractions).
*/
static void denormalize(InternalFPF *ptr,
int minimum_exponent)
{
long exponent_difference;
if (IsMantissaZero(ptr->mantissa))
{
printf("Error: zero significand in denormalize\n");
}
exponent_difference = ptr->exp-minimum_exponent;
if (exponent_difference < 0)
{
/*
** The number is subnormal
*/
exponent_difference = -exponent_difference;
if (exponent_difference >= (INTERNAL_FPF_PRECISION * 16))
{
/* Underflow */
SetInternalFPFZero(ptr, ptr->sign);
}
else
{
ptr->exp+=exponent_difference;
StickyShiftRightMant(ptr, exponent_difference);
}
}
}
/*********************
** RoundInternalFPF **
**********************
** Round an internal-representation number.
** The kind of rounding we do here is simplest...referred to as
** "chop". "Extraneous" rightmost bits are simply hacked off.
*/
static void
RoundInternalFPF(InternalFPF *ptr)
{
/* int i; */
if (ptr->type == IFPF_IS_NORMAL ||
ptr->type == IFPF_IS_SUBNORMAL)
{
denormalize(ptr, MIN_EXP);
if (ptr->type != IFPF_IS_ZERO)
{
/* clear the extraneous bits */
ptr->mantissa[3] &= 0xfff8;
/* for (i=4; i<INTERNAL_FPF_PRECISION; i++)
{
ptr->mantissa[i] = 0;
}
*/
/*
** Check for overflow
*/
/* Does not do anything as ptr->exp is a short and MAX_EXP=37268
if (ptr->exp > MAX_EXP)
{
SetInternalFPFInfinity(ptr, ptr->sign);
}
*/
}
}
return;
}
/*******************************************************
** ARITHMETIC OPERATIONS ON INTERNAL REPRESENTATION **
*******************************************************/
/***************
** choose_nan **
****************
** Called by routines that are forced to perform math on
** a pair of NaN's. This routine "selects" which NaN is
** to be returned.
*/
static void choose_nan(InternalFPF *x,
InternalFPF *y,
InternalFPF *z,
int intel_flag)
{
int i;
/*
** Compare the two mantissas,
** return the larger. Note that we will be emulating
** an 80387 in this operation.
*/
for (i=0; i<INTERNAL_FPF_PRECISION; i++)
{
if (x->mantissa[i] > y->mantissa[i])
{
memmove((void *)x,(void *)z,sizeof(InternalFPF));
return;
}
if (x->mantissa[i] < y->mantissa[i])
{
memmove((void *)y,(void *)z,sizeof(InternalFPF));
return;
}
}
/*
** They are equal
*/
if (!intel_flag)
/* if the operation is addition */
memmove((void *)x,(void *)z,sizeof(InternalFPF));
else
/* if the operation is multiplication */
memmove((void *)y,(void *)z,sizeof(InternalFPF));
return;
}
/**********************
** AddSubInternalFPF **
***********************
** Adding or subtracting internal-representation numbers.
** Internal-representation numbers pointed to by x and y are
** added/subtracted and the result returned in z.
*/
void AddSubInternalFPF(unsigned char operation,
InternalFPF *x,
InternalFPF *y,
InternalFPF *z)
{
int exponent_difference;
uint16_t borrow;
uint16_t carry;
int i;
InternalFPF locx,locy; /* Needed since we alter them */
/*
** Following big switch statement handles the
** various combinations of operand types.
*/
switch ((x->type * IFPF_TYPE_COUNT) + y->type)
{
case ZERO_ZERO:
memmove((void *)x,(void *)z,sizeof(InternalFPF));
if (x->sign ^ y->sign ^ operation)
{
z->sign = 0; /* positive */
}
break;
case NAN_ZERO:
case NAN_SUBNORMAL:
case NAN_NORMAL:
case NAN_INFINITY:
case SUBNORMAL_ZERO:
case NORMAL_ZERO:
case INFINITY_ZERO:
case INFINITY_SUBNORMAL:
case INFINITY_NORMAL:
memmove((void *)x,(void *)z,sizeof(InternalFPF));
break;
case ZERO_NAN:
case SUBNORMAL_NAN:
case NORMAL_NAN:
case INFINITY_NAN:
memmove((void *)y,(void *)z,sizeof(InternalFPF));
break;
case ZERO_SUBNORMAL:
case ZERO_NORMAL:
case ZERO_INFINITY:
case SUBNORMAL_INFINITY:
case NORMAL_INFINITY:
memmove((void *)y,(void *)z,sizeof(InternalFPF));
z->sign ^= operation;
break;
case SUBNORMAL_SUBNORMAL:
case SUBNORMAL_NORMAL:
case NORMAL_SUBNORMAL:
case NORMAL_NORMAL:
/*
** Copy x and y to locals, since we may have
** to alter them.
*/
memmove((void *)&locx,(void *)x,sizeof(InternalFPF));
memmove((void *)&locy,(void *)y,sizeof(InternalFPF));
/* compute sum/difference */
exponent_difference = locx.exp-locy.exp;
if (exponent_difference == 0)
{
/*
** locx.exp == locy.exp
** so, no shifting required
*/
if (locx.type == IFPF_IS_SUBNORMAL ||
locy.type == IFPF_IS_SUBNORMAL)
z->type = IFPF_IS_SUBNORMAL;
else
z->type = IFPF_IS_NORMAL;
/*
** Assume that locx.mantissa > locy.mantissa
*/
z->sign = locx.sign;
z->exp= locx.exp;
}
else
if (exponent_difference > 0)
{
/*
** locx.exp > locy.exp
*/
StickyShiftRightMant(&locy,
exponent_difference);
z->type = locx.type;
z->sign = locx.sign;
z->exp = locx.exp;
}
else /* if (exponent_difference < 0) */
{
/*
** locx.exp < locy.exp
*/
StickyShiftRightMant(&locx,
-exponent_difference);
z->type = locy.type;
z->sign = locy.sign ^ operation;
z->exp = locy.exp;
}
if (locx.sign ^ locy.sign ^ operation)
{
/*
** Signs are different, subtract mantissas
*/
borrow = 0;
for (i=(INTERNAL_FPF_PRECISION-1); i>=0; i--)
Sub16Bits(&borrow,
&z->mantissa[i],
locx.mantissa[i],
locy.mantissa[i]);
if (borrow)
{
/* The y->mantissa was larger than the
** x->mantissa leaving a negative
** result. Change the result back to
** an unsigned number and flip the
** sign flag.
*/
z->sign = locy.sign ^ operation;
borrow = 0;
for (i=(INTERNAL_FPF_PRECISION-1); i>=0; i--)
{
Sub16Bits(&borrow,
&z->mantissa[i],
0,
z->mantissa[i]);
}
}
else
{
/* The assumption made above
** (i.e. x->mantissa >= y->mantissa)
** was correct. Therefore, do nothing.
** z->sign = x->sign;
*/
}
if (IsMantissaZero(z->mantissa))
{
z->type = IFPF_IS_ZERO;
z->sign = 0; /* positive */
}
else
if (locx.type == IFPF_IS_NORMAL ||
locy.type == IFPF_IS_NORMAL)
{
normalize(z);
}
}
else
{
/* signs are the same, add mantissas */
carry = 0;
for (i=(INTERNAL_FPF_PRECISION-1); i>=0; i--)
{
Add16Bits(&carry,
&z->mantissa[i],
locx.mantissa[i],
locy.mantissa[i]);
}
if (carry)
{
z->exp++;
carry=0;
ShiftMantRight1(&carry,z->mantissa);
z->mantissa[0] |= 0x8000;
z->type = IFPF_IS_NORMAL;
}
else
if (z->mantissa[0] & 0x8000)
z->type = IFPF_IS_NORMAL;
}
break;
case INFINITY_INFINITY:
SetInternalFPFNaN(z);
break;
case NAN_NAN:
choose_nan(x, y, z, 1);
break;
}
/*
** All the math is done; time to round.
*/
RoundInternalFPF(z);
return;
}
/************************
** MultiplyInternalFPF **
*************************
** Two internal-representation numbers x and y are multiplied; the
** result is returned in z.
*/
void MultiplyInternalFPF(InternalFPF *x,
InternalFPF *y,
InternalFPF *z)
{
int i;
int j;
uint16_t carry;
uint16_t extra_bits[INTERNAL_FPF_PRECISION];
InternalFPF locy; /* Needed since this will be altered */
/*
** As in the preceding function, this large switch
** statement selects among the many combinations
** of operands.
*/
switch ((x->type * IFPF_TYPE_COUNT) + y->type)
{
case INFINITY_SUBNORMAL:
case INFINITY_NORMAL:
case INFINITY_INFINITY:
case ZERO_ZERO:
case ZERO_SUBNORMAL:
case ZERO_NORMAL:
memmove((void *)x,(void *)z,sizeof(InternalFPF));
z->sign ^= y->sign;
break;
case SUBNORMAL_INFINITY:
case NORMAL_INFINITY:
case SUBNORMAL_ZERO:
case NORMAL_ZERO:
memmove((void *)y,(void *)z,sizeof(InternalFPF));
z->sign ^= x->sign;
break;
case ZERO_INFINITY:
case INFINITY_ZERO:
SetInternalFPFNaN(z);
break;
case NAN_ZERO:
case NAN_SUBNORMAL:
case NAN_NORMAL:
case NAN_INFINITY:
memmove((void *)x,(void *)z,sizeof(InternalFPF));
break;
case ZERO_NAN:
case SUBNORMAL_NAN:
case NORMAL_NAN:
case INFINITY_NAN:
memmove((void *)y,(void *)z,sizeof(InternalFPF));
break;
case SUBNORMAL_SUBNORMAL:
case SUBNORMAL_NORMAL:
case NORMAL_SUBNORMAL:
case NORMAL_NORMAL:
/*
** Make a local copy of the y number, since we will be
** altering it in the process of multiplying.
*/
memmove((void *)&locy,(void *)y,sizeof(InternalFPF));
/*
** Check for unnormal zero arguments
*/
if (IsMantissaZero(x->mantissa) || IsMantissaZero(y->mantissa))
SetInternalFPFInfinity(z, 0);
/*
** Initialize the result
*/
if (x->type == IFPF_IS_SUBNORMAL ||
y->type == IFPF_IS_SUBNORMAL)
z->type = IFPF_IS_SUBNORMAL;
else
z->type = IFPF_IS_NORMAL;
z->sign = x->sign ^ y->sign;
z->exp = x->exp + y->exp ;
for (i=0; i<INTERNAL_FPF_PRECISION; i++)
{
z->mantissa[i] = 0;
extra_bits[i] = 0;
}
for (i=0; i<(INTERNAL_FPF_PRECISION*16); i++)
{
/*
** Get rightmost bit of the multiplier
*/
carry = 0;
ShiftMantRight1(&carry, locy.mantissa);
if (carry)
{
/*
** Add the multiplicand to the product
*/
carry = 0;
for (j=(INTERNAL_FPF_PRECISION-1); j>=0; j--)
Add16Bits(&carry,
&z->mantissa[j],
z->mantissa[j],
x->mantissa[j]);
}
else
{
carry = 0;
}
/*
** Shift the product right. Overflow bits get
** shifted into extra_bits. We'll use it later
** to help with the "sticky" bit.
*/
ShiftMantRight1(&carry, z->mantissa);
ShiftMantRight1(&carry, extra_bits);
}
/*
** Normalize
** Note that we use a "special" normalization routine
** because we need to use the extra bits. (These are
** bits that may have been shifted off the bottom that
** we want to reclaim...if we can.
*/
while ((z->mantissa[0] & 0x8000) == 0)
{
carry = 0;
ShiftMantLeft1(&carry, extra_bits);
ShiftMantLeft1(&carry, z->mantissa);
z->exp--;
}
/*
** Set the sticky bit if any bits set in extra bits.
*/
if (IsMantissaZero(extra_bits))
{
z->mantissa[INTERNAL_FPF_PRECISION-1] |= 1;
}
break;
case NAN_NAN:
choose_nan(x, y, z, 0);
break;
}
/*
** All math done...do rounding.
*/
RoundInternalFPF(z);
return;
}
/**********************
** DivideInternalFPF **
***********************
** Divide internal FPF number x by y. Return result in z.
*/
void DivideInternalFPF(InternalFPF *x,
InternalFPF *y,
InternalFPF *z)
{
int i;
int j;
uint16_t carry;
uint16_t extra_bits[INTERNAL_FPF_PRECISION];
InternalFPF locx; /* Local for x number */
/*
** As with preceding function, the following switch
** statement selects among the various possible
** operands.
*/
switch ((x->type * IFPF_TYPE_COUNT) + y->type)
{
case ZERO_ZERO:
case INFINITY_INFINITY:
SetInternalFPFNaN(z);
break;
case ZERO_SUBNORMAL:
case ZERO_NORMAL:
if (IsMantissaZero(y->mantissa))
{
SetInternalFPFNaN(z);
break;
}
case ZERO_INFINITY:
case SUBNORMAL_INFINITY:
case NORMAL_INFINITY:
SetInternalFPFZero(z, x->sign ^ y->sign);
break;
case SUBNORMAL_ZERO:
case NORMAL_ZERO:
if (IsMantissaZero(x->mantissa))
{
SetInternalFPFNaN(z);
break;
}
case INFINITY_ZERO:
case INFINITY_SUBNORMAL:
case INFINITY_NORMAL:
SetInternalFPFInfinity(z, 0);
z->sign = x->sign ^ y->sign;
break;
case NAN_ZERO:
case NAN_SUBNORMAL:
case NAN_NORMAL:
case NAN_INFINITY:
memmove((void *)x,(void *)z,sizeof(InternalFPF));
break;
case ZERO_NAN:
case SUBNORMAL_NAN:
case NORMAL_NAN:
case INFINITY_NAN:
memmove((void *)y,(void *)z,sizeof(InternalFPF));
break;
case SUBNORMAL_SUBNORMAL:
case NORMAL_SUBNORMAL:
case SUBNORMAL_NORMAL:
case NORMAL_NORMAL:
/*
** Make local copy of x number, since we'll be
** altering it in the process of dividing.
*/
memmove((void *)&locx,(void *)x,sizeof(InternalFPF));
/*
** Check for unnormal zero arguments
*/
if (IsMantissaZero(locx.mantissa))
{
if (IsMantissaZero(y->mantissa))
SetInternalFPFNaN(z);
else
SetInternalFPFZero(z, 0);
break;
}
if (IsMantissaZero(y->mantissa))
{
SetInternalFPFInfinity(z, 0);
break;
}
/*
** Initialize the result
*/
z->type = x->type;
z->sign = x->sign ^ y->sign;
z->exp = x->exp - y->exp +
((INTERNAL_FPF_PRECISION * 16 * 2));
for (i=0; i<INTERNAL_FPF_PRECISION; i++)
{
z->mantissa[i] = 0;
extra_bits[i] = 0;
}
while ((z->mantissa[0] & 0x8000) == 0)
{
carry = 0;
ShiftMantLeft1(&carry, locx.mantissa);
ShiftMantLeft1(&carry, extra_bits);
/*
** Time to subtract yet?
*/
if (carry == 0)
for (j=0; j<INTERNAL_FPF_PRECISION; j++)
{
if (y->mantissa[j] > extra_bits[j])
{
carry = 0;
goto no_subtract;
}
if (y->mantissa[j] < extra_bits[j])
break;
}
/*
** Divisor (y) <= dividend (x), subtract
*/
carry = 0;
for (j=(INTERNAL_FPF_PRECISION-1); j>=0; j--)
Sub16Bits(&carry,
&extra_bits[j],
extra_bits[j],
y->mantissa[j]);
carry = 1; /* 1 shifted into quotient */
no_subtract:
ShiftMantLeft1(&carry, z->mantissa);
z->exp--;
}
break;
case NAN_NAN:
choose_nan(x, y, z, 0);
break;
}
/*
** Math complete...do rounding
*/
RoundInternalFPF(z);
}
/**********************
** Int32ToInternalFPF **
***********************
** Convert a signed (long) 32-bit integer into an internal FPF number.
*/
void Int32ToInternalFPF(int32_t mylong,
InternalFPF *dest)
{
int i; /* Index */
uint16_t myword; /* Used to hold converted stuff */
/*
** Save the sign and get the absolute value. This will help us
** with 64-bit machines, since we use only the lower 32
** bits just in case. (No longer necessary after we use int32.)
*/
/* if(mylong<0L) */
if(mylong<0)
{ dest->sign=1;
mylong=0-mylong;
}
else
dest->sign=0;
/*
** Prepare the destination floating point number
*/
dest->type=IFPF_IS_NORMAL;
for(i=0;i<INTERNAL_FPF_PRECISION;i++)
dest->mantissa[i]=0;
/*
** See if we've got a zero. If so, make the resultant FP
** number a true zero and go home.
*/
if(mylong==0)
{ dest->type=IFPF_IS_ZERO;
dest->exp=0;
return;
}
/*
** Not a true zero. Set the exponent to 32 (internal FPFs have
** no bias) and load the low and high words into their proper
** locations in the mantissa. Then normalize. The action of
** normalizing slides the mantissa bits into place and sets
** up the exponent properly.
*/
dest->exp=32;
myword=(uint16_t)((mylong >> 16) & 0xFFFFL);
dest->mantissa[0]=myword;
myword=(uint16_t)(mylong & 0xFFFFL);
dest->mantissa[1]=myword;
normalize(dest);
return;
}
#ifdef DEBUG
/************************
** InternalFPFToString **
*************************
** FOR DEBUG PURPOSES
** This routine converts an internal floating point representation
** number to a string. Used in debugging the package.
** Returns length of converted number.
** NOTE: dest must point to a buffer big enough to hold the
** result. Also, this routine does append a null (an effect
** of using the sprintf() function). It also returns
** a length count.
** NOTE: This routine returns 5 significant digits. Thats
** about all I feel safe with, given the method of
** conversion. It should be more than enough for programmers
** to determine whether the package is properly ported.
*/
static int InternalFPFToString(char *dest,
InternalFPF *src)
{
InternalFPF locFPFNum; /* Local for src (will be altered) */
InternalFPF IFPF10; /* Floating-point 10 */
InternalFPF IFPFComp; /* For doing comparisons */
int msign; /* Holding for mantissa sign */
int expcount; /* Exponent counter */
int ccount; /* Character counter */
int i,j,k; /* Index */
uint16_t carryaccum; /* Carry accumulator */
uint16_t mycarry; /* Local for carry */
/*
** Check first for the simple things...Nan, Infinity, Zero.
** If found, copy the proper string in and go home.
*/
switch(src->type)
{
case IFPF_IS_NAN:
memcpy(dest,"NaN",3);
return(3);
case IFPF_IS_INFINITY:
if(src->sign==0)
memcpy(dest,"+Inf",4);
else
memcpy(dest,"-Inf",4);
return(4);
case IFPF_IS_ZERO:
if(src->sign==0)
memcpy(dest,"+0",2);
else
memcpy(dest,"-0",2);
return(2);
}
/*
** Move the internal number into our local holding area, since
** we'll be altering it to print it out.
*/
memcpy((void *)&locFPFNum,(void *)src,sizeof(InternalFPF));
/*
** Set up a floating-point 10...which we'll use a lot in a minute.
*/
/* LongToInternalFPF(10L,&IFPF10); */
Int32ToInternalFPF((int32_t)10,&IFPF10);
/*
** Save the mantissa sign and make it positive.
*/
msign=src->sign;
/* src->sign=0 */ /* bug, fixed Nov. 13, 1997 */
(&locFPFNum)->sign=0;
expcount=0; /* Init exponent counter */
/*
** See if the number is less than 10. If so, multiply
** the number repeatedly by 10 until it's not. For each
** multiplication, decrement a counter so we can keep track
** of the exponent.
*/
while(1)
{ AddSubInternalFPF(1,&locFPFNum,&IFPF10,&IFPFComp);
if(IFPFComp.sign==0) break;
MultiplyInternalFPF(&locFPFNum,&IFPF10,&IFPFComp);
expcount--;
memcpy((void *)&locFPFNum,(void *)&IFPFComp,sizeof(InternalFPF));
}
/*
** Do the reverse of the above. As long as the number is
** greater than or equal to 10, divide it by 10. Increment the
** exponent counter for each multiplication.
*/
while(1)
{
AddSubInternalFPF(1,&locFPFNum,&IFPF10,&IFPFComp);
if(IFPFComp.sign!=0) break;
DivideInternalFPF(&locFPFNum,&IFPF10,&IFPFComp);
expcount++;
memcpy((void *)&locFPFNum,(void *)&IFPFComp,sizeof(InternalFPF));
}
/*
** About time to start storing things. First, store the
** mantissa sign.
*/
ccount=1; /* Init character counter */
if(msign==0)
*dest++='+';
else
*dest++='-';
/*
** At this point we know that the number is in the range
** 10 > n >=1. We need to "strip digits" out of the
** mantissa. We do this by treating the mantissa as
** an integer and multiplying by 10. (Not a floating-point
** 10, but an integer 10. Since this is debug code and we
** could care less about speed, we'll do it the stupid
** way and simply add the number to itself 10 times.
** Anything that makes it to the left of the implied binary point
** gets stripped off and emitted. We'll do this for
** 5 significant digits (which should be enough to
** verify things).
*/
/*
** Re-position radix point
*/
carryaccum=0;
while(locFPFNum.exp>0)
{
mycarry=0;
ShiftMantLeft1(&mycarry,locFPFNum.mantissa);
carryaccum=(carryaccum<<1);
if(mycarry) carryaccum++;
locFPFNum.exp--;
}
while(locFPFNum.exp<0)
{
mycarry=0;
ShiftMantRight1(&mycarry,locFPFNum.mantissa);
locFPFNum.exp++;
}
for(i=0;i<6;i++)
if(i==1)
{ /* Emit decimal point */
*dest++='.';
ccount++;
}
else
{ /* Emit a digit */
*dest++=('0'+carryaccum);
ccount++;
carryaccum=0;
memcpy((void *)&IFPF10,
(void *)&locFPFNum,
sizeof(InternalFPF));
/* Do multiply via repeated adds */
for(j=0;j<9;j++)
{
mycarry=0;
for(k=(INTERNAL_FPF_PRECISION-1);k>=0;k--)
Add16Bits(&mycarry,&(IFPFComp.mantissa[k]),
locFPFNum.mantissa[k],
IFPF10.mantissa[k]);
carryaccum+=mycarry ? 1 : 0;
memcpy((void *)&locFPFNum,
(void *)&IFPFComp,
sizeof(InternalFPF));
}
}
/*
** Now move the 'E', the exponent sign, and the exponent
** into the string.
*/
*dest++='E';
/* sprint is supposed to return an integer, but it caused problems on SunOS
* with the native cc. Hence we force it.
* Uwe F. Mayer
*/
ccount+=(int)sprintf(dest,"%4d",expcount);
/*
** All done, go home.
*/
return(ccount);
}
#endif
|