| 1 | /* Round double to integer away from zero. |
| 2 | Copyright (C) 2011-2017 Free Software Foundation, Inc. |
| 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 |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 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 | |
| 33 | #include <math.h> |
| 34 | #include <math_private.h> |
| 35 | #include <stdint.h> |
| 36 | |
| 37 | /* |
| 38 | * floor(x) |
| 39 | * Return x rounded toward -inf to integral value |
| 40 | * Method: |
| 41 | * Bit twiddling. |
| 42 | */ |
| 43 | |
| 44 | |
| 45 | double |
| 46 | __floor (double x) |
| 47 | { |
| 48 | int64_t i0; |
| 49 | EXTRACT_WORDS64(i0,x); |
| 50 | int32_t j0 = ((i0>>52)&0x7ff)-0x3ff; |
| 51 | if(__builtin_expect(j0<52, 1)) { |
| 52 | if(j0<0) { |
| 53 | /* return 0*sign(x) if |x|<1 */ |
| 54 | if(i0>=0) {i0=0;} |
| 55 | else if((i0&0x7fffffffffffffffl)!=0) |
| 56 | { i0=0xbff0000000000000l;} |
| 57 | } else { |
| 58 | uint64_t i = (0x000fffffffffffffl)>>j0; |
| 59 | if((i0&i)==0) return x; /* x is integral */ |
| 60 | if(i0<0) i0 += (0x0010000000000000l)>>j0; |
| 61 | i0 &= (~i); |
| 62 | } |
| 63 | INSERT_WORDS64(x,i0); |
| 64 | } else if (j0==0x400) |
| 65 | return x+x; /* inf or NaN */ |
| 66 | return x; |
| 67 | } |
| 68 | #ifndef __floor |
| 69 | weak_alias (__floor, floor) |
| 70 | # ifdef NO_LONG_DOUBLE |
| 71 | strong_alias (__floor, __floorl) |
| 72 | weak_alias (__floor, floorl) |
| 73 | # endif |
| 74 | #endif |
| 75 | |