blob: 037a434fd3b002cbf065b771136b074906fdcfe1 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkFDot6_DEFINED
9#define SkFDot6_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkMath.h"
12#include "include/core/SkScalar.h"
13#include "include/private/SkFixed.h"
14#include "include/private/SkTo.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
16typedef int32_t SkFDot6;
17
george2f265282014-08-29 08:47:55 -070018/* This uses the magic number approach suggested here:
19 * http://stereopsis.com/sree/fpu2006.html and used in
20 * _cairo_fixed_from_double. It does banker's rounding
21 * (i.e. round to nearest even)
22 */
23inline SkFDot6 SkScalarRoundToFDot6(SkScalar x, int shift = 0)
24{
25 union {
26 double fDouble;
27 int32_t fBits[2];
28 } tmp;
29 int fractionalBits = 6 + shift;
30 double magic = (1LL << (52 - (fractionalBits))) * 1.5;
31
32 tmp.fDouble = SkScalarToDouble(x) + magic;
33#ifdef SK_CPU_BENDIAN
34 return tmp.fBits[1];
35#else
36 return tmp.fBits[0];
37#endif
38}
39
mike@reedtribe.orgbcc1d332011-04-09 19:16:54 +000040#define SK_FDot6One (64)
reed@android.com8a1c16f2008-12-17 15:59:43 +000041#define SK_FDot6Half (32)
42
43#ifdef SK_DEBUG
Mike Klein5595f6e2018-09-05 10:51:00 -040044 inline SkFDot6 SkIntToFDot6(int x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 SkASSERT(SkToS16(x) == x);
46 return x << 6;
47 }
48#else
49 #define SkIntToFDot6(x) ((x) << 6)
50#endif
51
52#define SkFDot6Floor(x) ((x) >> 6)
53#define SkFDot6Ceil(x) (((x) + 63) >> 6)
54#define SkFDot6Round(x) (((x) + 32) >> 6)
55
56#define SkFixedToFDot6(x) ((x) >> 10)
57
mike@reedtribe.orgbcc1d332011-04-09 19:16:54 +000058inline SkFixed SkFDot6ToFixed(SkFDot6 x) {
caryclark3127c992015-12-09 12:02:30 -080059 SkASSERT((SkLeftShift(x, 10) >> 10) == x);
reed@android.com8a1c16f2008-12-17 15:59:43 +000060
caryclark3127c992015-12-09 12:02:30 -080061 return SkLeftShift(x, 10);
reed@android.com8a1c16f2008-12-17 15:59:43 +000062}
63
reed@google.com8f4d2302013-12-17 16:44:46 +000064#define SkScalarToFDot6(x) (SkFDot6)((x) * 64)
65#define SkFDot6ToScalar(x) ((SkScalar)(x) * 0.015625f)
benjaminwagner6b3eacb2016-03-24 19:07:58 -070066#define SkFDot6ToFloat SkFDot6ToScalar
reed@android.com8a1c16f2008-12-17 15:59:43 +000067
mike@reedtribe.orgbcc1d332011-04-09 19:16:54 +000068inline SkFixed SkFDot6Div(SkFDot6 a, SkFDot6 b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 SkASSERT(b != 0);
70
Brian Osman50ea3c02019-02-04 10:01:53 -050071 if (SkTFitsIn<int16_t>(a)) {
caryclark3127c992015-12-09 12:02:30 -080072 return SkLeftShift(a, 16) / b;
mike@reedtribe.orgbcc1d332011-04-09 19:16:54 +000073 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 return SkFixedDiv(a, b);
mike@reedtribe.orgbcc1d332011-04-09 19:16:54 +000075 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000076}
77
78#endif