2.9
[glibc/nacl-glibc.git] / sysdeps / powerpc / powerpc64 / fpu / s_roundl.S
blob20da828a82d067a06ac4036b5e08b8da8985a3f2
1 /* long double round 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
27 .LC1:   /* 0.5 */
28         .tc FD_3fe00000_0[TC],0x3fe0000000000000
29         .section        ".text"
31 /* long double [fp1,fp2] roundl (long double x [fp1,fp2])
32    IEEE 1003.1 round function.  IEEE specifies "round to the nearest
33    integer value, rounding halfway cases away from zero, regardless of
34    the current rounding mode."  However PowerPC Architecture defines
35    "Round to Nearest" as "Choose the best approximation. In case of a
36    tie, choose the one that is even (least significant bit o).".
37    So we can't use the PowerPC "Round to Nearest" mode. Instead we set
38    "Round toward Zero" mode and round by adding +-0.5 before rounding
39    to the integer value.  */
41 ENTRY (__roundl)
42         mffs    fp11            /* Save current FPU rounding mode.  */
43         lfd     fp13,.LC0@toc(2)
44         fabs    fp0,fp1
45         fabs    fp9,fp2
46         fsub    fp12,fp13,fp13  /* generate 0.0  */
47         fcmpu   cr7,fp0,fp13    /* if (fabs(x) > TWO52)  */
48         fcmpu   cr6,fp1,fp12    /* if (x > 0.0)  */
49         bnl-    cr7,.L2
50         mtfsfi  7,1             /* Set rounding mode toward 0.  */
51         lfd     fp10,.LC1@toc(2)
52         ble-    cr6,.L1
53         fneg    fp2,fp12
54         fadd    fp1,fp1,fp10    /* x+= 0.5;  */
55         fadd    fp1,fp1,fp13    /* x+= TWO52;  */
56         fsub    fp1,fp1,fp13    /* x-= TWO52;  */
57         fabs    fp1,fp1         /* if (x == 0.0) x = 0.0;  */
58 .L0:
59         mtfsf   0x01,fp11       /* restore previous rounding mode.  */
60         blr
61 .L1:
62         fsub    fp9,fp1,fp10    /* x-= 0.5;  */
63         fneg    fp2,fp12
64         bge-    cr6,.L0         /* if (x < 0.0)  */
65         fsub    fp1,fp9,fp13    /* x-= TWO52;  */
66         fadd    fp1,fp1,fp13    /* x+= TWO52;  */
67         fnabs   fp1,fp1         /* if (x == 0.0) x = -0.0;  */
68         mtfsf   0x01,fp11       /* restore previous rounding mode.  */
69         blr
71 /* The high double is > TWO52 so we need to round the low double and
72    perhaps the high double.  In this case we have to round the low
73    double and handle any adjustment to the high double that may be
74    caused by rounding (up).  This is complicated by the fact that the
75    high double may already be rounded and the low double may have the
76    opposite sign to compensate.This gets a bit tricky so we use the
77    following algorithm:
79    tau = floor(x_high/TWO52);
80    x0 = x_high - tau;
81    x1 = x_low + tau;
82    r1 = rint(x1);
83    y_high = x0 + r1;
84    y_low = x0 - y_high + r1;
85    return y;  */
86 .L2:
87         fcmpu   cr7,fp9,fp13    /* if (|x_low| > TWO52)  */
88         fcmpu   cr0,fp9,fp12    /* || (|x_low| == 0.0)  */
89         fcmpu   cr5,fp2,fp12    /* if (x_low > 0.0)  */
90         lfd     fp10,.LC1@toc(2)
91         bgelr-  cr7             /*   return x;  */
92         beqlr-  cr0
93         mtfsfi  7,1             /* Set rounding mode toward 0.  */
94         fdiv    fp8,fp1,fp13    /* x_high/TWO52  */
95         
96         bng-    cr6,.L6         /* if (x > 0.0)  */
97         fctidz  fp0,fp8
98         fcfid   fp8,fp0         /* tau = floor(x_high/TWO52);  */
99         bng     cr5,.L4         /* if (x_low > 0.0)  */
100         fmr     fp3,fp1
101         fmr     fp4,fp2
102         b       .L5
103 .L4:                            /* if (x_low < 0.0)  */
104         fsub    fp3,fp1,fp8     /* x0 = x_high - tau;  */
105         fadd    fp4,fp2,fp8     /* x1 = x_low + tau;  */
106 .L5:
107         fadd    fp5,fp4,fp10    /* r1 = x1 + 0.5;  */
108         fadd    fp5,fp5,fp13    /* r1 = r1 + TWO52;  */
109         fsub    fp5,fp5,fp13    /* r1 = r1 - TWO52;  */
110         b       .L9
111 .L6:                            /* if (x < 0.0)  */
112         fctidz  fp0,fp8
113         fcfid   fp8,fp0         /* tau = floor(x_high/TWO52);  */       
114         bnl     cr5,.L7         /* if (x_low < 0.0)  */
115         fmr     fp3,fp1
116         fmr     fp4,fp2
117         b       .L8
118 .L7:                            /* if (x_low > 0.0)  */
119         fsub    fp3,fp1,fp8     /* x0 = x_high - tau;  */
120         fadd    fp4,fp2,fp8     /* x1 = x_low + tau;  */
121 .L8:
122         fsub    fp5,fp4,fp10    /* r1 = x1 - 0.5;  */
123         fsub    fp5,fp5,fp13    /* r1-= TWO52;  */
124         fadd    fp5,fp5,fp13    /* r1+= TWO52;  */
125 .L9:
126         mtfsf   0x01,fp11       /* restore previous rounding mode.  */
127         fadd    fp1,fp3,fp5     /* y_high = x0 + r1;  */
128         fsub    fp2,fp3,fp1     /* y_low = x0 - y_high + r1;  */
129         fadd    fp2,fp2,fp5
130         blr
131 END (__roundl)
133 long_double_symbol (libm, __roundl, roundl)