]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/ieee754/dbl-64/wordsize-64/s_floor.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / ieee754 / dbl-64 / wordsize-64 / s_floor.c
CommitLineData
d4a28569 1/* Round double to integer away from zero.
f7a9f785 2 Copyright (C) 2011-2016 Free Software Foundation, Inc.
d4a28569
UD
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2011.
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
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
d4a28569
UD
19
20/* Based on a version which carries the following copyright: */
21
22/*
23 * ====================================================
24 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
25 *
26 * Developed at SunPro, a Sun Microsystems, Inc. business.
27 * Permission to use, copy, modify, and distribute this
28 * software is freely granted, provided that this notice
29 * is preserved.
30 * ====================================================
31 */
32
1ed0291c
RH
33#include <math.h>
34#include <math_private.h>
e054f494 35#include <stdint.h>
d4a28569
UD
36
37/*
38 * floor(x)
39 * Return x rounded toward -inf to integral value
40 * Method:
41 * Bit twiddling.
42 * Exception:
43 * Inexact flag raised if x not equal to floor(x).
44 */
45
46static const double huge = 1.0e300;
47
48
49double
50__floor (double x)
51{
52 int64_t i0;
53 EXTRACT_WORDS64(i0,x);
54 int32_t j0 = ((i0>>52)&0x7ff)-0x3ff;
55 if(__builtin_expect(j0<52, 1)) {
56 if(j0<0) { /* raise inexact if x != 0 */
d7826aa1
UD
57 math_force_eval(huge+x);/* return 0*sign(x) if |x|<1 */
58 if(i0>=0) {i0=0;}
59 else if((i0&0x7fffffffffffffffl)!=0)
60 { i0=0xbff0000000000000l;}
d4a28569
UD
61 } else {
62 uint64_t i = (0x000fffffffffffffl)>>j0;
63 if((i0&i)==0) return x; /* x is integral */
d7826aa1
UD
64 math_force_eval(huge+x); /* raise inexact flag */
65 if(i0<0) i0 += (0x0010000000000000l)>>j0;
66 i0 &= (~i);
d4a28569
UD
67 }
68 INSERT_WORDS64(x,i0);
69 } else if (j0==0x400)
70 return x+x; /* inf or NaN */
71 return x;
72}
ad0f5cad 73#ifndef __floor
d4a28569 74weak_alias (__floor, floor)
ad0f5cad 75# ifdef NO_LONG_DOUBLE
d4a28569
UD
76strong_alias (__floor, __floorl)
77weak_alias (__floor, floorl)
ad0f5cad 78# endif
d4a28569 79#endif