Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.    nextafterl.c
  3.    Contributed by Danny Smith <dannysmith@users.sourceforge.net>
  4.    No copyright claimed, absolutely no warranties.
  5.  
  6.    2005-05-09
  7. */
  8.  
  9. #include <math.h>
  10.  
  11. long double
  12. nextafterl (long double x, long double y)
  13. {
  14.   union {
  15.       long double ld;
  16.       struct {
  17.         unsigned long long mantissa;
  18.         unsigned short expn;
  19.         unsigned short pad;
  20.       } __attribute__ ((packed)) parts;
  21.   } u;
  22.  
  23.   /* The normal bit is explicit for long doubles, unlike
  24.      float and double.  */
  25.   static const unsigned long long normal_bit = 0x8000000000000000ull;
  26.  
  27.   if (isnan (y) || isnan (x))
  28.     return x + y;
  29.  
  30.   if (x == y )
  31.      /* nextafter (0.0, -O.0) should return -0.0.  */
  32.      return y;
  33.  
  34.   u.ld = x;
  35.   if (x == 0.0L)
  36.     {
  37.       u.parts.mantissa = 1ull;
  38.       return y > 0.0L ? u.ld : -u.ld;
  39.     }
  40.  
  41.   if (((x > 0.0L) ^ (y > x)) == 0)
  42.     {
  43.       u.parts.mantissa++;
  44.       if ((u.parts.mantissa & ~normal_bit) == 0ull)
  45.         u.parts.expn++;
  46.     }
  47.   else
  48.     {
  49.       if ((u.parts.mantissa & ~normal_bit) == 0ull)
  50.         u.parts.expn--;
  51.       u.parts.mantissa--;
  52.     }
  53.  
  54.   /* If we have updated the expn of a normal number,
  55.      or moved from denormal to normal, [re]set the normal bit.  */
  56.      
  57.   if (u.parts.expn & 0x7fff)
  58.     u.parts.mantissa |=  normal_bit;
  59.  
  60.   return u.ld;
  61. }
  62.  
  63. /* nexttowardl is the same function with a different name.  */
  64. long double
  65. nexttowardl (long double, long double) __attribute__ ((alias("nextafterl")));
  66.