Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*---------------------------------------------------------------------------+
0003  |  poly_2xm1.c                                                              |
0004  |                                                                           |
0005  | Function to compute 2^x-1 by a polynomial approximation.                  |
0006  |                                                                           |
0007  | Copyright (C) 1992,1993,1994,1997                                         |
0008  |                  W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
0009  |                  E-mail   billm@suburbia.net                              |
0010  |                                                                           |
0011  |                                                                           |
0012  +---------------------------------------------------------------------------*/
0013 
0014 #include "exception.h"
0015 #include "reg_constant.h"
0016 #include "fpu_emu.h"
0017 #include "fpu_system.h"
0018 #include "control_w.h"
0019 #include "poly.h"
0020 
0021 #define HIPOWER 11
0022 static const unsigned long long lterms[HIPOWER] = {
0023     0x0000000000000000LL,   /* This term done separately as 12 bytes */
0024     0xf5fdeffc162c7543LL,
0025     0x1c6b08d704a0bfa6LL,
0026     0x0276556df749cc21LL,
0027     0x002bb0ffcf14f6b8LL,
0028     0x0002861225ef751cLL,
0029     0x00001ffcbfcd5422LL,
0030     0x00000162c005d5f1LL,
0031     0x0000000da96ccb1bLL,
0032     0x0000000078d1b897LL,
0033     0x000000000422b029LL
0034 };
0035 
0036 static const Xsig hiterm = MK_XSIG(0xb17217f7, 0xd1cf79ab, 0xc8a39194);
0037 
0038 /* Four slices: 0.0 : 0.25 : 0.50 : 0.75 : 1.0,
0039    These numbers are 2^(1/4), 2^(1/2), and 2^(3/4)
0040  */
0041 static const Xsig shiftterm0 = MK_XSIG(0, 0, 0);
0042 static const Xsig shiftterm1 = MK_XSIG(0x9837f051, 0x8db8a96f, 0x46ad2318);
0043 static const Xsig shiftterm2 = MK_XSIG(0xb504f333, 0xf9de6484, 0x597d89b3);
0044 static const Xsig shiftterm3 = MK_XSIG(0xd744fcca, 0xd69d6af4, 0x39a68bb9);
0045 
0046 static const Xsig *shiftterm[] = { &shiftterm0, &shiftterm1,
0047     &shiftterm2, &shiftterm3
0048 };
0049 
0050 /*--- poly_2xm1() -----------------------------------------------------------+
0051  | Requires st(0) which is TAG_Valid and < 1.                                |
0052  +---------------------------------------------------------------------------*/
0053 int poly_2xm1(u_char sign, FPU_REG *arg, FPU_REG *result)
0054 {
0055     long int exponent, shift;
0056     unsigned long long Xll;
0057     Xsig accumulator, Denom, argSignif;
0058     u_char tag;
0059 
0060     exponent = exponent16(arg);
0061 
0062 #ifdef PARANOID
0063     if (exponent >= 0) {    /* Don't want a |number| >= 1.0 */
0064         /* Number negative, too large, or not Valid. */
0065         EXCEPTION(EX_INTERNAL | 0x127);
0066         return 1;
0067     }
0068 #endif /* PARANOID */
0069 
0070     argSignif.lsw = 0;
0071     XSIG_LL(argSignif) = Xll = significand(arg);
0072 
0073     if (exponent == -1) {
0074         shift = (argSignif.msw & 0x40000000) ? 3 : 2;
0075         /* subtract 0.5 or 0.75 */
0076         exponent -= 2;
0077         XSIG_LL(argSignif) <<= 2;
0078         Xll <<= 2;
0079     } else if (exponent == -2) {
0080         shift = 1;
0081         /* subtract 0.25 */
0082         exponent--;
0083         XSIG_LL(argSignif) <<= 1;
0084         Xll <<= 1;
0085     } else
0086         shift = 0;
0087 
0088     if (exponent < -2) {
0089         /* Shift the argument right by the required places. */
0090         if (FPU_shrx(&Xll, -2 - exponent) >= 0x80000000U)
0091             Xll++;  /* round up */
0092     }
0093 
0094     accumulator.lsw = accumulator.midw = accumulator.msw = 0;
0095     polynomial_Xsig(&accumulator, &Xll, lterms, HIPOWER - 1);
0096     mul_Xsig_Xsig(&accumulator, &argSignif);
0097     shr_Xsig(&accumulator, 3);
0098 
0099     mul_Xsig_Xsig(&argSignif, &hiterm); /* The leading term */
0100     add_two_Xsig(&accumulator, &argSignif, &exponent);
0101 
0102     if (shift) {
0103         /* The argument is large, use the identity:
0104            f(x+a) = f(a) * (f(x) + 1) - 1;
0105          */
0106         shr_Xsig(&accumulator, -exponent);
0107         accumulator.msw |= 0x80000000;  /* add 1.0 */
0108         mul_Xsig_Xsig(&accumulator, shiftterm[shift]);
0109         accumulator.msw &= 0x3fffffff;  /* subtract 1.0 */
0110         exponent = 1;
0111     }
0112 
0113     if (sign != SIGN_POS) {
0114         /* The argument is negative, use the identity:
0115            f(-x) = -f(x) / (1 + f(x))
0116          */
0117         Denom.lsw = accumulator.lsw;
0118         XSIG_LL(Denom) = XSIG_LL(accumulator);
0119         if (exponent < 0)
0120             shr_Xsig(&Denom, -exponent);
0121         else if (exponent > 0) {
0122             /* exponent must be 1 here */
0123             XSIG_LL(Denom) <<= 1;
0124             if (Denom.lsw & 0x80000000)
0125                 XSIG_LL(Denom) |= 1;
0126             (Denom.lsw) <<= 1;
0127         }
0128         Denom.msw |= 0x80000000;    /* add 1.0 */
0129         div_Xsig(&accumulator, &Denom, &accumulator);
0130     }
0131 
0132     /* Convert to 64 bit signed-compatible */
0133     exponent += round_Xsig(&accumulator);
0134 
0135     result = &st(0);
0136     significand(result) = XSIG_LL(accumulator);
0137     setexponent16(result, exponent);
0138 
0139     tag = FPU_round(result, 1, 0, FULL_PRECISION, sign);
0140 
0141     setsign(result, sign);
0142     FPU_settag0(tag);
0143 
0144     return 0;
0145 
0146 }