summaryrefslogblamecommitdiff
path: root/src/math/nexttowardf.c
blob: 8648be6a684a669a2fd3a37784ccdac3733f060a (plain) (tree)
1
2
3
4
5
6
7
8
9

                 

                                         

                        
 

                                 
                   















                                              
         




                                                                    

                                                    
                        
 
#include "libm.h"

float nexttowardf(float x, long double y)
{
	union fshape ux;
	uint32_t e;

	if (isnan(x) || isnan(y))
		return x + y;
	if (x == y)
		return y;
	ux.value = x;
	if (x == 0) {
		ux.bits = 1;
		if (signbit(y))
			ux.bits |= 0x80000000;
	} else if (x < y) {
		if (signbit(x))
			ux.bits--;
		else
			ux.bits++;
	} else {
		if (signbit(x))
			ux.bits++;
		else
			ux.bits--;
	}
	e = ux.bits & 0x7f800000;
	/* raise overflow if ux.value is infinite and x is finite */
	if (e == 0x7f800000)
		return x + x;
	/* raise underflow if ux.value is subnormal or zero */
	if (e == 0)
		FORCE_EVAL(x*x + ux.value*ux.value);
	return ux.value;
}