2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkFixed_DEFINED
|
|
|
|
#define SkFixed_DEFINED
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/private/SkSafe_math.h"
|
|
|
|
#include "include/private/SkTo.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
/** \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)
|
2018-06-05 17:32:12 +00:00
|
|
|
#define SK_FixedQuarter (1 << 14)
|
2008-12-17 15:59:43 +00:00
|
|
|
#define SK_FixedMax (0x7FFFFFFF)
|
2011-05-09 22:32:52 +00:00
|
|
|
#define SK_FixedMin (-SK_FixedMax)
|
2008-12-17 15:59:43 +00:00
|
|
|
#define SK_FixedPI (0x3243F)
|
|
|
|
#define SK_FixedSqrt2 (92682)
|
|
|
|
#define SK_FixedTanPIOver8 (0x6A0A)
|
|
|
|
#define SK_FixedRoot2Over2 (0xB505)
|
|
|
|
|
2017-12-18 15:14:12 +00:00
|
|
|
// NOTE: SkFixedToFloat is exact. SkFloatToFixed seems to lack a rounding step. For all fixed-point
|
|
|
|
// values, this version is as accurate as possible for (fixed -> float -> fixed). Rounding reduces
|
|
|
|
// accuracy if the intermediate floats are in the range that only holds integers (adding 0.5f to an
|
|
|
|
// odd integer then snaps to nearest even). Using double for the rounding math gives maximum
|
|
|
|
// accuracy for (float -> fixed -> float), but that's usually overkill.
|
2015-04-17 17:05:43 +00:00
|
|
|
#define SkFixedToFloat(x) ((x) * 1.52587890625e-5f)
|
2017-11-27 15:11:47 +00:00
|
|
|
#define SkFloatToFixed(x) sk_float_saturate2int((x) * SK_Fixed1)
|
2016-04-07 16:23:11 +00:00
|
|
|
|
2016-04-08 02:27:45 +00:00
|
|
|
#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
|
2012-08-03 12:45:14 +00:00
|
|
|
|
2016-04-08 02:27:45 +00:00
|
|
|
#define SkFixedToDouble(x) ((x) * 1.52587890625e-5)
|
|
|
|
#define SkDoubleToFixed(x) ((SkFixed)((x) * SK_Fixed1))
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
/** 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);
|
2015-11-11 16:46:34 +00:00
|
|
|
// Left shifting a negative value has undefined behavior in C, so we cast to unsigned before
|
|
|
|
// shifting.
|
2020-06-01 20:50:19 +00:00
|
|
|
return (SkFixed)( (unsigned)n << 16 );
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
#else
|
2015-11-11 16:46:34 +00:00
|
|
|
// 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)
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|
|
|
|
|
2011-08-01 20:49:45 +00:00
|
|
|
#define SkFixedRoundToInt(x) (((x) + SK_FixedHalf) >> 16)
|
|
|
|
#define SkFixedCeilToInt(x) (((x) + SK_Fixed1 - 1) >> 16)
|
|
|
|
#define SkFixedFloorToInt(x) ((x) >> 16)
|
|
|
|
|
2016-10-20 18:23:09 +00:00
|
|
|
static inline SkFixed SkFixedRoundToFixed(SkFixed x) {
|
2020-06-01 20:50:19 +00:00
|
|
|
return (SkFixed)( (uint32_t)(x + SK_FixedHalf) & 0xFFFF0000 );
|
2016-10-20 18:23:09 +00:00
|
|
|
}
|
|
|
|
static inline SkFixed SkFixedCeilToFixed(SkFixed x) {
|
2020-06-01 20:50:19 +00:00
|
|
|
return (SkFixed)( (uint32_t)(x + SK_Fixed1 - 1) & 0xFFFF0000 );
|
2016-10-20 18:23:09 +00:00
|
|
|
}
|
|
|
|
static inline SkFixed SkFixedFloorToFixed(SkFixed x) {
|
2020-06-01 20:50:19 +00:00
|
|
|
return (SkFixed)( (uint32_t)x & 0xFFFF0000 );
|
2016-10-20 18:23:09 +00:00
|
|
|
}
|
2011-08-01 20:49:45 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#define SkFixedAbs(x) SkAbs32(x)
|
|
|
|
#define SkFixedAve(a, b) (((a) + (b)) >> 1)
|
|
|
|
|
2016-04-28 01:45:36 +00:00
|
|
|
// The divide may exceed 32 bits. Clamp to a signed 32 bit result.
|
|
|
|
#define SkFixedDiv(numer, denom) \
|
2016-07-13 20:34:46 +00:00
|
|
|
SkToS32(SkTPin<int64_t>((SkLeftShift((int64_t)(numer), 16) / (denom)), SK_MinS32, SK_MaxS32))
|
2008-12-17 15:59:43 +00:00
|
|
|
|
remove SkFixedMul_arm()
The portable SkFixedMul_longlong (here now renamed SkFixedMul)
generates shorter, equivalent code,
from
0: fb81 0200 smull r0, r2, r1, r0
4: ea4f 4010 mov.w r0, r0, lsr #16
8: ea40 4002 orr.w r0, r0, r2, lsl #16
to
0: fb81 0100 smull r0, r1, r1, r0
4: 0c00 lsrs r0, r0, #16
6: ea40 4001 orr.w r0, r0, r1, lsl #16
(Notice, 2 bytes saved.)
Change-Id: Icb0f7e6d4379086fc602f956a4beb1265a9759bc
Reviewed-on: https://skia-review.googlesource.com/69440
Reviewed-by: Ben Wagner <benjaminwagner@google.com>
Commit-Queue: Mike Klein <mtklein@chromium.org>
2017-11-09 17:55:32 +00:00
|
|
|
static inline SkFixed SkFixedMul(SkFixed a, SkFixed b) {
|
2016-04-08 02:27:45 +00:00
|
|
|
return (SkFixed)((int64_t)a * b >> 16);
|
|
|
|
}
|
remove SkFixedMul_arm()
The portable SkFixedMul_longlong (here now renamed SkFixedMul)
generates shorter, equivalent code,
from
0: fb81 0200 smull r0, r2, r1, r0
4: ea4f 4010 mov.w r0, r0, lsr #16
8: ea40 4002 orr.w r0, r0, r2, lsl #16
to
0: fb81 0100 smull r0, r1, r1, r0
4: 0c00 lsrs r0, r0, #16
6: ea40 4001 orr.w r0, r0, r1, lsl #16
(Notice, 2 bytes saved.)
Change-Id: Icb0f7e6d4379086fc602f956a4beb1265a9759bc
Reviewed-on: https://skia-review.googlesource.com/69440
Reviewed-by: Ben Wagner <benjaminwagner@google.com>
Commit-Queue: Mike Klein <mtklein@chromium.org>
2017-11-09 17:55:32 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Platform-specific alternatives to our portable versions.
|
2016-04-08 02:27:45 +00:00
|
|
|
|
2017-11-09 17:18:09 +00:00
|
|
|
// The VCVT float-to-fixed instruction is part of the VFPv3 instruction set.
|
|
|
|
#if defined(__ARM_VFPV3__)
|
2020-07-28 14:20:58 +00:00
|
|
|
/* This does not handle NaN or other obscurities, but is faster than
|
2016-04-08 02:27:45 +00:00
|
|
|
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)
|
|
|
|
{
|
2017-03-08 00:58:08 +00:00
|
|
|
int32_t y;
|
|
|
|
asm("vcvt.s32.f32 %0, %0, #16": "+w"(x));
|
|
|
|
memcpy(&y, &x, sizeof(y));
|
2016-04-08 02:27:45 +00:00
|
|
|
return y;
|
|
|
|
}
|
2017-11-09 17:18:09 +00:00
|
|
|
#undef SkFloatToFixed
|
|
|
|
#define SkFloatToFixed(x) SkFloatToFixed_arm(x)
|
|
|
|
#endif
|
|
|
|
|
2012-03-07 21:47:41 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-04-07 15:49:31 +00:00
|
|
|
#define SkFixedToScalar(x) SkFixedToFloat(x)
|
|
|
|
#define SkScalarToFixed(x) SkFloatToFixed(x)
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-11-21 20:10:33 +00:00
|
|
|
typedef int64_t SkFixed3232; // 32.32
|
2012-03-07 21:47:41 +00:00
|
|
|
|
2018-01-31 20:55:47 +00:00
|
|
|
#define SkFixed3232Max SK_MaxS64
|
2017-02-10 15:42:49 +00:00
|
|
|
#define SkFixed3232Min (-SkFixed3232Max)
|
|
|
|
|
2016-04-08 02:27:45 +00:00
|
|
|
#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))
|
2018-01-31 20:55:47 +00:00
|
|
|
#define SkFloatToFixed3232(x) sk_float_saturate2int64((x) * (65536.0f * 65536.0f))
|
2017-02-10 15:42:49 +00:00
|
|
|
#define SkFixed3232ToFloat(x) (x * (1 / (65536.0f * 65536.0f)))
|
2012-03-07 21:47:41 +00:00
|
|
|
|
2014-11-21 20:10:33 +00:00
|
|
|
#define SkScalarToFixed3232(x) SkFloatToFixed3232(x)
|
2012-03-07 21:47:41 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|