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