skia2/include/private/SkFixed.h
fmalita e7365065ac Revert of For *ToFixed, in debug mode, assert that the value is in range. (patchset #6 id:140001 of https://codereview.chromium.org/1824733002/ )
Reason for revert:
Asserts in Blink rolls:

https://storage.googleapis.com/chromium-layout-test-archives/linux_blink_rel/84520/layout-test-results/results.html

STDERR: [1:1:0407/120919:1455366829:INFO:SkFixed.h(88)] ../../third_party/skia/include/private/SkFixed.h:88: fatal error: ""truncf(x * (1 << 16)) == static_cast<float>

Original issue's description:
> For *ToFixed, in debug mode, assert that the value is in range.
>
> Use SkFloatPinToFixed in SkTwoPointConicalGradient.cpp because it is failing in DM. fmalita is working on replacing this code with the 4f version, so I'm not bothering to fix it. (The beginnings of a real fix are in https://codereview.chromium.org/1767163003, which I do not plan to commit.)
>
> BUG=skia:4632
> GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1824733002
>
> Committed: https://skia.googlesource.com/skia/+/93dc33972cd6a418e84270298b856d2de08d9c1c

TBR=mtklein@google.com,reed@google.com,benjaminwagner@google.com
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=skia:4632

Review URL: https://codereview.chromium.org/1868933004
2016-04-07 19:27:45 -07:00

189 lines
6.3 KiB
C

/*
* Copyright 2006 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkFixed_DEFINED
#define SkFixed_DEFINED
#include "SkScalar.h"
#include "math.h"
#include "SkTypes.h"
/** \file SkFixed.h
Types and macros for 16.16 fixed point
*/
/** 32 bit signed integer used to represent fractions values with 16 bits to the right of the decimal point
*/
typedef int32_t SkFixed;
#define SK_Fixed1 (1 << 16)
#define SK_FixedHalf (1 << 15)
#define SK_FixedMax (0x7FFFFFFF)
#define SK_FixedMin (-SK_FixedMax)
#define SK_FixedPI (0x3243F)
#define SK_FixedSqrt2 (92682)
#define SK_FixedTanPIOver8 (0x6A0A)
#define SK_FixedRoot2Over2 (0xB505)
#define SkFixedToFloat(x) ((x) * 1.52587890625e-5f)
#define SkFloatToFixed(x) ((SkFixed)((x) * SK_Fixed1))
// Pins over/under flows to SK_FixedMax/SK_FixedMin (slower than just a cast).
static inline SkFixed SkFloatPinToFixed(float x) {
x *= SK_Fixed1;
// Casting float to int outside the range of the target type (int32_t) is undefined behavior.
if (x >= SK_FixedMax) return SK_FixedMax;
if (x <= SK_FixedMin) return SK_FixedMin;
const SkFixed result = static_cast<SkFixed>(x);
SkASSERT(truncf(x) == static_cast<float>(result));
return result;
}
#ifdef SK_DEBUG
static inline SkFixed SkFloatToFixed_Check(float x) {
int64_t n64 = (int64_t)(x * SK_Fixed1);
SkFixed n32 = (SkFixed)n64;
SkASSERT(n64 == n32);
return n32;
}
#else
#define SkFloatToFixed_Check(x) SkFloatToFixed(x)
#endif
#define SkFixedToDouble(x) ((x) * 1.52587890625e-5)
#define SkDoubleToFixed(x) ((SkFixed)((x) * SK_Fixed1))
// Pins over/under flows to SK_FixedMax/SK_FixedMin (slower than just a cast).
static inline SkFixed SkDoublePinToFixed(double x) {
x *= SK_Fixed1;
// Casting double to int outside the range of the target type (int32_t) is undefined behavior.
if (x >= SK_FixedMax) return SK_FixedMax;
if (x <= SK_FixedMin) return SK_FixedMin;
const SkFixed result = static_cast<SkFixed>(x);
SkASSERT(trunc(x) == static_cast<double>(result));
return result;
}
/** Converts an integer to a SkFixed, asserting that the result does not overflow
a 32 bit signed integer
*/
#ifdef SK_DEBUG
inline SkFixed SkIntToFixed(int n)
{
SkASSERT(n >= -32768 && n <= 32767);
// Left shifting a negative value has undefined behavior in C, so we cast to unsigned before
// shifting.
return (unsigned)n << 16;
}
#else
// Left shifting a negative value has undefined behavior in C, so we cast to unsigned before
// shifting. Then we force the cast to SkFixed to ensure that the answer is signed (like the
// debug version).
#define SkIntToFixed(n) (SkFixed)((unsigned)(n) << 16)
#endif
#define SkFixedRoundToInt(x) (((x) + SK_FixedHalf) >> 16)
#define SkFixedCeilToInt(x) (((x) + SK_Fixed1 - 1) >> 16)
#define SkFixedFloorToInt(x) ((x) >> 16)
#define SkFixedRoundToFixed(x) (((x) + SK_FixedHalf) & 0xFFFF0000)
#define SkFixedCeilToFixed(x) (((x) + SK_Fixed1 - 1) & 0xFFFF0000)
#define SkFixedFloorToFixed(x) ((x) & 0xFFFF0000)
#define SkFixedAbs(x) SkAbs32(x)
#define SkFixedAve(a, b) (((a) + (b)) >> 1)
// Blink layout tests are baselined to Clang optimizing through undefined behavior in SkDivBits.
#if defined(SK_SUPPORT_LEGACY_DIVBITS_UB)
#define SkFixedDiv(numer, denom) SkDivBits(numer, denom, 16)
#else
// The divide may exceed 32 bits. Clamp to a signed 32 bit result.
#define SkFixedDiv(numer, denom) \
SkToS32(SkTPin<int64_t>((SkLeftShift((int64_t)numer, 16) / denom), SK_MinS32, SK_MaxS32))
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Now look for ASM overrides for our portable versions (should consider putting this in its own file)
inline SkFixed SkFixedMul_longlong(SkFixed a, SkFixed b) {
return (SkFixed)((int64_t)a * b >> 16);
}
#define SkFixedMul(a,b) SkFixedMul_longlong(a,b)
#if defined(SK_CPU_ARM32)
/* This guy does not handle NaN or other obscurities, but is faster than
than (int)(x*65536). When built on Android with -Os, needs forcing
to inline or we lose the speed benefit.
*/
SK_ALWAYS_INLINE SkFixed SkFloatToFixed_arm(float x)
{
int32_t y, z;
asm("movs %1, %3, lsl #1 \n"
"mov %2, #0x8E \n"
"sub %1, %2, %1, lsr #24 \n"
"mov %2, %3, lsl #8 \n"
"orr %2, %2, #0x80000000 \n"
"mov %1, %2, lsr %1 \n"
"it cs \n"
"rsbcs %1, %1, #0 \n"
: "=r"(x), "=&r"(y), "=&r"(z)
: "r"(x)
: "cc"
);
return y;
}
inline SkFixed SkFixedMul_arm(SkFixed x, SkFixed y)
{
int32_t t;
asm("smull %0, %2, %1, %3 \n"
"mov %0, %0, lsr #16 \n"
"orr %0, %0, %2, lsl #16 \n"
: "=r"(x), "=&r"(y), "=r"(t)
: "r"(x), "1"(y)
:
);
return x;
}
#undef SkFixedMul
#define SkFixedMul(x, y) SkFixedMul_arm(x, y)
#undef SkFloatToFixed
#define SkFloatToFixed(x) SkFloatToFixed_arm(x)
#endif
///////////////////////////////////////////////////////////////////////////////
#if SK_SCALAR_IS_FLOAT
#define SkFixedToScalar(x) SkFixedToFloat(x)
#define SkScalarToFixed(x) SkFloatToFixed(x)
#define SkScalarPinToFixed(x) SkFloatPinToFixed(x)
#else // SK_SCALAR_IS_DOUBLE
#define SkFixedToScalar(x) SkFixedToDouble(x)
#define SkScalarToFixed(x) SkDoubleToFixed(x)
#define SkScalarPinToFixed(x) SkDoublePinToFixed(x)
#endif
///////////////////////////////////////////////////////////////////////////////
typedef int64_t SkFixed3232; // 32.32
#define SkIntToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 32))
#define SkFixed3232ToInt(x) ((int)((x) >> 32))
#define SkFixedToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 16))
#define SkFixed3232ToFixed(x) ((SkFixed)((x) >> 16))
#define SkFloatToFixed3232(x) ((SkFixed3232)((x) * (65536.0f * 65536.0f)))
#define SkScalarToFixed3232(x) SkFloatToFixed3232(x)
#endif