* mingwex/math/lround.c: Rewrite.

* mingwex/math/lroundf.c: Rewrite.
	* mingwex/math/lroundl.c: Rewrite.
	* mingwex/math/llround.c: Rewrite.
	* mingwex/math/llroundf.c: Rewrite.
	* mingwex/math/llroundl.c: Rewrite.
This commit is contained in:
Danny Smith 2004-04-22 04:38:24 +00:00
parent b7ede86cfe
commit 1a4b7623f4
7 changed files with 101 additions and 116 deletions

View File

@ -1,3 +1,12 @@
2004-04-22 Danny Smith <dannysmith@users.sourceforge.net>
* mingwex/math/lround.c: Rewrite.
* mingwex/math/lroundf.c: Rewrite.
* mingwex/math/lroundl.c: Rewrite.
* mingwex/math/llround.c: Rewrite.
* mingwex/math/llroundf.c: Rewrite.
* mingwex/math/llroundl.c: Rewrite.
2004-04-20 Earnie Boyd <earnie@users.sf.net>
* CONTRIBUTORS: New file.

View File

@ -1,24 +1,19 @@
#include <fenv.h>
#include <math.h>
#include <limits.h>
#include <errno.h>
long long
llround (double x) {
long long retval;
unsigned short saved_cw, _cw;
__asm__ (
"fnstcw %0;" : "=m" (saved_cw)
); /* save control word */
_cw = ~(FE_TONEAREST | FE_DOWNWARD | FE_UPWARD | FE_TOWARDZERO)
| (x > 0.0 ? FE_UPWARD : FE_DOWNWARD); /* round away from zero */
__asm__ (
"fldcw %0;" : : "m" (_cw)
); /* load the rounding control */
__asm__ __volatile__ (
"fistpll %0" : "=m" (retval) : "t" (x) : "st"
);
__asm__ (
"fldcw %0;" : : "m" (saved_cw)
); /* restore control word */
return retval;
llround (double x)
{
/* Add +/- 0.5, then round towards zero. */
double tmp = trunc (x + (x >= 0.0 ? 0.5 : -0.5));
if (!isfinite (tmp)
|| tmp > (double)LONG_LONG_MAX
|| tmp < (double)LONG_LONG_MIN)
{
errno = ERANGE;
/* Undefined behaviour, so we could return anything. */
/* return tmp > 0.0 ? LONG_LONG_MAX : LONG_LONG_MIN; */
}
return (long long)tmp;
}

View File

@ -1,23 +1,19 @@
#include <fenv.h>
#include <math.h>
#include <limits.h>
#include <errno.h>
long long
llroundf (float x) {
long long retval;
unsigned short saved_cw, _cw;
__asm__ (
"fnstcw %0;" : "=m" (saved_cw)
); /* save control word */
_cw = ~(FE_TONEAREST | FE_DOWNWARD | FE_UPWARD | FE_TOWARDZERO)
| (x > 0.0 ? FE_UPWARD : FE_DOWNWARD); /* round away from zero */
__asm__ (
"fldcw %0;" : : "m" (_cw)
); /* load the rounding control */
__asm__ __volatile__ (
"fistpll %0" : "=m" (retval) : "t" (x) : "st"
);
__asm__ (
"fldcw %0;" : : "m" (saved_cw)
); /* restore control word */
return retval;
}
llroundf (float x)
{
/* Add +/- 0.5, then round towards zero. */
float tmp = truncf (x + (x >= 0.0F ? 0.5F : -0.5F));
if (!isfinite (tmp)
|| tmp > (float)LONG_LONG_MAX
|| tmp < (float)LONG_LONG_MIN)
{
errno = ERANGE;
/* Undefined behaviour, so we could return anything. */
/* return tmp > 0.0F ? LONG_LONG_MAX : LONG_LONG_MIN; */
}
return (long long)tmp;
}

View File

@ -1,22 +1,19 @@
#include <fenv.h>
#include <math.h>
#include <limits.h>
#include <errno.h>
long long
llroundl (long double x) {
long long retval;
unsigned short saved_cw, _cw;
__asm__ (
"fnstcw %0;" : "=m" (saved_cw)
); /* save control word */
_cw = ~(FE_TONEAREST | FE_DOWNWARD | FE_UPWARD | FE_TOWARDZERO)
| (x > 0.0 ? FE_UPWARD : FE_DOWNWARD); /* round away from zero */
__asm__ (
"fldcw %0;" : : "m" (_cw)
); /* load the rounding control */
__asm__ __volatile__ (
"fistpll %0" : "=m" (retval) : "t" (x) : "st");
__asm__ (
"fldcw %0;" : : "m" (saved_cw)
); /* restore control word */
return retval;
llroundl (long double x)
{
/* Add +/- 0.5, then round towards zero. */
long double tmp = truncl (x + (x >= 0.0L ? 0.5L : -0.5L));
if (!isfinite (tmp)
|| tmp > (long double)LONG_LONG_MAX
|| tmp < (long double)LONG_LONG_MIN)
{
errno = ERANGE;
/* Undefined behaviour, so we could return anything. */
/* return tmp > 0.0L ? LONG_LONG_MAX : LONG_LONG_MIN; */
}
return (long long)tmp;
}

View File

@ -1,24 +1,19 @@
#include <fenv.h>
#include <math.h>
#include <limits.h>
#include <errno.h>
long
lround (double x) {
long retval;
unsigned short saved_cw, _cw;
__asm__ (
"fnstcw %0;" : "=m" (saved_cw)
); /* save control word */
_cw = ~(FE_TONEAREST | FE_DOWNWARD | FE_UPWARD | FE_TOWARDZERO)
| (x > 0.0 ? FE_UPWARD : FE_DOWNWARD); /* round away from zero */
__asm__ (
"fldcw %0;" : : "m" (_cw)
); /* load the rounding control */
__asm__ __volatile__ (
"fistpl %0" : "=m" (retval) : "t" (x) : "st"
);
__asm__ (
"fldcw %0;" : : "m" (saved_cw)
); /* restore control word */
return retval;
lround (double x)
{
/* Add +/- 0.5 then then round towards zero. */
double tmp = trunc (x + (x >= 0.0 ? 0.5 : -0.5));
if (!isfinite (tmp)
|| tmp > (double)LONG_MAX
|| tmp < (double)LONG_MIN)
{
errno = ERANGE;
/* Undefined behaviour, so we could return anything. */
/* return tmp > 0.0 ? LONG_MAX : LONG_MIN; */
}
return (long)tmp;
}

View File

@ -1,23 +1,19 @@
#include <fenv.h>
#include <math.h>
#include <limits.h>
#include <errno.h>
long
lroundf (float x) {
long retval;
unsigned short saved_cw, _cw;
__asm__ (
"fnstcw %0;" : "=m" (saved_cw)
); /* save control word */
_cw = ~(FE_TONEAREST | FE_DOWNWARD | FE_UPWARD | FE_TOWARDZERO)
| (x > 0.0 ? FE_UPWARD : FE_DOWNWARD); /* round away from zero */
__asm__ (
"fldcw %0;" : : "m" (_cw)
); /* load the rounding control */
__asm__ __volatile__ (
"fistpl %0" : "=m" (retval) : "t" (x) : "st"
);
__asm__ (
"fldcw %0;" : : "m" (saved_cw)
); /* restore control word */
return retval;
}
lroundf (float x)
{
/* Add +/- 0.5, then round towards zero. */
float tmp = truncf (x + (x >= 0.0F ? 0.5F : -0.5F));
if (!isfinite (tmp)
|| tmp > (float)LONG_MAX
|| tmp < (float)LONG_MIN)
{
errno = ERANGE;
/* Undefined behaviour, so we could return anything. */
/* return tmp > 0.0F ? LONG_MAX : LONG_MIN; */
}
return (long)tmp;
}

View File

@ -1,22 +1,19 @@
#include <fenv.h>
#include <math.h>
#include <limits.h>
#include <errno.h>
long
lroundl (long double x) {
long retval;
unsigned short saved_cw, _cw;
__asm__ (
"fnstcw %0;" : "=m" (saved_cw)
); /* save control word */
_cw = ~(FE_TONEAREST | FE_DOWNWARD | FE_UPWARD | FE_TOWARDZERO)
| (x > 0.0 ? FE_UPWARD : FE_DOWNWARD); /* round away from zero */
__asm__ (
"fldcw %0;" : : "m" (_cw)
); /* load the rounding control */
__asm__ __volatile__ (
"fistpl %0" : "=m" (retval) : "t" (x) : "st");
__asm__ (
"fldcw %0;" : : "m" (saved_cw)
); /* restore control word */
return retval;
lroundl (long double x)
{
/* Add +/- 0.5, then round towards zero. */
long double tmp = truncl (x + (x >= 0.0L ? 0.5L : -0.5L));
if (!isfinite (tmp)
|| tmp > (long double)LONG_MAX
|| tmp < (long double)LONG_MIN)
{
errno = ERANGE;
/* Undefined behaviour, so we could return anything. */
/* return tmp > 0.0L ? LONG_MAX : LONG_MIN; */
}
return (long)tmp;
}