Previous 15994 Revisions Next

r12356 Thursday 28th July, 2011 at 01:56:55 UTC by R. Belmont
m68k: implemented FSIN, FCOS, and FTAN using SoftFloat extentions [Stanislav Shwartsman, R. Belmont]
[src/emu/cpu/m68000]m68kfpu.c
[src/lib/softfloat]fpu_constant.h* fsincos.c* softfloat-macros softfloat-specialize softfloat.c softfloat.h

src/emu/cpu/m68000/m68kfpu.c
r12355r12356
13181318         m68k->remaining_cycles -= 109;
13191319         break;
13201320      }
1321//      case 0x0e:      // FSIN
1322//      {
1323//          // TODO
1324//          break;
1325//      }
1321      case 0x0e:      // FSIN
1322      {
1323         REG_FP[dst] = source;
1324         floatx80_fsin(REG_FP[dst]);
1325         SET_CONDITION_CODES(m68k, REG_FP[dst]);
1326         m68k->remaining_cycles -= 75;
1327         break;
1328      }
1329      case 0x0f:      // FTAN
1330      {
1331         REG_FP[dst] = source;
1332         floatx80_ftan(REG_FP[dst]);
1333         SET_CONDITION_CODES(m68k, REG_FP[dst]);
1334         m68k->remaining_cycles -= 75;
1335         break;
1336      }
13261337      case 0x18:      // FABS
13271338      {
13281339         REG_FP[dst] = source;
r12355r12356
13391350         m68k->remaining_cycles -= 3;
13401351         break;
13411352      }
1353      case 0x1d:      // FCOS
1354      {
1355         REG_FP[dst] = source;
1356         floatx80_fcos(REG_FP[dst]);
1357         SET_CONDITION_CODES(m68k, REG_FP[dst]);
1358         m68k->remaining_cycles -= 75;
1359         break;
1360      }
13421361      case 0x1e:      // FGETEXP
13431362      {
1344//          floatx80 temp = source;
13451363         INT16 temp2;
13461364
13471365         temp2 = source.high;   // get the exponent
src/lib/softfloat/softfloat.c
r12355r12356
3737| Floating-point rounding mode, extended double-precision rounding precision,
3838| and exception flags.
3939*----------------------------------------------------------------------------*/
40int8 float_rounding_mode = float_round_nearest_even;
4140int8 float_exception_flags = 0;
4241#ifdef FLOATX80
4342int8 floatx80_rounding_precision = 80;
4443#endif
4544
46/*----------------------------------------------------------------------------
47| Primitive arithmetic functions, including multi-word arithmetic, and
48| division and square root approximations.  (Can be specialized to target if
49| desired.)
50*----------------------------------------------------------------------------*/
51#include "softfloat-macros"
45int8 float_rounding_mode = float_round_nearest_even;
5246
5347/*----------------------------------------------------------------------------
5448| Functions and definitions to determine:  (1) whether tininess for underflow
r12355r12356
489483#ifdef FLOATX80
490484
491485/*----------------------------------------------------------------------------
492| Returns the fraction bits of the extended double-precision floating-point
493| value `a'.
494*----------------------------------------------------------------------------*/
495
496INLINE bits64 extractFloatx80Frac( floatx80 a )
497{
498
499    return a.low;
500
501}
502
503/*----------------------------------------------------------------------------
504| Returns the exponent bits of the extended double-precision floating-point
505| value `a'.
506*----------------------------------------------------------------------------*/
507
508INLINE int32 extractFloatx80Exp( floatx80 a )
509{
510
511    return a.high & 0x7FFF;
512
513}
514
515/*----------------------------------------------------------------------------
516| Returns the sign bit of the extended double-precision floating-point value
517| `a'.
518*----------------------------------------------------------------------------*/
519
520INLINE flag extractFloatx80Sign( floatx80 a )
521{
522
523    return a.high>>15;
524
525}
526
527/*----------------------------------------------------------------------------
528486| Normalizes the subnormal extended double-precision floating-point value
529487| represented by the denormalized significand `aSig'.  The normalized exponent
530488| and significand are stored at the locations pointed to by `zExpPtr' and
r12355r12356
543501}
544502
545503/*----------------------------------------------------------------------------
546| Packs the sign `zSign', exponent `zExp', and significand `zSig' into an
547| extended double-precision floating-point value, returning the result.
548*----------------------------------------------------------------------------*/
549
550INLINE floatx80 packFloatx80( flag zSign, int32 zExp, bits64 zSig )
551{
552    floatx80 z;
553
554    z.low = zSig;
555    z.high = ( ( (bits16) zSign )<<15 ) + zExp;
556    return z;
557
558}
559
560/*----------------------------------------------------------------------------
561504| Takes an abstract floating-point value having sign `zSign', exponent `zExp',
562505| and extended significand formed by the concatenation of `zSig0' and `zSig1',
563506| and returns the proper extended double-precision floating-point value
r12355r12356
861804
862805}
863806
864/*----------------------------------------------------------------------------
865| Packs the sign `zSign', the exponent `zExp', and the significand formed
866| by the concatenation of `zSig0' and `zSig1' into a quadruple-precision
867| floating-point value, returning the result.  After being shifted into the
868| proper positions, the three fields `zSign', `zExp', and `zSig0' are simply
869| added together to form the most significant 32 bits of the result.  This
870| means that any integer portion of `zSig0' will be added into the exponent.
871| Since a properly normalized significand will have an integer portion equal
872| to 1, the `zExp' input should be 1 less than the desired result exponent
873| whenever `zSig0' and `zSig1' concatenated form a complete, normalized
874| significand.
875*----------------------------------------------------------------------------*/
876
877INLINE float128
878 packFloat128( flag zSign, int32 zExp, bits64 zSig0, bits64 zSig1 )
879{
880    float128 z;
881
882    z.low = zSig1;
883    z.high = ( ( (bits64) zSign )<<63 ) + ( ( (bits64) zExp )<<48 ) + zSig0;
884    return z;
885
886}
887
888/*----------------------------------------------------------------------------
889| Takes an abstract floating-point value having sign `zSign', exponent `zExp',
890| and extended significand formed by the concatenation of `zSig0', `zSig1',
891| and `zSig2', and returns the proper quadruple-precision floating-point value
892| corresponding to the abstract input.  Ordinarily, the abstract value is
893| simply rounded and packed into the quadruple-precision format, with the
894| inexact exception raised if the abstract input cannot be represented
895| exactly.  However, if the abstract value is too large, the overflow and
896| inexact exceptions are raised and an infinity or maximal finite value is
897| returned.  If the abstract value is too small, the input value is rounded to
898| a subnormal number, and the underflow and inexact exceptions are raised if
899| the abstract input cannot be represented exactly as a subnormal quadruple-
900| precision floating-point number.
901|     The input significand must be normalized or smaller.  If the input
902| significand is not normalized, `zExp' must be 0; in that case, the result
903| returned is a subnormal number, and it must not require rounding.  In the
904| usual case that the input significand is normalized, `zExp' must be 1 less
905| than the ``true'' floating-point exponent.  The handling of underflow and
906| overflow follows the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
907*----------------------------------------------------------------------------*/
908
909static float128
910 roundAndPackFloat128(
911     flag zSign, int32 zExp, bits64 zSig0, bits64 zSig1, bits64 zSig2 )
912{
913    int8 roundingMode;
914    flag roundNearestEven, increment, isTiny;
915
916    roundingMode = float_rounding_mode;
917    roundNearestEven = ( roundingMode == float_round_nearest_even );
918    increment = ( (sbits64) zSig2 < 0 );
919    if ( ! roundNearestEven ) {
920        if ( roundingMode == float_round_to_zero ) {
921            increment = 0;
922        }
923        else {
924            if ( zSign ) {
925                increment = ( roundingMode == float_round_down ) && zSig2;
926            }
927            else {
928                increment = ( roundingMode == float_round_up ) && zSig2;
929            }
930        }
931    }
932    if ( 0x7FFD <= (bits32) zExp ) {
933        if (    ( 0x7FFD < zExp )
934             || (    ( zExp == 0x7FFD )
935                  && eq128(
936                         LIT64( 0x0001FFFFFFFFFFFF ),
937                         LIT64( 0xFFFFFFFFFFFFFFFF ),
938                         zSig0,
939                         zSig1
940                     )
941                  && increment
942                )
943           ) {
944            float_raise( float_flag_overflow | float_flag_inexact );
945            if (    ( roundingMode == float_round_to_zero )
946                 || ( zSign && ( roundingMode == float_round_up ) )
947                 || ( ! zSign && ( roundingMode == float_round_down ) )
948               ) {
949                return
950                    packFloat128(
951                        zSign,
952                        0x7FFE,
953                        LIT64( 0x0000FFFFFFFFFFFF ),
954                        LIT64( 0xFFFFFFFFFFFFFFFF )
955                    );
956            }
957            return packFloat128( zSign, 0x7FFF, 0, 0 );
958        }
959        if ( zExp < 0 ) {
960            isTiny =
961                   ( float_detect_tininess == float_tininess_before_rounding )
962                || ( zExp < -1 )
963                || ! increment
964                || lt128(
965                       zSig0,
966                       zSig1,
967                       LIT64( 0x0001FFFFFFFFFFFF ),
968                       LIT64( 0xFFFFFFFFFFFFFFFF )
969                   );
970            shift128ExtraRightJamming(
971                zSig0, zSig1, zSig2, - zExp, &zSig0, &zSig1, &zSig2 );
972            zExp = 0;
973            if ( isTiny && zSig2 ) float_raise( float_flag_underflow );
974            if ( roundNearestEven ) {
975                increment = ( (sbits64) zSig2 < 0 );
976            }
977            else {
978                if ( zSign ) {
979                    increment = ( roundingMode == float_round_down ) && zSig2;
980                }
981                else {
982                    increment = ( roundingMode == float_round_up ) && zSig2;
983                }
984            }
985        }
986    }
987    if ( zSig2 ) float_exception_flags |= float_flag_inexact;
988    if ( increment ) {
989        add128( zSig0, zSig1, 0, 1, &zSig0, &zSig1 );
990        zSig1 &= ~ ( ( zSig2 + zSig2 == 0 ) & roundNearestEven );
991    }
992    else {
993        if ( ( zSig0 | zSig1 ) == 0 ) zExp = 0;
994    }
995    return packFloat128( zSign, zExp, zSig0, zSig1 );
996
997}
998
999/*----------------------------------------------------------------------------
1000| Takes an abstract floating-point value having sign `zSign', exponent `zExp',
1001| and significand formed by the concatenation of `zSig0' and `zSig1', and
1002| returns the proper quadruple-precision floating-point value corresponding
1003| to the abstract input.  This routine is just like `roundAndPackFloat128'
1004| except that the input significand has fewer bits and does not have to be
1005| normalized.  In all cases, `zExp' must be 1 less than the ``true'' floating-
1006| point exponent.
1007*----------------------------------------------------------------------------*/
1008
1009static float128
1010 normalizeRoundAndPackFloat128(
1011     flag zSign, int32 zExp, bits64 zSig0, bits64 zSig1 )
1012{
1013    int8 shiftCount;
1014    bits64 zSig2;
1015
1016    if ( zSig0 == 0 ) {
1017        zSig0 = zSig1;
1018        zSig1 = 0;
1019        zExp -= 64;
1020    }
1021    shiftCount = countLeadingZeros64( zSig0 ) - 15;
1022    if ( 0 <= shiftCount ) {
1023        zSig2 = 0;
1024        shortShift128Left( zSig0, zSig1, shiftCount, &zSig0, &zSig1 );
1025    }
1026    else {
1027        shift128ExtraRightJamming(
1028            zSig0, zSig1, 0, - shiftCount, &zSig0, &zSig1, &zSig2 );
1029    }
1030    zExp -= shiftCount;
1031    return roundAndPackFloat128( zSign, zExp, zSig0, zSig1, zSig2 );
1032
1033}
1034
1035807#endif
1036808
1037809/*----------------------------------------------------------------------------
src/lib/softfloat/softfloat-macros
r12355r12356
543543| unsigned integer is returned.
544544*----------------------------------------------------------------------------*/
545545
546static bits64 estimateDiv128To64( bits64 a0, bits64 a1, bits64 b )
546INLINE bits64 estimateDiv128To64( bits64 a0, bits64 a1, bits64 b )
547547{
548548    bits64 b0, b1;
549549    bits64 rem0, rem1, term0, term1;
r12355r12356
575575| value.
576576*----------------------------------------------------------------------------*/
577577
578static bits32 estimateSqrt32( int16 aExp, bits32 a )
578INLINE bits32 estimateSqrt32( int16 aExp, bits32 a )
579579{
580580    static const bits16 sqrtOddAdjustments[] = {
581581        0x0004, 0x0022, 0x005D, 0x00B1, 0x011D, 0x019F, 0x0236, 0x02E0,
r12355r12356
718718
719719}
720720
721/*-----------------------------------------------------------------------------
722| Changes the sign of the extended double-precision floating-point value 'a'.
723| The operation is performed according to the IEC/IEEE Standard for Binary
724| Floating-Point Arithmetic.
725*----------------------------------------------------------------------------*/
726
727INLINE floatx80 floatx80_chs(floatx80 reg)
728{
729    reg.high ^= 0x8000;
730    return reg;
731}
732
src/lib/softfloat/fsincos.c
r0r12356
1/*============================================================================
2This source file is an extension to the SoftFloat IEC/IEEE Floating-point
3Arithmetic Package, Release 2b, written for Bochs (x86 achitecture simulator)
4floating point emulation.
5
6THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE.  Although reasonable effort has
7been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT TIMES
8RESULT IN INCORRECT BEHAVIOR.  USE OF THIS SOFTWARE IS RESTRICTED TO PERSONS
9AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ALL LOSSES,
10COSTS, OR OTHER PROBLEMS THEY INCUR DUE TO THE SOFTWARE, AND WHO FURTHERMORE
11EFFECTIVELY INDEMNIFY JOHN HAUSER AND THE INTERNATIONAL COMPUTER SCIENCE
12INSTITUTE (possibly via similar legal warning) AGAINST ALL LOSSES, COSTS, OR
13OTHER PROBLEMS INCURRED BY THEIR CUSTOMERS AND CLIENTS DUE TO THE SOFTWARE.
14
15Derivative works are acceptable, even for commercial purposes, so long as
16(1) the source code for the derivative work includes prominent notice that
17the work is derivative, and (2) the source code includes prominent notice with
18these four paragraphs for those parts of this code that are retained.
19=============================================================================*/
20
21/*============================================================================
22 * Written for Bochs (x86 achitecture simulator) by
23 *            Stanislav Shwartsman [sshwarts at sourceforge net]
24 * ==========================================================================*/
25
26#define FLOAT128
27
28#define USE_estimateDiv128To64
29#include "mamesf.h"
30#include "softfloat.h"
31//#include "softfloat-specialize"
32#include "fpu_constant.h"
33
34static const floatx80 floatx80_one = packFloatx80(0, 0x3fff, U64(0x8000000000000000));
35static const floatx80 floatx80_default_nan = packFloatx80(0, 0xffff, U64(0xffffffffffffffff));
36
37#define packFloat2x128m(zHi, zLo) {(zLo), (zHi)}
38#define PACK_FLOAT_128(hi,lo) packFloat2x128m(LIT64(hi),LIT64(lo))
39
40#define EXP_BIAS 0x3FFF
41
42/*----------------------------------------------------------------------------
43| Returns the fraction bits of the extended double-precision floating-point
44| value `a'.
45*----------------------------------------------------------------------------*/
46
47INLINE bits64 extractFloatx80Frac( floatx80 a )
48{
49
50    return a.low;
51
52}
53
54/*----------------------------------------------------------------------------
55| Returns the exponent bits of the extended double-precision floating-point
56| value `a'.
57*----------------------------------------------------------------------------*/
58
59INLINE int32 extractFloatx80Exp( floatx80 a )
60{
61
62    return a.high & 0x7FFF;
63
64}
65
66/*----------------------------------------------------------------------------
67| Returns the sign bit of the extended double-precision floating-point value
68| `a'.
69*----------------------------------------------------------------------------*/
70
71INLINE flag extractFloatx80Sign( floatx80 a )
72{
73
74    return a.high>>15;
75
76}
77
78/*----------------------------------------------------------------------------
79| Takes extended double-precision floating-point  NaN  `a' and returns the
80| appropriate NaN result. If `a' is a signaling NaN, the invalid exception
81| is raised.
82*----------------------------------------------------------------------------*/
83
84INLINE floatx80 propagateFloatx80NaNOneArg(floatx80 a)
85{
86    if (floatx80_is_signaling_nan(a))
87        float_raise(float_flag_invalid);
88
89    a.low |= U64(0xC000000000000000);
90
91    return a;
92}
93
94/*----------------------------------------------------------------------------
95| Normalizes the subnormal extended double-precision floating-point value
96| represented by the denormalized significand `aSig'.  The normalized exponent
97| and significand are stored at the locations pointed to by `zExpPtr' and
98| `zSigPtr', respectively.
99*----------------------------------------------------------------------------*/
100
101void normalizeFloatx80Subnormal(UINT64 aSig, INT32 *zExpPtr, UINT64 *zSigPtr)
102{
103    int shiftCount = countLeadingZeros64(aSig);
104    *zSigPtr = aSig<<shiftCount;
105    *zExpPtr = 1 - shiftCount;
106}
107
108/* reduce trigonometric function argument using 128-bit precision
109   M_PI approximation */
110static UINT64 argument_reduction_kernel(UINT64 aSig0, int Exp, UINT64 *zSig0, UINT64 *zSig1)
111{
112    UINT64 term0, term1, term2;
113    UINT64 aSig1 = 0;
114
115    shortShift128Left(aSig1, aSig0, Exp, &aSig1, &aSig0);
116    UINT64 q = estimateDiv128To64(aSig1, aSig0, FLOAT_PI_HI);
117    mul128By64To192(FLOAT_PI_HI, FLOAT_PI_LO, q, &term0, &term1, &term2);
118    sub128(aSig1, aSig0, term0, term1, zSig1, zSig0);
119    while ((INT64)(*zSig1) < 0) {
120        --q;
121        add192(*zSig1, *zSig0, term2, 0, FLOAT_PI_HI, FLOAT_PI_LO, zSig1, zSig0, &term2);
122    }
123    *zSig1 = term2;
124    return q;
125}
126
127static int reduce_trig_arg(int expDiff, int &zSign, UINT64 &aSig0, UINT64 &aSig1)
128{
129    UINT64 term0, term1, q = 0;
130
131    if (expDiff < 0) {
132        shift128Right(aSig0, 0, 1, &aSig0, &aSig1);
133        expDiff = 0;
134    }
135    if (expDiff > 0) {
136        q = argument_reduction_kernel(aSig0, expDiff, &aSig0, &aSig1);
137    }
138    else {
139        if (FLOAT_PI_HI <= aSig0) {
140            aSig0 -= FLOAT_PI_HI;
141            q = 1;
142        }
143    }
144
145    shift128Right(FLOAT_PI_HI, FLOAT_PI_LO, 1, &term0, &term1);
146    if (! lt128(aSig0, aSig1, term0, term1))
147    {
148        int lt = lt128(term0, term1, aSig0, aSig1);
149        int eq = eq128(aSig0, aSig1, term0, term1);
150
151        if ((eq && (q & 1)) || lt) {
152            zSign = !zSign;
153            ++q;
154        }
155        if (lt) sub128(FLOAT_PI_HI, FLOAT_PI_LO, aSig0, aSig1, &aSig0, &aSig1);
156    }
157
158    return (int)(q & 3);
159}
160
161#define SIN_ARR_SIZE 11
162#define COS_ARR_SIZE 11
163
164static float128 sin_arr[SIN_ARR_SIZE] =
165{
166    PACK_FLOAT_128(0x3fff000000000000, 0x0000000000000000), /*  1 */
167    PACK_FLOAT_128(0xbffc555555555555, 0x5555555555555555), /*  3 */
168    PACK_FLOAT_128(0x3ff8111111111111, 0x1111111111111111), /*  5 */
169    PACK_FLOAT_128(0xbff2a01a01a01a01, 0xa01a01a01a01a01a), /*  7 */
170    PACK_FLOAT_128(0x3fec71de3a556c73, 0x38faac1c88e50017), /*  9 */
171    PACK_FLOAT_128(0xbfe5ae64567f544e, 0x38fe747e4b837dc7), /* 11 */
172    PACK_FLOAT_128(0x3fde6124613a86d0, 0x97ca38331d23af68), /* 13 */
173    PACK_FLOAT_128(0xbfd6ae7f3e733b81, 0xf11d8656b0ee8cb0), /* 15 */
174    PACK_FLOAT_128(0x3fce952c77030ad4, 0xa6b2605197771b00), /* 17 */
175    PACK_FLOAT_128(0xbfc62f49b4681415, 0x724ca1ec3b7b9675), /* 19 */
176    PACK_FLOAT_128(0x3fbd71b8ef6dcf57, 0x18bef146fcee6e45)  /* 21 */
177};
178
179static float128 cos_arr[COS_ARR_SIZE] =
180{
181    PACK_FLOAT_128(0x3fff000000000000, 0x0000000000000000), /*  0 */
182    PACK_FLOAT_128(0xbffe000000000000, 0x0000000000000000), /*  2 */
183    PACK_FLOAT_128(0x3ffa555555555555, 0x5555555555555555), /*  4 */
184    PACK_FLOAT_128(0xbff56c16c16c16c1, 0x6c16c16c16c16c17), /*  6 */
185    PACK_FLOAT_128(0x3fefa01a01a01a01, 0xa01a01a01a01a01a), /*  8 */
186    PACK_FLOAT_128(0xbfe927e4fb7789f5, 0xc72ef016d3ea6679), /* 10 */
187    PACK_FLOAT_128(0x3fe21eed8eff8d89, 0x7b544da987acfe85), /* 12 */
188    PACK_FLOAT_128(0xbfda93974a8c07c9, 0xd20badf145dfa3e5), /* 14 */
189    PACK_FLOAT_128(0x3fd2ae7f3e733b81, 0xf11d8656b0ee8cb0), /* 16 */
190    PACK_FLOAT_128(0xbfca6827863b97d9, 0x77bb004886a2c2ab), /* 18 */
191    PACK_FLOAT_128(0x3fc1e542ba402022, 0x507a9cad2bf8f0bb)  /* 20 */
192};
193
194extern float128 OddPoly (float128 x, float128 *arr, unsigned n);
195
196/* 0 <= x <= pi/4 */
197INLINE float128 poly_sin(float128 x)
198{
199    //                 3     5     7     9     11     13     15
200    //                x     x     x     x     x      x      x
201    // sin (x) ~ x - --- + --- - --- + --- - ---- + ---- - ---- =
202    //                3!    5!    7!    9!    11!    13!    15!
203    //
204    //                 2     4     6     8     10     12     14
205    //                x     x     x     x     x      x      x
206    //   = x * [ 1 - --- + --- - --- + --- - ---- + ---- - ---- ] =
207    //                3!    5!    7!    9!    11!    13!    15!
208    //
209    //           3                          3
210    //          --       4k                --        4k+2
211    //   p(x) = >  C  * x   > 0     q(x) = >  C   * x     < 0
212    //          --  2k                     --  2k+1
213    //          k=0                        k=0
214    //
215    //                          2
216    //   sin(x) ~ x * [ p(x) + x * q(x) ]
217    //
218
219    return OddPoly(x, sin_arr, SIN_ARR_SIZE);
220}
221
222extern float128 EvenPoly(float128 x, float128 *arr, unsigned n);
223
224/* 0 <= x <= pi/4 */
225INLINE float128 poly_cos(float128 x)
226{
227    //                 2     4     6     8     10     12     14
228    //                x     x     x     x     x      x      x
229    // cos (x) ~ 1 - --- + --- - --- + --- - ---- + ---- - ----
230    //                2!    4!    6!    8!    10!    12!    14!
231    //
232    //           3                          3
233    //          --       4k                --        4k+2
234    //   p(x) = >  C  * x   > 0     q(x) = >  C   * x     < 0
235    //          --  2k                     --  2k+1
236    //          k=0                        k=0
237    //
238    //                      2
239    //   cos(x) ~ [ p(x) + x * q(x) ]
240    //
241
242    return EvenPoly(x, cos_arr, COS_ARR_SIZE);
243}
244
245INLINE void sincos_invalid(floatx80 *sin_a, floatx80 *cos_a, floatx80 a)
246{
247    if (sin_a) *sin_a = a;
248    if (cos_a) *cos_a = a;
249}
250
251INLINE void sincos_tiny_argument(floatx80 *sin_a, floatx80 *cos_a, floatx80 a)
252{
253    if (sin_a) *sin_a = a;
254    if (cos_a) *cos_a = floatx80_one;
255}
256
257static floatx80 sincos_approximation(int neg, float128 r, UINT64 quotient)
258{
259    if (quotient & 0x1) {
260        r = poly_cos(r);
261        neg = 0;
262    } else  {
263        r = poly_sin(r);
264    }
265
266    floatx80 result = float128_to_floatx80(r);
267    if (quotient & 0x2)
268        neg = ! neg;
269
270    if (neg)
271        floatx80_chs(result);
272
273    return result;
274}
275
276// =================================================
277// SFFSINCOS               Compute sin(x) and cos(x)
278// =================================================
279
280//
281// Uses the following identities:
282// ----------------------------------------------------------
283//
284//  sin(-x) = -sin(x)
285//  cos(-x) =  cos(x)
286//
287//  sin(x+y) = sin(x)*cos(y)+cos(x)*sin(y)
288//  cos(x+y) = sin(x)*sin(y)+cos(x)*cos(y)
289//
290//  sin(x+ pi/2)  =  cos(x)
291//  sin(x+ pi)    = -sin(x)
292//  sin(x+3pi/2)  = -cos(x)
293//  sin(x+2pi)    =  sin(x)
294//
295
296int sf_fsincos(floatx80 a, floatx80 *sin_a, floatx80 *cos_a)
297{
298    UINT64 aSig0, aSig1 = 0;
299    INT32 aExp, zExp, expDiff;
300    int aSign, zSign;
301    int q = 0;
302
303    aSig0 = extractFloatx80Frac(a);
304    aExp = extractFloatx80Exp(a);
305    aSign = extractFloatx80Sign(a);
306
307    /* invalid argument */
308    if (aExp == 0x7FFF) {
309        if ((UINT64) (aSig0<<1)) {
310            sincos_invalid(sin_a, cos_a, propagateFloatx80NaNOneArg(a));
311            return 0;
312        }
313
314        float_raise(float_flag_invalid);
315        sincos_invalid(sin_a, cos_a, floatx80_default_nan);
316        return 0;
317    }
318
319    if (aExp == 0) {
320        if (aSig0 == 0) {
321            sincos_tiny_argument(sin_a, cos_a, a);
322            return 0;
323        }
324
325//        float_raise(float_flag_denormal);
326
327        /* handle pseudo denormals */
328        if (! (aSig0 & U64(0x8000000000000000)))
329        {
330            float_raise(float_flag_inexact);
331            if (sin_a)
332                float_raise(float_flag_underflow);
333            sincos_tiny_argument(sin_a, cos_a, a);
334            return 0;
335        }
336
337        normalizeFloatx80Subnormal(aSig0, &aExp, &aSig0);
338    }
339
340    zSign = aSign;
341    zExp = EXP_BIAS;
342    expDiff = aExp - zExp;
343
344    /* argument is out-of-range */
345    if (expDiff >= 63)
346        return -1;
347
348    float_raise(float_flag_inexact);
349
350    if (expDiff < -1) {    // doesn't require reduction
351        if (expDiff <= -68) {
352            a = packFloatx80(aSign, aExp, aSig0);
353            sincos_tiny_argument(sin_a, cos_a, a);
354            return 0;
355        }
356        zExp = aExp;
357    }
358    else {
359        q = reduce_trig_arg(expDiff, zSign, aSig0, aSig1);
360    }
361
362    /* **************************** */
363    /* argument reduction completed */
364    /* **************************** */
365
366    /* using float128 for approximation */
367    float128 r = normalizeRoundAndPackFloat128(0, zExp-0x10, aSig0, aSig1);
368
369    if (aSign) q = -q;
370    if (sin_a) *sin_a = sincos_approximation(zSign, r,   q);
371    if (cos_a) *cos_a = sincos_approximation(zSign, r, q+1);
372
373    return 0;
374}
375
376int floatx80_fsin(floatx80 &a)
377{
378    return sf_fsincos(a, &a, 0);
379}
380
381int floatx80_fcos(floatx80 &a)
382{
383    return sf_fsincos(a, 0, &a);
384}
385
386// =================================================
387// FPTAN                 Compute tan(x)
388// =================================================
389
390//
391// Uses the following identities:
392//
393// 1. ----------------------------------------------------------
394//
395//  sin(-x) = -sin(x)
396//  cos(-x) =  cos(x)
397//
398//  sin(x+y) = sin(x)*cos(y)+cos(x)*sin(y)
399//  cos(x+y) = sin(x)*sin(y)+cos(x)*cos(y)
400//
401//  sin(x+ pi/2)  =  cos(x)
402//  sin(x+ pi)    = -sin(x)
403//  sin(x+3pi/2)  = -cos(x)
404//  sin(x+2pi)    =  sin(x)
405//
406// 2. ----------------------------------------------------------
407//
408//           sin(x)
409//  tan(x) = ------
410//           cos(x)
411//
412
413int floatx80_ftan(floatx80 &a)
414{
415    UINT64 aSig0, aSig1 = 0;
416    INT32 aExp, zExp, expDiff;
417    int aSign, zSign;
418    int q = 0;
419
420    aSig0 = extractFloatx80Frac(a);
421    aExp = extractFloatx80Exp(a);
422    aSign = extractFloatx80Sign(a);
423
424    /* invalid argument */
425    if (aExp == 0x7FFF) {
426        if ((UINT64) (aSig0<<1))
427        {
428            a = propagateFloatx80NaNOneArg(a);
429            return 0;
430        }
431
432        float_raise(float_flag_invalid);
433        a = floatx80_default_nan;
434        return 0;
435    }
436
437    if (aExp == 0) {
438        if (aSig0 == 0) return 0;
439//        float_raise(float_flag_denormal);
440        /* handle pseudo denormals */
441        if (! (aSig0 & U64(0x8000000000000000)))
442        {
443            float_raise(float_flag_inexact | float_flag_underflow);
444            return 0;
445        }
446        normalizeFloatx80Subnormal(aSig0, &aExp, &aSig0);
447    }
448
449    zSign = aSign;
450    zExp = EXP_BIAS;
451    expDiff = aExp - zExp;
452
453    /* argument is out-of-range */
454    if (expDiff >= 63)
455        return -1;
456
457    float_raise(float_flag_inexact);
458
459    if (expDiff < -1) {    // doesn't require reduction
460        if (expDiff <= -68) {
461            a = packFloatx80(aSign, aExp, aSig0);
462            return 0;
463        }
464        zExp = aExp;
465    }
466    else {
467        q = reduce_trig_arg(expDiff, zSign, aSig0, aSig1);
468    }
469
470    /* **************************** */
471    /* argument reduction completed */
472    /* **************************** */
473
474    /* using float128 for approximation */
475    float128 r = normalizeRoundAndPackFloat128(0, zExp-0x10, aSig0, aSig1);
476
477    float128 sin_r = poly_sin(r);
478    float128 cos_r = poly_cos(r);
479
480    if (q & 0x1) {
481        r = float128_div(cos_r, sin_r);
482        zSign = ! zSign;
483    } else {
484        r = float128_div(sin_r, cos_r);
485    }
486
487    a = float128_to_floatx80(r);
488    if (zSign)
489        floatx80_chs(a);
490
491    return 0;
492}
493
494//                            2         3         4               n
495// f(x) ~ C + (C * x) + (C * x) + (C * x) + (C * x) + ... + (C * x)
496//         0    1         2         3         4               n
497//
498//          --       2k                --        2k+1
499//   p(x) = >  C  * x           q(x) = >  C   * x
500//          --  2k                     --  2k+1
501//
502//   f(x) ~ [ p(x) + x * q(x) ]
503//
504
505float128 EvalPoly(float128 x, float128 *arr, unsigned n)
506{
507    float128 x2 = float128_mul(x, x);
508    unsigned i;
509
510    assert(n > 1);
511
512    float128 r1 = arr[--n];
513    i = n;
514    while(i >= 2) {
515        r1 = float128_mul(r1, x2);
516        i -= 2;
517        r1 = float128_add(r1, arr[i]);
518    }
519    if (i) r1 = float128_mul(r1, x);
520
521    float128 r2 = arr[--n];
522    i = n;
523    while(i >= 2) {
524        r2 = float128_mul(r2, x2);
525        i -= 2;
526        r2 = float128_add(r2, arr[i]);
527    }
528    if (i) r2 = float128_mul(r2, x);
529
530    return float128_add(r1, r2);
531}
532
533//                  2         4         6         8               2n
534// f(x) ~ C + (C * x) + (C * x) + (C * x) + (C * x) + ... + (C * x)
535//         0    1         2         3         4               n
536//
537//          --       4k                --        4k+2
538//   p(x) = >  C  * x           q(x) = >  C   * x
539//          --  2k                     --  2k+1
540//
541//                    2
542//   f(x) ~ [ p(x) + x * q(x) ]
543//
544
545float128 EvenPoly(float128 x, float128 *arr, unsigned n)
546{
547     return EvalPoly(float128_mul(x, x), arr, n);
548}
549
550//                        3         5         7         9               2n+1
551// f(x) ~ (C * x) + (C * x) + (C * x) + (C * x) + (C * x) + ... + (C * x)
552//          0         1         2         3         4               n
553//                        2         4         6         8               2n
554//      = x * [ C + (C * x) + (C * x) + (C * x) + (C * x) + ... + (C * x)
555//               0    1         2         3         4               n
556//
557//          --       4k                --        4k+2
558//   p(x) = >  C  * x           q(x) = >  C   * x
559//          --  2k                     --  2k+1
560//
561//                        2
562//   f(x) ~ x * [ p(x) + x * q(x) ]
563//
564
565float128 OddPoly(float128 x, float128 *arr, unsigned n)
566{
567     return float128_mul(x, EvenPoly(x, arr, n));
568}
569
src/lib/softfloat/softfloat-specialize
r12355r12356
331331
332332}
333333
334#define EXP_BIAS 0x3FFF
335
336/*----------------------------------------------------------------------------
337| Returns the fraction bits of the extended double-precision floating-point
338| value `a'.
339*----------------------------------------------------------------------------*/
340
341INLINE bits64 extractFloatx80Frac( floatx80 a )
342{
343
344    return a.low;
345
346}
347
348/*----------------------------------------------------------------------------
349| Returns the exponent bits of the extended double-precision floating-point
350| value `a'.
351*----------------------------------------------------------------------------*/
352
353INLINE int32 extractFloatx80Exp( floatx80 a )
354{
355
356    return a.high & 0x7FFF;
357
358}
359
360/*----------------------------------------------------------------------------
361| Returns the sign bit of the extended double-precision floating-point value
362| `a'.
363*----------------------------------------------------------------------------*/
364
365INLINE flag extractFloatx80Sign( floatx80 a )
366{
367
368    return a.high>>15;
369
370}
371
334372#endif
335373
336374#ifdef FLOAT128
src/lib/softfloat/softfloat.h
r12355r12356
5858#endif
5959
6060/*----------------------------------------------------------------------------
61| Primitive arithmetic functions, including multi-word arithmetic, and
62| division and square root approximations.  (Can be specialized to target if
63| desired.)
64*----------------------------------------------------------------------------*/
65#include "softfloat-macros"
66
67/*----------------------------------------------------------------------------
6168| Software IEC/IEEE floating-point underflow tininess-detection mode.
6269*----------------------------------------------------------------------------*/
6370extern int8 float_detect_tininess;
r12355r12356
197204#endif
198205
199206/*----------------------------------------------------------------------------
207| Packs the sign `zSign', exponent `zExp', and significand `zSig' into an
208| extended double-precision floating-point value, returning the result.
209*----------------------------------------------------------------------------*/
210
211INLINE floatx80 packFloatx80( flag zSign, int32 zExp, bits64 zSig )
212{
213    floatx80 z;
214
215    z.low = zSig;
216    z.high = ( ( (bits16) zSign )<<15 ) + zExp;
217    return z;
218
219}
220
221/*----------------------------------------------------------------------------
200222| Software IEC/IEEE extended double-precision rounding precision.  Valid
201223| values are 32, 64, and 80.
202224*----------------------------------------------------------------------------*/
r12355r12356
220242flag floatx80_lt_quiet( floatx80, floatx80 );
221243flag floatx80_is_signaling_nan( floatx80 );
222244
245int floatx80_fsin(floatx80 &a);
246int floatx80_fcos(floatx80 &a);
247int floatx80_ftan(floatx80 &a);
248
223249#endif
224250
225251#ifdef FLOAT128
r12355r12356
255281flag float128_lt_quiet( float128, float128 );
256282flag float128_is_signaling_nan( float128 );
257283
284/*----------------------------------------------------------------------------
285| Packs the sign `zSign', the exponent `zExp', and the significand formed
286| by the concatenation of `zSig0' and `zSig1' into a quadruple-precision
287| floating-point value, returning the result.  After being shifted into the
288| proper positions, the three fields `zSign', `zExp', and `zSig0' are simply
289| added together to form the most significant 32 bits of the result.  This
290| means that any integer portion of `zSig0' will be added into the exponent.
291| Since a properly normalized significand will have an integer portion equal
292| to 1, the `zExp' input should be 1 less than the desired result exponent
293| whenever `zSig0' and `zSig1' concatenated form a complete, normalized
294| significand.
295*----------------------------------------------------------------------------*/
296
297INLINE float128
298 packFloat128( flag zSign, int32 zExp, bits64 zSig0, bits64 zSig1 )
299{
300    float128 z;
301
302    z.low = zSig1;
303    z.high = ( ( (bits64) zSign )<<63 ) + ( ( (bits64) zExp )<<48 ) + zSig0;
304    return z;
305
306}
307
308/*----------------------------------------------------------------------------
309| Takes an abstract floating-point value having sign `zSign', exponent `zExp',
310| and extended significand formed by the concatenation of `zSig0', `zSig1',
311| and `zSig2', and returns the proper quadruple-precision floating-point value
312| corresponding to the abstract input.  Ordinarily, the abstract value is
313| simply rounded and packed into the quadruple-precision format, with the
314| inexact exception raised if the abstract input cannot be represented
315| exactly.  However, if the abstract value is too large, the overflow and
316| inexact exceptions are raised and an infinity or maximal finite value is
317| returned.  If the abstract value is too small, the input value is rounded to
318| a subnormal number, and the underflow and inexact exceptions are raised if
319| the abstract input cannot be represented exactly as a subnormal quadruple-
320| precision floating-point number.
321|     The input significand must be normalized or smaller.  If the input
322| significand is not normalized, `zExp' must be 0; in that case, the result
323| returned is a subnormal number, and it must not require rounding.  In the
324| usual case that the input significand is normalized, `zExp' must be 1 less
325| than the ``true'' floating-point exponent.  The handling of underflow and
326| overflow follows the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
327*----------------------------------------------------------------------------*/
328
329INLINE float128
330 roundAndPackFloat128(
331     flag zSign, int32 zExp, bits64 zSig0, bits64 zSig1, bits64 zSig2 )
332{
333    int8 roundingMode;
334    flag roundNearestEven, increment, isTiny;
335
336    roundingMode = float_rounding_mode;
337    roundNearestEven = ( roundingMode == float_round_nearest_even );
338    increment = ( (sbits64) zSig2 < 0 );
339    if ( ! roundNearestEven ) {
340        if ( roundingMode == float_round_to_zero ) {
341            increment = 0;
342        }
343        else {
344            if ( zSign ) {
345                increment = ( roundingMode == float_round_down ) && zSig2;
346            }
347            else {
348                increment = ( roundingMode == float_round_up ) && zSig2;
349            }
350        }
351    }
352    if ( 0x7FFD <= (bits32) zExp ) {
353        if (    ( 0x7FFD < zExp )
354             || (    ( zExp == 0x7FFD )
355                  && eq128(
356                         LIT64( 0x0001FFFFFFFFFFFF ),
357                         LIT64( 0xFFFFFFFFFFFFFFFF ),
358                         zSig0,
359                         zSig1
360                     )
361                  && increment
362                )
363           ) {
364            float_raise( float_flag_overflow | float_flag_inexact );
365            if (    ( roundingMode == float_round_to_zero )
366                 || ( zSign && ( roundingMode == float_round_up ) )
367                 || ( ! zSign && ( roundingMode == float_round_down ) )
368               ) {
369                return
370                    packFloat128(
371                        zSign,
372                        0x7FFE,
373                        LIT64( 0x0000FFFFFFFFFFFF ),
374                        LIT64( 0xFFFFFFFFFFFFFFFF )
375                    );
376            }
377            return packFloat128( zSign, 0x7FFF, 0, 0 );
378        }
379        if ( zExp < 0 ) {
380            isTiny =
381                   ( float_detect_tininess == float_tininess_before_rounding )
382                || ( zExp < -1 )
383                || ! increment
384                || lt128(
385                       zSig0,
386                       zSig1,
387                       LIT64( 0x0001FFFFFFFFFFFF ),
388                       LIT64( 0xFFFFFFFFFFFFFFFF )
389                   );
390            shift128ExtraRightJamming(
391                zSig0, zSig1, zSig2, - zExp, &zSig0, &zSig1, &zSig2 );
392            zExp = 0;
393            if ( isTiny && zSig2 ) float_raise( float_flag_underflow );
394            if ( roundNearestEven ) {
395                increment = ( (sbits64) zSig2 < 0 );
396            }
397            else {
398                if ( zSign ) {
399                    increment = ( roundingMode == float_round_down ) && zSig2;
400                }
401                else {
402                    increment = ( roundingMode == float_round_up ) && zSig2;
403                }
404            }
405        }
406    }
407    if ( zSig2 ) float_exception_flags |= float_flag_inexact;
408    if ( increment ) {
409        add128( zSig0, zSig1, 0, 1, &zSig0, &zSig1 );
410        zSig1 &= ~ ( ( zSig2 + zSig2 == 0 ) & roundNearestEven );
411    }
412    else {
413        if ( ( zSig0 | zSig1 ) == 0 ) zExp = 0;
414    }
415    return packFloat128( zSign, zExp, zSig0, zSig1 );
416
417}
418
419/*----------------------------------------------------------------------------
420| Takes an abstract floating-point value having sign `zSign', exponent `zExp',
421| and significand formed by the concatenation of `zSig0' and `zSig1', and
422| returns the proper quadruple-precision floating-point value corresponding
423| to the abstract input.  This routine is just like `roundAndPackFloat128'
424| except that the input significand has fewer bits and does not have to be
425| normalized.  In all cases, `zExp' must be 1 less than the ``true'' floating-
426| point exponent.
427*----------------------------------------------------------------------------*/
428
429INLINE float128
430 normalizeRoundAndPackFloat128(
431     flag zSign, int32 zExp, bits64 zSig0, bits64 zSig1 )
432{
433    int8 shiftCount;
434    bits64 zSig2;
435
436    if ( zSig0 == 0 ) {
437        zSig0 = zSig1;
438        zSig1 = 0;
439        zExp -= 64;
440    }
441    shiftCount = countLeadingZeros64( zSig0 ) - 15;
442    if ( 0 <= shiftCount ) {
443        zSig2 = 0;
444        shortShift128Left( zSig0, zSig1, shiftCount, &zSig0, &zSig1 );
445    }
446    else {
447        shift128ExtraRightJamming(
448            zSig0, zSig1, 0, - shiftCount, &zSig0, &zSig1, &zSig2 );
449    }
450    zExp -= shiftCount;
451    return roundAndPackFloat128( zSign, zExp, zSig0, zSig1, zSig2 );
452
453}
454
258455#endif
259456
src/lib/softfloat/fpu_constant.h
r0r12356
1/*============================================================================
2This source file is an extension to the SoftFloat IEC/IEEE Floating-point
3Arithmetic Package, Release 2b, written for Bochs (x86 achitecture simulator)
4floating point emulation.
5
6THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE.  Although reasonable effort has
7been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT TIMES
8RESULT IN INCORRECT BEHAVIOR.  USE OF THIS SOFTWARE IS RESTRICTED TO PERSONS
9AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ALL LOSSES,
10COSTS, OR OTHER PROBLEMS THEY INCUR DUE TO THE SOFTWARE, AND WHO FURTHERMORE
11EFFECTIVELY INDEMNIFY JOHN HAUSER AND THE INTERNATIONAL COMPUTER SCIENCE
12INSTITUTE (possibly via similar legal warning) AGAINST ALL LOSSES, COSTS, OR
13OTHER PROBLEMS INCURRED BY THEIR CUSTOMERS AND CLIENTS DUE TO THE SOFTWARE.
14
15Derivative works are acceptable, even for commercial purposes, so long as
16(1) the source code for the derivative work includes prominent notice that
17the work is derivative, and (2) the source code includes prominent notice with
18these four paragraphs for those parts of this code that are retained.
19=============================================================================*/
20
21#ifndef _FPU_CONSTANTS_H_
22#define _FPU_CONSTANTS_H_
23
24// Pentium CPU uses only 68-bit precision M_PI approximation
25#define BETTER_THAN_PENTIUM
26
27/*============================================================================
28 * Written for Bochs (x86 achitecture simulator) by
29 *            Stanislav Shwartsman [sshwarts at sourceforge net]
30 * ==========================================================================*/
31
32//////////////////////////////
33// PI, PI/2, PI/4 constants
34//////////////////////////////
35
36#define FLOATX80_PI_EXP  (0x4000)
37
38// 128-bit PI fraction
39#ifdef BETTER_THAN_PENTIUM
40#define FLOAT_PI_HI (U64(0xc90fdaa22168c234))
41#define FLOAT_PI_LO (U64(0xc4c6628b80dc1cd1))
42#else
43#define FLOAT_PI_HI (U64(0xc90fdaa22168c234))
44#define FLOAT_PI_LO (U64(0xC000000000000000))
45#endif
46
47#define FLOATX80_PI2_EXP  (0x3FFF)
48#define FLOATX80_PI4_EXP  (0x3FFE)
49
50//////////////////////////////
51// 3PI/4 constant
52//////////////////////////////
53
54#define FLOATX80_3PI4_EXP (0x4000)
55
56// 128-bit 3PI/4 fraction
57#ifdef BETTER_THAN_PENTIUM
58#define FLOAT_3PI4_HI (U64(0x96cbe3f9990e91a7))
59#define FLOAT_3PI4_LO (U64(0x9394c9e8a0a5159c))
60#else
61#define FLOAT_3PI4_HI (U64(0x96cbe3f9990e91a7))
62#define FLOAT_3PI4_LO (U64(0x9000000000000000))
63#endif
64
65//////////////////////////////
66// 1/LN2 constant
67//////////////////////////////
68
69#define FLOAT_LN2INV_EXP  (0x3FFF)
70
71// 128-bit 1/LN2 fraction
72#ifdef BETTER_THAN_PENTIUM
73#define FLOAT_LN2INV_HI (U64(0xb8aa3b295c17f0bb))
74#define FLOAT_LN2INV_LO (U64(0xbe87fed0691d3e89))
75#else
76#define FLOAT_LN2INV_HI (U64(0xb8aa3b295c17f0bb))
77#define FLOAT_LN2INV_LO (U64(0xC000000000000000))
78#endif
79
80#endif

Previous 15994 Revisions Next


© 1998-2018 The MESS Team