mirror of
https://github.com/reactos/reactos.git
synced 2025-08-05 02:05:50 +00:00
[CRT] Implement portable ceil/floor
This commit is contained in:
parent
2943ea2cfe
commit
3aa3b3af56
7 changed files with 139 additions and 192 deletions
65
sdk/lib/crt/math/ceil.c
Normal file
65
sdk/lib/crt/math/ceil.c
Normal file
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* PROJECT: ReactOS CRT library
|
||||
* LICENSE: MIT (https://spdx.org/licenses/MIT)
|
||||
* PURPOSE: Portable implementation of ceil
|
||||
* COPYRIGHT: Copyright 2021 Timo Kreuzer <timo.kreuzer@reactos.org>
|
||||
*/
|
||||
|
||||
#define _USE_MATH_DEFINES
|
||||
#include <math.h>
|
||||
#include <limits.h>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma function(ceil)
|
||||
#endif
|
||||
|
||||
double
|
||||
__cdecl
|
||||
ceil(double x)
|
||||
{
|
||||
/* Load the value as uint64 */
|
||||
unsigned long long u64 = *(unsigned long long*)&x;
|
||||
|
||||
/* Check for NAN */
|
||||
if ((u64 & ~(1ULL << 63)) > 0x7FF0000000000000ull)
|
||||
{
|
||||
/* Set error bit */
|
||||
u64 |= 0x0008000000000000ull;
|
||||
return *(double*)&u64;
|
||||
}
|
||||
|
||||
/* Check if x is positive */
|
||||
if ((u64 & (1ULL << 63)) == 0)
|
||||
{
|
||||
/* Check if it fits into an int64 */
|
||||
if (x < (double)_I64_MAX)
|
||||
{
|
||||
/* Cast to int64 to truncate towards 0. If this matches the
|
||||
input, return it as is, otherwise add 1 */
|
||||
double y = (double)(long long)x;
|
||||
return (x > y) ? y + 1 : y;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* The exponent is larger than the fraction bits.
|
||||
This means the number is already an integer. */
|
||||
return x;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Check if it fits into an int64 */
|
||||
if (x > (double)_I64_MIN)
|
||||
{
|
||||
/* Cast to int64 to truncate towards 0. */
|
||||
x = (double)(long long)x;
|
||||
return (x == 0.) ? -0.0 : x;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* The exponent is larger than the fraction bits.
|
||||
This means the number is already an integer. */
|
||||
return x;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue