Back to index

glibc  2.9
s_llrint.c
Go to the documentation of this file.
00001 /* Round argument to nearest integral value according to current rounding
00002    direction.
00003    Copyright (C) 1997, 1999 Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005    Contributed by Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
00006 
00007    The GNU C Library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2.1 of the License, or (at your option) any later version.
00011 
00012    The GNU C Library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with the GNU C Library; if not, write to the Free
00019    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00020    02111-1307 USA.  */
00021 
00022 #include <math.h>
00023 #include "math_private.h"
00024 #include "mathimpl.h"
00025 
00026 long long int
00027 __llrint (double x)
00028 {
00029   int32_t e;
00030   u_int32_t h, l, s;
00031   long long int result;
00032 
00033   x = __m81_u(__rint) (x);
00034 
00035   /* We could use __fixxfdi from libgcc, but here we can take advantage of
00036      the known floating point format.  */
00037   EXTRACT_WORDS (h, l, x);
00038 
00039   e = ((h >> 20) & 0x7ff) - 0x3ff;
00040   if (e < 0)
00041     return 0;
00042   s = h;
00043   h &= 0xfffff;
00044   h |= 0x100000;
00045 
00046   if (e < 63)
00047     {
00048       if (e > 52)
00049        {
00050          h <<= e - 52;
00051          h |= l >> (84 - e);
00052          l <<= e - 52;
00053          result = ((long long int) h << 32) | l;
00054        }
00055       else if (e > 20)
00056        {
00057          l >>= 52 - e;
00058          l |= h << (e - 20);
00059          h >>= 52 - e;
00060          result = ((long long int) h << 32) | l;
00061        }
00062       else
00063        result = h >> (20 - e);
00064       if (s & 0x80000000)
00065        result = -result;
00066     }
00067   else
00068     /* The number is too large or not finite.  The standard leaves it
00069        undefined what to return when the number is too large to fit in a
00070        `long long int'.  */
00071     result = -1LL;
00072 
00073   return result;
00074 }
00075 
00076 weak_alias (__llrint, llrint)