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
/*
* Copyright 2008 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/private/SkFloatingPoint.h"
#include "src/core/SkMathPriv.h"
#include "src/core/SkSafeMath.h"
namespace pk {
///////////////////////////////////////////////////////////////////////////////
/* www.worldserver.com/turk/computergraphics/FixedSqrt.pdf
*/
int32_t SkSqrtBits(int32_t x, int count) {
uint32_t root = 0;
uint32_t remHi = 0;
uint32_t remLo = x;
do {
root <<= 1;
remHi = (remHi<<2) | (remLo>>30);
remLo <<= 2;
uint32_t testDiv = (root << 1) + 1;
if (remHi >= testDiv) {
remHi -= testDiv;
root++;
}
} while (--count >= 0);
return root;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
size_t SkSafeMath::Add(size_t x, size_t y) {
SkSafeMath tmp;
size_t sum = tmp.add(x, y);
return tmp.ok() ? sum : SIZE_MAX;
}
size_t SkSafeMath::Mul(size_t x, size_t y) {
SkSafeMath tmp;
size_t prod = tmp.mul(x, y);
return tmp.ok() ? prod : SIZE_MAX;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
bool sk_floats_are_unit(const float array[], size_t count) {
bool is_unit = true;
for (size_t i = 0; i < count; ++i) {
is_unit &= (array[i] >= 0) & (array[i] <= 1);
}
return is_unit;
}
} // namespace pk