1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
/*
* Copyright (C) 2006-2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "SkFloat.h"
#include "SkMath.h"
#define EXP_BIAS (127+23)
static int get_unsigned_exp(uint32_t packed)
{
return (packed << 1 >> 24);
}
static unsigned get_unsigned_value(uint32_t packed)
{
return (packed << 9 >> 9) | (1 << 23);
}
static int get_signed_value(int32_t packed)
{
return SkApplySign(get_unsigned_value(packed), SkExtractSign(packed));
}
/////////////////////////////////////////////////////////////////////////
int SkFloat::GetShift(int32_t packed, int shift)
{
if (packed == 0)
return 0;
int exp = get_unsigned_exp(packed) - EXP_BIAS - shift;
int value = get_unsigned_value(packed);
if (exp >= 0)
{
if (exp > 8) // overflow
value = SK_MaxS32;
else
value <<= exp;
}
else
{
exp = -exp;
if (exp > 23) // underflow
value = 0;
else
value >>= exp;
}
return SkApplySign(value, SkExtractSign(packed));
}
/////////////////////////////////////////////////////////////////////////////////////
int32_t SkFloat::SetShift(int value, int shift)
{
if (value == 0)
return 0;
// record the sign and make value positive
int sign = SkExtractSign(value);
value = SkApplySign(value, sign);
if (value >> 24) // value is too big (has more than 24 bits set)
{
int bias = 8 - SkCLZ(value);
SkASSERT(bias > 0 && bias < 8);
value >>= bias;
shift += bias;
}
else
{
int zeros = SkCLZ(value << 8);
SkASSERT(zeros >= 0 && zeros <= 23);
value <<= zeros;
shift -= zeros;
}
// now value is left-aligned to 24 bits
SkASSERT((value >> 23) == 1);
shift += EXP_BIAS;
if (shift < 0) // underflow
return 0;
else
{
if (shift > 255) // overflow
{
shift = 255;
value = 0x00FFFFFF;
}
int32_t packed = sign << 31; // set the sign-bit
packed |= shift << 23; // store the packed exponent
packed |= ((unsigned)(value << 9) >> 9); // clear 24th bit of value (its implied)
#ifdef SK_DEBUG
{
int n;
n = SkExtractSign(packed);
SkASSERT(n == sign);
n = get_unsigned_exp(packed);
SkASSERT(n == shift);
n = get_unsigned_value(packed);
SkASSERT(n == value);
}
#endif
return packed;
}
}
int32_t SkFloat::Neg(int32_t packed)
{
if (packed)
packed = packed ^ (1 << 31);
return packed;
}
int32_t SkFloat::Add(int32_t packed_a, int32_t packed_b)
{
if (packed_a == 0)
return packed_b;
if (packed_b == 0)
return packed_a;
int exp_a = get_unsigned_exp(packed_a);
int exp_b = get_unsigned_exp(packed_b);
int exp_diff = exp_a - exp_b;
int shift_a = 0, shift_b = 0;
int exp;
if (exp_diff >= 0)
{
if (exp_diff > 24) // B is too small to contribute
return packed_a;
shift_b = exp_diff;
exp = exp_a;
}
else
{
exp_diff = -exp_diff;
if (exp_diff > 24) // A is too small to contribute
return packed_b;
shift_a = exp_diff;
exp = exp_b;
}
int value_a = get_signed_value(packed_a) >> shift_a;
int value_b = get_signed_value(packed_b) >> shift_b;
return SkFloat::SetShift(value_a + value_b, exp - EXP_BIAS);
}
#include "Sk64.h"
static inline int32_t mul24(int32_t a, int32_t b)
{
Sk64 tmp;
tmp.setMul(a, b);
tmp.roundRight(24);
return tmp.get32();
}
int32_t SkFloat::Mul(int32_t packed_a, int32_t packed_b)
{
if (packed_a == 0 || packed_b == 0)
return 0;
int exp_a = get_unsigned_exp(packed_a);
int exp_b = get_unsigned_exp(packed_b);
int value_a = get_signed_value(packed_a);
int value_b = get_signed_value(packed_b);
return SkFloat::SetShift(mul24(value_a, value_b), exp_a + exp_b - 2*EXP_BIAS + 24);
}
int32_t SkFloat::MulInt(int32_t packed, int n)
{
return Mul(packed, SetShift(n, 0));
}
int32_t SkFloat::Div(int32_t packed_n, int32_t packed_d)
{
SkASSERT(packed_d != 0);
if (packed_n == 0)
return 0;
int exp_n = get_unsigned_exp(packed_n);
int exp_d = get_unsigned_exp(packed_d);
int value_n = get_signed_value(packed_n);
int value_d = get_signed_value(packed_d);
return SkFloat::SetShift(SkDivBits(value_n, value_d, 24), exp_n - exp_d - 24);
}
int32_t SkFloat::DivInt(int32_t packed, int n)
{
return Div(packed, SetShift(n, 0));
}
int32_t SkFloat::Invert(int32_t packed)
{
return Div(packed, SetShift(1, 0));
}
int32_t SkFloat::Sqrt(int32_t packed)
{
if (packed < 0)
{
SkASSERT(!"can't sqrt a negative number");
return 0;
}
int exp = get_unsigned_exp(packed);
int value = get_unsigned_value(packed);
int nexp = exp - EXP_BIAS;
int root = SkSqrtBits(value << (nexp & 1), 26);
nexp >>= 1;
return SkFloat::SetShift(root, nexp - 11);
}
#if defined _WIN32 && _MSC_VER >= 1300 // disable warning : unreachable code
#pragma warning ( push )
#pragma warning ( disable : 4702 )
#endif
int32_t SkFloat::CubeRoot(int32_t packed)
{
sk_throw();
return 0;
}
#if defined _WIN32 && _MSC_VER >= 1300
#pragma warning ( pop )
#endif
static inline int32_t clear_high_bit(int32_t n)
{
return ((uint32_t)(n << 1)) >> 1;
}
static inline int int_sign(int32_t a, int32_t b)
{
return a > b ? 1 : (a < b ? -1 : 0);
}
int SkFloat::Cmp(int32_t packed_a, int32_t packed_b)
{
packed_a = SkApplySign(clear_high_bit(packed_a), SkExtractSign(packed_a));
packed_b = SkApplySign(clear_high_bit(packed_b), SkExtractSign(packed_b));
return int_sign(packed_a, packed_b);
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
#ifdef SK_DEBUG
#include "SkRandom.h"
#ifdef SK_CAN_USE_FLOAT
#include "SkFloatingPoint.h"
#endif
void SkFloat::UnitTest()
{
#ifdef SK_SUPPORT_UNITTEST
SkFloat a, b, c, d;
int n;
a.setZero();
n = a.getInt();
SkASSERT(n == 0);
b.setInt(5);
n = b.getInt();
SkASSERT(n == 5);
c.setInt(-3);
n = c.getInt();
SkASSERT(n == -3);
d.setAdd(c, b);
SkDebugf("SkFloat: %d + %d = %d\n", c.getInt(), b.getInt(), d.getInt());
SkRandom rand;
#ifdef SK_CAN_USE_FLOAT
int i;
for (i = 0; i < 1000; i++)
{
float fa, fb;
int aa = rand.nextS() >> 14;
int bb = rand.nextS() >> 14;
a.setInt(aa);
b.setInt(bb);
SkASSERT(a.getInt() == aa);
SkASSERT(b.getInt() == bb);
c.setAdd(a, b);
int cc = c.getInt();
SkASSERT(cc == aa + bb);
c.setSub(a, b);
cc = c.getInt();
SkASSERT(cc == aa - bb);
aa >>= 5;
bb >>= 5;
a.setInt(aa);
b.setInt(bb);
c.setMul(a, b);
cc = c.getInt();
SkASSERT(cc == aa * bb);
/////////////////////////////////////
aa = rand.nextS() >> 11;
a.setFixed(aa);
cc = a.getFixed();
SkASSERT(aa == cc);
bb = rand.nextS() >> 11;
b.setFixed(bb);
cc = b.getFixed();
SkASSERT(bb == cc);
cc = SkFixedMul(aa, bb);
c.setMul(a, b);
SkFixed dd = c.getFixed();
int diff = cc - dd;
SkASSERT(SkAbs32(diff) <= 1);
fa = (float)aa / 65536.0f;
fb = (float)bb / 65536.0f;
a.assertEquals(fa);
b.assertEquals(fb);
fa = a.getFloat();
fb = b.getFloat();
c.assertEquals(fa * fb, 1);
c.setDiv(a, b);
cc = SkFixedDiv(aa, bb);
dd = c.getFixed();
diff = cc - dd;
SkASSERT(SkAbs32(diff) <= 3);
c.assertEquals(fa / fb, 1);
SkASSERT((aa == bb) == (a == b));
SkASSERT((aa != bb) == (a != b));
SkASSERT((aa < bb) == (a < b));
SkASSERT((aa <= bb) == (a <= b));
SkASSERT((aa > bb) == (a > b));
SkASSERT((aa >= bb) == (a >= b));
if (aa < 0)
{
aa = -aa;
fa = -fa;
}
a.setFixed(aa);
c.setSqrt(a);
cc = SkFixedSqrt(aa);
dd = c.getFixed();
SkASSERT(dd == cc);
c.assertEquals(sk_float_sqrt(fa), 2);
// cuberoot
#if 0
a.setInt(1);
a.cubeRoot();
a.assertEquals(1.0f, 0);
a.setInt(8);
a.cubeRoot();
a.assertEquals(2.0f, 0);
a.setInt(27);
a.cubeRoot();
a.assertEquals(3.0f, 0);
#endif
}
#endif
#endif
}
#endif
|