blob: 6eff790c85c5c08e56ecda331426f2281ba8add4 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2008 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
reed@google.com4b163ed2012-08-07 21:35:13 +00008#include "SkMathPriv.h"
benjaminwagner6c71e0a2016-04-07 08:49:31 -07009#include "SkFixed.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkFloatBits.h"
11#include "SkFloatingPoint.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkScalar.h"
13
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#define sub_shift(zeros, x, n) \
15 zeros -= n; \
16 x >>= n
rmistry@google.comfbfcd562012-08-23 18:09:54 +000017
reed@android.com8a1c16f2008-12-17 15:59:43 +000018int SkCLZ_portable(uint32_t x) {
19 if (x == 0) {
20 return 32;
21 }
22
reed@android.com8a1c16f2008-12-17 15:59:43 +000023 int zeros = 31;
24 if (x & 0xFFFF0000) {
25 sub_shift(zeros, x, 16);
26 }
27 if (x & 0xFF00) {
28 sub_shift(zeros, x, 8);
29 }
30 if (x & 0xF0) {
31 sub_shift(zeros, x, 4);
32 }
33 if (x & 0xC) {
34 sub_shift(zeros, x, 2);
35 }
36 if (x & 0x2) {
37 sub_shift(zeros, x, 1);
38 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000039
40 return zeros;
41}
42
reed@android.com8a1c16f2008-12-17 15:59:43 +000043///////////////////////////////////////////////////////////////////////////////
44
reed@android.com8a1c16f2008-12-17 15:59:43 +000045/* www.worldserver.com/turk/computergraphics/FixedSqrt.pdf
46*/
47int32_t SkSqrtBits(int32_t x, int count) {
48 SkASSERT(x >= 0 && count > 0 && (unsigned)count <= 30);
49
50 uint32_t root = 0;
51 uint32_t remHi = 0;
52 uint32_t remLo = x;
53
54 do {
55 root <<= 1;
56
57 remHi = (remHi<<2) | (remLo>>30);
58 remLo <<= 2;
59
60 uint32_t testDiv = (root << 1) + 1;
61 if (remHi >= testDiv) {
62 remHi -= testDiv;
63 root++;
64 }
65 } while (--count >= 0);
66
67 return root;
68}
69
reed@android.com8a1c16f2008-12-17 15:59:43 +000070///////////////////////////////////////////////////////////////////////////////
71
reed@android.com8a1c16f2008-12-17 15:59:43 +000072float SkScalarSinCos(float radians, float* cosValue) {
73 float sinValue = sk_float_sin(radians);
74
75 if (cosValue) {
76 *cosValue = sk_float_cos(radians);
77 if (SkScalarNearlyZero(*cosValue)) {
78 *cosValue = 0;
79 }
80 }
81
82 if (SkScalarNearlyZero(sinValue)) {
83 sinValue = 0;
84 }
85 return sinValue;
86}