2.9
[glibc/nacl-glibc.git] / sysdeps / powerpc / powerpc64 / fpu / s_truncl.S
blob1864fc14b75ce6e8520999f509618f35d086fd93
1 /* long double trunc 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] truncl (long double x [fp1,fp2])  */
33 ENTRY (__truncl)
34         mffs    fp11            /* Save current FPU rounding mode.  */
35         lfd     fp13,.LC0@toc(2)
36         fabs    fp0,fp1
37         fabs    fp9,fp2
38         fsub    fp12,fp13,fp13  /* generate 0.0  */
39         fcmpu   cr7,fp0,fp13    /* if (fabs(x) > TWO52)  */
40         fcmpu   cr6,fp1,fp12    /* if (x > 0.0)  */
41         bnl-    cr7,.L2
42         mtfsfi  7,1             /* Set rounding mode toward 0.  */
43         ble-    cr6,.L1
44         fneg    fp2,fp12
45         fadd    fp1,fp1,fp13    /* x+= TWO52;  */
46         fsub    fp1,fp1,fp13    /* x-= TWO52;  */
47         fabs    fp1,fp1         /* if (x == 0.0) x = 0.0;  */
48 .L0:
49         mtfsf   0x01,fp11       /* restore previous rounding mode.  */
50         blr
51 .L1:
52         fneg    fp2,fp12
53         bge-    cr6,.L0         /* if (x < 0.0)  */
54         fsub    fp1,fp1,fp13    /* x-= TWO52;  */
55         fadd    fp1,fp1,fp13    /* x+= TWO52;  */
56         fnabs   fp1,fp1         /* if (x == 0.0) x = -0.0;  */
57         mtfsf   0x01,fp11       /* restore previous rounding mode.  */
58         blr
60 /* The high double is > TWO52 so we need to round the low double and
61    perhaps the high double.  In this case we have to round the low
62    double and handle any adjustment to the high double that may be
63    caused by rounding (up).  This is complicated by the fact that the
64    high double may already be rounded and the low double may have the
65    opposite sign to compensate.This gets a bit tricky so we use the
66    following algorithm:
68    tau = floor(x_high/TWO52);
69    x0 = x_high - tau;
70    x1 = x_low + tau;
71    r1 = rint(x1);
72    y_high = x0 + r1;
73    y_low = x0 - y_high + r1;
74    return y;  */
75 .L2:
76         fcmpu   cr7,fp9,fp13    /* if (|x_low| > TWO52)  */
77         fcmpu   cr0,fp9,fp12    /* || (|x_low| == 0.0)  */
78         fcmpu   cr5,fp2,fp12    /* if (x_low > 0.0)  */
79         bgelr-  cr7             /*   return x;  */
80         beqlr-  cr0
81         mtfsfi  7,1             /* Set rounding mode toward 0.  */
82         fdiv    fp8,fp1,fp13    /* x_high/TWO52  */
83         
84         bng-    cr6,.L6         /* if (x > 0.0)  */
85         fctidz  fp0,fp8
86         fcfid   fp8,fp0         /* tau = floor(x_high/TWO52);  */
87         fadd    fp8,fp8,fp8     /* tau++; Make tau even  */
88         bng     cr5,.L4         /* if (x_low > 0.0)  */
89         fmr     fp3,fp1
90         fmr     fp4,fp2
91         b       .L5
92 .L4:                            /* if (x_low < 0.0)  */
93         fsub    fp3,fp1,fp8     /* x0 = x_high - tau;  */
94         fadd    fp4,fp2,fp8     /* x1 = x_low + tau;  */
95 .L5:
96         fadd    fp5,fp4,fp13    /* r1 = r1 + TWO52;  */
97         fsub    fp5,fp5,fp13    /* r1 = r1 - TWO52;  */
98         b       .L9
99 .L6:                            /* if (x < 0.0)  */
100         fctidz  fp0,fp8
101         fcfid   fp8,fp0         /* tau = floor(x_high/TWO52);  */
102         fadd    fp8,fp8,fp8     /* tau++; Make tau even  */     
103         bnl     cr5,.L7         /* if (x_low < 0.0)  */
104         fmr     fp3,fp1
105         fmr     fp4,fp2
106         b       .L8
107 .L7:                            /* if (x_low > 0.0)  */
108         fsub    fp3,fp1,fp8     /* x0 = x_high - tau;  */
109         fadd    fp4,fp2,fp8     /* x1 = x_low + tau;  */
110 .L8:
111         fsub    fp5,fp4,fp13    /* r1-= TWO52;  */
112         fadd    fp5,fp5,fp13    /* r1+= TWO52;  */
113 .L9:
114         mtfsf   0x01,fp11       /* restore previous rounding mode.  */
115         fadd    fp1,fp3,fp5     /* y_high = x0 + r1;  */
116         fsub    fp2,fp3,fp1     /* y_low = x0 - y_high + r1;  */
117         fadd    fp2,fp2,fp5
118         blr
119 END (__truncl)
121 long_double_symbol (libm, __truncl, truncl)