Commit b2cecfb5 authored by Amol Bhave's avatar Amol Bhave Committed by Facebook Github Bot

Add portability support for std::remainder

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
parent e265e1ef
......@@ -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 {
......@@ -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);
}
......
......@@ -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__)
......@@ -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) {
......@@ -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__
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment