-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrem_pio2.c
197 lines (176 loc) · 6.11 KB
/
rem_pio2.c
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
/**
* This file include functions to compute y=x-N*pi/2 and return the last two bits of N
* in order to know which quadrant we are considering.
*
* We use an scs representation to compute it by Payne and Hanek methods. For more information
* you can read K. C. Ng research report from Sun Microsystems:
* "Argument reduction for huge argument: Good to the last bit" (July 13, 1992)
*
*/
#include "stdio.h"
#include "coefpi2.h"
/**
* Case X_IND = -1:
* 0
* 2 ^
* X : <> |--| |--| |--| 0 0 0 0 0
* 2/Pi : <> |--| |--| |--| |--| .....
*
* Case X_IND = 0:
* 0
* 2 ^
* X : |--| <> |--| |--| 0 0 0 0 0
* 2/Pi : <> |--| |--| |--| |--| .....
*
* Case X_IND = 1:
* 0
* 2 ^
* X : |--| |--| <> |--| |--| 0 0 0 0 0
* 2/Pi : <> |--| |--| |--| |--| .....
*
* Case ...
*
* Step 1:
*
* Compute r = X . 2/Pi where:
* - r[0] hold the integer part. (if x>0 or the once complement integer part if x<0 )
* - r[1] to r[SCS_NB_WORDS+2] hold the reduced part
* the 3 extra 30 bits are here to prevent possible
* cancellation due to a number x too close to a
* multiple of Pi/2.
*
* Step 2:
* Compute result = (r[1] ... r[SCS_NB_WORDS]) . Pi/2.
*
* description of local variables :
* - ind : where to start multiplying into 2opi table
*
*/
/* TODO OPTIM
better 64-bit multiplication, see in scs_mult */
int rem_pio2_scs(scs_ptr result, const scs_ptr x){
unsigned long long int r[SCS_NB_WORDS+3], tmp;
unsigned int N;
/* result r[0],...,r[10] could store till 300 bits of precision */
/* that is really enough for computing the reduced argument */
int sign, i, j, ind;
int *two_over_pi_pt;
if ((X_EXP != 1)||(X_IND < -1)){
scs_set(result, x);
return 0;
}
/* Compute the product |x| * 2/Pi */
if ((X_IND == -1)){
/* In this case we consider number between ]-1,+1[ */
/* we may use simpler algorithm such as Cody And Waite */
r[0] = 0; r[1] = 0;
r[2] = (unsigned long long int)(two_over_pi[0]) * X_HW[0];
r[3] = ((unsigned long long int)(two_over_pi[0]) * X_HW[1]
+(unsigned long long int)(two_over_pi[1]) * X_HW[0]);
if(X_HW[2] == 0){
for(i=4; i<(SCS_NB_WORDS+3); i++){
r[i] = ((unsigned long long int)(two_over_pi[i-3]) * X_HW[1]
+(unsigned long long int)(two_over_pi[i-2]) * X_HW[0]);
}}else {
for(i=4; i<(SCS_NB_WORDS+3); i++){
r[i] = ((unsigned long long int)(two_over_pi[i-4]) * X_HW[2]
+(unsigned long long int)(two_over_pi[i-3]) * X_HW[1]
+(unsigned long long int)(two_over_pi[i-2]) * X_HW[0]);
}
}
}else {
if (X_IND == 0){
r[0] = 0;
r[1] = (unsigned long long int)(two_over_pi[0]) * X_HW[0];
r[2] = ((unsigned long long int)(two_over_pi[0]) * X_HW[1]
+(unsigned long long int)(two_over_pi[1]) * X_HW[0]);
if(X_HW[2] == 0){
for(i=3; i<(SCS_NB_WORDS+3); i++){
r[i] = ((unsigned long long int)(two_over_pi[i-2]) * X_HW[1]
+(unsigned long long int)(two_over_pi[i-1]) * X_HW[0]);
}}else {
for(i=3; i<(SCS_NB_WORDS+3); i++){
r[i] = ((unsigned long long int)(two_over_pi[i-3]) * X_HW[2]
+(unsigned long long int)(two_over_pi[i-2]) * X_HW[1]
+(unsigned long long int)(two_over_pi[i-1]) * X_HW[0]);
}}
}else {
if (X_IND == 1){
r[0] = (unsigned long long int)(two_over_pi[0]) * X_HW[0];
r[1] = ((unsigned long long int)(two_over_pi[0]) * X_HW[1]
+(unsigned long long int)(two_over_pi[1]) * X_HW[0]);
if(X_HW[2] == 0){
for(i=2; i<(SCS_NB_WORDS+3); i++){
r[i] = ((unsigned long long int)(two_over_pi[i-1]) * X_HW[1]
+(unsigned long long int)(two_over_pi[ i ]) * X_HW[0]);
}}else {
for(i=2; i<(SCS_NB_WORDS+3); i++){
r[i] = ((unsigned long long int)(two_over_pi[i-2]) * X_HW[2]
+(unsigned long long int)(two_over_pi[i-1]) * X_HW[1]
+(unsigned long long int)(two_over_pi[ i ]) * X_HW[0]);
}}
}else {
if (X_IND == 2){
r[0] = ((unsigned long long int)(two_over_pi[0]) * X_HW[1]
+(unsigned long long int)(two_over_pi[1]) * X_HW[0]);
if(X_HW[2] == 0){
for(i=1; i<(SCS_NB_WORDS+3); i++){
r[i] = ((unsigned long long int)(two_over_pi[ i ]) * X_HW[1]
+(unsigned long long int)(two_over_pi[i+1]) * X_HW[0]);
}}else {
for(i=1; i<(SCS_NB_WORDS+3); i++){
r[i] = ((unsigned long long int)(two_over_pi[i-1]) * X_HW[2]
+(unsigned long long int)(two_over_pi[ i ]) * X_HW[1]
+(unsigned long long int)(two_over_pi[i+1]) * X_HW[0]);
}}
}else {
ind = (X_IND - 3);
two_over_pi_pt = (int*)&(two_over_pi[ind]);
if(X_HW[2] == 0){
for(i=0; i<(SCS_NB_WORDS+3); i++){
r[i] = ((unsigned long long int)(two_over_pi_pt[i+1]) * X_HW[1]
+(unsigned long long int)(two_over_pi_pt[i+2]) * X_HW[0]);
}}else {
for(i=0; i<(SCS_NB_WORDS+3); i++){
r[i] = ((unsigned long long int)(two_over_pi_pt[ i ]) * X_HW[2]
+(unsigned long long int)(two_over_pi_pt[i+1]) * X_HW[1]
+(unsigned long long int)(two_over_pi_pt[i+2]) * X_HW[0]);
}
}
}
}
}
}
/* Carry propagate */
r[SCS_NB_WORDS+1] += r[SCS_NB_WORDS+2]>>30;
for(i=(SCS_NB_WORDS+1); i>0; i--) {tmp=r[i]>>30; r[i-1] += tmp; r[i] -= (tmp<<30);}
/* The integer part is in r[0] */
N = r[0];
#if 0
printf("r[0] = %d\n", N);
#endif
if (r[1] > (SCS_RADIX)/2){ /* test if the reduced part is bigger than Pi/4 */
N += 1;
sign = -1;
for(i=1; i<(SCS_NB_WORDS+3); i++) { r[i]=((~(unsigned int)(r[i])) & 0x3fffffff);}
}
else
sign = 1;
/* Now we get the reduce argument and check for possible
* cancellation By Kahan algorithm we will have at most 2 digits
* of cancellations r[1] and r[2] in the worst case.
*/
if (r[1] == 0)
if (r[2] == 0) i = 3;
else i = 2;
else i = 1;
for(j=0; j<SCS_NB_WORDS; j++) { R_HW[j] = r[i+j];}
R_EXP = 1;
R_IND = -i;
R_SGN = sign*X_SGN;
/* Last step :
* Multiplication by pi/2
*/
scs_mul(result, Pio2_ptr, result);
return N*X_SGN;
}