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 ///////////////////////////////////////////////////////////////////////////////
16
17 /* www.worldserver.com/turk/computergraphics/FixedSqrt.pdf
18 */
SkSqrtBits(int32_t x,int count)19 int32_t SkSqrtBits(int32_t x, int count) {
20 SkASSERT(x >= 0 && count > 0 && (unsigned)count <= 30);
21
22 uint32_t root = 0;
23 uint32_t remHi = 0;
24 uint32_t remLo = x;
25
26 do {
27 root <<= 1;
28
29 remHi = (remHi<<2) | (remLo>>30);
30 remLo <<= 2;
31
32 uint32_t testDiv = (root << 1) + 1;
33 if (remHi >= testDiv) {
34 remHi -= testDiv;
35 root++;
36 }
37 } while (--count >= 0);
38
39 return root;
40 }
41
42 ///////////////////////////////////////////////////////////////////////////////////////////////////
43
Add(size_t x,size_t y)44 size_t SkSafeMath::Add(size_t x, size_t y) {
45 SkSafeMath tmp;
46 size_t sum = tmp.add(x, y);
47 return tmp.ok() ? sum : SIZE_MAX;
48 }
49
Mul(size_t x,size_t y)50 size_t SkSafeMath::Mul(size_t x, size_t y) {
51 SkSafeMath tmp;
52 size_t prod = tmp.mul(x, y);
53 return tmp.ok() ? prod : SIZE_MAX;
54 }
55
56 ///////////////////////////////////////////////////////////////////////////////////////////////////
57
sk_floats_are_unit(const float array[],size_t count)58 bool sk_floats_are_unit(const float array[], size_t count) {
59 bool is_unit = true;
60 for (size_t i = 0; i < count; ++i) {
61 is_unit &= (array[i] >= 0) & (array[i] <= 1);
62 }
63 return is_unit;
64 }
65