1 /* $NetBSD: s_nexttowardf.c,v 1.5 2024/05/05 14:06:47 riastradh Exp $ */ 2 3 /* 4 * ==================================================== 5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 6 * 7 * Developed at SunPro, a Sun Microsystems, Inc. business. 8 * Permission to use, copy, modify, and distribute this 9 * software is freely granted, provided that this notice 10 * is preserved. 11 * ==================================================== 12 */ 13 14 #include <sys/cdefs.h> 15 #if 0 16 __FBSDID("$FreeBSD: src/lib/msun/src/s_nexttowardf.c,v 1.3 2011/02/10 07:38:38 das Exp $"); 17 #else 18 __RCSID("$NetBSD: s_nexttowardf.c,v 1.5 2024/05/05 14:06:47 riastradh Exp $"); 19 #endif 20 21 #include <string.h> 22 #include <float.h> 23 #include <machine/ieee.h> 24 25 #include "math.h" 26 #include "math_private.h" 27 28 /* 29 * On ports where long double is just double, reuse the ieee_double_u 30 * union as if it were ieee_ext_u -- broken-down components of (long) 31 * double values. 32 */ 33 #ifndef __HAVE_LONG_DOUBLE 34 #define ieee_ext_u ieee_double_u 35 #define extu_ld dblu_d 36 #define extu_ext dblu_dbl 37 #define ext_sign dbl_sign 38 #define ext_exp dbl_exp 39 #define ext_frach dbl_frach 40 #define ext_fracl dbl_fracl 41 #define EXT_EXP_INFNAN DBL_EXP_INFNAN 42 #define LDBL_NBIT 0 43 #endif 44 45 /* 46 * XXX We should arrange to define LDBL_NBIT unconditionally in the 47 * appropriate MD header file. 48 */ 49 #ifdef LDBL_IMPLICIT_NBIT 50 #define LDBL_NBIT 0 51 #endif 52 53 float 54 nexttowardf(float x, long double y) 55 { 56 volatile float t; 57 int32_t hx,ix; 58 union ieee_ext_u uy; 59 60 GET_FLOAT_WORD(hx,x); 61 ix = hx&0x7fffffff; /* |x| */ 62 63 memset(&uy, 0, sizeof(uy)); 64 uy.extu_ld = y; 65 uy.extu_ext.ext_frach &= ~LDBL_NBIT; 66 67 if((ix>0x7f800000) || 68 (uy.extu_ext.ext_exp == EXT_EXP_INFNAN && 69 (uy.extu_ext.ext_frach | uy.extu_ext.ext_fracl) != 0)) 70 return x+y; /* x or y is nan */ 71 if(x==y) return (float)y; /* x=y, return y */ 72 if(ix==0) { /* x == 0 */ 73 SET_FLOAT_WORD(x,(uy.extu_ext.ext_sign<<31)|1);/* return +-minsubnormal */ 74 t = x*x; 75 if(t==x) return t; else return x; /* raise underflow flag */ 76 } 77 if((hx >= 0) ^ (x < y)) /* x -= ulp */ 78 hx -= 1; 79 else /* x += ulp */ 80 hx += 1; 81 ix = hx&0x7f800000; 82 if(ix>=0x7f800000) return x+x; /* overflow */ 83 if(ix<0x00800000) { /* underflow */ 84 t = x*x; 85 if(t!=x) { /* raise underflow flag */ 86 SET_FLOAT_WORD(x,hx); 87 return x; 88 } 89 } 90 SET_FLOAT_WORD(x,hx); 91 return x; 92 } 93