1 #ifndef _CORE_HELPERS_H_
2 #define _CORE_HELPERS_H_
3
4 //#include <Windows.h>
5 #include <stdio.h>
6 #include <vector>
7 #include <string>
8 #include <math.h>
9
10 #ifndef restrict
11 #define restrict(x)
12 #endif
13 #ifndef __int64
14 #define __int64 long long int
15 #endif
16 #ifndef BOOL
17 #define BOOL bool
18 #endif
19
20 #define ASSERT_ALWAYS(expression) ALWAYS_ASSERT_RAW(expression,__FILE__,__LINE__,__FUNCTION__,#expression)
21
22 void ALWAYS_ASSERT_RAW(bool cond, const char fileName[], const int lineNum, const char funcName[], const char expression[]);
23
24 #define MY_SAFE_RELEASE(p) { if (p!=NULL) { p->Release(); p = NULL; } }
25
SafeInv(float x)26 inline float SafeInv(float x) restrict(amp) restrict(cpu)
27 {
28 return x == 0 ? 0.0f : 1.0f/x;
29 }
30
FastSqr(float x)31 inline float FastSqr(float x) restrict(amp) restrict(cpu)
32 {
33 return x*x;
34 }
35
MaxFloat(float x,float y)36 inline float MaxFloat(float x, float y) restrict(amp) restrict(cpu)
37 {
38 return x > y ? x : y;
39 }
40
MinFloat(float x,float y)41 inline float MinFloat(float x, float y) restrict(amp) restrict(cpu)
42 {
43 return x < y ? x : y;
44 }
45
MaxInt(int x,int y)46 inline int MaxInt(int x, int y) restrict(amp) restrict(cpu)
47 {
48 return x > y ? x : y;
49 }
50
MinInt(int x,int y)51 inline int MinInt(int x, int y) restrict(amp) restrict(cpu)
52 {
53 return x < y ? x : y;
54 }
55
RandNorm()56 inline float RandNorm()
57 {
58 //return float(rand()%10001)/10000.0f;
59 return 0;
60 }
61
Abs(float f)62 inline float Abs(float f) restrict(amp) restrict(cpu)
63 {
64 return f >= 0.0f ? f : -f;
65 }
66
Saturate(float x)67 inline float Saturate(float x) restrict(amp) restrict(cpu)
68 {
69 return MaxFloat(0.0f,MinFloat(1.0f,x));
70 }
71
Saturate255(float x)72 inline float Saturate255(float x) restrict(amp) restrict(cpu)
73 {
74 return MaxFloat(0.0f,MinFloat(255.0f,x));
75 }
76
ColorToChar(float x)77 inline unsigned char ColorToChar(float x)
78 {
79 return (unsigned char)Saturate255(x*256.0f);
80 }
81
82 template <class A>
Swap(A & lhs,A & rhs)83 inline void Swap(A & lhs, A & rhs) restrict(amp) restrict(cpu)
84 {
85 A temp = lhs;
86 lhs = rhs;
87 rhs = temp;
88 }
89
90
FourDigitString(int i)91 inline std::string FourDigitString(int i)
92 {
93 char camName[2048];
94 sprintf(camName,"%04d",i);
95 return std::string(camName);
96 }
97
98
GetQualityTimeMicroSec()99 inline unsigned __int64 GetQualityTimeMicroSec()
100 {
101 /*
102 LARGE_INTEGER freq;
103 LARGE_INTEGER currTime;
104 BOOL bRet;
105 bRet = QueryPerformanceFrequency(&freq);
106 ASSERT_ALWAYS(bRet);
107
108 bRet = QueryPerformanceCounter(&currTime);
109 ASSERT_ALWAYS(bRet);
110
111 double numSec = ((double)currTime.QuadPart)/((double)freq.QuadPart);
112 unsigned __int64 microSec = (unsigned __int64)(numSec * 1000000.0);
113 return microSec;
114 */
115 return 0;
116 }
117
118
119
120
121 template <class A, class B>
AlignSize(A x,B size)122 inline A AlignSize(A x, B size)
123 {
124 return (((x+size)-1)/size)*size;
125 /*
126 A extra = (x%A(size));
127 A ret = x;
128 if (extra != 0)
129 ret += A(size)-extra;
130 return ret;
131 */
132 }
133
AlignSize64(unsigned __int64 x,unsigned __int64 size)134 inline unsigned __int64 AlignSize64(unsigned __int64 x, unsigned __int64 size)
135 {
136 return (((x+size)-1)/size)*size;
137 }
138
AlignSize32(unsigned int x,unsigned int size)139 inline unsigned int AlignSize32(unsigned int x, unsigned int size)
140 {
141 return (((x+size)-1)/size)*size;
142 }
143
144
145 std::string GetNextLineFromFile(FILE * fin, bool & isEof);
146
147 std::string LocalTimeAsString();
148
149
150 #endif
151
152