2002-06-08 05:59:57 +08:00
|
|
|
|
|
|
|
/* @(#)s_lrint.c 5.1 93/09/24 */
|
|
|
|
/*
|
|
|
|
* ====================================================
|
|
|
|
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Developed at SunPro, a Sun Microsystems, Inc. business.
|
|
|
|
* Permission to use, copy, modify, and distribute this
|
|
|
|
* software is freely granted, provided that this notice
|
|
|
|
* is preserved.
|
|
|
|
* ====================================================
|
|
|
|
*/
|
2009-03-25 Craig Howland <howland@LGSInnovations.com>
* libc/include/math.h: (llround, llroundf): Declare.
* libm/common/s_llround.c: New file, implementing llround().
* libm/common/sf_llround.c: New file, implementing llroundf().
* libm/common/sf_lround.c: Remove spurious cast in _DOUBLE_IS_32BITS
version of function.
* libm/common/sf_lrint.c: Ditto.
* libm/common/sf_logb.c: Corrected return for subnormal argument
by replacing existing function with a version created from sf_ilogb.c.
* libm/common/s_logb.c: Ditto, except starting point s_ilogb.c. Also
added documentation for logb() and logbf().
* libm/common/s_signbit.c: Add signbit() documentation.
* libm/common/s_log2.c: Update return values to match what w_log2.c has,
since log2 uses log(); add note about being derived instead of direct.
* libm/common/sf_fma.c: Add casts to attempt to get correct results,
as well as comments pointing out problems with the implementation.
* libm/common/s_fma.c: Add fma() and fmaf() documentation.
* libm/common/sf_remquo.c: Incorrect quotient returns for large values
corrected by discarding existing function and replacing with Sun
verion, with some enhancements.
* libm/common/s_remquo.c: Ditto. Add remquo() and remquof()
documentation.
* libm/common/s_fmax.c: Add fmax() and fmaxf() documentation.
* libm/common/s_fmin.c: Add fmin() and fminf() documentation.
* libm/common/s_fdim.c: Return NAN for NAN arg, add fdim() and fdimf()
documentation.
* libm/common/sf_fdim.c: Return NAN for NAN arg, HUGE_VALF for inf arg.
* libm/common/s_trunc.c: Add trunc() and truncf() documentation.
* libm/common/s_rint.c: Add rint() and rintf() documentation.
* libm/common/s_round.c: Add round() and roundf() documentation.
* libm/common/s_scalbn.c: Add scalbln() and scalblnf() documentation.
* libm/common/s_infinity.c: Add infinity() and infinityf()
documentation.
* libm/common/s_lround.c: Add lround(), lroundf(), llround(), and
llroundf() documentation.
* libm/common/s_lrint.c: Add lrint(), lrintf(), llrint(), and llrintf()
documentation.
* libm/common/isgreater.c: New file for documenting math.h function-like
macros isgreater(), isgreaterequal(), isless(), islessequal(),
islessgreater(), and isunordered().
* libm/common/s_isnan.c: Add documentation for function-like macros
fpclassify(), isfinite(), isinf(), isnan(), and isnormal().
* libm/common/s_nearbyint.c: Add nearbyint() and nearbyintf()
documentation.
* libm/common/Makefile.am: Add s_llround.c (src); sf_llround.c (fsrc);
s_fdim.def, s_fma.def, s_fmax.def, s_fmin.def,
s_logb.def, s_lrint.def, s_lround.def, s_nearbyint.def, s_remquo.def,
s_rint.def, s_round.def, s_signbit.def, s_trunc.def, and
isgreater.def (chobj);
re-name all existing chew files (chobj) to match source file base
names (put in underscores), delete all special targets for chew files
(leaving all to be generated by rule).
* libm/common/Makefile.in: regenerate.
* libm/math/w_exp2.c: Add "base 2" to documentation description (and
delete TRAD_SYNOPSIS).
* libm/math/w_gamma.c: Add tgamma() and tgammaf() documentation, along
with some history behind the function names.
* libm/math/math.tex: Add includes for newly-added documentation (see
.def additions to common/Makefile.am and math/Makefile.am in this
ChangeLog list), adjusted existing .def file names to match source file
base names (added underscores); add mention of HUGE_VALF; rename
"Version of library" section to "Error Handling" and add some text
about floating-point exception; added section "Standards Compliance And
Portability".
* libm/math/Makefile.am: Add w_exp2.def (chobj);
re-name all existing chew files (chobj) to match source file base
names, delete all special targets for chew files (leaving all to be
generated by rule).
* libm/math/Makefile.in: regenerated
* doc/makedoc.c: Change silent ignoring of commands < 5 characters
to a failure when reading macro file for commands < 4 characters;
add -v (verbose) option for printing some debugging information;
get rid of spurious translation of "@*" to "*" (no source files used @*,
so no existing doc pages were affected); clean up some compiler
warnings.
* doc/doc.str: add BUGS and SEEALSO sections (to match texi2pod.pl
which has them); Remove ITEM command (redundant with makedoc built-in
"o", not used in any present source file so nothing is lost, anyway).
* HOWTO: New file to hold information for maintainers regarding how
to do things. Initial sections on documentation and ELIX levels.
2009-03-26 03:13:24 +08:00
|
|
|
/*
|
|
|
|
FUNCTION
|
2015-11-03 05:38:30 +08:00
|
|
|
<<lrint>>, <<lrintf>>, <<llrint>>, <<llrintf>>---round to integer
|
2009-03-25 Craig Howland <howland@LGSInnovations.com>
* libc/include/math.h: (llround, llroundf): Declare.
* libm/common/s_llround.c: New file, implementing llround().
* libm/common/sf_llround.c: New file, implementing llroundf().
* libm/common/sf_lround.c: Remove spurious cast in _DOUBLE_IS_32BITS
version of function.
* libm/common/sf_lrint.c: Ditto.
* libm/common/sf_logb.c: Corrected return for subnormal argument
by replacing existing function with a version created from sf_ilogb.c.
* libm/common/s_logb.c: Ditto, except starting point s_ilogb.c. Also
added documentation for logb() and logbf().
* libm/common/s_signbit.c: Add signbit() documentation.
* libm/common/s_log2.c: Update return values to match what w_log2.c has,
since log2 uses log(); add note about being derived instead of direct.
* libm/common/sf_fma.c: Add casts to attempt to get correct results,
as well as comments pointing out problems with the implementation.
* libm/common/s_fma.c: Add fma() and fmaf() documentation.
* libm/common/sf_remquo.c: Incorrect quotient returns for large values
corrected by discarding existing function and replacing with Sun
verion, with some enhancements.
* libm/common/s_remquo.c: Ditto. Add remquo() and remquof()
documentation.
* libm/common/s_fmax.c: Add fmax() and fmaxf() documentation.
* libm/common/s_fmin.c: Add fmin() and fminf() documentation.
* libm/common/s_fdim.c: Return NAN for NAN arg, add fdim() and fdimf()
documentation.
* libm/common/sf_fdim.c: Return NAN for NAN arg, HUGE_VALF for inf arg.
* libm/common/s_trunc.c: Add trunc() and truncf() documentation.
* libm/common/s_rint.c: Add rint() and rintf() documentation.
* libm/common/s_round.c: Add round() and roundf() documentation.
* libm/common/s_scalbn.c: Add scalbln() and scalblnf() documentation.
* libm/common/s_infinity.c: Add infinity() and infinityf()
documentation.
* libm/common/s_lround.c: Add lround(), lroundf(), llround(), and
llroundf() documentation.
* libm/common/s_lrint.c: Add lrint(), lrintf(), llrint(), and llrintf()
documentation.
* libm/common/isgreater.c: New file for documenting math.h function-like
macros isgreater(), isgreaterequal(), isless(), islessequal(),
islessgreater(), and isunordered().
* libm/common/s_isnan.c: Add documentation for function-like macros
fpclassify(), isfinite(), isinf(), isnan(), and isnormal().
* libm/common/s_nearbyint.c: Add nearbyint() and nearbyintf()
documentation.
* libm/common/Makefile.am: Add s_llround.c (src); sf_llround.c (fsrc);
s_fdim.def, s_fma.def, s_fmax.def, s_fmin.def,
s_logb.def, s_lrint.def, s_lround.def, s_nearbyint.def, s_remquo.def,
s_rint.def, s_round.def, s_signbit.def, s_trunc.def, and
isgreater.def (chobj);
re-name all existing chew files (chobj) to match source file base
names (put in underscores), delete all special targets for chew files
(leaving all to be generated by rule).
* libm/common/Makefile.in: regenerate.
* libm/math/w_exp2.c: Add "base 2" to documentation description (and
delete TRAD_SYNOPSIS).
* libm/math/w_gamma.c: Add tgamma() and tgammaf() documentation, along
with some history behind the function names.
* libm/math/math.tex: Add includes for newly-added documentation (see
.def additions to common/Makefile.am and math/Makefile.am in this
ChangeLog list), adjusted existing .def file names to match source file
base names (added underscores); add mention of HUGE_VALF; rename
"Version of library" section to "Error Handling" and add some text
about floating-point exception; added section "Standards Compliance And
Portability".
* libm/math/Makefile.am: Add w_exp2.def (chobj);
re-name all existing chew files (chobj) to match source file base
names, delete all special targets for chew files (leaving all to be
generated by rule).
* libm/math/Makefile.in: regenerated
* doc/makedoc.c: Change silent ignoring of commands < 5 characters
to a failure when reading macro file for commands < 4 characters;
add -v (verbose) option for printing some debugging information;
get rid of spurious translation of "@*" to "*" (no source files used @*,
so no existing doc pages were affected); clean up some compiler
warnings.
* doc/doc.str: add BUGS and SEEALSO sections (to match texi2pod.pl
which has them); Remove ITEM command (redundant with makedoc built-in
"o", not used in any present source file so nothing is lost, anyway).
* HOWTO: New file to hold information for maintainers regarding how
to do things. Initial sections on documentation and ELIX levels.
2009-03-26 03:13:24 +08:00
|
|
|
INDEX
|
|
|
|
lrint
|
|
|
|
INDEX
|
|
|
|
lrintf
|
|
|
|
INDEX
|
|
|
|
llrint
|
|
|
|
INDEX
|
|
|
|
llrintf
|
|
|
|
|
2017-11-30 16:36:17 +08:00
|
|
|
SYNOPSIS
|
2009-03-25 Craig Howland <howland@LGSInnovations.com>
* libc/include/math.h: (llround, llroundf): Declare.
* libm/common/s_llround.c: New file, implementing llround().
* libm/common/sf_llround.c: New file, implementing llroundf().
* libm/common/sf_lround.c: Remove spurious cast in _DOUBLE_IS_32BITS
version of function.
* libm/common/sf_lrint.c: Ditto.
* libm/common/sf_logb.c: Corrected return for subnormal argument
by replacing existing function with a version created from sf_ilogb.c.
* libm/common/s_logb.c: Ditto, except starting point s_ilogb.c. Also
added documentation for logb() and logbf().
* libm/common/s_signbit.c: Add signbit() documentation.
* libm/common/s_log2.c: Update return values to match what w_log2.c has,
since log2 uses log(); add note about being derived instead of direct.
* libm/common/sf_fma.c: Add casts to attempt to get correct results,
as well as comments pointing out problems with the implementation.
* libm/common/s_fma.c: Add fma() and fmaf() documentation.
* libm/common/sf_remquo.c: Incorrect quotient returns for large values
corrected by discarding existing function and replacing with Sun
verion, with some enhancements.
* libm/common/s_remquo.c: Ditto. Add remquo() and remquof()
documentation.
* libm/common/s_fmax.c: Add fmax() and fmaxf() documentation.
* libm/common/s_fmin.c: Add fmin() and fminf() documentation.
* libm/common/s_fdim.c: Return NAN for NAN arg, add fdim() and fdimf()
documentation.
* libm/common/sf_fdim.c: Return NAN for NAN arg, HUGE_VALF for inf arg.
* libm/common/s_trunc.c: Add trunc() and truncf() documentation.
* libm/common/s_rint.c: Add rint() and rintf() documentation.
* libm/common/s_round.c: Add round() and roundf() documentation.
* libm/common/s_scalbn.c: Add scalbln() and scalblnf() documentation.
* libm/common/s_infinity.c: Add infinity() and infinityf()
documentation.
* libm/common/s_lround.c: Add lround(), lroundf(), llround(), and
llroundf() documentation.
* libm/common/s_lrint.c: Add lrint(), lrintf(), llrint(), and llrintf()
documentation.
* libm/common/isgreater.c: New file for documenting math.h function-like
macros isgreater(), isgreaterequal(), isless(), islessequal(),
islessgreater(), and isunordered().
* libm/common/s_isnan.c: Add documentation for function-like macros
fpclassify(), isfinite(), isinf(), isnan(), and isnormal().
* libm/common/s_nearbyint.c: Add nearbyint() and nearbyintf()
documentation.
* libm/common/Makefile.am: Add s_llround.c (src); sf_llround.c (fsrc);
s_fdim.def, s_fma.def, s_fmax.def, s_fmin.def,
s_logb.def, s_lrint.def, s_lround.def, s_nearbyint.def, s_remquo.def,
s_rint.def, s_round.def, s_signbit.def, s_trunc.def, and
isgreater.def (chobj);
re-name all existing chew files (chobj) to match source file base
names (put in underscores), delete all special targets for chew files
(leaving all to be generated by rule).
* libm/common/Makefile.in: regenerate.
* libm/math/w_exp2.c: Add "base 2" to documentation description (and
delete TRAD_SYNOPSIS).
* libm/math/w_gamma.c: Add tgamma() and tgammaf() documentation, along
with some history behind the function names.
* libm/math/math.tex: Add includes for newly-added documentation (see
.def additions to common/Makefile.am and math/Makefile.am in this
ChangeLog list), adjusted existing .def file names to match source file
base names (added underscores); add mention of HUGE_VALF; rename
"Version of library" section to "Error Handling" and add some text
about floating-point exception; added section "Standards Compliance And
Portability".
* libm/math/Makefile.am: Add w_exp2.def (chobj);
re-name all existing chew files (chobj) to match source file base
names, delete all special targets for chew files (leaving all to be
generated by rule).
* libm/math/Makefile.in: regenerated
* doc/makedoc.c: Change silent ignoring of commands < 5 characters
to a failure when reading macro file for commands < 4 characters;
add -v (verbose) option for printing some debugging information;
get rid of spurious translation of "@*" to "*" (no source files used @*,
so no existing doc pages were affected); clean up some compiler
warnings.
* doc/doc.str: add BUGS and SEEALSO sections (to match texi2pod.pl
which has them); Remove ITEM command (redundant with makedoc built-in
"o", not used in any present source file so nothing is lost, anyway).
* HOWTO: New file to hold information for maintainers regarding how
to do things. Initial sections on documentation and ELIX levels.
2009-03-26 03:13:24 +08:00
|
|
|
#include <math.h>
|
|
|
|
long int lrint(double <[x]>);
|
|
|
|
long int lrintf(float <[x]>);
|
|
|
|
long long int llrint(double <[x]>);
|
|
|
|
long long int llrintf(float <[x]>);
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
The <<lrint>> and <<llrint>> functions round their argument to the nearest
|
|
|
|
integer value, using the current rounding direction. If the rounded value is
|
|
|
|
outside the range of the return type, the numeric result is unspecified. A
|
|
|
|
range error may occur if the magnitude of <[x]> is too large.
|
|
|
|
The "inexact" floating-point exception is raised in implementations that
|
|
|
|
support it when the result differs in value from the argument (i.e., when
|
|
|
|
a fraction actually has been truncated).
|
|
|
|
|
|
|
|
RETURNS
|
|
|
|
<[x]> rounded to an integral value, using the current rounding direction.
|
|
|
|
|
|
|
|
SEEALSO
|
|
|
|
<<lround>>
|
|
|
|
|
|
|
|
PORTABILITY
|
|
|
|
ANSI C, POSIX
|
|
|
|
|
|
|
|
*/
|
2002-06-08 05:59:57 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* lrint(x)
|
|
|
|
* Return x rounded to integral value according to the prevailing
|
|
|
|
* rounding mode.
|
|
|
|
* Method:
|
|
|
|
* Using floating addition.
|
|
|
|
* Exception:
|
|
|
|
* Inexact flag raised if x not equal to lrint(x).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fdlibm.h"
|
|
|
|
|
|
|
|
#ifndef _DOUBLE_IS_32BITS
|
|
|
|
|
|
|
|
#ifdef __STDC__
|
|
|
|
static const double
|
|
|
|
#else
|
|
|
|
static double
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Adding a double, x, to 2^52 will cause the result to be rounded based on
|
|
|
|
the fractional part of x, according to the implementation's current rounding
|
|
|
|
mode. 2^52 is the smallest double that can be represented using all 52 significant
|
|
|
|
digits. */
|
|
|
|
TWO52[2]={
|
|
|
|
4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */
|
|
|
|
-4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef __STDC__
|
|
|
|
long int lrint(double x)
|
|
|
|
#else
|
|
|
|
long int lrint(x)
|
|
|
|
double x;
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
__int32_t i0,j0,sx;
|
|
|
|
__uint32_t i1;
|
|
|
|
double t;
|
|
|
|
volatile double w;
|
|
|
|
long int result;
|
|
|
|
|
|
|
|
EXTRACT_WORDS(i0,i1,x);
|
2005-06-29 01:03:18 +08:00
|
|
|
|
|
|
|
/* Extract sign bit. */
|
2002-06-08 05:59:57 +08:00
|
|
|
sx = (i0>>31)&1;
|
2005-06-29 01:03:18 +08:00
|
|
|
|
|
|
|
/* Extract exponent field. */
|
2002-06-08 05:59:57 +08:00
|
|
|
j0 = ((i0 & 0x7ff00000) >> 20) - 1023;
|
2010-08-04 02:21:20 +08:00
|
|
|
/* j0 in [-1023,1024] */
|
2002-06-08 05:59:57 +08:00
|
|
|
|
|
|
|
if(j0 < 20)
|
|
|
|
{
|
2010-08-04 02:21:20 +08:00
|
|
|
/* j0 in [-1023,19] */
|
2002-06-08 05:59:57 +08:00
|
|
|
if(j0 < -1)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
{
|
2010-08-04 02:21:20 +08:00
|
|
|
/* j0 in [0,19] */
|
|
|
|
/* shift amt in [0,19] */
|
2002-06-08 05:59:57 +08:00
|
|
|
w = TWO52[sx] + x;
|
|
|
|
t = w - TWO52[sx];
|
|
|
|
GET_HIGH_WORD(i0, t);
|
2005-06-29 01:03:18 +08:00
|
|
|
/* Detect the all-zeros representation of plus and
|
|
|
|
minus zero, which fails the calculation below. */
|
2009-04-16 23:39:46 +08:00
|
|
|
if ((i0 & ~(1L << 31)) == 0)
|
2005-06-29 01:03:18 +08:00
|
|
|
return 0;
|
2010-08-04 02:21:20 +08:00
|
|
|
/* After round: j0 in [0,20] */
|
2002-06-08 05:59:57 +08:00
|
|
|
j0 = ((i0 & 0x7ff00000) >> 20) - 1023;
|
|
|
|
i0 &= 0x000fffff;
|
|
|
|
i0 |= 0x00100000;
|
2010-08-04 02:21:20 +08:00
|
|
|
/* shift amt in [20,0] */
|
2002-06-08 05:59:57 +08:00
|
|
|
result = i0 >> (20 - j0);
|
|
|
|
}
|
|
|
|
}
|
2005-06-29 01:03:18 +08:00
|
|
|
else if (j0 < (int)(8 * sizeof (long int)) - 1)
|
2002-06-08 05:59:57 +08:00
|
|
|
{
|
2010-08-04 02:21:20 +08:00
|
|
|
/* 32bit return: j0 in [20,30] */
|
|
|
|
/* 64bit return: j0 in [20,62] */
|
2002-06-08 05:59:57 +08:00
|
|
|
if (j0 >= 52)
|
2010-08-04 02:21:20 +08:00
|
|
|
/* 64bit return: j0 in [52,62] */
|
|
|
|
/* 64bit return: left shift amt in [32,42] */
|
2018-05-14 21:00:18 +08:00
|
|
|
result = ((long int) ((i0 & 0x000fffff) | 0x00100000) << (j0 - 20)) |
|
2010-08-04 02:21:20 +08:00
|
|
|
/* 64bit return: right shift amt in [0,10] */
|
2018-05-14 21:00:18 +08:00
|
|
|
((long int) i1 << (j0 - 52));
|
2002-06-08 05:59:57 +08:00
|
|
|
else
|
|
|
|
{
|
2010-08-04 02:21:20 +08:00
|
|
|
/* 32bit return: j0 in [20,30] */
|
|
|
|
/* 64bit return: j0 in [20,51] */
|
2002-06-08 05:59:57 +08:00
|
|
|
w = TWO52[sx] + x;
|
|
|
|
t = w - TWO52[sx];
|
|
|
|
EXTRACT_WORDS (i0, i1, t);
|
|
|
|
j0 = ((i0 & 0x7ff00000) >> 20) - 1023;
|
|
|
|
i0 &= 0x000fffff;
|
|
|
|
i0 |= 0x00100000;
|
2010-08-04 02:21:20 +08:00
|
|
|
/* After round:
|
|
|
|
* 32bit return: j0 in [20,31];
|
|
|
|
* 64bit return: j0 in [20,52] */
|
|
|
|
/* 32bit return: left shift amt in [0,11] */
|
|
|
|
/* 64bit return: left shift amt in [0,32] */
|
|
|
|
/* ***32bit return: right shift amt in [32,21] */
|
|
|
|
/* ***64bit return: right shift amt in [32,0] */
|
|
|
|
result = ((long int) i0 << (j0 - 20))
|
|
|
|
| SAFE_RIGHT_SHIFT (i1, (52 - j0));
|
2002-06-08 05:59:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return (long int) x;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sx ? -result : result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _DOUBLE_IS_32BITS */
|