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 "include/core/SkScalar.h"
9#include "include/private/SkFixed.h"
10#include "include/private/SkFloatBits.h"
11#include "include/private/SkFloatingPoint.h"
12#include "src/core/SkMathPriv.h"
13#include "src/core/SkSafeMath.h"
14
15#define sub_shift(zeros, x, n) \
16 zeros -= n; \
17 x >>= n
18
19int SkCLZ_portable(uint32_t x) {
20 if (x == 0) {
21 return 32;
22 }
23
24 int zeros = 31;
25 if (x & 0xFFFF0000) {
26 sub_shift(zeros, x, 16);
27 }
28 if (x & 0xFF00) {
29 sub_shift(zeros, x, 8);
30 }
31 if (x & 0xF0) {
32 sub_shift(zeros, x, 4);
33 }
34 if (x & 0xC) {
35 sub_shift(zeros, x, 2);
36 }
37 if (x & 0x2) {
38 sub_shift(zeros, x, 1);
39 }
40
41 return zeros;
42}
43
44///////////////////////////////////////////////////////////////////////////////
45
46/* www.worldserver.com/turk/computergraphics/FixedSqrt.pdf
47*/
48int32_t SkSqrtBits(int32_t x, int count) {
49 SkASSERT(x >= 0 && count > 0 && (unsigned)count <= 30);
50
51 uint32_t root = 0;
52 uint32_t remHi = 0;
53 uint32_t remLo = x;
54
55 do {
56 root <<= 1;
57
58 remHi = (remHi<<2) | (remLo>>30);
59 remLo <<= 2;
60
61 uint32_t testDiv = (root << 1) + 1;
62 if (remHi >= testDiv) {
63 remHi -= testDiv;
64 root++;
65 }
66 } while (--count >= 0);
67
68 return root;
69}
70
71///////////////////////////////////////////////////////////////////////////////////////////////////
72
73size_t SkSafeMath::Add(size_t x, size_t y) {
74 SkSafeMath tmp;
75 size_t sum = tmp.add(x, y);
76 return tmp.ok() ? sum : SIZE_MAX;
77}
78
79size_t SkSafeMath::Mul(size_t x, size_t y) {
80 SkSafeMath tmp;
81 size_t prod = tmp.mul(x, y);
82 return tmp.ok() ? prod : SIZE_MAX;
83}
84
85///////////////////////////////////////////////////////////////////////////////////////////////////
86
87bool sk_floats_are_unit(const float array[], size_t count) {
88 bool is_unit = true;
89 for (size_t i = 0; i < count; ++i) {
90 is_unit &= (array[i] >= 0) & (array[i] <= 1);
91 }
92 return is_unit;
93}
94