blob: 7aaa43d33ff21878a8dec108451b4bc335a2b997 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
2 Copyright 2010 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17
18#ifndef GrScalar_DEFINED
19#define GrScalar_DEFINED
20
21#include "GrTypes.h"
22
23#include <float.h>
24#include <math.h>
25
26#define GR_Int32Max (0x7fffffff)
27#define GR_Int32Min (0x80000000)
28
29/**
30 * Convert an int to fixed point
31 */
32#if GR_DEBUG
33 inline GrFixed GrIntToFixed(int i) {
34 GrAssert(((i & 0xffff0000) == 0xffff0000) || ((i & 0xffff0000) == 0x0));
35 return i << 16;
36 }
37#else
38 #define GrIntToFixed(i) (GrFixed)((i) << 16)
39#endif
40
41#define GR_Fixed1 (1 << 16)
42#define GR_FixedHalf (1 << 15)
43#define GR_FixedMax GR_Int32Max
44#define GR_FixedMin GR_Int32Min
45
46#define GrFixedFloorToFixed(x) ((x) & ~0xFFFF)
47#define GrFixedFloorToInt(x) ((x) >> 16)
48
49/**
50 * Convert fixed point to floating point
51 */
52#define GrFixedToFloat(x) ((x) * 0.0000152587890625f)
53
54/**
55 * Convert floating point to fixed point
56 */
57#define GrFloatToFixed(x) ((GrFixed)((x) * GR_Fixed1))
58
bsalomon@google.com205d4602011-04-25 12:43:45 +000059static inline GrFixed GrFixedAbs(GrFixed x) {
reed@google.comac10a2d2010-12-22 21:39:39 +000060 int32_t s = (x & 0x80000000) >> 31;
61 return (GrFixed)(((int32_t)x ^ s) - s);
62}
63
bsalomon@google.com205d4602011-04-25 12:43:45 +000064static inline bool GrFixedIsInt(GrFixed x) {
65 return 0 == (x & 0xffff);
66}
67
68static inline bool GrFloatIsInt(float x) {
69 return x == (float)(int)x;
70}
71
reed@google.comac10a2d2010-12-22 21:39:39 +000072///////////////////////////////////////////////////////////////////////////////
73
74#if GR_SCALAR_IS_FIXED
75 typedef GrFixed GrScalar;
76 #define GrIntToScalar(x) GrIntToFixed(x)
77 #define GrFixedToScalar(x) (x)
78 #define GrScalarToFloat(x) GrFixedToFloat(x)
79 #define GrFloatToScalar(x) GrFloatToFixed(x)
80 #define GrScalarHalf(x) ((x) >> 1)
81 #define GrScalarAve(x,y) (((x)+(y)) >> 1)
82 #define GrScalarAbs(x) GrFixedAbs(x)
bsalomon@google.com205d4602011-04-25 12:43:45 +000083 #define GrScalarIsInt GrFixedIsInt
reed@google.comac10a2d2010-12-22 21:39:39 +000084 #define GR_Scalar1 GR_Fixed1
85 #define GR_ScalarHalf GR_FixedHalf
86 #define GR_ScalarMax GR_FixedMax
87 #define GR_ScalarMin GR_FixedMin
88#elif GR_SCALAR_IS_FLOAT
89 typedef float GrScalar;
90 #define GrIntToScalar(x) ((GrScalar)x)
91 #define GrFixedToScalar(x) GrFixedToFloat(x)
92 #define GrScalarToFloat(x) (x)
93 #define GrFloatToScalar(x) (x)
94 #define GrScalarHalf(x) ((x) * 0.5f)
95 #define GrScalarAbs(x) fabsf(x)
96 #define GrScalarAve(x,y) (((x) + (y)) * 0.5f)
bsalomon@google.com205d4602011-04-25 12:43:45 +000097 #define GrScalarIsInt GrFloatIsInt
reed@google.comac10a2d2010-12-22 21:39:39 +000098 #define GR_Scalar1 1.f
99 #define GR_ScalarHalf 0.5f
100 #define GR_ScalarMax (FLT_MAX)
101 #define GR_ScalarMin (-FLT_MAX)
102
103 static inline int32_t GrScalarFloorToInt(float x) {
104 return (int32_t)::floorf(x);
105 }
106 static inline int32_t GrScalarCeilToInt(float x) {
107 return (int32_t)::ceilf(x);
108 }
109#else
110 #error "Scalar type not defined"
111#endif
112
113/**
114 * Multiply two GrScalar values
115 */
116static inline GrScalar GrMul(GrScalar a, GrScalar b) {
117#if GR_SCALAR_IS_FLOAT
118 return a * b;
119#else
120 int64_t tmp = (int64_t)a * b;
121 return (tmp + GR_FixedHalf) >> 16;
122#endif
123}
124
125#endif
126