constexpr NaN,+Inf,-Inf

Reading extern values meant these couldn't be compile-time constants.

math.h has INFINITY, which is macro that is supposed to expand to float +inf.
On MSVC it seems it's natively a double, so we cast just to make sure.

There's nan(const char*) in math.h for NaN too, but I don't trust that
to be compile-time evaluated.  So instead, we keep reinterpreting a bit pattern.

I did try to write

    static constexpr float float_nan() { ... }

and completely failed.  constexpr seems a bit too restrictive in C++11 to make
it work, but Clang kept telling me, you'll be able to do this with C++14.

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2233853002

Review-Url: https://codereview.chromium.org/2233853002
This commit is contained in:
mtklein 2016-08-10 08:31:42 -07:00 committed by Commit bot
parent 6e90d42d3d
commit 3ff2cc81a5
3 changed files with 6 additions and 14 deletions

View File

@ -100,13 +100,10 @@ static inline float sk_float_pow(float base, float exp) {
#define sk_double_round2int(x) (int)floor((x) + 0.5f) #define sk_double_round2int(x) (int)floor((x) + 0.5f)
#define sk_double_ceil2int(x) (int)ceil(x) #define sk_double_ceil2int(x) (int)ceil(x)
extern const uint32_t gIEEENotANumber; static const uint32_t kIEEENotANumber = 0x7fffffff;
extern const uint32_t gIEEEInfinity; #define SK_FloatNaN (*SkTCast<const float*>(&kIEEENotANumber))
extern const uint32_t gIEEENegativeInfinity; #define SK_FloatInfinity (+(float)INFINITY)
#define SK_FloatNegativeInfinity (-(float)INFINITY)
#define SK_FloatNaN (*SkTCast<const float*>(&gIEEENotANumber))
#define SK_FloatInfinity (*SkTCast<const float*>(&gIEEEInfinity))
#define SK_FloatNegativeInfinity (*SkTCast<const float*>(&gIEEENegativeInfinity))
static inline float sk_float_rsqrt_portable(float x) { static inline float sk_float_rsqrt_portable(float x) {
// Get initial estimate. // Get initial estimate.

View File

@ -14,12 +14,11 @@
#include "SkRSXform.h" #include "SkRSXform.h"
#include "SkSpinlock.h" #include "SkSpinlock.h"
#include "SkTextBlob.h" #include "SkTextBlob.h"
#include <math.h>
// A stand-in for an optional SkRect which was not set, e.g. bounds for a saveLayer(). // A stand-in for an optional SkRect which was not set, e.g. bounds for a saveLayer().
static const SkRect kUnset = {(SkScalar)INFINITY, 0,0,0}; static const SkRect kUnset = { SK_ScalarInfinity, 0,0,0};
static const SkRect* maybe_unset(const SkRect& r) { static const SkRect* maybe_unset(const SkRect& r) {
return r.left() == (SkScalar)INFINITY ? nullptr : &r; return r.left() == SK_ScalarInfinity ? nullptr : &r;
} }
// copy_v(dst, src,n, src,n, ...) copies an arbitrary number of typed srcs into dst. // copy_v(dst, src,n, src,n, ...) copies an arbitrary number of typed srcs into dst.

View File

@ -11,10 +11,6 @@
#include "SkFloatingPoint.h" #include "SkFloatingPoint.h"
#include "SkScalar.h" #include "SkScalar.h"
const uint32_t gIEEENotANumber = 0x7FFFFFFF;
const uint32_t gIEEEInfinity = 0x7F800000;
const uint32_t gIEEENegativeInfinity = 0xFF800000;
#define sub_shift(zeros, x, n) \ #define sub_shift(zeros, x, n) \
zeros -= n; \ zeros -= n; \
x >>= n x >>= n