1#include "libm.h"
2
3float nextafterf(float x, float y) {
4    union {
5        float f;
6        uint32_t i;
7    } ux = {x}, uy = {y};
8    uint32_t ax, ay, e;
9
10    if (isnan(x) || isnan(y))
11        return x + y;
12    if (ux.i == uy.i)
13        return y;
14    ax = ux.i & 0x7fffffff;
15    ay = uy.i & 0x7fffffff;
16    if (ax == 0) {
17        if (ay == 0)
18            return y;
19        ux.i = (uy.i & 0x80000000) | 1;
20    } else if (ax > ay || ((ux.i ^ uy.i) & 0x80000000))
21        ux.i--;
22    else
23        ux.i++;
24    e = ux.i & 0x7f800000;
25    /* raise overflow if ux.f is infinite and x is finite */
26    if (e == 0x7f800000)
27        FORCE_EVAL(x + x);
28    /* raise underflow if ux.f is subnormal or zero */
29    if (e == 0)
30        FORCE_EVAL(x * x + ux.f * ux.f);
31    return ux.f;
32}
33