1
/* -*- Mode: Asm -*- */
3
1
/* Copyright (c) 2002 Michael Stumpf <mistumpf@de.pepperl-fuchs.com>
4
Copyright (c) 2006 Anatoly Sokolov <aesok@post.ru>
2
Copyright (c) 2006 Dmitry Xmelkov
8
5
Redistribution and use in source and binary forms, with or without
9
6
modification, are permitted provided that the following conditions are met:
11
8
* Redistributions of source code must retain the above copyright
12
9
notice, this list of conditions and the following disclaimer.
14
10
* Redistributions in binary form must reproduce the above copyright
15
11
notice, this list of conditions and the following disclaimer in
16
12
the documentation and/or other materials provided with the
19
14
* Neither the name of the copyright holders nor the names of
20
15
contributors may be used to endorse or promote products derived
21
16
from this software without specific prior written permission.
30
25
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
26
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
27
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
POSSIBILITY OF SUCH DAMAGE.
36
/* $Id: sqrt.S,v 1.5.2.2 2006/01/20 20:39:28 aesok Exp $ */
39
sqrt.S is part of FPlib V 0.3.0 ported to avr-as
40
for details see readme.fplib
42
*----------------------------------------------------------------------------------------
28
POSSIBILITY OF SUCH DAMAGE. */
30
/* $Id: sqrt.S,v 1.8 2007/01/14 15:13:10 dmix Exp $ */
35
/* double sqrt (double);
70
RJMP _U(__fp_nanEDOM) ; sign bit is set -> argument range error
72
/* A = sign(=0):(exp+7F):[1].mant = 2^(exp)*1.mant
73
* = (LSB(exp)+7F) + (exp>>1) + (exp>>1):[1].mant = (2^(exp>>1))^2 * 2^(LSB(exp))*1.mant
75
* A' = [2^0*1.0... 2^1*1.1111111] = [1.0...4.0[
77
* sqrt(A) = 2^(exp>>1) * sqrt( A' )
78
* sqrt(A') = [1.0...2.0[ -> result of srqt(A') has definetely exponent 7F! -> exp(X0) = 7F
80
* the matissa of X0 is taken as ( 0.mant >> 1 ) 0.0mant
81
* plus if LSB(exp)==1 [LSB(exp-7F)==0] 0.100000
82
* plus the implicit one 1.000000
89
MOV rB2, rA2 ; needed later on
91
RCALL _U(__fp_split_a) ; does not return on NaN
95
ASR rTI0 ; this is dExp!
99
PUSH rTI0 ; exponent offset
113
X_movw rAbak2, rA2 ; Abak = A
115
SUBI rB2, 0x80 ; == EOR 0x80
117
CLR rB1 ; a slightly smaller X(0) than the calculated is better
118
CLR rB0 ; and saves 2 right shifts
120
LDI rB3, 0x3F ; load exponent 7F
41
.L_nf: brne .L_pk ; NaN, return as is
42
brtc .L_pk ; sqrt(+Inf) --> +Inf
43
.L_nan: rjmp _U(__fp_nan)
44
.L_pk: rjmp _U(__fp_mpack)
47
; split and check arg.
49
brcs .L_nf ; !isfinite(A)
51
breq .L_pk ; return 0 with original sign
52
brts .L_nan ; sqrt(negative) --> NaN
55
sbc rB3, rB3 ; exponent high byte
56
; normalize, if A is subnormal
59
; calculate result exponent
62
; expand A mantissa to rAE.rA2.rA1.rA0
64
brcc 1f ; after 'ror rA3'
124
X_movw rA2, rAbak2 ; A = Abak
127
X_movw rBbak2, rB2 ; Bbak = B
129
XCALL __divsf3 ; FP1X = arg/xn
132
X_movw rB2, rBbak2 ; B = Bbak
134
XCALL __addsf3 ; FP1X = arg/xn + xn
138
RCALL _U(ldexp) ; div by 2 := Xn+1
141
X_movw rB2, rA2 ; B = A
146
CPC rBbak3, rB3 ; cmp B to Bbak
160
RCALL _U(__fp_split_a) ; does not return on NaN
78
; save temporary regs.
82
/* arg's mantissa: rAE.rA2.rA1.rA0
84
moving bit: mvb2.mvb1.mvb0
85
remain value: rem3.rem2.rem1.rem0 */
138
cpc rem3, rAE ; C is set if rem < A
142
; pop temporary regs.
145
; merge result and return
148
subi rA3, lo8(-127) ; exponent bias