/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include "libm.h"
#include <fenv.h>
float
__nearbyintf(float x) {
union {
unsigned i;
float f;
} xx;
int rm;
xx.f = x;
/* handle trivial cases */
return (x * x); /* + -> * for Cheetah */
return (x);
} else if (hx == 0) /* x is zero */
return (x);
/* get the rounding mode */
rm = fegetround();
/* flip the sense of directed roundings if x is negative */
/* handle |x| < 1 */
if (hx < 0x3f800000) {
else
return (xx.f);
}
/* round x at the integer bit */
if (!frac)
return (x);
hx &= ~(i - 1);
else
return (xx.f);
}
#if 0
/*
* Alternate implementations for SPARC, x86, using fp ops. These may
* be faster depending on how expensive saving and restoring the fp
* modes and status flags is.
*/
#include "libm.h"
#include "fma.h"
#if defined(__sparc)
float
__nearbyintf(float x) {
union {
unsigned i;
float f;
float z;
int rm;
xx.f = x;
/* handle trivial cases */
return (x + 0.0f);
else if (hx == 0) /* x is zero */
return (x);
/* save the fsr */
/* handle |x| < 1 */
if (hx < 0x3f800000) {
/* flip the sense of directed roundings if x is negative */
if (sx)
else
return (xx.f);
}
/* clear the inexact trap */
__fenv_setfsr(&fsr);
/* round x at the integer bit */
/* restore the old fsr */
return (z);
}
/* inline template */
extern long double frndint(long double);
float
__nearbyintf(float x) {
long double z;
/* save the control and status words, mask the inexact exception */
z = frndint((long double) x);
/*
* restore the control and status words, preserving all but the
* inexact flag
*/
/* note: the value of z is representable in single precision */
return (z);
}
#else
#endif
#endif