blob: 1353fb2148cbf4ecc9bb211613c2915c5e687d91 [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
59inline GrFixed GrFixedAbs(GrFixed x) {
60 int32_t s = (x & 0x80000000) >> 31;
61 return (GrFixed)(((int32_t)x ^ s) - s);
62}
63
64///////////////////////////////////////////////////////////////////////////////
65
66#if GR_SCALAR_IS_FIXED
67 typedef GrFixed GrScalar;
68 #define GrIntToScalar(x) GrIntToFixed(x)
69 #define GrFixedToScalar(x) (x)
70 #define GrScalarToFloat(x) GrFixedToFloat(x)
71 #define GrFloatToScalar(x) GrFloatToFixed(x)
72 #define GrScalarHalf(x) ((x) >> 1)
73 #define GrScalarAve(x,y) (((x)+(y)) >> 1)
74 #define GrScalarAbs(x) GrFixedAbs(x)
75 #define GR_Scalar1 GR_Fixed1
76 #define GR_ScalarHalf GR_FixedHalf
77 #define GR_ScalarMax GR_FixedMax
78 #define GR_ScalarMin GR_FixedMin
79#elif GR_SCALAR_IS_FLOAT
80 typedef float GrScalar;
81 #define GrIntToScalar(x) ((GrScalar)x)
82 #define GrFixedToScalar(x) GrFixedToFloat(x)
83 #define GrScalarToFloat(x) (x)
84 #define GrFloatToScalar(x) (x)
85 #define GrScalarHalf(x) ((x) * 0.5f)
86 #define GrScalarAbs(x) fabsf(x)
87 #define GrScalarAve(x,y) (((x) + (y)) * 0.5f)
88 #define GR_Scalar1 1.f
89 #define GR_ScalarHalf 0.5f
90 #define GR_ScalarMax (FLT_MAX)
91 #define GR_ScalarMin (-FLT_MAX)
92
93 static inline int32_t GrScalarFloorToInt(float x) {
94 return (int32_t)::floorf(x);
95 }
96 static inline int32_t GrScalarCeilToInt(float x) {
97 return (int32_t)::ceilf(x);
98 }
99#else
100 #error "Scalar type not defined"
101#endif
102
103/**
104 * Multiply two GrScalar values
105 */
106static inline GrScalar GrMul(GrScalar a, GrScalar b) {
107#if GR_SCALAR_IS_FLOAT
108 return a * b;
109#else
110 int64_t tmp = (int64_t)a * b;
111 return (tmp + GR_FixedHalf) >> 16;
112#endif
113}
114
115#endif
116