2.9
[glibc/nacl-glibc.git] / sysdeps / powerpc / powerpc32 / power5 / fpu / w_sqrt.S
blob925930bf779a13e94429fe8cbdafb1ad52b77336
1 /* sqrt function.  PowerPC32 version.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 1 Franklin Street, Fifth Floor, Boston MA
18    02110-1301 USA.  */
20 #include <sysdep.h>
21 #include <math_ldbl_opt.h>
23 /* double [fp1] sqrt (double x [fp1])
24    Power4 (ISA V2.0) and above implement sqrt in hardware (not optional).
25    The fsqrt instruction generates the correct value for all inputs and
26    sets the appropriate floating point exceptions.  Extented checking is
27    only needed to set errno (via __kernel_standard) if the input value
28    is negative.
29    
30    So compare the input value against the absolute value of itself.
31    This will compare equal unless the value is negative (EDOM) or a NAN,
32    in which case we branch to the extend wrapper.  If equal we can return
33    the result directly.
34    
35    This part of the function looks like a leaf routine,  so no need to
36    stack a frame or execute prologue/epilogue code. It is safe to
37    branch directly to w_sqrt as long as the input value (f1) is
38    preserved. Putting the the sqrt result into f2 (float parameter 2)
39    allows passing both the input value and sqrt result into the extended
40    wrapper so there is no need to recompute.
41    
42    This tactic avoids the overhead of stacking a frame for the normal
43    (non-error) case.  Until gcc supports prologue shrink-wrapping
44    this is the best we can do.  */
46         .section        ".text"
47         .machine power4
48 EALIGN (__sqrt, 5, 0)
49         fabs    fp0,fp1
50         fsqrt   fp2,fp1
51         fcmpu   cr1,fp0,fp1
52         bne-    cr1,.Lw_sqrt
53         fmr     fp1,fp2
54         blr
55         .align  4
56 .Lw_sqrt:
57         mflr    r0
58         stwu    r1,-16(r1)
59         cfi_adjust_cfa_offset(16)
60         fmr     fp12,fp2
61         stw     r0,20(r1)
62         stw     r30,8(r1)
63         cfi_offset(lr,20)
64         cfi_offset(r30,8)
65 #ifdef SHARED
66 # ifdef HAVE_ASM_PPC_REL16
67         bcl     20,31,.LCF1
68 .LCF1:
69         mflr    r30
70         addis   r30,r30,_GLOBAL_OFFSET_TABLE_-.LCF1@ha
71         addi    r30,r30,_GLOBAL_OFFSET_TABLE_-.LCF1@l
72         lwz     r9,_LIB_VERSION@got(30)
73         lwz     r0,0(r9)
74 # else
75         bl      _GLOBAL_OFFSET_TABLE_@local-4
76         mflr    r30
77         lwz     r9,_LIB_VERSION@got(30)
78         lwz     r0,0(r9)
79 # endif
80 #else
81         lis     r9,_LIB_VERSION@ha
82         lwz     r0,_LIB_VERSION@l(r9)
83 #endif
84 /*  if (_LIB_VERSION == _IEEE_) return z; */
85         cmpwi   cr7,r0,-1
86         beq-    cr7,.L4
87 /*  if (x != x) return z; !isnan*/
88         fcmpu   cr7,fp1,fp1
89         bne-    cr7,.L4
90 /*  if  (x < 0.0)
91     return __kernel_standard (x, x, 26) */
92         fmr     fp2,fp1
93         li      r3,26
94         bne-    cr1,.L11
95 .L4:
96         lwz     r0,20(r1)
97         fmr     fp1,fp12
98         lwz     r30,8(r1)
99         addi    r1,r1,16
100         mtlr    r0
101         blr
102 .L11:
103         bl      __kernel_standard@plt
104         fmr     fp12,fp1
105         b       .L4
106         END     (__sqrt)
108 weak_alias (__sqrt, sqrt)
110 #ifdef NO_LONG_DOUBLE
111 weak_alias (__sqrt, sqrtl)
112 strong_alias (__sqrt, __sqrtl)
113 #endif
114 #if LONG_DOUBLE_COMPAT(libm, GLIBC_2_0)
115 compat_symbol (libm, __sqrt, sqrtl, GLIBC_2_0)
116 #endif