Skip to content

Commit 2f29b4e

Browse files
Added the main code files
1 parent afc3297 commit 2f29b4e

14 files changed

+7235
-0
lines changed

SYS_Math.h

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright (c) 2018 Side Effects Software Inc.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*
22+
* COMMENTS:
23+
* Miscellaneous math functions.
24+
*/
25+
26+
#pragma once
27+
28+
#ifndef __SYS_Math__
29+
#define __SYS_Math__
30+
31+
#include "SYS_Types.h"
32+
33+
#include <float.h>
34+
#include <limits>
35+
#include <math.h>
36+
37+
// NOTE:
38+
// These have been carefully written so that in the case of equality
39+
// we always return the first parameter. This is so that NANs in
40+
// in the second parameter are suppressed.
41+
#define h_min(a, b) (((a) > (b)) ? (b) : (a))
42+
#define h_max(a, b) (((a) < (b)) ? (b) : (a))
43+
// DO NOT CHANGE THE ABOVE WITHOUT READING THE COMMENT
44+
#define h_abs(a) (((a) > 0) ? (a) : -(a))
45+
46+
static constexpr inline int16 SYSmin(int16 a, int16 b) { return h_min(a,b); }
47+
static constexpr inline int16 SYSmax(int16 a, int16 b) { return h_max(a,b); }
48+
static constexpr inline int16 SYSabs(int16 a) { return h_abs(a); }
49+
static constexpr inline int32 SYSmin(int32 a, int32 b) { return h_min(a,b); }
50+
static constexpr inline int32 SYSmax(int32 a, int32 b) { return h_max(a,b); }
51+
static constexpr inline int32 SYSabs(int32 a) { return h_abs(a); }
52+
static constexpr inline int64 SYSmin(int64 a, int64 b) { return h_min(a,b); }
53+
static constexpr inline int64 SYSmax(int64 a, int64 b) { return h_max(a,b); }
54+
static constexpr inline int64 SYSmin(int32 a, int64 b) { return h_min(a,b); }
55+
static constexpr inline int64 SYSmax(int32 a, int64 b) { return h_max(a,b); }
56+
static constexpr inline int64 SYSmin(int64 a, int32 b) { return h_min(a,b); }
57+
static constexpr inline int64 SYSmax(int64 a, int32 b) { return h_max(a,b); }
58+
static constexpr inline int64 SYSabs(int64 a) { return h_abs(a); }
59+
static constexpr inline uint16 SYSmin(uint16 a, uint16 b) { return h_min(a,b); }
60+
static constexpr inline uint16 SYSmax(uint16 a, uint16 b) { return h_max(a,b); }
61+
static constexpr inline uint32 SYSmin(uint32 a, uint32 b) { return h_min(a,b); }
62+
static constexpr inline uint32 SYSmax(uint32 a, uint32 b) { return h_max(a,b); }
63+
static constexpr inline uint64 SYSmin(uint64 a, uint64 b) { return h_min(a,b); }
64+
static constexpr inline uint64 SYSmax(uint64 a, uint64 b) { return h_max(a,b); }
65+
static constexpr inline fpreal32 SYSmin(fpreal32 a, fpreal32 b) { return h_min(a,b); }
66+
static constexpr inline fpreal32 SYSmax(fpreal32 a, fpreal32 b) { return h_max(a,b); }
67+
static constexpr inline fpreal64 SYSmin(fpreal64 a, fpreal64 b) { return h_min(a,b); }
68+
static constexpr inline fpreal64 SYSmax(fpreal64 a, fpreal64 b) { return h_max(a,b); }
69+
70+
// Some systems have size_t as a seperate type from uint. Some don't.
71+
#if (defined(LINUX) && defined(IA64)) || defined(MBSD)
72+
static constexpr inline size_t SYSmin(size_t a, size_t b) { return h_min(a,b); }
73+
static constexpr inline size_t SYSmax(size_t a, size_t b) { return h_max(a,b); }
74+
#endif
75+
76+
#undef h_min
77+
#undef h_max
78+
#undef h_abs
79+
80+
#define h_clamp(val, min, max, tol) \
81+
((val <= min+tol) ? min : ((val >= max-tol) ? max : val))
82+
83+
static constexpr inline int
84+
SYSclamp(int v, int min, int max)
85+
{ return h_clamp(v, min, max, 0); }
86+
87+
static constexpr inline uint
88+
SYSclamp(uint v, uint min, uint max)
89+
{ return h_clamp(v, min, max, 0); }
90+
91+
static constexpr inline int64
92+
SYSclamp(int64 v, int64 min, int64 max)
93+
{ return h_clamp(v, min, max, int64(0)); }
94+
95+
static constexpr inline uint64
96+
SYSclamp(uint64 v, uint64 min, uint64 max)
97+
{ return h_clamp(v, min, max, uint64(0)); }
98+
99+
static constexpr inline fpreal32
100+
SYSclamp(fpreal32 v, fpreal32 min, fpreal32 max, fpreal32 tol=(fpreal32)0)
101+
{ return h_clamp(v, min, max, tol); }
102+
103+
static constexpr inline fpreal64
104+
SYSclamp(fpreal64 v, fpreal64 min, fpreal64 max, fpreal64 tol=(fpreal64)0)
105+
{ return h_clamp(v, min, max, tol); }
106+
107+
#undef h_clamp
108+
109+
static inline fpreal64 SYSsqrt(fpreal64 arg)
110+
{ return ::sqrt(arg); }
111+
static inline fpreal32 SYSsqrt(fpreal32 arg)
112+
{ return ::sqrtf(arg); }
113+
static inline fpreal64 SYSatan2(fpreal64 a, fpreal64 b)
114+
{ return ::atan2(a, b); }
115+
static inline fpreal32 SYSatan2(fpreal32 a, fpreal32 b)
116+
{ return ::atan2(a, b); }
117+
118+
static inline fpreal32 SYSabs(fpreal32 a) { return ::fabsf(a); }
119+
static inline fpreal64 SYSabs(fpreal64 a) { return ::fabs(a); }
120+
121+
#endif

SYS_Types.h

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright (c) 2018 Side Effects Software Inc.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*
22+
* COMMENTS:
23+
* Common type definitions.
24+
*/
25+
26+
#pragma once
27+
28+
#ifndef __SYS_Types__
29+
#define __SYS_Types__
30+
31+
/* Include system types */
32+
#include <limits>
33+
#include <type_traits>
34+
#include <sys/types.h>
35+
36+
/*
37+
* Integer types
38+
*/
39+
typedef signed char int8;
40+
typedef unsigned char uint8;
41+
typedef short int16;
42+
typedef unsigned short uint16;
43+
typedef int int32;
44+
typedef unsigned int uint32;
45+
46+
#ifndef MBSD
47+
typedef unsigned int uint;
48+
#endif
49+
50+
/*
51+
* Avoid using uint64.
52+
* The extra bit of precision is NOT worth the cost in pain and suffering
53+
* induced by use of unsigned.
54+
*/
55+
#if defined(_WIN32)
56+
typedef __int64 int64;
57+
typedef unsigned __int64 uint64;
58+
#elif defined(MBSD)
59+
// On MBSD, int64/uint64 are also defined in the system headers so we must
60+
// declare these in the same way or else we get conflicts.
61+
#include <stdint.h>
62+
typedef int64_t int64;
63+
typedef uint64_t uint64;
64+
#elif defined(AMD64)
65+
typedef long int64;
66+
typedef unsigned long uint64;
67+
#else
68+
typedef long long int64;
69+
typedef unsigned long long uint64;
70+
#endif
71+
72+
/// The problem with int64 is that it implies that it is a fixed 64-bit quantity
73+
/// that is saved to disk. Therefore, we need another integral type for
74+
/// indexing our arrays.
75+
typedef int64 exint;
76+
77+
/// Mark function to be inlined. If this is done, taking the address of such
78+
/// a function is not allowed.
79+
#if defined(__GNUC__) || defined(__clang__)
80+
#define SYS_FORCE_INLINE __attribute__ ((always_inline)) inline
81+
#elif defined(_MSC_VER)
82+
#define SYS_FORCE_INLINE __forceinline
83+
#else
84+
#define SYS_FORCE_INLINE inline
85+
#endif
86+
87+
/// Floating Point Types
88+
typedef float fpreal32;
89+
typedef double fpreal64;
90+
91+
/// SYS_FPRealUnionT for type-safe casting with integral types
92+
template <typename T>
93+
union SYS_FPRealUnionT;
94+
95+
template <>
96+
union SYS_FPRealUnionT<fpreal32>
97+
{
98+
typedef int32 int_type;
99+
typedef uint32 uint_type;
100+
typedef fpreal32 fpreal_type;
101+
102+
enum {
103+
EXPONENT_BITS = 8,
104+
MANTISSA_BITS = 23,
105+
EXPONENT_BIAS = 127 };
106+
107+
int_type ival;
108+
uint_type uval;
109+
fpreal_type fval;
110+
111+
struct
112+
{
113+
uint_type mantissa_val: 23;
114+
uint_type exponent_val: 8;
115+
uint_type sign_val: 1;
116+
};
117+
};
118+
119+
template <>
120+
union SYS_FPRealUnionT<fpreal64>
121+
{
122+
typedef int64 int_type;
123+
typedef uint64 uint_type;
124+
typedef fpreal64 fpreal_type;
125+
126+
enum {
127+
EXPONENT_BITS = 11,
128+
MANTISSA_BITS = 52,
129+
EXPONENT_BIAS = 1023 };
130+
131+
int_type ival;
132+
uint_type uval;
133+
fpreal_type fval;
134+
135+
struct
136+
{
137+
uint_type mantissa_val: 52;
138+
uint_type exponent_val: 11;
139+
uint_type sign_val: 1;
140+
};
141+
};
142+
143+
typedef union SYS_FPRealUnionT<fpreal32> SYS_FPRealUnionF;
144+
typedef union SYS_FPRealUnionT<fpreal64> SYS_FPRealUnionD;
145+
146+
/// Asserts are disabled
147+
/// @{
148+
#define UT_ASSERT_P(ZZ) ((void)0)
149+
#define UT_ASSERT(ZZ) ((void)0)
150+
#define UT_ASSERT_MSG_P(ZZ, MM) ((void)0)
151+
#define UT_ASSERT_MSG(ZZ, MM) ((void)0)
152+
/// @}
153+
154+
#endif

UT_Array.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2018 Side Effects Software Inc.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*
22+
* COMMENTS:
23+
* A wrapper function for the "free" function, used by UT_(Small)Array
24+
*/
25+
26+
#include "UT_Array.h"
27+
#include "UT_SmallArray.h"
28+
#include <stdlib.h>
29+
30+
// This needs to be here or else the warning suppression doesn't work because
31+
// the templated calling code won't otherwise be compiled until after we've
32+
// already popped the warning.state. So we just always disable this at file
33+
// scope here.
34+
#if defined(__GNUC__) && !defined(__clang__)
35+
_Pragma("GCC diagnostic push")
36+
_Pragma("GCC diagnostic ignored \"-Wfree-nonheap-object\"")
37+
#endif
38+
void ut_ArrayImplFree(void *p)
39+
{
40+
free(p);
41+
}
42+
#if defined(__GNUC__) && !defined(__clang__)
43+
_Pragma("GCC diagnostic pop")
44+
#endif

0 commit comments

Comments
 (0)