gwnavruntime/math/checkintegeroverflow.h Source File

checkintegeroverflow.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 #pragma once
8 
11 
12 namespace Kaim
13 {
14 
15 // uncomment to assert on integer overflow
16 //#define KY_FULL_DEBUG_IntegerOverflow
17 
18 #if defined(KY_FULL_DEBUG_IntegerOverflow)
19 KY_TEMPORARY_CODE
20 #endif
21 
22 
23 #ifdef KY_FULL_DEBUG_IntegerOverflow
24 
25 inline void CheckPrecision(const Vec2LL& P)
26 {
27  // Be sure that coordinates are on 31 bits to avoid overFlow (we don't check the sign bit)
28  // 2^31 == 1LL << 31 = 2 147 483 648
29  KY_DEV_BREAK_IF(P.x >= (1LL << 31) || -P.x >= (1LL << 31));
30  KY_DEV_BREAK_IF(P.y >= (1LL << 31) || -P.y >= (1LL << 31));
31 }
32 
33 inline void CheckPrecision(const Vec2LL& v1, const Vec2LL& v2, const Vec2LL& v3, const Vec2LL& v4, const Vec2LL& v5)
34 {
35  CheckPrecision(v1);
36  CheckPrecision(v2);
37  CheckPrecision(v3);
38  CheckPrecision(v4);
39  CheckPrecision(v5);
40 }
41 
42 inline void CheckPrecision(const Vec2LL& v1, const Vec2LL& v2, const Vec2LL& v3, const Vec2LL& v4, const Vec2LL& v5, const Vec2LL& v6)
43 {
44  CheckPrecision(v1);
45  CheckPrecision(v2);
46  CheckPrecision(v3);
47  CheckPrecision(v4);
48  CheckPrecision(v5);
49  CheckPrecision(v6);
50 }
51 
52 inline void CheckPrecision(const Vec2i& P)
53 {
54  // Be sure that coordinates are on 15 bits to avoid overFlow (we don't check the sign bit)
55  // 2^15 == 1 << 15 = 32 768
56  KY_DEV_BREAK_IF(P.x >= (1 << 15) || -P.x >= (1 << 15);
57  KY_DEV_BREAK_IF(P.y >= (1 << 15) || -P.y >= (1 << 15);
58 }
59 
60 inline void CheckPrecision(const Vec2i& v1, const Vec2i& v2, const Vec2i& v3, const Vec2i& v4, const Vec2i& v5, const Vec2i& v6)
61 {
62  CheckPrecision(v1);
63  CheckPrecision(v2);
64  CheckPrecision(v3);
65  CheckPrecision(v4);
66  CheckPrecision(v5);
67  CheckPrecision(v6);
68 }
69 
70 inline void CheckPrecisionOn20Bits(KyInt64 x)
71 {
72  KY_DEV_BREAK_IF((x >= 1LL << 20 || -x >= 1LL << 20));
73 }
74 
75 inline void CheckPrecisionOn20Bits(KyInt64 x1, KyInt64 x2, KyInt64 x3, KyInt64 x4, KyInt64 x5, KyInt64 x6)
76 {
77  CheckPrecisionOn20Bits(KyInt64 x1);
78  CheckPrecisionOn20Bits(KyInt64 x2);
79  CheckPrecisionOn20Bits(KyInt64 x3);
80  CheckPrecisionOn20Bits(KyInt64 x4);
81  CheckPrecisionOn20Bits(KyInt64 x5);
82  CheckPrecisionOn20Bits(KyInt64 x6);
83 }
84 
85 
86 inline void AssertUnder8bits(const Vec2i& P)
87 {
88  // Be sure that coordinates are on 8 bits to avoid overFlow (we don't check the sign bit)
89  // 2^8 == 1 << 8
90  KY_DEV_BREAK_IF(P.x >= (1 << 8) || -P.x >= (1 << 8);
91  KY_DEV_BREAK_IF(P.y >= (1 << 8) || -P.y >= (1 << 8);
92 }
93 
94 inline void AssertUnder8bits(const Vec2i& A, const Vec2i& B, const Vec2i& C, const Vec2i& D)
95 {
96  AssertUnder8bits(A);
97  AssertUnder8bits(B);
98  AssertUnder8bits(C);
99  AssertUnder8bits(D);
100 }
101 
102 template<typename T>
103 inline void CheckDegenerated2d(const T& A, const T& B, const T& C, const T& D)
104 {
105  KY_DEV_BREAK_IF(A.x == B.x && A.y == B.y);
106  KY_DEV_BREAK_IF(C.x == D.x && C.y == D.y);
107 }
108 
109 #else
110 
111 inline void CheckPrecision(const Vec2LL&) {}
112 inline void CheckPrecision(const Vec2LL&, const Vec2LL&, const Vec2LL&, const Vec2LL&, const Vec2LL&) {}
113 inline void CheckPrecision(const Vec2LL&, const Vec2LL&, const Vec2LL&, const Vec2LL&, const Vec2LL&, const Vec2LL&) {}
114 inline void CheckPrecision(const Vec2i&) {}
115 inline void CheckPrecision(const Vec2i&, const Vec2i&, const Vec2i&, const Vec2i&, const Vec2i&, const Vec2i&) {}
116 inline void CheckPrecisionOn20Bits(KyInt64) {}
117 inline void CheckPrecisionOn20Bits(KyInt64, KyInt64, KyInt64, KyInt64, KyInt64, KyInt64) {}
118 inline void AssertUnder8bits(const Vec2i&) {}
119 inline void AssertUnder8bits(const Vec2i&, const Vec2i&, const Vec2i&, const Vec2i&) {}
120 template<typename T> inline void CheckDegenerated2d(const T&, const T&, const T&, const T&) {}
121 
122 #endif
123 
124 }
std::int64_t KyInt64
int64_t
Definition: types.h:25
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17