Linux Audio

Check our new training course

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