summaryrefslogblamecommitdiff
path: root/src/math/nexttowardf.c
blob: e8e6f67641ae672f9534f9dbe95b01f29f2ed53a (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) {
		volatile float z;
		z = x*x + ux.value*ux.value;
	}
	return ux.value;
}