Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

math: improve POSIX compliance #393

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
extern "C" {
#endif

#define MATH_ERRNO 1
#define MATH_ERREXCEPT 2
#define math_errhandling (MATH_ERRNO)

#define FP_NAN 0
#define FP_INFINITE 1
#define FP_ZERO 2
Expand Down
52 changes: 34 additions & 18 deletions math/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,36 @@
*/

#include <stdint.h>
#include <errno.h>
#include <math.h>
#include <float.h>
#include "common.h"


void normalizeSub(double *x, int *exp)
{
conv_t *conv = (conv_t *)x;

if (conv->i.mantisa == 0)
if (conv->i.mantisa == 0) {
return;
}

while (!(conv->i.mantisa & 0xffffff0000000LL)) {
while ((conv->i.mantisa & 0xffffff0000000LL) == 0) {
conv->i.mantisa <<= 24;
*exp -= 24;
}

while (!(conv->i.mantisa & 0xff00000000000LL)) {
while ((conv->i.mantisa & 0xff00000000000LL) == 0) {
conv->i.mantisa <<= 8;
*exp -= 8;
}

while (!(conv->i.mantisa & 0xf000000000000LL)) {
while ((conv->i.mantisa & 0xf000000000000LL) == 0) {
conv->i.mantisa <<= 4;
*exp -= 4;
}

while (!(conv->i.mantisa & 0x8000000000000LL)) {
while ((conv->i.mantisa & 0x8000000000000LL) == 0) {
conv->i.mantisa <<= 1;
*exp -= 1;
}
Expand Down Expand Up @@ -89,8 +93,9 @@ void createSub(double *x, int exp)
++exp;
}

if (conv->i.mantisa == 0)
if (conv->i.mantisa == 0) {
*x = 0.0;
}
}


Expand All @@ -99,34 +104,43 @@ double quickPow(double x, int e)
double res;
unsigned int eabs;

if (e == 0)
if (e == 0) {
return 1.0;
else if (e == 1)
}
else if (e == 1) {
return x;
}

res = 1.0;

if (e < 0) {
eabs = -e;
eabs = (unsigned int)(-e);

while (eabs != 0) {
if (eabs & 1)
while (eabs != 0u) {
if (eabs & 1u) {
res /= x;
}
x *= x;
eabs >>= 1;
}
}
else {
eabs = e;
eabs = (unsigned int)e;

while (eabs != 0) {
if (eabs & 1)
while (eabs != 0u) {
if (eabs & 1u) {
res *= x;
}
x *= x;
eabs >>= 1;
}
}

/* ia32 use 80-bit extended precision registers so isinf() may not be true */
if ((isinf(res) != 0) || (fabs(res) > DBL_MAX)) {
errno = ERANGE;
}

return res;
}

Expand All @@ -135,15 +149,17 @@ int isInteger(double x)
{
conv_t *conv = (conv_t *)&x;
int exp = conv->i.exponent - 1023;
uint64_t mask = 0xfffffffffffffLL;
uint64_t mask = 0xfffffffffffffULL;
uint64_t m;

if (exp > 52)
if (exp > 52) {
return 1;
else if (exp < 0)
}
else if (exp < 0) {
return 0;
}

m = conv->i.mantisa & ~(mask >> exp);

return m == conv->i.mantisa;
return (m == conv->i.mantisa);
}
18 changes: 9 additions & 9 deletions math/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@


typedef union {
struct {
struct {
#if __FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__
uint64_t mantisa:52;
uint16_t exponent:11;
uint8_t sign:1;
uint64_t mantisa : 52;
uint16_t exponent : 11;
uint8_t sign : 1;
#elif __FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__
uint8_t sign:1;
uint16_t exponent:11;
uint64_t mantisa:52;
uint8_t sign : 1;
uint16_t exponent : 11;
uint64_t mantisa : 52;
#else
#error "Unsupported byte order"
#endif
} i;
double d;
} i;
double d;
} conv_t;


Expand Down
Loading
Loading