clean up useage of SkFloatBits

Bug: skia:
Change-Id: I6d3a0019f2fcf11feca69123e4ce6eb35de43613
Reviewed-on: https://skia-review.googlesource.com/31222
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
This commit is contained in:
Mike Reed 2017-08-06 22:29:57 -04:00 committed by Skia Commit-Bot
parent b2a5973d02
commit 35ee0e09b4
7 changed files with 10 additions and 77 deletions

View File

@ -598,29 +598,3 @@ DEF_BENCH( return new CLZBench(true); )
DEF_BENCH( return new NormalizeBench(); )
DEF_BENCH( return new FixedMathBench(); )
struct FloatToIntBench : public Benchmark {
enum { N = 1000000 };
float fFloats[N];
int fInts [N];
const char* onGetName() override { return "float_to_int"; }
bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
void onDelayedSetup() override {
const auto f32 = 4294967296.0f;
for (int i = 0; i < N; ++i) {
fFloats[i] = -f32 + i*(2*f32/N);
}
}
void onDraw(int loops, SkCanvas*) override {
while (loops --> 0) {
for (int i = 0; i < N; i++) {
fInts[i] = SkFloatToIntFloor(fFloats[i]);
}
}
}
};
DEF_BENCH( return new FloatToIntBench; )

View File

@ -41,7 +41,7 @@ protected:
struct BitmapCellRenderer : public CellRenderer {
BitmapCellRenderer(SkColor color, SkFilterQuality quality, float scale = 1.0f)
: fQuality(quality) {
int scaledSize = SkFloatToIntRound(scale * gRectSize);
int scaledSize = sk_float_round2int(scale * gRectSize);
fBitmap.allocPixels(SkImageInfo::MakeS32(scaledSize, scaledSize, kPremul_SkAlphaType));
fBitmap.eraseColor(color);
const char* qualityNames[] = { "None", "Low", "Medium", "High" };

View File

@ -72,46 +72,8 @@ static inline float Sk2sComplimentAsFloat(int32_t x) {
return SkBits2Float(Sk2sComplimentToSignBit(x));
}
static inline int32_t pin_double_to_int(double x) {
return (int32_t)SkTPin<double>(x, SK_MinS32, SK_MaxS32);
}
/** Return the floor of the float as an int.
If the value is out of range, or NaN, return +/- SK_MaxS32
*/
static inline int32_t SkFloatToIntFloor(float x) {
#ifdef SK_SUPPORT_LEGACY_FLOATBITS
return pin_double_to_int(floor(x));
#else
return (int)floorf(x);
#endif
}
/** Return the float rounded to an int.
If the value is out of range, or NaN, return +/- SK_MaxS32
*/
static inline int32_t SkFloatToIntRound(float x) {
#ifdef SK_SUPPORT_LEGACY_FLOATBITS
return pin_double_to_int(floor((double)x + 0.5));
#else
return (int)floorf(x + 0.5f);
#endif
}
/** Return the ceiling of the float as an int.
If the value is out of range, or NaN, return +/- SK_MaxS32
*/
static inline int32_t SkFloatToIntCeil(float x) {
#ifdef SK_SUPPORT_LEGACY_FLOATBITS
return pin_double_to_int(ceil(x));
#else
return (int)ceilf(x);
#endif
}
// Scalar wrappers for float-bit routines
#define SkScalarAs2sCompliment(x) SkFloatAs2sCompliment(x)
#define Sk2sComplimentAsScalar(x) Sk2sComplimentAsFloat(x)
#endif

View File

@ -1,4 +1,3 @@
/*
* Copyright 2006 The Android Open Source Project
*
@ -6,10 +5,10 @@
* found in the LICENSE file.
*/
#ifndef SkFloatingPoint_DEFINED
#define SkFloatingPoint_DEFINED
#include "../private/SkFloatBits.h"
#include "SkTypes.h"
#include "SkSafe_math.h"
#include <float.h>
@ -25,8 +24,6 @@
#include <unistd.h>
#endif
#include "SkFloatBits.h"
// C++98 cmath std::pow seems to be the earliest portable way to get float pow.
// However, on Linux including cmath undefines isfinite.
// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14608
@ -71,8 +68,7 @@ static inline float sk_float_pow(float base, float exp) {
#define sk_float_isfinite(x) _finite(x)
#define sk_float_isnan(x) _isnan(x)
static inline int sk_float_isinf(float x) {
int32_t bits = SkFloat2Bits(x);
return (bits << 1) == (0xFF << 24);
return x && (x + x == x);
}
#else
#define sk_float_isfinite(x) isfinite(x)

View File

@ -9,6 +9,7 @@
#define SkRandom_DEFINED
#include "../private/SkFixed.h"
#include "../private/SkFloatBits.h"
#include "SkScalar.h"
/** \class SkRandom

View File

@ -867,10 +867,10 @@ void SkScalerContext_DW::generateColorGlyphImage(const SkGlyph& glyph) {
SkColor color;
if (colorGlyph->paletteIndex != 0xffff) {
color = SkColorSetARGB(SkFloatToIntRound(colorGlyph->runColor.a * 255),
SkFloatToIntRound(colorGlyph->runColor.r * 255),
SkFloatToIntRound(colorGlyph->runColor.g * 255),
SkFloatToIntRound(colorGlyph->runColor.b * 255));
color = SkColorSetARGB(sk_float_round2int(colorGlyph->runColor.a * 255),
sk_float_round2int(colorGlyph->runColor.r * 255),
sk_float_round2int(colorGlyph->runColor.g * 255),
sk_float_round2int(colorGlyph->runColor.b * 255));
} else {
// If all components of runColor are 0 or (equivalently) paletteIndex is 0xFFFF then
// the 'current brush' is used. fRec.getLuminanceColor() is kinda sorta what is wanted

View File

@ -636,7 +636,7 @@ void SkLinearGradient::LinearGradientContext::shade4_dx_clamp(SkPMColor dstC[],
if (fx < 0) {
// count is guaranteed to be positive, but the first arg may overflow int32 after
// increment => casting to uint32 ensures correct clamping.
int n = SkTMin<uint32_t>(static_cast<uint32_t>(SkFloatToIntFloor(-fx * invDx)) + 1,
int n = SkTMin<uint32_t>(static_cast<uint32_t>(sk_float_floor2int(-fx * invDx)) + 1,
count);
SkASSERT(n > 0);
fill<apply_alpha>(dstC, n, rec[0].fColor);
@ -652,7 +652,7 @@ void SkLinearGradient::LinearGradientContext::shade4_dx_clamp(SkPMColor dstC[],
if (fx > 1) {
// count is guaranteed to be positive, but the first arg may overflow int32 after
// increment => casting to uint32 ensures correct clamping.
int n = SkTMin<uint32_t>(static_cast<uint32_t>(SkFloatToIntFloor((1 - fx) * invDx)) + 1,
int n = SkTMin<uint32_t>(static_cast<uint32_t>(sk_float_floor2int((1 - fx) * invDx)) + 1,
count);
SkASSERT(n > 0);
fill<apply_alpha>(dstC, n, rec[fRecs.count() - 1].fColor);