Line data Source code
1 : /*
2 : * Copyright 2008 The Android Open Source Project
3 : *
4 : * Use of this source code is governed by a BSD-style license that can be
5 : * found in the LICENSE file.
6 : */
7 :
8 : #include "SkMathPriv.h"
9 : #include "SkFixed.h"
10 : #include "SkFloatBits.h"
11 : #include "SkFloatingPoint.h"
12 : #include "SkScalar.h"
13 :
14 : #define sub_shift(zeros, x, n) \
15 : zeros -= n; \
16 : x >>= n
17 :
18 0 : int SkCLZ_portable(uint32_t x) {
19 0 : if (x == 0) {
20 0 : return 32;
21 : }
22 :
23 0 : int zeros = 31;
24 0 : if (x & 0xFFFF0000) {
25 0 : sub_shift(zeros, x, 16);
26 : }
27 0 : if (x & 0xFF00) {
28 0 : sub_shift(zeros, x, 8);
29 : }
30 0 : if (x & 0xF0) {
31 0 : sub_shift(zeros, x, 4);
32 : }
33 0 : if (x & 0xC) {
34 0 : sub_shift(zeros, x, 2);
35 : }
36 0 : if (x & 0x2) {
37 0 : sub_shift(zeros, x, 1);
38 : }
39 :
40 0 : return zeros;
41 : }
42 :
43 : ///////////////////////////////////////////////////////////////////////////////
44 :
45 : /* www.worldserver.com/turk/computergraphics/FixedSqrt.pdf
46 : */
47 0 : int32_t SkSqrtBits(int32_t x, int count) {
48 0 : SkASSERT(x >= 0 && count > 0 && (unsigned)count <= 30);
49 :
50 0 : uint32_t root = 0;
51 0 : uint32_t remHi = 0;
52 0 : uint32_t remLo = x;
53 :
54 0 : do {
55 0 : root <<= 1;
56 :
57 0 : remHi = (remHi<<2) | (remLo>>30);
58 0 : remLo <<= 2;
59 :
60 0 : uint32_t testDiv = (root << 1) + 1;
61 0 : if (remHi >= testDiv) {
62 0 : remHi -= testDiv;
63 0 : root++;
64 : }
65 : } while (--count >= 0);
66 :
67 0 : return root;
68 : }
69 :
70 : ///////////////////////////////////////////////////////////////////////////////
71 :
72 0 : float SkScalarSinCos(float radians, float* cosValue) {
73 0 : float sinValue = sk_float_sin(radians);
74 :
75 0 : if (cosValue) {
76 0 : *cosValue = sk_float_cos(radians);
77 0 : if (SkScalarNearlyZero(*cosValue)) {
78 0 : *cosValue = 0;
79 : }
80 : }
81 :
82 0 : if (SkScalarNearlyZero(sinValue)) {
83 0 : sinValue = 0;
84 : }
85 0 : return sinValue;
86 : }
|