2.9
[glibc/nacl-glibc.git] / sysdeps / powerpc / powerpc32 / fpu / s_lround.S
blobd73749e1348d460f98a1074f34a5217c605aac47
1 /* lround function.  PowerPC32 version.
2    Copyright (C) 2004, 2006, 2007, 2008 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         .section        .rodata.cst4,"aM",@progbits,4
24         .align  2
25 .LC0:   /* 0.5 */
26         .long 0x3f000000
27         .section        ".text"
28         
29 /* long [r3] lround (float x [fp1])
30    IEEE 1003.1 lround function.  IEEE specifies "round to the nearest 
31    integer value, rounding halfway cases away from zero, regardless of
32    the current rounding mode."  However PowerPC Architecture defines
33    "round to Nearest" as "Choose the best approximation. In case of a 
34    tie, choose the one that is even (least significant bit o).". 
35    So we can't use the PowerPC "round to Nearest" mode. Instead we set
36    "round toward Zero" mode and round by adding +-0.5 before rounding
37    to the integer value.  It is necessary to detect when x is
38    (+-)0x1.fffffffffffffp-2 because adding +-0.5 in this case will
39    cause an erroneous shift, carry and round.  We simply return 0 if
40    0.5 > x > -0.5.  */
42 ENTRY (__lround)
43         stwu    r1,-16(r1)
44         cfi_adjust_cfa_offset (16)
45 #ifdef SHARED
46         mflr    r11
47         cfi_register(lr,r11)
48 # ifdef HAVE_ASM_PPC_REL16
49         bcl     20,31,1f
50 1:      mflr    r9
51         addis   r9,r9,.LC0-1b@ha
52         lfs     fp10,.LC0-1b@l(r9)
53 # else
54         bl      _GLOBAL_OFFSET_TABLE_@local-4
55         mflr    r10
56         lwz     r9,.LC0@got(10)
57         lfs     fp10,0(r9)
58 # endif
59         mtlr    r11
60         cfi_same_value (lr)
61 #else
62         lis     r9,.LC0@ha
63         lfs     fp10,.LC0@l(r9)
64 #endif
65         fabs    fp2, fp1        /* Get the absolute value of x.  */
66         fsub    fp12,fp10,fp10  /* Compute 0.0.  */
67         fcmpu   cr6, fp2, fp10  /* if |x| < 0.5  */
68         fcmpu   cr7, fp1, fp12  /* x is negative? x < 0.0  */
69         blt-    cr6,.Lretzero
70         fadd    fp3,fp2,fp10    /* |x|+=0.5 bias to prepare to round.  */
71         bge     cr7,.Lconvert   /* x is positive so don't negate x.  */
72         fnabs   fp3,fp3         /* -(|x|+=0.5)  */ 
73 .Lconvert:
74         fctiwz  fp4,fp3         /* Convert to Integer word lround toward 0.  */
75         stfd    fp4,8(r1)
76         nop     /* Ensure the following load is in a different dispatch  */
77         nop     /* group to avoid pipe stall on POWER4&5.  */
78         nop
79         lwz     r3,12(r1)       /* Load return as integer.  */
80 .Lout:
81         addi    r1,r1,16
82         blr
83 .Lretzero:                      /* when 0.5 > x > -0.5  */
84         li      r3,0            /* return 0.  */
85         b       .Lout
86         END (__lround)
88 weak_alias (__lround, lround)
90 strong_alias (__lround, __lroundf)
91 weak_alias (__lround, lroundf)
93 #ifdef NO_LONG_DOUBLE
94 weak_alias (__lround, lroundl)
95 strong_alias (__lround, __lroundl)
96 #endif
97 #if LONG_DOUBLE_COMPAT(libm, GLIBC_2_1)
98 compat_symbol (libm, __lround, lroundl, GLIBC_2_1)
99 #endif