From 785a5b941a4d74a1f272729fe8dca55c6eda6bb8 Mon Sep 17 00:00:00 2001 From: mtklein Date: Fri, 27 May 2016 10:47:31 -0700 Subject: Clean up SkFloatBits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- bench/MathBench.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'bench/MathBench.cpp') 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; ) -- cgit v1.2.3