Skip to content

Commit

Permalink
Add portability support for std::remainder
Browse files Browse the repository at this point in the history
Summary:
std::remainder isn't supplied by all platforms. Specifically, uclibc
doesn't have it. Do a similar case as with std::nextafter, i.e. implement a
folly version in case it doesn't exists.

Reviewed By: yfeldblum

Differential Revision: D15291533

fbshipit-source-id: 96a70b52af11135102fda3183626df69cdaf4261
  • Loading branch information
ammubhave authored and facebook-github-bot committed May 10, 2019
1 parent e265e1e commit b2cecfb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
3 changes: 2 additions & 1 deletion folly/experimental/JSONSchema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <folly/Singleton.h>
#include <folly/String.h>
#include <folly/json.h>
#include <folly/portability/Math.h>

namespace folly {
namespace jsonschema {
Expand Down Expand Up @@ -141,7 +142,7 @@ struct MultipleOfValidator final : IValidator {
return none;
}
if (schema_.isDouble() || value.isDouble()) {
const auto rem = std::remainder(value.asDouble(), schema_.asDouble());
const auto rem = folly::remainder(value.asDouble(), schema_.asDouble());
if (std::abs(rem) > std::numeric_limits<double>::epsilon()) {
return makeError("a multiple of ", schema_, value);
}
Expand Down
35 changes: 30 additions & 5 deletions folly/portability/Math.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@ namespace folly {
#if !defined(__ANDROID__) && !defined(__UCLIBC__)

/**
* Most platforms hopefully provide std::nextafter.
* Most platforms hopefully provide std::nextafter, std::remainder.
*/

/* using override */ using std::nextafter;
/* using override */ using std::remainder;

#else // !__ANDROID__ && !__UCLIBC__

/**
* On Android and uclibc, std::nextafter isn't implemented. However, the C
* functions and compiler builtins are still provided. Using the GCC builtin is
* actually slightly faster, as they're constexpr and the use cases within folly
* are in constexpr context.
* On Android and uclibc, std::nextafter or std::remainder isn't implemented.
* However, the C functions and compiler builtins are still provided. Using the
* GCC builtin is actually slightly faster, as they're constexpr and the use
* cases within folly are in constexpr context.
*/

#if defined(__GNUC__) && !defined(__clang__)
Expand All @@ -51,6 +52,18 @@ constexpr long double nextafter(long double x, long double y) {
return __builtin_nextafterl(x, y);
}

constexpr float remainder(float x, float y) {
return __builtin_remainderf(x, y);
}

constexpr double remainder(double x, double y) {
return __builtin_remainder(x, y);
}

constexpr long double remainder(long double x, long double y) {
return __builtin_remainderl(x, y);
}

#else // __GNUC__

inline float nextafter(float x, float y) {
Expand All @@ -65,6 +78,18 @@ inline long double nextafter(long double x, long double y) {
return ::nextafterl(x, y);
}

inline float remainder(float x, float y) {
return ::remainderf(x, y);
}

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

inline long double remainder(long double x, long double y) {
return ::remainderl(x, y);
}

#endif // __GNUC__

#endif // !__ANDROID__ && !__UCLIBC__
Expand Down

0 comments on commit b2cecfb

Please sign in to comment.