home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Mobile / Chip_Mobile_2001.iso / palm / hobby / palmoon / palmoon.EXE / s_trunc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-16  |  1.8 KB  |  64 lines

  1. // 15 August 1997, Rick Huebner:  Small changes made to adapt for MathLib
  2.  
  3. /* Truncate argument to nearest integral value not larger than the argument.
  4.    Copyright (C) 1997 Free Software Foundation, Inc.
  5.    This file is part of the GNU C Library.
  6.    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
  7.  
  8.    The GNU C Library is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU Library General Public License as
  10.    published by the Free Software Foundation; either version 2 of the
  11.    License, or (at your option) any later version.
  12.  
  13.    The GNU C Library is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    Library General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU Library General Public
  19.    License along with the GNU C Library; see the file COPYING.LIB.  If not,
  20.    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21.    Boston, MA 02111-1307, USA.  */
  22.  
  23. #include "math.h"
  24.  
  25. #include "math_private.h"
  26.  
  27.  
  28. double
  29. __trunc (double x)
  30. {
  31.   int32_t i0, j0;
  32.   u_int32_t i1;
  33.   int sx;
  34.  
  35.   EXTRACT_WORDS (i0, i1, x);
  36.   sx = i0 & 0x80000000;
  37.   j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
  38.   if (j0 < 20)
  39.     {
  40.       if (j0 < 0)
  41.     /* The magnitude of the number is < 1 so the result is +-0.  */
  42.     INSERT_WORDS (x, sx, 0);
  43.       else
  44.     INSERT_WORDS (x, sx | i0 & ~(0x000fffff >> j0), 0);
  45.     }
  46.   else if (j0 > 51)
  47.     {
  48.       if (j0 == 0x400)
  49.     /* x is inf or NaN.  */
  50.     return x + x;
  51.     }
  52.   else
  53.     {
  54.       INSERT_WORDS (x, i0, i1 & ~(0xffffffffu >> (j0 - 20)));
  55.     }
  56.  
  57.   return x;
  58. }
  59. weak_alias (__trunc, trunc)
  60. #ifdef NO_LONG_DOUBLE
  61. strong_alias (__trunc, __truncl)
  62. weak_alias (__trunc, truncl)
  63. #endif
  64.