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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
/*-------------------------------------------------------------------------
expf.c - Computes e**x of a 32-bit float as outlined in [1]
Copyright (C) 2001, 2002, Jesus Calvino-Fraga, jesusc@ieee.org
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this library; see the file COPYING. If not, write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
As a special exception, if you link this library with other files,
some of which are compiled with SDCC, to produce an executable,
this library does not by itself cause the resulting executable to
be covered by the GNU General Public License. This exception does
not however invalidate any other reasons why the executable file
might be covered by the GNU General Public License.
-------------------------------------------------------------------------*/
/* [1] William James Cody and W. M. Waite. _Software manual for the
elementary functions_, Englewood Cliffs, N.J.:Prentice-Hall, 1980. */
/* Version 1.0 - Initial release */
#define __SDCC_MATH_LIB
#include <math.h>
#include <errno.h>
#include <stdbool.h>
#ifdef MATH_ASM_MCS51
#define __SDCC_FLOAT_LIB
#include <float.h>
// TODO: share with other temps
static __bit sign_bit;
static __data unsigned char expf_y[4];
static __data unsigned char n;
float expf(float x)
{
x;
__asm
mov c, acc.7
mov _sign_bit, c // remember sign
clr acc.7 // and make input positive
mov r0, a
mov c, b.7
rlc a // extract exponent
add a, #153
jc expf_not_zero
// input is a very small number, so e^x is 1.000000
mov dptr, #0
mov b, #0x80
mov a, #0x3F
ljmp expf_exit
expf_not_zero:
// TODO: check exponent for very small values, and return zero
mov _n, #0
mov a, dpl
add a, #0xE8 // is x >= 0.69314718
mov a, dph
addc a, #0x8D
mov a, b
addc a, #0xCE
mov a, r0
addc a, #0xC0
mov a, r0
jnc expf_no_range_reduction
expf_range_reduction:
mov (_expf_y + 0), dpl // keep copy of x in "exp_y"
mov (_expf_y + 1), dph
mov (_expf_y + 2), b
mov (_expf_y + 3), a
mov r0, #0x3B
push ar0
mov r0, #0xAA
push ar0
mov r0, #0xB8
push ar0
mov r0, #0x3F
push ar0
lcall ___fsmul // x * 1.442695041 = x / ln(2)
dec sp
dec sp
dec sp
dec sp
lcall ___fs2uchar // n = int(x * 1.442695041)
mov a, dpl
mov _n, a
add a, #128
jnc expf_range_ok
lcall fs_return_inf // exponent overflow
ljmp expf_exit
expf_range_ok:
mov r0,#0x00
mov r1,#0x80
mov r2,#0x31
mov r3,#0xBF
lcall expf_scale_and_add
mov (_expf_y + 0), dpl
mov (_expf_y + 1), dph
mov (_expf_y + 2), b
mov (_expf_y + 3), a
mov r0,#0x83
mov r1,#0x80
mov r2,#0x5E
mov r3,#0x39
lcall expf_scale_and_add
expf_no_range_reduction:
// Compute e^x using the cordic algorithm. This works over an
// input range of 0 to 0.69314712. Can be extended to work from
// 0 to 1.0 if the results are normalized, but over the range
// we use, the result is always from 1.0 to 1.99999988 (fixed
// exponent of 127)
expf_cordic_begin:
mov c, b.7
rlc a // extract exponent to acc
setb b.7
mov r1, dpl // mantissa to r4/r3/r2/r1
mov r2, dph
mov r3, b
mov r4, #0
// first, align the input into a 32 bit long
cjne a, #121, exp_lshift
sjmp exp_noshift
exp_lshift:
jc exp_rshift
// exp_a is greater than 121, so left shift
add a, #135
lcall fs_lshift_a
sjmp exp_noshift
exp_rshift:
// exp_a is less than 121, so right shift
cpl a
add a, #122
lcall fs_rshift_a
exp_noshift: // r4/r3/r2/r1 = x
clr a
mov (_expf_y + 0), a // y = 1.0;
mov (_expf_y + 1), a
mov (_expf_y + 2), a
mov (_expf_y + 3), #0x20
mov dptr, #__fs_natural_log_table
mov r0, a // r0 = i
exp_cordic_loop:
clr a
movc a, @a+dptr
mov b, a
inc dptr
clr a
movc a, @a+dptr // r7/r6/r5/b = table[i]
mov r5, a
inc dptr
clr a
movc a, @a+dptr
mov r6, a
inc dptr
clr a
movc a, @a+dptr
mov r7, a
inc dptr
clr c
mov a, r1
subb a, b // compare x to table[i]
mov a, r2
subb a, r5
mov a, r3
subb a, r6
mov a, r4
subb a, r7
jc exp_cordic_skip // if table[i] < x
clr c
mov a, r1
subb a, b
mov r1, a // x -= table[i]
mov a, r2
subb a, r5
mov r2, a
mov a, r3
subb a, r6
mov r3, a
mov a, r4
subb a, r7
mov r4, a
mov b, (_expf_y + 0)
mov r5, (_expf_y + 1) // r7/r6/r5/b = y >> i
mov r6, (_expf_y + 2)
mov r7, (_expf_y + 3)
mov a, r0
lcall __fs_cordic_rshift_r765_unsigned
mov a, (_expf_y + 0)
add a, b
mov (_expf_y + 0), a
mov a, (_expf_y + 1)
addc a, r5
mov (_expf_y + 1), a // y += (y >> i)
mov a, (_expf_y + 2)
addc a, r6
mov (_expf_y + 2), a
mov a, (_expf_y + 3)
addc a, r7
mov (_expf_y + 3), a
exp_cordic_skip:
inc r0
cjne r0, #27, exp_cordic_loop
mov r4, (_expf_y + 3)
mov r3, (_expf_y + 2)
mov r2, (_expf_y + 1)
mov r1, (_expf_y + 0)
mov exp_a, #121
lcall fs_normalize_a // end of cordic
mov a, #127
add a, _n // ldexpf(x, n)
mov exp_a, a
lcall fs_round_and_return
jnb _sign_bit, expf_done
push dpl
push dph
push b
push acc
mov dptr, #0
mov b, #0x80
mov a, #0x3F
lcall ___fsdiv // 1.0 / x
dec sp
dec sp
dec sp
dec sp
expf_done:
clr acc.7 // Result is always positive!
expf_exit:
__endasm;
#pragma less_pedantic
}
static void dummy1(void) __naked
{
__asm
.globl fs_lshift_a
expf_scale_and_add:
push ar0
push ar1
push ar2
push ar3
mov dpl, _n
lcall ___uchar2fs // turn n into float
lcall ___fsmul // n * scale_factor
dec sp
dec sp
dec sp
dec sp
push dpl
push dph
push b
push acc
mov dpl, (_expf_y + 0)
mov dph, (_expf_y + 1)
mov b, (_expf_y + 2)
mov a, (_expf_y + 3)
lcall ___fsadd // x += (n * scale_factor)
dec sp
dec sp
dec sp
dec sp
ret
__endasm;
}
static void dummy(void) __naked
{
__asm
.globl fs_lshift_a
fs_lshift_a:
jz fs_lshift_done
push ar0
mov r0, a
fs_lshift_loop:
clr c
mov a, r1
rlc a
mov r1, a
mov a, r2
rlc a
mov r2, a
mov a, r3
rlc a
mov r3, a
mov a, r4
rlc a
mov r4, a
djnz r0, fs_lshift_loop
pop ar0
fs_lshift_done:
ret
__endasm;
}
#else // not MATH_ASM_MCS51
#define P0 0.2499999995E+0
#define P1 0.4160288626E-2
#define Q0 0.5000000000E+0
#define Q1 0.4998717877E-1
#define P(z) ((P1*z)+P0)
#define Q(z) ((Q1*z)+Q0)
#define C1 0.693359375
#define C2 -2.1219444005469058277e-4
#define BIGX 88.72283911 /* ln(HUGE_VALF) */
#define EXPEPS 1.0E-7 /* exp(1.0E-7)=0.0000001 */
#define K1 1.4426950409 /* 1/ln(2) */
float expf(float x) _FLOAT_FUNC_REENTRANT
{
int n;
float xn, g, r, z, y;
bool sign;
if(x>=0.0)
{ y=x; sign=0; }
else
{ y=-x; sign=1; }
if(y<EXPEPS) return 1.0;
if(y>BIGX)
{
if(sign)
{
errno=ERANGE;
return HUGE_VALF
;
}
else
{
return 0.0;
}
}
z=y*K1;
n=z;
if(n<0) --n;
if(z-n>=0.5) ++n;
xn=n;
g=((y-xn*C1))-xn*C2;
z=g*g;
r=P(z)*g;
r=0.5+(r/(Q(z)-r));
n++;
z=ldexpf(r, n);
if(sign)
return 1.0/z;
else
return z;
}
#endif
|