~ubuntu-branches/ubuntu/breezy/avr-libc/breezy

« back to all changes in this revision

Viewing changes to libm/fplib/sqrt.S

  • Committer: Bazaar Package Importer
  • Author(s): Hakan Ardo
  • Date: 2002-04-15 14:53:38 UTC
  • Revision ID: james.westby@ubuntu.com-20020415145338-c8hi0tn5bx74w7o3
Tags: upstream-20020203
ImportĀ upstreamĀ versionĀ 20020203

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  -*- Mode: Asm -*-  */
 
2
/*
 
3
    sqrt.S is part of     FPlib V 0.3.0       ported to avr-as
 
4
    for copyright and details see readme.fplib
 
5
 
 
6
 *----------------------------------------------------------------------------------------
 
7
 *
 
8
 *      A = sqrt(A)
 
9
 */
 
10
 
 
11
#include "gasava.inc"
 
12
#include "fplib.inc"
 
13
 
 
14
          TEXT_SEG(fplib, sqrt)
 
15
          FUNCTION(sqrt)
 
16
 
 
17
GLOBAL(sqrt)
 
18
 
 
19
    SBRC    rA3,7
 
20
    RJMP    _U(__fp_nanEDOM)    ; sign bit is set -> argument range error
 
21
 
 
22
   /* A = sign(=0):(exp+7F):[1].mant = 2^(exp)*1.mant
 
23
    *   =          (LSB(exp)+7F) + (exp>>1) + (exp>>1):[1].mant = (2^(exp>>1))^2 * 2^(LSB(exp))*1.mant
 
24
    *                                                           = 2^(exp>>1)^2 * A'
 
25
    * A' = [2^0*1.0... 2^1*1.1111111] = [1.0...4.0[
 
26
    *
 
27
    * sqrt(A) = 2^(exp>>1) * sqrt( A' )
 
28
    * sqrt(A') = [1.0...2.0[  -> result of srqt(A') has definetely exponent 7F! -> exp(X0) = 7F
 
29
    *
 
30
    * the matissa of X0 is taken as ( 0.mant >> 1 )     0.0mant
 
31
    *  plus if LSB(exp)==1 [LSB(exp-7F)==0]             0.100000
 
32
    *  plus the implicit one                            1.000000
 
33
    */
 
34
 
 
35
    MOV     rB2,rA2             ; needed later on
 
36
    RCALL   _U(__fp_split1)     ; does not return on NaN
 
37
    TST     rA3
 
38
    BREQ    _sqrt_100           ; sqrt(0) = 0
 
39
 _sqrt_00:
 
40
    MOV     rTI0,rA3
 
41
    SUBI    rTI0,0x7F
 
42
    ASR     rTI0                ; this is dExp!
 
43
    SUB     rA3,rTI0
 
44
    SUB     rA3,rTI0
 
45
    PUSH    rTI0                ; exponent offset
 
46
    SUBI    rB2,0x80            ; == EOR 0x80
 
47
    ROR     rB2
 
48
    CLR     rB1                 ; a slightly smaller X(0) than the calculated is better
 
49
    CLR     rB0                 ; and saves 2 right shifts
 
50
    ORI     rB2,0x80            ; implicit one
 
51
    LDI     rB3,0x7F            ; load exponent 7F
 
52
 
 
53
    CLT                         ; clears T
 
54
 
 
55
    PUSH    rS0
 
56
    PUSH    rS1
 
57
    PUSH    rS2
 
58
    PUSH    rS3                 ; rS3::rS0 = Abak
 
59
 
 
60
    PUSH    rS4
 
61
    PUSH    rS5
 
62
    PUSH    rS6
 
63
    PUSH    rS7                 ; rS7::rS4 = Bbak
 
64
 
 
65
    MOV     rS3,rA3
 
66
    MOV     rS2,rA2
 
67
    MOV     rS1,rA1
 
68
    MOV     rS0,rA0             ; Abak = A
 
69
 _sqrt_10:
 
70
    MOV     rA0,rS0             ;
 
71
    MOV     rA1,rS1             ;
 
72
    MOV     rA2,rS2             ;
 
73
    MOV     rA3,rS3             ; A = Abak
 
74
 
 
75
    MOV     rS7,rB3
 
76
    MOV     rS6,rB2
 
77
    MOV     rS5,rB1
 
78
    MOV     rS4,rB0             ; Bbak = B
 
79
 
 
80
                ; divsf3x does not need preset rAE,rBE
 
81
    RCALL   _U(__divsf3x)   ; FP1X = arg/xn
 
82
                ; now rAE = rBE = 0 or 80 or 0xFF ,ok for addsf3x
 
83
 
 
84
    MOV     rB3,rS7
 
85
    MOV     rB2,rS6
 
86
    MOV     rB1,rS5
 
87
    MOV     rB0,rS4             ; B = Bbak
 
88
 
 
89
    CLR     rT1c                ; addx uses R1.7 as sign
 
90
    RCALL   _U(__addsf3x)       ; FP1X = arg/xn + xn
 
91
    DEC     rA3                 ; div by 2 := Xn+1
 
92
 
 
93
    MOV     rB3,rA3
 
94
    MOV     rB2,rA2
 
95
    MOV     rB1,rA1
 
96
    MOV     rB0,rA0             ;
 
97
 
 
98
    CP      rS4,rB0             ;
 
99
    CPC     rS5,rB1             ;
 
100
    CPC     rS6,rB2
 
101
    CPC     rS7,rB3             ; cmp B to Bbak
 
102
    BRNE    _sqrt_10
 
103
 
 
104
    POP     rS7
 
105
    POP     rS6
 
106
    POP     rS5
 
107
    POP     rS4
 
108
    POP     rS3
 
109
    POP     rS2
 
110
    POP     rS1
 
111
    POP     rS0
 
112
 
 
113
    POP     rT0
 
114
    ADD     rA3,rT0             ;
 
115
 _sqrt_100:
 
116
    CLR     rT0                 ; no rounding beyond rAE
 
117
    RJMP    _U(__fp_merge)
 
118
 
 
119
          ENDFUNC
 
120
 
 
121