1286438Sbapt/* @(#)s_floor.c 5.1 93/09/24 */ 2286438Sbapt/* 3286438Sbapt * ==================================================== 4286438Sbapt * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5286438Sbapt * 6286438Sbapt * Developed at SunPro, a Sun Microsystems, Inc. business. 7286438Sbapt * Permission to use, copy, modify, and distribute this 8286438Sbapt * software is freely granted, provided that this notice 9286438Sbapt * is preserved. 10286438Sbapt * ==================================================== 11286438Sbapt */ 12286438Sbapt 13286438Sbapt#include <sys/cdefs.h> 14286438Sbapt__FBSDID("$FreeBSD$"); 15312336Sbapt 16286438Sbapt/* 17286438Sbapt * truncf(x) 18286438Sbapt * Return x rounded toward 0 to integral value 19286438Sbapt * Method: 20286438Sbapt * Bit twiddling. 21286438Sbapt * Exception: 22286438Sbapt * Inexact flag raised if x not equal to truncf(x). 23286438Sbapt */ 24286438Sbapt 25286438Sbapt#include "math.h" 26286438Sbapt#include "math_private.h" 27286438Sbapt 28286438Sbaptstatic const float huge = 1.0e30F; 29286438Sbapt 30286438Sbaptfloat 31286438Sbapttruncf(float x) 32286438Sbapt{ 33286438Sbapt int32_t i0,j0; 34286438Sbapt u_int32_t i; 35286438Sbapt GET_FLOAT_WORD(i0,x); 36286438Sbapt j0 = ((i0>>23)&0xff)-0x7f; 37286438Sbapt if(j0<23) { 38286438Sbapt if(j0<0) { /* raise inexact if x != 0 */ 39286438Sbapt if(huge+x>0.0F) /* |x|<1, so return 0*sign(x) */ 40286438Sbapt i0 &= 0x80000000; 41286438Sbapt } else { 42286438Sbapt i = (0x007fffff)>>j0; 43286438Sbapt if((i0&i)==0) return x; /* x is integral */ 44286438Sbapt if(huge+x>0.0F) /* raise inexact flag */ 45286438Sbapt i0 &= (~i); 46286438Sbapt } 47286438Sbapt } else { 48286438Sbapt if(j0==0x80) return x+x; /* inf or NaN */ 49325928Sbapt else return x; /* x is integral */ 50325928Sbapt } 51325928Sbapt SET_FLOAT_WORD(x,i0); 52286438Sbapt return x; 53286438Sbapt} 54312336Sbapt