OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [i386/] [math-emu/] [poly_2xm1.c] - Blame information for rev 1777

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1623 jcastillo
/*---------------------------------------------------------------------------+
2
 |  poly_2xm1.c                                                              |
3
 |                                                                           |
4
 | Function to compute 2^x-1 by a polynomial approximation.                  |
5
 |                                                                           |
6
 | Copyright (C) 1992,1993,1994                                              |
7
 |                       W. Metzenthen, 22 Parker St, Ormond, Vic 3163,      |
8
 |                       Australia.  E-mail   billm@vaxc.cc.monash.edu.au    |
9
 |                                                                           |
10
 |                                                                           |
11
 +---------------------------------------------------------------------------*/
12
 
13
#include "exception.h"
14
#include "reg_constant.h"
15
#include "fpu_emu.h"
16
#include "control_w.h"
17
#include "poly.h"
18
 
19
 
20
#define HIPOWER 11
21
static const unsigned long long lterms[HIPOWER] =
22
{
23
  0x0000000000000000LL,  /* This term done separately as 12 bytes */
24
  0xf5fdeffc162c7543LL,
25
  0x1c6b08d704a0bfa6LL,
26
  0x0276556df749cc21LL,
27
  0x002bb0ffcf14f6b8LL,
28
  0x0002861225ef751cLL,
29
  0x00001ffcbfcd5422LL,
30
  0x00000162c005d5f1LL,
31
  0x0000000da96ccb1bLL,
32
  0x0000000078d1b897LL,
33
  0x000000000422b029LL
34
};
35
 
36
static const Xsig hiterm = MK_XSIG(0xb17217f7, 0xd1cf79ab, 0xc8a39194);
37
 
38
/* Four slices: 0.0 : 0.25 : 0.50 : 0.75 : 1.0,
39
   These numbers are 2^(1/4), 2^(1/2), and 2^(3/4)
40
 */
41
static const Xsig shiftterm0 = MK_XSIG(0, 0, 0);
42
static const Xsig shiftterm1 = MK_XSIG(0x9837f051, 0x8db8a96f, 0x46ad2318);
43
static const Xsig shiftterm2 = MK_XSIG(0xb504f333, 0xf9de6484, 0x597d89b3);
44
static const Xsig shiftterm3 = MK_XSIG(0xd744fcca, 0xd69d6af4, 0x39a68bb9);
45
 
46
static const Xsig *shiftterm[] = { &shiftterm0, &shiftterm1,
47
                                     &shiftterm2, &shiftterm3 };
48
 
49
 
50
/*--- poly_2xm1() -----------------------------------------------------------+
51
 | Requires an argument which is TW_Valid and < 1.                           |
52
 +---------------------------------------------------------------------------*/
53
int     poly_2xm1(FPU_REG const *arg, FPU_REG *result)
54
{
55
  long int               exponent, shift;
56
  unsigned long long     Xll;
57
  Xsig                   accumulator, Denom, argSignif;
58
 
59
 
60
  exponent = arg->exp - EXP_BIAS;
61
 
62
#ifdef PARANOID
63
  if (   (exponent >= 0)         /* Don't want a |number| >= 1.0 */
64
      || (arg->tag != TW_Valid) )
65
    {
66
      /* Number negative, too large, or not Valid. */
67
      EXCEPTION(EX_INTERNAL|0x127);
68
      return 1;
69
    }
70
#endif PARANOID
71
 
72
  argSignif.lsw = 0;
73
  XSIG_LL(argSignif) = Xll = significand(arg);
74
 
75
  if ( exponent == -1 )
76
    {
77
      shift = (argSignif.msw & 0x40000000) ? 3 : 2;
78
      /* subtract 0.5 or 0.75 */
79
      exponent -= 2;
80
      XSIG_LL(argSignif) <<= 2;
81
      Xll <<= 2;
82
    }
83
  else if ( exponent == -2 )
84
    {
85
      shift = 1;
86
      /* subtract 0.25 */
87
      exponent--;
88
      XSIG_LL(argSignif) <<= 1;
89
      Xll <<= 1;
90
    }
91
  else
92
    shift = 0;
93
 
94
  if ( exponent < -2 )
95
    {
96
      /* Shift the argument right by the required places. */
97
      if ( shrx(&Xll, -2-exponent) >= 0x80000000U )
98
        Xll++;  /* round up */
99
    }
100
 
101
  accumulator.lsw = accumulator.midw = accumulator.msw = 0;
102
  polynomial_Xsig(&accumulator, &Xll, lterms, HIPOWER-1);
103
  mul_Xsig_Xsig(&accumulator, &argSignif);
104
  shr_Xsig(&accumulator, 3);
105
 
106
  mul_Xsig_Xsig(&argSignif, &hiterm);   /* The leading term */
107
  add_two_Xsig(&accumulator, &argSignif, &exponent);
108
 
109
  if ( shift )
110
    {
111
      /* The argument is large, use the identity:
112
         f(x+a) = f(a) * (f(x) + 1) - 1;
113
         */
114
      shr_Xsig(&accumulator, - exponent);
115
      accumulator.msw |= 0x80000000;      /* add 1.0 */
116
      mul_Xsig_Xsig(&accumulator, shiftterm[shift]);
117
      accumulator.msw &= 0x3fffffff;      /* subtract 1.0 */
118
      exponent = 1;
119
    }
120
 
121
  if ( arg->sign != SIGN_POS )
122
    {
123
      /* The argument is negative, use the identity:
124
             f(-x) = -f(x) / (1 + f(x))
125
         */
126
      Denom.lsw = accumulator.lsw;
127
      XSIG_LL(Denom) = XSIG_LL(accumulator);
128
      if ( exponent < 0 )
129
        shr_Xsig(&Denom, - exponent);
130
      else if ( exponent > 0 )
131
        {
132
          /* exponent must be 1 here */
133
          XSIG_LL(Denom) <<= 1;
134
          if ( Denom.lsw & 0x80000000 )
135
            XSIG_LL(Denom) |= 1;
136
          (Denom.lsw) <<= 1;
137
        }
138
      Denom.msw |= 0x80000000;      /* add 1.0 */
139
      div_Xsig(&accumulator, &Denom, &accumulator);
140
    }
141
 
142
  /* Convert to 64 bit signed-compatible */
143
  exponent += round_Xsig(&accumulator);
144
 
145
  significand(result) = XSIG_LL(accumulator);
146
  result->tag = TW_Valid;
147
  result->exp = exponent + EXP_BIAS;
148
  result->sign = arg->sign;
149
 
150
  return 0;
151
 
152
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.