summaryrefslogtreecommitdiff
path: root/sys/gnu/arch/i386/fpemul/poly_l2.c
blob: e50a9e59df3f3d3a31a71fbe6ee20f79f4a5f680 (plain)
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
/*	$OpenBSD: poly_l2.c,v 1.1 1996/08/27 10:32:54 downsj Exp $	*/
/*
 *  poly_l2.c
 *
 * Compute the base 2 log of a FPU_REG, using a polynomial approximation.
 *
 *
 * Copyright (C) 1992,1993,1994
 *                       W. Metzenthen, 22 Parker St, Ormond, Vic 3163,
 *                       Australia.  E-mail   billm@vaxc.cc.monash.edu.au
 * All rights reserved.
 *
 * This copyright notice covers the redistribution and use of the
 * FPU emulator developed by W. Metzenthen. It covers only its use
 * in the 386BSD, FreeBSD and NetBSD operating systems. Any other
 * use is not permitted under this copyright.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must include information specifying
 *    that source code for the emulator is freely available and include
 *    either:
 *      a) an offer to provide the source code for a nominal distribution
 *         fee, or
 *      b) list at least two alternative methods whereby the source
 *         can be obtained, e.g. a publically accessible bulletin board
 *         and an anonymous ftp site from which the software can be
 *         downloaded.
 * 3. All advertising materials specifically mentioning features or use of
 *    this emulator must acknowledge that it was developed by W. Metzenthen.
 * 4. The name of W. Metzenthen may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * W. METZENTHEN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 * The purpose of this copyright, based upon the Berkeley copyright, is to
 * ensure that the covered software remains freely available to everyone.
 *
 * The software (with necessary differences) is also available, but under
 * the terms of the GNU copyleft, for the Linux operating system and for
 * the djgpp ms-dos extender.
 *
 * W. Metzenthen   June 1994.
 *
 *
 *     $FreeBSD: poly_l2.c,v 1.5 1994/06/10 07:44:38 rich Exp $
 *
 */


#include <gnu/arch/i386/fpemul/exception.h>
#include <gnu/arch/i386/fpemul/reg_constant.h>
#include <gnu/arch/i386/fpemul/fpu_emu.h>
#include <gnu/arch/i386/fpemul/control_w.h>



#define	HIPOWER	9
static unsigned short lterms[HIPOWER][4] =
{
	/* Ideal computation with these coeffs gives about 64.6 bit rel
	 * accuracy. */
	{0xe177, 0xb82f, 0x7652, 0x7154},
	{0xee0f, 0xe80f, 0x2770, 0x7b1c},
	{0x0fc0, 0xbe87, 0xb143, 0x49dd},
	{0x78b9, 0xdadd, 0xec54, 0x34c2},
	{0x003a, 0x5de9, 0x628b, 0x2909},
	{0x5588, 0xed16, 0x4abf, 0x2193},
	{0xb461, 0x85f7, 0x347a, 0x1c6a},
	{0x0975, 0x87b3, 0xd5bf, 0x1876},
	{0xe85c, 0xcec9, 0x84e7, 0x187d}
};




/*--- poly_l2() -------------------------------------------------------------+
 |   Base 2 logarithm by a polynomial approximation.                         |
 +---------------------------------------------------------------------------*/
void
poly_l2(FPU_REG * arg, FPU_REG * result)
{
	short   exponent;
	char    zero;		/* flag for an Xx == 0 */
	unsigned short bits, shift;
	long long Xsq;
	FPU_REG accum, denom, num, Xx;


	exponent = arg->exp - EXP_BIAS;

	accum.tag = TW_Valid;	/* set the tags to Valid */

	if (arg->sigh > (unsigned) 0xb504f334) {
		/* This is good enough for the computation of the polynomial
		 * sum, but actually results in a loss of precision for the
		 * computation of Xx. This will matter only if exponent
		 * becomes zero. */
		exponent++;
		accum.sign = 1;	/* sign to negative */
		num.exp = EXP_BIAS;	/* needed to prevent errors in div
					 * routine */
		reg_u_div(&CONST_1, arg, &num, FULL_PRECISION);
	} else {
		accum.sign = 0;	/* set the sign to positive */
		num.sigl = arg->sigl;	/* copy the mantissa */
		num.sigh = arg->sigh;
	}


	/* shift num left, lose the ms bit */
	num.sigh <<= 1;
	if (num.sigl & 0x80000000)
		num.sigh |= 1;
	num.sigl <<= 1;

	denom.sigl = num.sigl;
	denom.sigh = num.sigh;
	poly_div4((long long *) &(denom.sigl));
	denom.sigh += 0x80000000;	/* set the msb */
	Xx.exp = EXP_BIAS;	/* needed to prevent errors in div routine */
	reg_u_div(&num, &denom, &Xx, FULL_PRECISION);

	zero = !(Xx.sigh | Xx.sigl);

	mul64((long long *) &Xx.sigl, (long long *) &Xx.sigl, &Xsq);
	poly_div16(&Xsq);

	accum.exp = -1;		/* exponent of accum */

	/* Do the basic fixed point polynomial evaluation */
	polynomial((unsigned *) &accum.sigl, (unsigned *) &Xsq, lterms, HIPOWER - 1);

	if (!exponent) {
		/* If the exponent is zero, then we would lose precision by
		 * sticking to fixed point computation here */
		/* We need to re-compute Xx because of loss of precision. */
		FPU_REG lXx;
		char    sign;

		sign = accum.sign;
		accum.sign = 0;

		/* make accum compatible and normalize */
		accum.exp = EXP_BIAS + accum.exp;
		normalize(&accum);

		if (zero) {
			reg_move(&CONST_Z, result);
		} else {
			/* we need to re-compute lXx to better accuracy */
			num.tag = TW_Valid;	/* set the tags to Vaild */
			num.sign = 0;	/* set the sign to positive */
			num.exp = EXP_BIAS - 1;
			if (sign) {
				/* The argument is of the form 1-x */
				/* Use  1-1/(1-x) = x/(1-x) */
				*((long long *) &num.sigl) = -*((long long *) &(arg->sigl));
				normalize(&num);
				reg_div(&num, arg, &num, FULL_PRECISION);
			} else {
				normalize(&num);
			}

			denom.tag = TW_Valid;	/* set the tags to Valid */
			denom.sign = SIGN_POS;	/* set the sign to positive */
			denom.exp = EXP_BIAS;

			reg_div(&num, &denom, &lXx, FULL_PRECISION);

			reg_u_mul(&lXx, &accum, &accum, FULL_PRECISION);

			reg_u_add(&lXx, &accum, result, FULL_PRECISION);

			normalize(result);
		}

		result->sign = sign;
		return;
	}
	mul64((long long *) &accum.sigl,
	    (long long *) &Xx.sigl, (long long *) &accum.sigl);

	*((long long *) (&accum.sigl)) += *((long long *) (&Xx.sigl));

	if (Xx.sigh > accum.sigh) {
		/* There was an overflow */

		poly_div2((long long *) &accum.sigl);
		accum.sigh |= 0x80000000;
		accum.exp++;
	}
	/* When we add the exponent to the accum result later, we will require
	 * that their signs are the same. Here we ensure that this is so. */
	if (exponent && ((exponent < 0) ^ (accum.sign))) {
		/* signs are different */

		accum.sign = !accum.sign;

		/* An exceptional case is when accum is zero */
		if (accum.sigl | accum.sigh) {
			/* find 1-accum */
			/* Shift to get exponent == 0 */
			if (accum.exp < 0) {
				poly_div2((long long *) &accum.sigl);
				accum.exp++;
			}
			/* Just negate, but throw away the sign */
			*((long long *) &(accum.sigl)) = -*((long long *) &(accum.sigl));
			if (exponent < 0)
				exponent++;
			else
				exponent--;
		}
	}
	shift = exponent >= 0 ? exponent : -exponent;
	bits = 0;
	if (shift) {
		if (accum.exp) {
			accum.exp++;
			poly_div2((long long *) &accum.sigl);
		}
		while (shift) {
			poly_div2((long long *) &accum.sigl);
			if (shift & 1)
				accum.sigh |= 0x80000000;
			shift >>= 1;
			bits++;
		}
	}
	/* Convert to 64 bit signed-compatible */
	accum.exp += bits + EXP_BIAS - 1;

	reg_move(&accum, result);
	normalize(result);

	return;
}


/*--- poly_l2p1() -----------------------------------------------------------+
 |   Base 2 logarithm by a polynomial approximation.                         |
 |   log2(x+1)                                                               |
 +---------------------------------------------------------------------------*/
int
poly_l2p1(FPU_REG * arg, FPU_REG * result)
{
	char    sign = 0;
	long long Xsq;
	FPU_REG arg_pl1, denom, accum, local_arg, poly_arg;


	sign = arg->sign;

	reg_add(arg, &CONST_1, &arg_pl1, FULL_PRECISION);

	if ((arg_pl1.sign) | (arg_pl1.tag)) {	/* We need a valid positive
						 * number! */
		return 1;
	}
	reg_add(&CONST_1, &arg_pl1, &denom, FULL_PRECISION);
	reg_div(arg, &denom, &local_arg, FULL_PRECISION);
	local_arg.sign = 0;	/* Make the sign positive */

	/* Now we need to check that  |local_arg| is less than 3-2*sqrt(2) =
	 * 0.17157.. = .0xafb0ccc0 * 2^-2 */

	if (local_arg.exp >= EXP_BIAS - 3) {
		if ((local_arg.exp > EXP_BIAS - 3) ||
		    (local_arg.sigh > (unsigned) 0xafb0ccc0)) {
			/* The argument is large */
			poly_l2(&arg_pl1, result);
			return 0;
		}
	}
	/* Make a copy of local_arg */
	reg_move(&local_arg, &poly_arg);

	/* Get poly_arg bits aligned as required */
	shrx((unsigned *) &(poly_arg.sigl), -(poly_arg.exp - EXP_BIAS + 3));

	mul64((long long *) &(poly_arg.sigl), (long long *) &(poly_arg.sigl), &Xsq);
	poly_div16(&Xsq);

	/* Do the basic fixed point polynomial evaluation */
	polynomial((u_int *) &accum.sigl, (unsigned *) &Xsq, lterms, HIPOWER - 1);

	accum.tag = TW_Valid;	/* set the tags to Valid */
	accum.sign = SIGN_POS;	/* and make accum positive */

	/* make accum compatible and normalize */
	accum.exp = EXP_BIAS - 1;
	normalize(&accum);

	reg_u_mul(&local_arg, &accum, &accum, FULL_PRECISION);

	reg_u_add(&local_arg, &accum, result, FULL_PRECISION);

	/* Multiply the result by 2 */
	result->exp++;

	result->sign = sign;

	return 0;
}