GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
math_utils.h
Go to the documentation of this file.
1// Copyright 2016 The Draco Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15#ifndef DRACO_CORE_MATH_UTILS_H_
16#define DRACO_CORE_MATH_UTILS_H_
17
18#include <inttypes.h>
19
20#include "draco/core/vector_d.h"
21
22namespace draco {
23
24#define DRACO_INCREMENT_MOD(I, M) (((I) == ((M)-1)) ? 0 : ((I) + 1))
25
26// Returns floor(sqrt(x)) where x is an integer number. The main intend of this
27// function is to provide a cross platform and deterministic implementation of
28// square root for integer numbers. This function is not intended to be a
29// replacement for std::sqrt() for general cases. IntSqrt is in fact about 3X
30// slower compared to most implementation of std::sqrt().
31inline uint64_t IntSqrt(uint64_t number) {
32 if (number == 0) {
33 return 0;
34 }
35 // First estimate good initial value of the square root as log2(number).
36 uint64_t act_number = number;
37 uint64_t square_root = 1;
38 while (act_number >= 2) {
39 // Double the square root until |square_root * square_root > number|.
40 square_root *= 2;
41 act_number /= 4;
42 }
43 // Perform Newton's (or Babylonian) method to find the true floor(sqrt()).
44 do {
45 // New |square_root| estimate is computed as the average between
46 // |square_root| and |number / square_root|.
47 square_root = (square_root + number / square_root) / 2;
48
49 // Note that after the first iteration, the estimate is always going to be
50 // larger or equal to the true square root value. Therefore to check
51 // convergence, we can simply detect condition when the square of the
52 // estimated square root is larger than the input.
53 } while (square_root * square_root > number);
54 return square_root;
55}
56
57// Performs the addition in unsigned type to avoid signed integer overflow. Note
58// that the result will be the same (for non-overflowing values).
59template <
60 typename DataTypeT,
61 typename std::enable_if<std::is_integral<DataTypeT>::value &&
62 std::is_signed<DataTypeT>::value>::type * = nullptr>
63inline DataTypeT AddAsUnsigned(DataTypeT a, DataTypeT b) {
64 typedef typename std::make_unsigned<DataTypeT>::type DataTypeUT;
65 return static_cast<DataTypeT>(static_cast<DataTypeUT>(a) +
66 static_cast<DataTypeUT>(b));
67}
68
69template <typename DataTypeT,
70 typename std::enable_if<!std::is_integral<DataTypeT>::value ||
71 !std::is_signed<DataTypeT>::value>::type * =
72 nullptr>
73inline DataTypeT AddAsUnsigned(DataTypeT a, DataTypeT b) {
74 return a + b;
75}
76
77} // namespace draco
78
79#endif // DRACO_CORE_MATH_UTILS_H_
Definition attribute_octahedron_transform.cc:21
uint64_t IntSqrt(uint64_t number)
Definition math_utils.h:31
DataTypeT AddAsUnsigned(DataTypeT a, DataTypeT b)
Definition math_utils.h:63