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