Skip to content

Commit

Permalink
Remove OSCompat floating point functions
Browse files Browse the repository at this point in the history
Summary: These now seem to work with `libc++`.

Reviewed By: tmikov

Differential Revision: D31027975

fbshipit-source-id: fef7e54f110f24612af9cbfee19ec4eec7e2d051
  • Loading branch information
neildhar authored and facebook-github-bot committed Sep 23, 2021
1 parent 6c42e64 commit f38e4dc
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 43 deletions.
23 changes: 0 additions & 23 deletions include/hermes/Support/OSCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,29 +190,6 @@ std::vector<bool> sched_getaffinity();
/// or -1 on error.
int sched_getcpu();

/// The following functions are defined in Android's standard library, but not
/// in the \c std namespace.
inline double log2(double n);
inline double trunc(double n);
inline double copysign(double x, double y);
inline double nextafter(double x, double y);

inline double log2(double n) {
return ::log(n) / ::log(2.0);
}

inline double trunc(double n) {
return ::trunc(n);
}

inline double copysign(double x, double y) {
return ::copysign(x, y);
}

inline double nextafter(double x, double y) {
return ::nextafter(x, y);
}

#ifdef _WINDOWS

#define STDIN_FILENO 0
Expand Down
4 changes: 2 additions & 2 deletions lib/VM/JSLib/Date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ static CallResult<double> makeTimeFromArgs(Runtime *runtime, NativeArgs args) {
}

// Years between 0 and 99 are treated as offsets from 1900.
double yint = oscompat::trunc(fields[y]);
double yint = std::trunc(fields[y]);
if (!std::isnan(fields[y]) && 0 <= yint && yint <= 99) {
fields[yr] = 1900 + yint;
} else {
Expand Down Expand Up @@ -1033,7 +1033,7 @@ datePrototypeSetYear(void *ctx, Runtime *runtime, NativeArgs args) {
self->setPrimitiveValue(std::numeric_limits<double>::quiet_NaN());
return HermesValue::encodeNaNValue();
}
double yint = oscompat::trunc(y);
double yint = std::trunc(y);
double yr = 0 <= yint && yint <= 99 ? yint + 1900 : y;
double date = utcTime(makeDate(
makeDay(yr, monthFromTime(t), dateFromTime(t)), timeWithinDay(t)));
Expand Down
14 changes: 7 additions & 7 deletions lib/VM/JSLib/DateUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,9 @@ double makeTime(double hour, double min, double sec, double ms) {
!std::isfinite(ms)) {
return std::numeric_limits<double>::quiet_NaN();
}
double h = oscompat::trunc(hour);
double m = oscompat::trunc(min);
double s = oscompat::trunc(sec);
double h = std::trunc(hour);
double m = std::trunc(min);
double s = std::trunc(sec);
double milli = trunc(ms);
return h * MS_PER_HOUR + m * MS_PER_MINUTE + s * MS_PER_SECOND + milli;
}
Expand All @@ -457,9 +457,9 @@ double makeDay(double year, double month, double date) {
if (!std::isfinite(year) || !std::isfinite(month) || !std::isfinite(date)) {
return std::numeric_limits<double>::quiet_NaN();
}
double y = oscompat::trunc(year);
double m = oscompat::trunc(month);
double dt = oscompat::trunc(date);
double y = std::trunc(year);
double m = std::trunc(month);
double dt = std::trunc(date);

// Actual year and month, accounting for the month being greater than 11.
// Need to do this because it changes the leap year calculations.
Expand Down Expand Up @@ -498,7 +498,7 @@ double timeClip(double t) {
}

// Truncate and make -0 into +0.
return oscompat::trunc(t) + 0;
return std::trunc(t) + 0;
}

//===----------------------------------------------------------------------===//
Expand Down
4 changes: 2 additions & 2 deletions lib/VM/JSLib/Math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ static double roundHalfwaysTowardsInfinity(double x) {
return x;
} else if (absf < 0.5) {
// x may have too much precision to add 0.5. Just round to +/- 0.
return oscompat::copysign(0, x);
return std::copysign(0, x);
} else {
// Here we can apply the normal rounding algorithm, but we need to be
// careful about -0.5, which must round to -0.
return oscompat::copysign(std::floor(x + 0.5), x);
return std::copysign(std::floor(x + 0.5), x);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/VM/JSLib/MathStdFunctions.def
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ MATHFUNC_1ARG(floor, std::floor)
MATHFUNC_1ARG(log, std::log)
MATHFUNC_1ARG(log10, std::log10)
MATHFUNC_1ARG(log1p, ::log1p)
MATHFUNC_1ARG(log2, oscompat::log2)
MATHFUNC_1ARG(trunc, oscompat::trunc)
MATHFUNC_1ARG(log2, std::log2)
MATHFUNC_1ARG(trunc, std::trunc)
MATHFUNC_1ARG(round, roundHalfwaysTowardsInfinity)
MATHFUNC_1ARG(sin, std::sin)
MATHFUNC_1ARG(sinh, ::sinh)
Expand Down
8 changes: 4 additions & 4 deletions lib/VM/JSLib/Number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ numberIsInteger(void *, Runtime *runtime, NativeArgs args) {
}
// Let integer be ToInteger(number).
assert(!std::isnan(number) && "number must not be NaN after the check");
// Call oscompat::trunc because we've alredy checked NaN with isfinite.
double integer = oscompat::trunc(number);
// Call std::trunc because we've alredy checked NaN with isfinite.
double integer = std::trunc(number);

// If integer is not equal to number, return false.
// Otherwise, return true.
Expand Down Expand Up @@ -245,8 +245,8 @@ numberIsSafeInteger(void *, Runtime *runtime, NativeArgs args) {

// Let integer be ToInteger(number).
assert(!std::isnan(number) && "number must not be NaN after the check");
// Call oscompat::trunc because we've alredy checked NaN with isfinite.
double integer = oscompat::trunc(number);
// Call std::trunc because we've alredy checked NaN with isfinite.
double integer = std::trunc(number);

if (integer != number) {
// If integer is not equal to number, return false.
Expand Down
6 changes: 3 additions & 3 deletions lib/VM/Operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ CallResult<HermesValue> toInteger(Runtime *runtime, Handle<> valueHandle) {
if (std::isnan(num)) {
result = 0;
} else {
result = oscompat::trunc(num);
result = std::trunc(num);
}

return HermesValue::encodeDoubleValue(result);
Expand Down Expand Up @@ -1009,9 +1009,9 @@ numberToStringWithRadix(Runtime *runtime, double number, unsigned radix) {
if (fPart != 0) {
// Distance to the next double value.
double next =
oscompat::nextafter(number, std::numeric_limits<double>::infinity());
std::nextafter(number, std::numeric_limits<double>::infinity());
double minDenorm =
oscompat::nextafter(0.0, std::numeric_limits<double>::infinity());
std::nextafter(0.0, std::numeric_limits<double>::infinity());

// Precision of the input (half the distance to the next double).
// We only compute digits up to that precision.
Expand Down

0 comments on commit f38e4dc

Please sign in to comment.