File: pow.S

package info (click to toggle)
avr-libc 1%3A1.6.2.cvs20080610-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 14,848 kB
  • ctags: 55,619
  • sloc: ansic: 92,267; asm: 6,692; sh: 4,131; makefile: 2,481; python: 976; pascal: 426; perl: 116
file content (214 lines) | stat: -rw-r--r-- 5,086 bytes parent folder | download | duplicates (2)
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
/* Copyright (c) 2002  Michael Stumpf  <mistumpf@de.pepperl-fuchs.com>
   Copyright (c) 2006  Dmitry Xmelkov
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.
   * Neither the name of the copyright holders nor the names of
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   POSSIBILITY OF SUCH DAMAGE. */

/* $Id: pow.S,v 1.9 2007/01/14 15:12:25 dmix Exp $ */

#include "fp32def.h"
#include "asmdef.h"

/* float pow (float x, float y);
     The pow() function returns the value of x raised to the power of y.

   Args combinations:
	x	y		pow(x,y)
	--------------------------------
	+0	NaN		NaN	exp(log(x)*y)
	+0	+0,-0		+1
	+0	+Inf		+0	exp(log(x)*y)
	+0	-Inf		+Inf	exp(log(x)*y)
	+0	y > 0		+0	exp(log(x)*y)
	+0	y < 0		+Inf	exp(log(x)*y)
	
	-0	NaN		NaN	exp(log(x)*y)
	-0	+0,-0		+1
	-0	1,3,5...	-0	-exp(log(x)*y)
	-0	y > 0		+0	exp(log(x)*y)
	-0	-1,-3,-5...	-Inf	-exp(log(x)*y)
	-0	y < 0		+Inf	exp(log(x)*y)
	
	+1	NaN		+1
	+1	+Inf,-Inf	+1
	+1	else		+1	exp(log(x)*y)
	
	-1	+0,-0		+1
	-1	1,3,5...	-1
	-1	-1,-3,-5...	-1
	-1	2,4,6...	+1
	-1	-2,-4,-6...	+1
	-1	else		NaN	exp(log(x)*y)
	
	+Inf	NaN		NaN	exp(log(x)*y)
	+Inf	+0,-0		+1
	+Inf	y > 0		+Inf	exp(log(x)*y)
	+Inf	y < 0		+0	exp(log(x)*y)
	
	-Inf	NaN		NaN	exp(log(x)*y)
	-Inf	+0,-0		+1
	-Inf	1,3,5...	-Inf
	-Inf	y > 0		+Inf
	-Inf	-1,-3,-5...	-0
	-Inf	y < 0		+0
	
	NaN	+0,-0		+1
	NaN	else		NaN	exp(log(x)*y)
	
	(0,1)	NaN		NaN	exp(log(x)*y)
	(0,1)	+0,-0		+1	exp(log(x)*y)
	(0,1)	+Inf		+0	exp(log(x)*y)
	(0,1)	-Inf		+Inf	exp(log(x)*y)
	
	(-1,0)	NaN		NaN
	(-1,0)	+0,-0		+1
	(-1,0)	+Inf		+0
	(-1,0)	-Inf		+Inf
	(-1,0)	nonintegral	NaN
	
	x > 1	NaN		NaN
	x > 1	+0,-0		+1
	x > 1   +Inf		+Inf
	x > 1	-Inf		+0
	
	x < -1	NaN		NaN
	x < -1	+0,-0		+1
	x < -1	+Inf		+Inf
	x < -1	-Inf		+0
	x < -1	nonintegral	NaN
 */

#define	FL_1	0x3f800000	/* +1.0	*/

ENTRY pow
  ; ZH := exponent of y
	X_movw	ZL, rB2
	lsl	ZL
	rol	ZH
  ; y == 0 ?
	adiw	ZL, 0
	cpc	rB0, r1
	cpc	rB1, r1
	breq	.L_one
  ; preliminary check
	cp	rA0, r1
	cpc	rA1, r1
	brne	0f		; skip a bit of comparisons
  ; x == 1.0 ?
	cpi	rA2, hlo8(FL_1)
	ldi	rAE, hhi8(FL_1)
	cpc	rA3, rAE
	breq	.L_ret
  ; x == -0.0 ?
	set			; flag: nonintegral y is a legal value
	cpi	rA3, 0x80
	cpc	rA2, r1
	breq	.L_int
  ; x == -Inf ?
	cpi	rA2, 0x80
	ldi	rAE, 0xff
	cpc	rA3, rAE
	breq	.L_int
  ; x >= 0 ?
0:	tst	rA3
	brpl	.L_pow
  ; isinf(y) ?
	cpi	ZH, 0xff
	cpc	ZL, r1
	cpc	rB1, r1
	cpc	rB0, r1
	breq	.L_big
  ; isintegral(y) ?
	clt			; nonintegral y is not a legal value
.L_int:
	/* Now we have:
	     y is nonzero value
	     ZL == (rB2 << 1)
	     ZH == exponenta,  ZH <= 254	*/
	sec		; hidden bit
	ror	ZL	; This is incorrect for subnormals, no sense:
			;   result would NaN.
  ; ffs().  Next two loops are finite due to above 'sec'.
	X_movw	XL, rB0
  ; Byte search loop.
1:	tst	XL
	brne	2f
	mov	XL, XH
	mov	XH, ZL
	subi	ZH, -8
	brcs	1b
	rjmp	.L_noint	; mantisa too big
  ; Bit search loop.
2:	subi	ZH, -1
	brcc	.L_noint	; mantisa too big
	lsr	XL
	brcc	2b
  ; Check exponent, is y an integral value?
	/* Example:  1.0 == 0x3f800000:
	    exponent:	 ZH := 0x7f
	    byte search: ZH += 2*8       --> 0x8f
	    bit search:  ZH += 8         --> 0x97	*/
	cpi	ZH, 0x97
	brlo	.L_noint
	breq	3f		; y % 2 == 1
	cpi	ZH, 0x97 + 24
	brsh	.L_noint
	andi	rA3, 0x7f	; y is integral, y % 2 == 0
3:	push	rA3
	rcall	.L_pow
	pop	r0
	sbrc	r0, 7
	subi	rA3, 0x80
.L_ret:
	ret
  ; y is not an integral number
.L_noint:
	brts	.L_pow
.L_nan:
	rjmp	_U(__fp_nan)
.L_one:
	ldi	rA0,  lo8(FL_1)
	ldi	rA1,  hi8(FL_1)
	ldi	rA2, hlo8(FL_1)
	ldi	rA3, hhi8(FL_1)
	ret
  ; replace Inf --> big finite (to exclude '0 * Inf' for legal x == -1)
.L_big:
	ldi	rB2, 0x7f
.L_pow:
	andi	rA3, 0x7f
	push	rB3
	push	rB2
	push	rB1
	push	rB0
	rcall	_U(log)
	pop	rB0
	pop	rB1
	pop	rB2
	pop	rB3
	rcall	_U(__mulsf3)
	rjmp	_U(exp)
ENDFUNC