aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/MathBench.cpp
diff options
context:
space:
mode:
authorGravatar mtklein <mtklein@chromium.org>2016-05-27 10:47:31 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-05-27 10:47:32 -0700
commit785a5b941a4d74a1f272729fe8dca55c6eda6bb8 (patch)
tree895897fb08b687abaac95e9be208272db32e4342 /bench/MathBench.cpp
parent78d58d1084f0390c1c0f9123ac6e48efcd226f39 (diff)
Clean up SkFloatBits
- remove dead code - rewrite float -> int converters The strategy for the new converters is: - convert input to double - floor/ceil/round in double space - pin that double to [SK_MinS32, SK_MaxS32] - truncate that double to int32_t This simpler strategy does not work: - floor/ceil/round in float space - pin that float to [SK_MinS32, SK_MaxS32] - truncate that float to int32_t SK_MinS32 and SK_MaxS32 are not representable as floats: they round to the nearest float, ±2^31, which makes the pin insufficient for floats near SK_MinS32 (-2^31+1) or SK_MaxS32 (+2^31-1). float only has 24 bits of precision, and we need 31. double can represent all integers up to 50-something bits. An alternative is to pin in float to ±2147483520, the last exactly representable float before SK_MaxS32 (127 too small). Our tests test that we round as floor(x+0.5), which can return different numbers than round(x) for negative x. So this CL explicitly uses floor(x+0.5). I've updated the tests with ±inf and ±NaN, and tried to make them a little clearer, especially using SK_MinS32 instead of -SK_MaxS32. I have not timed anything here. I have never seen any of these methods in a profile. BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2012333003 Review-Url: https://codereview.chromium.org/2012333003
Diffstat (limited to 'bench/MathBench.cpp')
-rw-r--r--bench/MathBench.cpp26
1 files changed, 26 insertions, 0 deletions
diff --git a/bench/MathBench.cpp b/bench/MathBench.cpp
index 77ba2f04be..877d42663c 100644
--- a/bench/MathBench.cpp
+++ b/bench/MathBench.cpp
@@ -607,3 +607,29 @@ 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; )