Subversion Repositories Kolibri OS

Rev

Rev 4872 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1.  
  2. /* @(#)s_ilogb.c 5.1 93/09/24 */
  3. /*
  4.  * ====================================================
  5.  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  6.  *
  7.  * Developed at SunPro, a Sun Microsystems, Inc. business.
  8.  * Permission to use, copy, modify, and distribute this
  9.  * software is freely granted, provided that this notice
  10.  * is preserved.
  11.  * ====================================================
  12.  */
  13.  
  14. /*
  15. FUNCTION
  16.        <<ilogb>>, <<ilogbf>>---get exponent of floating-point number
  17. INDEX
  18.         ilogb
  19. INDEX
  20.         ilogbf
  21.  
  22. ANSI_SYNOPSIS
  23.         #include <math.h>
  24.         int ilogb(double <[val]>);
  25.         int ilogbf(float <[val]>);
  26.  
  27. TRAD_SYNOPSIS
  28.         #include <math.h>
  29.         int ilogb(<[val]>)
  30.         double <[val]>;
  31.  
  32.         int ilogbf(<[val]>)
  33.         float <[val]>;
  34.  
  35.  
  36. DESCRIPTION
  37.  
  38.         All nonzero, normal numbers can be described as <[m]> *
  39.         2**<[p]>.  <<ilogb>> and <<ilogbf>> examine the argument
  40.         <[val]>, and return <[p]>.  The functions <<frexp>> and
  41.         <<frexpf>> are similar to <<ilogb>> and <<ilogbf>>, but also
  42.         return <[m]>.
  43.  
  44. RETURNS
  45.  
  46. <<ilogb>> and <<ilogbf>> return the power of two used to form the
  47. floating-point argument.
  48. If <[val]> is <<0>>, they return <<FP_ILOGB0>>.
  49. If <[val]> is infinite, they return <<INT_MAX>>.
  50. If <[val]> is NaN, they return <<FP_ILOGBNAN>>.
  51. (<<FP_ILOGB0>> and <<FP_ILOGBNAN>> are defined in math.h, but in turn are
  52. defined as INT_MIN or INT_MAX from limits.h.  The value of FP_ILOGB0 may be
  53. either INT_MIN or -INT_MAX.  The value of FP_ILOGBNAN may be either INT_MAX or
  54. INT_MIN.)
  55.  
  56. @comment The bugs might not be worth noting, given the mass non-C99/POSIX
  57. @comment behavior of much of the Newlib math library.
  58. @comment BUGS
  59. @comment On errors, errno is not set per C99 and POSIX requirements even if
  60. @comment (math_errhandling & MATH_ERRNO) is non-zero.
  61.  
  62. PORTABILITY
  63. C99, POSIX
  64. */
  65.  
  66. /* ilogb(double x)
  67.  * return the binary exponent of non-zero x
  68.  * ilogb(0) = 0x80000001
  69.  * ilogb(inf/NaN) = 0x7fffffff (no signal is raised)
  70.  */
  71.  
  72. #include <limits.h>
  73. #include "fdlibm.h"
  74.  
  75. #ifndef _DOUBLE_IS_32BITS
  76.  
  77. #ifdef __STDC__
  78.         int ilogb(double x)
  79. #else
  80.         int ilogb(x)
  81.         double x;
  82. #endif
  83. {
  84.         __int32_t hx,lx,ix;
  85.  
  86.         EXTRACT_WORDS(hx,lx,x);
  87.         hx &= 0x7fffffff;
  88.         if(hx<0x00100000) {
  89.             if((hx|lx)==0)
  90.                 return FP_ILOGB0;       /* ilogb(0) = special case error */
  91.             else                        /* subnormal x */
  92.                 if(hx==0) {
  93.                     for (ix = -1043; lx>0; lx<<=1) ix -=1;
  94.                 } else {
  95.                     for (ix = -1022,hx<<=11; hx>0; hx<<=1) ix -=1;
  96.                 }
  97.             return ix;
  98.         }
  99.         else if (hx<0x7ff00000) return (hx>>20)-1023;
  100.         #if FP_ILOGBNAN != INT_MAX
  101.         else if (hx>0x7ff00000) return FP_ILOGBNAN;     /* NAN */
  102.         #endif
  103.         else return INT_MAX;    /* infinite (or, possibly, NAN) */
  104. }
  105.  
  106. #endif /* _DOUBLE_IS_32BITS */
  107.