^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*---------------------------------------------------------------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) | poly_atan.c |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) | |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) | Compute the arctan of a FPU_REG, using a polynomial approximation. |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) | |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) | Copyright (C) 1992,1993,1994,1997 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) | E-mail billm@suburbia.net |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) | |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) | |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) +---------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "exception.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "reg_constant.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "fpu_emu.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "fpu_system.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "status_w.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "control_w.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "poly.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define HIPOWERon 6 /* odd poly, negative terms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static const unsigned long long oddnegterms[HIPOWERon] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) 0x0000000000000000LL, /* Dummy (not for - 1.0) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) 0x015328437f756467LL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) 0x0005dda27b73dec6LL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) 0x0000226bf2bfb91aLL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) 0x000000ccc439c5f7LL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) 0x0000000355438407LL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define HIPOWERop 6 /* odd poly, positive terms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static const unsigned long long oddplterms[HIPOWERop] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* 0xaaaaaaaaaaaaaaabLL, transferred to fixedpterm[] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) 0x0db55a71875c9ac2LL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) 0x0029fce2d67880b0LL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) 0x0000dfd3908b4596LL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) 0x00000550fd61dab4LL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) 0x0000001c9422b3f9LL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) 0x000000003e3301e1LL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static const unsigned long long denomterm = 0xebd9b842c5c53a0eLL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static const Xsig fixedpterm = MK_XSIG(0xaaaaaaaa, 0xaaaaaaaa, 0xaaaaaaaa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static const Xsig pi_signif = MK_XSIG(0xc90fdaa2, 0x2168c234, 0xc4c6628b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /*--- poly_atan() -----------------------------------------------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) | |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) +---------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) void poly_atan(FPU_REG *st0_ptr, u_char st0_tag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) FPU_REG *st1_ptr, u_char st1_tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) u_char transformed, inverted, sign1, sign2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int exponent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) long int dummy_exp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) Xsig accumulator, Numer, Denom, accumulatore, argSignif, argSq, argSqSq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) u_char tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) sign1 = getsign(st0_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) sign2 = getsign(st1_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (st0_tag == TAG_Valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) exponent = exponent(st0_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* This gives non-compatible stack contents... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) FPU_to_exp16(st0_ptr, st0_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) exponent = exponent16(st0_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (st1_tag == TAG_Valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) exponent -= exponent(st1_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /* This gives non-compatible stack contents... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) FPU_to_exp16(st1_ptr, st1_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) exponent -= exponent16(st1_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if ((exponent < 0) || ((exponent == 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ((st0_ptr->sigh < st1_ptr->sigh) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) ((st0_ptr->sigh == st1_ptr->sigh) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) (st0_ptr->sigl < st1_ptr->sigl))))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) inverted = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) Numer.lsw = Denom.lsw = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) XSIG_LL(Numer) = significand(st0_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) XSIG_LL(Denom) = significand(st1_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) inverted = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) exponent = -exponent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) Numer.lsw = Denom.lsw = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) XSIG_LL(Numer) = significand(st1_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) XSIG_LL(Denom) = significand(st0_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) div_Xsig(&Numer, &Denom, &argSignif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) exponent += norm_Xsig(&argSignif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if ((exponent >= -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) || ((exponent == -2) && (argSignif.msw > 0xd413ccd0))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* The argument is greater than sqrt(2)-1 (=0.414213562...) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /* Convert the argument by an identity for atan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) transformed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (exponent >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #ifdef PARANOID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (!((exponent == 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) (argSignif.lsw == 0) && (argSignif.midw == 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) (argSignif.msw == 0x80000000))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) EXCEPTION(EX_INTERNAL | 0x104); /* There must be a logic error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #endif /* PARANOID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) argSignif.msw = 0; /* Make the transformed arg -> 0.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) Numer.lsw = Denom.lsw = argSignif.lsw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) XSIG_LL(Numer) = XSIG_LL(Denom) = XSIG_LL(argSignif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (exponent < -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) shr_Xsig(&Numer, -1 - exponent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) negate_Xsig(&Numer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) shr_Xsig(&Denom, -exponent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) Denom.msw |= 0x80000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) div_Xsig(&Numer, &Denom, &argSignif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) exponent = -1 + norm_Xsig(&argSignif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) transformed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) argSq.lsw = argSignif.lsw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) argSq.midw = argSignif.midw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) argSq.msw = argSignif.msw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) mul_Xsig_Xsig(&argSq, &argSq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) argSqSq.lsw = argSq.lsw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) argSqSq.midw = argSq.midw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) argSqSq.msw = argSq.msw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) mul_Xsig_Xsig(&argSqSq, &argSqSq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) accumulatore.lsw = argSq.lsw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) XSIG_LL(accumulatore) = XSIG_LL(argSq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) shr_Xsig(&argSq, 2 * (-1 - exponent - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) shr_Xsig(&argSqSq, 4 * (-1 - exponent - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /* Now have argSq etc with binary point at the left
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) .1xxxxxxxx */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* Do the basic fixed point polynomial evaluation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) accumulator.msw = accumulator.midw = accumulator.lsw = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) polynomial_Xsig(&accumulator, &XSIG_LL(argSqSq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) oddplterms, HIPOWERop - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) mul64_Xsig(&accumulator, &XSIG_LL(argSq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) negate_Xsig(&accumulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) polynomial_Xsig(&accumulator, &XSIG_LL(argSqSq), oddnegterms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) HIPOWERon - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) negate_Xsig(&accumulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) add_two_Xsig(&accumulator, &fixedpterm, &dummy_exp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) mul64_Xsig(&accumulatore, &denomterm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) shr_Xsig(&accumulatore, 1 + 2 * (-1 - exponent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) accumulatore.msw |= 0x80000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) div_Xsig(&accumulator, &accumulatore, &accumulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) mul_Xsig_Xsig(&accumulator, &argSignif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) mul_Xsig_Xsig(&accumulator, &argSq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) shr_Xsig(&accumulator, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) negate_Xsig(&accumulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) add_Xsig_Xsig(&accumulator, &argSignif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (transformed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) /* compute pi/4 - accumulator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) shr_Xsig(&accumulator, -1 - exponent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) negate_Xsig(&accumulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) add_Xsig_Xsig(&accumulator, &pi_signif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) exponent = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (inverted) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /* compute pi/2 - accumulator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) shr_Xsig(&accumulator, -exponent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) negate_Xsig(&accumulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) add_Xsig_Xsig(&accumulator, &pi_signif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) exponent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (sign1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /* compute pi - accumulator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) shr_Xsig(&accumulator, 1 - exponent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) negate_Xsig(&accumulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) add_Xsig_Xsig(&accumulator, &pi_signif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) exponent = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) exponent += round_Xsig(&accumulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) significand(st1_ptr) = XSIG_LL(accumulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) setexponent16(st1_ptr, exponent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) tag = FPU_round(st1_ptr, 1, 0, FULL_PRECISION, sign2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) FPU_settagi(1, tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) set_precision_flag_up(); /* We do not really know if up or down,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) use this as the default. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }