1*59d799daSIngo Weinhold
2*59d799daSIngo Weinhold /*
3*59d799daSIngo Weinhold * M_APM - mapm5sin.c
4*59d799daSIngo Weinhold *
5*59d799daSIngo Weinhold * Copyright (C) 1999 - 2007 Michael C. Ring
6*59d799daSIngo Weinhold *
7*59d799daSIngo Weinhold * Permission to use, copy, and distribute this software and its
8*59d799daSIngo Weinhold * documentation for any purpose with or without fee is hereby granted,
9*59d799daSIngo Weinhold * provided that the above copyright notice appear in all copies and
10*59d799daSIngo Weinhold * that both that copyright notice and this permission notice appear
11*59d799daSIngo Weinhold * in supporting documentation.
12*59d799daSIngo Weinhold *
13*59d799daSIngo Weinhold * Permission to modify the software is granted. Permission to distribute
14*59d799daSIngo Weinhold * the modified code is granted. Modifications are to be distributed by
15*59d799daSIngo Weinhold * using the file 'license.txt' as a template to modify the file header.
16*59d799daSIngo Weinhold * 'license.txt' is available in the official MAPM distribution.
17*59d799daSIngo Weinhold *
18*59d799daSIngo Weinhold * This software is provided "as is" without express or implied warranty.
19*59d799daSIngo Weinhold */
20*59d799daSIngo Weinhold
21*59d799daSIngo Weinhold /*
22*59d799daSIngo Weinhold * $Id: mapm5sin.c,v 1.10 2007/12/03 01:26:16 mike Exp $
23*59d799daSIngo Weinhold *
24*59d799daSIngo Weinhold * This file contains the functions that implement the sin (5x)
25*59d799daSIngo Weinhold * and cos (4x) multiple angle relations
26*59d799daSIngo Weinhold *
27*59d799daSIngo Weinhold * $Log: mapm5sin.c,v $
28*59d799daSIngo Weinhold * Revision 1.10 2007/12/03 01:26:16 mike
29*59d799daSIngo Weinhold * Update license
30*59d799daSIngo Weinhold *
31*59d799daSIngo Weinhold * Revision 1.9 2002/11/03 21:50:36 mike
32*59d799daSIngo Weinhold * Updated function parameters to use the modern style
33*59d799daSIngo Weinhold *
34*59d799daSIngo Weinhold * Revision 1.8 2001/03/25 20:57:03 mike
35*59d799daSIngo Weinhold * move cos_to_sin func in here
36*59d799daSIngo Weinhold *
37*59d799daSIngo Weinhold * Revision 1.7 2000/05/04 23:50:21 mike
38*59d799daSIngo Weinhold * use multiple angle identity 4 times of larger COS angles
39*59d799daSIngo Weinhold *
40*59d799daSIngo Weinhold * Revision 1.6 1999/06/30 00:08:53 mike
41*59d799daSIngo Weinhold * pass more decimal places to raw functions
42*59d799daSIngo Weinhold *
43*59d799daSIngo Weinhold * Revision 1.5 1999/06/20 23:41:32 mike
44*59d799daSIngo Weinhold * changed COS to use 4x multiple angle identity instead of 5x
45*59d799daSIngo Weinhold *
46*59d799daSIngo Weinhold * Revision 1.4 1999/06/20 19:42:26 mike
47*59d799daSIngo Weinhold * tweak number of dec places passed to sub-functions
48*59d799daSIngo Weinhold *
49*59d799daSIngo Weinhold * Revision 1.3 1999/06/20 19:03:56 mike
50*59d799daSIngo Weinhold * changed local static variables to MAPM stack variables
51*59d799daSIngo Weinhold *
52*59d799daSIngo Weinhold * Revision 1.2 1999/05/12 21:30:09 mike
53*59d799daSIngo Weinhold * replace local 5.0 with global
54*59d799daSIngo Weinhold *
55*59d799daSIngo Weinhold * Revision 1.1 1999/05/10 20:56:31 mike
56*59d799daSIngo Weinhold * Initial revision
57*59d799daSIngo Weinhold */
58*59d799daSIngo Weinhold
59*59d799daSIngo Weinhold #include "m_apm_lc.h"
60*59d799daSIngo Weinhold
61*59d799daSIngo Weinhold /****************************************************************************/
M_5x_sin(M_APM r,int places,M_APM x)62*59d799daSIngo Weinhold void M_5x_sin(M_APM r, int places, M_APM x)
63*59d799daSIngo Weinhold {
64*59d799daSIngo Weinhold M_APM tmp8, tmp9;
65*59d799daSIngo Weinhold
66*59d799daSIngo Weinhold tmp8 = M_get_stack_var();
67*59d799daSIngo Weinhold tmp9 = M_get_stack_var();
68*59d799daSIngo Weinhold
69*59d799daSIngo Weinhold m_apm_multiply(tmp9, x, MM_5x_125R); /* 1 / (5*5*5) */
70*59d799daSIngo Weinhold M_raw_sin(tmp8, (places + 6), tmp9);
71*59d799daSIngo Weinhold M_5x_do_it(tmp9, (places + 4), tmp8);
72*59d799daSIngo Weinhold M_5x_do_it(tmp8, (places + 4), tmp9);
73*59d799daSIngo Weinhold M_5x_do_it(r, places, tmp8);
74*59d799daSIngo Weinhold
75*59d799daSIngo Weinhold M_restore_stack(2);
76*59d799daSIngo Weinhold }
77*59d799daSIngo Weinhold /****************************************************************************/
M_4x_cos(M_APM r,int places,M_APM x)78*59d799daSIngo Weinhold void M_4x_cos(M_APM r, int places, M_APM x)
79*59d799daSIngo Weinhold {
80*59d799daSIngo Weinhold M_APM tmp8, tmp9;
81*59d799daSIngo Weinhold
82*59d799daSIngo Weinhold tmp8 = M_get_stack_var();
83*59d799daSIngo Weinhold tmp9 = M_get_stack_var();
84*59d799daSIngo Weinhold
85*59d799daSIngo Weinhold /*
86*59d799daSIngo Weinhold * if |x| >= 1.0 use multiple angle identity 4 times
87*59d799daSIngo Weinhold * if |x| < 1.0 use multiple angle identity 3 times
88*59d799daSIngo Weinhold */
89*59d799daSIngo Weinhold
90*59d799daSIngo Weinhold if (x->m_apm_exponent > 0)
91*59d799daSIngo Weinhold {
92*59d799daSIngo Weinhold m_apm_multiply(tmp9, x, MM_5x_256R); /* 1 / (4*4*4*4) */
93*59d799daSIngo Weinhold M_raw_cos(tmp8, (places + 8), tmp9);
94*59d799daSIngo Weinhold M_4x_do_it(tmp9, (places + 8), tmp8);
95*59d799daSIngo Weinhold M_4x_do_it(tmp8, (places + 6), tmp9);
96*59d799daSIngo Weinhold M_4x_do_it(tmp9, (places + 4), tmp8);
97*59d799daSIngo Weinhold M_4x_do_it(r, places, tmp9);
98*59d799daSIngo Weinhold }
99*59d799daSIngo Weinhold else
100*59d799daSIngo Weinhold {
101*59d799daSIngo Weinhold m_apm_multiply(tmp9, x, MM_5x_64R); /* 1 / (4*4*4) */
102*59d799daSIngo Weinhold M_raw_cos(tmp8, (places + 6), tmp9);
103*59d799daSIngo Weinhold M_4x_do_it(tmp9, (places + 4), tmp8);
104*59d799daSIngo Weinhold M_4x_do_it(tmp8, (places + 4), tmp9);
105*59d799daSIngo Weinhold M_4x_do_it(r, places, tmp8);
106*59d799daSIngo Weinhold }
107*59d799daSIngo Weinhold
108*59d799daSIngo Weinhold M_restore_stack(2);
109*59d799daSIngo Weinhold }
110*59d799daSIngo Weinhold /****************************************************************************/
111*59d799daSIngo Weinhold /*
112*59d799daSIngo Weinhold * calculate the multiple angle identity for sin (5x)
113*59d799daSIngo Weinhold *
114*59d799daSIngo Weinhold * sin (5x) == 16 * sin^5 (x) - 20 * sin^3 (x) + 5 * sin(x)
115*59d799daSIngo Weinhold */
M_5x_do_it(M_APM rr,int places,M_APM xx)116*59d799daSIngo Weinhold void M_5x_do_it(M_APM rr, int places, M_APM xx)
117*59d799daSIngo Weinhold {
118*59d799daSIngo Weinhold M_APM tmp0, tmp1, t2, t3, t5;
119*59d799daSIngo Weinhold
120*59d799daSIngo Weinhold tmp0 = M_get_stack_var();
121*59d799daSIngo Weinhold tmp1 = M_get_stack_var();
122*59d799daSIngo Weinhold t2 = M_get_stack_var();
123*59d799daSIngo Weinhold t3 = M_get_stack_var();
124*59d799daSIngo Weinhold t5 = M_get_stack_var();
125*59d799daSIngo Weinhold
126*59d799daSIngo Weinhold m_apm_multiply(tmp1, xx, xx);
127*59d799daSIngo Weinhold m_apm_round(t2, (places + 4), tmp1); /* x ^ 2 */
128*59d799daSIngo Weinhold
129*59d799daSIngo Weinhold m_apm_multiply(tmp1, t2, xx);
130*59d799daSIngo Weinhold m_apm_round(t3, (places + 4), tmp1); /* x ^ 3 */
131*59d799daSIngo Weinhold
132*59d799daSIngo Weinhold m_apm_multiply(t5, t2, t3); /* x ^ 5 */
133*59d799daSIngo Weinhold
134*59d799daSIngo Weinhold m_apm_multiply(tmp0, xx, MM_Five);
135*59d799daSIngo Weinhold m_apm_multiply(tmp1, t5, MM_5x_Sixteen);
136*59d799daSIngo Weinhold m_apm_add(t2, tmp0, tmp1);
137*59d799daSIngo Weinhold m_apm_multiply(tmp1, t3, MM_5x_Twenty);
138*59d799daSIngo Weinhold m_apm_subtract(tmp0, t2, tmp1);
139*59d799daSIngo Weinhold
140*59d799daSIngo Weinhold m_apm_round(rr, places, tmp0);
141*59d799daSIngo Weinhold M_restore_stack(5);
142*59d799daSIngo Weinhold }
143*59d799daSIngo Weinhold /****************************************************************************/
144*59d799daSIngo Weinhold /*
145*59d799daSIngo Weinhold * calculate the multiple angle identity for cos (4x)
146*59d799daSIngo Weinhold *
147*59d799daSIngo Weinhold * cos (4x) == 8 * [ cos^4 (x) - cos^2 (x) ] + 1
148*59d799daSIngo Weinhold */
M_4x_do_it(M_APM rr,int places,M_APM xx)149*59d799daSIngo Weinhold void M_4x_do_it(M_APM rr, int places, M_APM xx)
150*59d799daSIngo Weinhold {
151*59d799daSIngo Weinhold M_APM tmp0, tmp1, t2, t4;
152*59d799daSIngo Weinhold
153*59d799daSIngo Weinhold tmp0 = M_get_stack_var();
154*59d799daSIngo Weinhold tmp1 = M_get_stack_var();
155*59d799daSIngo Weinhold t2 = M_get_stack_var();
156*59d799daSIngo Weinhold t4 = M_get_stack_var();
157*59d799daSIngo Weinhold
158*59d799daSIngo Weinhold m_apm_multiply(tmp1, xx, xx);
159*59d799daSIngo Weinhold m_apm_round(t2, (places + 4), tmp1); /* x ^ 2 */
160*59d799daSIngo Weinhold m_apm_multiply(t4, t2, t2); /* x ^ 4 */
161*59d799daSIngo Weinhold
162*59d799daSIngo Weinhold m_apm_subtract(tmp0, t4, t2);
163*59d799daSIngo Weinhold m_apm_multiply(tmp1, tmp0, MM_5x_Eight);
164*59d799daSIngo Weinhold m_apm_add(tmp0, MM_One, tmp1);
165*59d799daSIngo Weinhold m_apm_round(rr, places, tmp0);
166*59d799daSIngo Weinhold M_restore_stack(4);
167*59d799daSIngo Weinhold }
168*59d799daSIngo Weinhold /****************************************************************************/
169*59d799daSIngo Weinhold /*
170*59d799daSIngo Weinhold * compute r = sqrt(1 - a ^ 2).
171*59d799daSIngo Weinhold */
M_cos_to_sin(M_APM r,int places,M_APM a)172*59d799daSIngo Weinhold void M_cos_to_sin(M_APM r, int places, M_APM a)
173*59d799daSIngo Weinhold {
174*59d799daSIngo Weinhold M_APM tmp1, tmp2;
175*59d799daSIngo Weinhold
176*59d799daSIngo Weinhold tmp1 = M_get_stack_var();
177*59d799daSIngo Weinhold tmp2 = M_get_stack_var();
178*59d799daSIngo Weinhold
179*59d799daSIngo Weinhold m_apm_multiply(tmp1, a, a);
180*59d799daSIngo Weinhold m_apm_subtract(tmp2, MM_One, tmp1);
181*59d799daSIngo Weinhold m_apm_sqrt(r, places, tmp2);
182*59d799daSIngo Weinhold M_restore_stack(2);
183*59d799daSIngo Weinhold }
184*59d799daSIngo Weinhold /****************************************************************************/
185