2.9
[glibc/nacl-glibc.git] / sysdeps / powerpc / powerpc64 / fpu / s_floorl.S
blob01b3c2101d1bacc0ba3f0bcf4a856491174ba082
1 /* long double floor function.
2    IBM extended format long double version.
3    Copyright (C) 2004, 2006 Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
21 #include <sysdep.h>
22 #include <math_ldbl_opt.h>
24         .section        ".toc","aw"
25 .LC0:   /* 2**52 */
26         .tc FD_43300000_0[TC],0x4330000000000000
28         .section        ".text"
29 /* long double [fp1,fp2] floorl (long double x [fp1,fp2])
30    IEEE 1003.1 floor function.
32    PowerPC64 long double uses the IBM extended format which is
33    represented two 64-floating point double values. The values are
34    non-overlapping giving an effective precision of 106 bits. The first
35    double contains the high order bits of mantisa and is always rounded
36    to represent a normal rounding of long double to double. Since the
37    long double value is sum of the high and low values, the low double
38    normally has the opposite sign to compensate for the this rounding.
40    For long double there are two cases:
41    1) |x| < 2**52, all the integer bits are in the high double.
42       floor the high double and set the low double to -0.0.
43    2) |x| >= 2**52, Rounding involves both doubles.
44       See the comment before lable .L2 for details.
45    */
47 ENTRY (__floorl)
48         mffs    fp11            /* Save current FPU rounding mode.  */
49         lfd     fp13,.LC0@toc(2)
50         fabs    fp0,fp1
51         fabs    fp9,fp2
52         fsub    fp12,fp13,fp13  /* generate 0.0  */
53         fcmpu   cr7,fp0,fp13    /* if (fabs(x) > TWO52)  */
54         fcmpu   cr6,fp1,fp12    /* if (x > 0.0)  */
55         bnl-    cr7,.L2
56         mtfsfi  7,3             /* Set rounding mode toward -inf.  */
57         fneg    fp2,fp12        /* set low double to -0.0.  */
58         ble-    cr6,.L0
59         fadd    fp1,fp1,fp13    /* x+= TWO52;  */
60         fsub    fp1,fp1,fp13    /* x-= TWO52;  */
61         fcmpu   cr5,fp1,fp12    /* if (x > 0.0)  */
62         mtfsf   0x01,fp11       /* restore previous rounding mode.  */
63         bnelr+  cr5
64         fmr     fp1,fp12        /* x must be +0.0 for the 0.0 case.  */
65         blr
66 .L0:
67         bge-    cr6,.L1         /* if (x < 0.0)  */
68         fsub    fp1,fp1,fp13    /* x-= TWO52;  */
69         fadd    fp1,fp1,fp13    /* x+= TWO52;  */
70 .L1:
71         mtfsf   0x01,fp11       /* restore previous rounding mode.  */
72         blr
73         
75 /* The high double is > TWO52 so we need to round the low double and
76    perhaps the high double.  In this case we have to round the low
77    double and handle any adjustment to the high double that may be
78    caused by rounding (up).  This is complicated by the fact that the
79    high double may already be rounded and the low double may have the
80    opposite sign to compensate.This gets a bit tricky so we use the
81    following algorithm:
83    tau = floor(x_high/TWO52);
84    x0 = x_high - tau;
85    x1 = x_low + tau;
86    r1 = rint(x1);
87    y_high = x0 + r1;
88    y_low = x0 - y_high + r1;
89    return y;  */
90 .L2:
91         fcmpu   cr7,fp9,fp13    /* if (|x_low| > TWO52)  */
92         fcmpu   cr0,fp9,fp12    /* || (|x_low| == 0.0)  */
93         fcmpu   cr5,fp2,fp12    /* if (x_low > 0.0)  */
94         bgelr-  cr7             /*   return x;  */
95         beqlr-  cr0
96         mtfsfi  7,3             /* Set rounding mode toward -inf.  */
97         fdiv    fp8,fp1,fp13    /* x_high/TWO52  */
98         
99         bng-    cr6,.L6         /* if (x > 0.0)  */
100         fctidz  fp0,fp8
101         fcfid   fp8,fp0         /* tau = floor(x_high/TWO52);  */
102         bng     cr5,.L4         /* if (x_low > 0.0)  */
103         fmr     fp3,fp1
104         fmr     fp4,fp2
105         b       .L5
106 .L4:                            /* if (x_low < 0.0)  */
107         fsub    fp3,fp1,fp8     /* x0 = x_high - tau;  */
108         fadd    fp4,fp2,fp8     /* x1 = x_low + tau;  */
109 .L5:
110         fadd    fp5,fp4,fp13    /* r1 = r1 + TWO52;  */
111         fsub    fp5,fp5,fp13    /* r1 = r1 - TWO52;  */
112         b       .L9
113 .L6:                            /* if (x < 0.0)  */
114         fctidz  fp0,fp8
115         fcfid   fp8,fp0         /* tau = floor(x_high/TWO52);  */       
116         bnl     cr5,.L7         /* if (x_low < 0.0)  */
117         fmr     fp3,fp1
118         fmr     fp4,fp2
119         b       .L8
120 .L7:                            /* if (x_low > 0.0)  */
121         fsub    fp3,fp1,fp8     /* x0 = x_high - tau;  */
122         fadd    fp4,fp2,fp8     /* x1 = x_low + tau;  */
123 .L8:
124         fsub    fp5,fp4,fp13    /* r1-= TWO52;  */
125         fadd    fp5,fp5,fp13    /* r1+= TWO52;  */
126 .L9:
127         mtfsf   0x01,fp11       /* restore previous rounding mode.  */
128         fadd    fp1,fp3,fp5     /* y_high = x0 + r1;  */
129         fsub    fp2,fp3,fp1     /* y_low = x0 - y_high + r1;  */
130         fadd    fp2,fp2,fp5
131         blr
132 END (__floorl)
134 long_double_symbol (libm, __floorl, floorl)