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