1+ #pragma once
12#ifndef _STD_EXT_internal
23#define _STD_EXT_internal
34
5+ #ifdef WINDOWS
6+
7+
8+ #include < math.h>
9+ #include < float.h>
10+ #include < stdio.h>
11+ #include " xrCommon/math_funcs_inline.h"
12+ // #include "xr_token.h"
13+
14+ #ifdef abs
15+ #undef abs
16+ #endif
17+
18+ #ifdef _MIN
19+ #undef _MIN
20+ #endif
21+
22+ #ifdef _MAX
23+ #undef _MAX
24+ #endif
25+
26+ #ifdef min
27+ #undef min
28+ #endif
29+
30+ #ifdef max
31+ #undef max
32+ #endif
33+
34+ #if 0 //def _EDITOR
35+ IC char* strncpy_s(char* strDestination, size_t sizeInBytes, const char* strSource, size_t count)
36+ {
37+ return strncpy(strDestination, strSource, count);
38+ }
39+
40+ IC char* xr_strcpy(char* strDestination, size_t sizeInBytes, const char* strSource)
41+ {
42+ return strcpy(strDestination, strSource);
43+ }
44+
45+ IC char* xr_strcpy(char* strDestination, const char* strSource) { return strcpy(strDestination, strSource); }
46+ IC char* _strlwr_s(char* strDestination, size_t sizeInBytes) { return xr_strlwr(strDestination); }
47+ IC char* xr_strcat(char* strDestination, size_t sizeInBytes, const char* strSource)
48+ {
49+ return strncat(strDestination, strSource, sizeInBytes);
50+ }
51+
52+ IC char* xr_strcat(char* strDestination, const char* strSource) { return strcat(strDestination, strSource); }
53+ IC int xr_sprintf(char* dest, size_t sizeOfBuffer, const char* format, ...)
54+ {
55+ va_list mark;
56+ va_start(mark, format);
57+ int sz = _vsnprintf(dest, sizeOfBuffer, format, mark);
58+ dest[sizeOfBuffer - 1] = 0;
59+ va_end(mark);
60+ return sz;
61+ }
62+ #endif // _EDITOR
63+
64+ // generic
65+ template <class T >
66+ IC T _min (T a, T b)
67+ {
68+ return a < b ? a : b;
69+ }
70+ template <class T >
71+ IC T _max (T a, T b)
72+ {
73+ return a > b ? a : b;
74+ }
75+ template <class T >
76+ IC T _sqr (T a)
77+ {
78+ return a * a;
79+ }
80+
81+ IC bool _valid (const float x) noexcept
82+ {
83+ // check for: Signaling NaN, Quiet NaN, Negative infinity ( –INF), Positive infinity (+INF), Negative denormalized,
84+ // Positive denormalized
85+ int cls = _fpclass (double (x));
86+ if (cls & (_FPCLASS_SNAN + _FPCLASS_QNAN + _FPCLASS_NINF + _FPCLASS_PINF + _FPCLASS_ND + _FPCLASS_PD))
87+ return false ;
88+
89+ /* *****other cases are*****
90+ _FPCLASS_NN Negative normalized non-zero
91+ _FPCLASS_NZ Negative zero ( – 0)
92+ _FPCLASS_PZ Positive 0 (+0)
93+ _FPCLASS_PN Positive normalized non-zero
94+ */
95+ return true ;
96+ }
97+
98+ // double
99+ IC bool _valid (const double x)
100+ {
101+ // check for: Signaling NaN, Quiet NaN, Negative infinity ( –INF), Positive infinity (+INF), Negative denormalized,
102+ // Positive denormalized
103+ int cls = _fpclass (x);
104+ if (cls & (_FPCLASS_SNAN + _FPCLASS_QNAN + _FPCLASS_NINF + _FPCLASS_PINF + _FPCLASS_ND + _FPCLASS_PD))
105+ return false ;
106+
107+ /* *****other cases are*****
108+ _FPCLASS_NN Negative normalized non-zero
109+ _FPCLASS_NZ Negative zero ( – 0)
110+ _FPCLASS_PZ Positive 0 (+0)
111+ _FPCLASS_PN Positive normalized non-zero
112+ */
113+ return true ;
114+ }
115+
116+ // XXX: "magic" specializations, that really require profiling to see if they are worth this effort.
117+ // int8
118+ IC s8 _abs (s8 x) { return (x >= 0 ) ? x : s8 (-x); }
119+ IC s8 _min (s8 x, s8 y) { return y + ((x - y) & ((x - y) >> (sizeof (s8) * 8 - 1 ))); };
120+ IC s8 _max (s8 x, s8 y) { return x - ((x - y) & ((x - y) >> (sizeof (s8) * 8 - 1 ))); };
121+ // unsigned int8
122+ IC u8 _abs (u8 x) { return x; }
123+ // int16
124+ IC s16 _abs (s16 x) { return (x >= 0 ) ? x : s16 (-x); }
125+ IC s16 _min (s16 x, s16 y) { return y + ((x - y) & ((x - y) >> (sizeof (s16) * 8 - 1 ))); };
126+ IC s16 _max (s16 x, s16 y) { return x - ((x - y) & ((x - y) >> (sizeof (s16) * 8 - 1 ))); };
127+ // unsigned int16
128+ IC u16 _abs (u16 x) { return x; }
129+ // int32
130+ IC s32 _abs (s32 x) { return (x >= 0 ) ? x : s32 (-x); }
131+ IC s32 _min (s32 x, s32 y) { return y + ((x - y) & ((x - y) >> (sizeof (s32) * 8 - 1 ))); };
132+ IC s32 _max (s32 x, s32 y) { return x - ((x - y) & ((x - y) >> (sizeof (s32) * 8 - 1 ))); };
133+ // int64
134+ IC s64 _abs (s64 x) { return (x >= 0 ) ? x : s64 (-x); }
135+ IC s64 _min (s64 x, s64 y) { return y + ((x - y) & ((x - y) >> (sizeof (s64) * 8 - 1 ))); };
136+ IC s64 _max (s64 x, s64 y) { return x - ((x - y) & ((x - y) >> (sizeof (s64) * 8 - 1 ))); };
137+
138+ // string management
139+
140+ // return pointer to ".ext"
141+ IC char * strext (const char * S) { return (char *)strrchr (S, ' .' ); }
142+ IC size_t xr_strlen (const char * S) { return strlen (S); }
143+
144+ // #ifndef _EDITOR
145+ #ifndef MASTER_GOLD
146+
147+ inline int xr_strcpy (LPSTR destination, size_t const destination_size, LPCSTR source)
148+ {
149+ return strcpy_s (destination, destination_size, source);
150+ }
151+
152+ inline int xr_strcat (LPSTR destination, size_t const buffer_size, LPCSTR source)
153+ {
154+ return strcat_s (destination, buffer_size, source);
155+ }
156+
157+ inline int __cdecl xr_sprintf (LPSTR destination, size_t const buffer_size, LPCSTR format_string, ...)
158+ {
159+ va_list args;
160+ va_start (args, format_string);
161+ return vsprintf_s (destination, buffer_size, format_string, args);
162+ }
163+
164+ template <int count>
165+ inline int __cdecl xr_sprintf (char (&destination)[count], LPCSTR format_string, ...)
166+ {
167+ va_list args;
168+ va_start (args, format_string);
169+ return vsprintf_s (destination, count, format_string, args);
170+ }
171+ #else // #ifndef MASTER_GOLD
172+
173+ inline int xr_strcpy (LPSTR destination, size_t const destination_size, LPCSTR source)
174+ {
175+ return strncpy_s (destination, destination_size, source, destination_size);
176+ }
177+
178+ inline int xr_strcat (LPSTR destination, size_t const buffer_size, LPCSTR source)
179+ {
180+ size_t const destination_length = xr_strlen (destination);
181+ LPSTR i = destination + destination_length;
182+ LPSTR const e = destination + buffer_size - 1 ;
183+ if (i > e)
184+ return 0 ;
185+
186+ for (LPCSTR j = source; *j && (i != e); ++i, ++j)
187+ *i = *j;
188+
189+ *i = 0 ;
190+ return 0 ;
191+ }
192+
193+ inline int __cdecl xr_sprintf (LPSTR destination, size_t const buffer_size, LPCSTR format_string, ...)
194+ {
195+ va_list args;
196+ va_start (args, format_string);
197+ return vsnprintf_s (destination, buffer_size, buffer_size - 1 , format_string, args);
198+ }
199+
200+ template <int count>
201+ inline int __cdecl xr_sprintf (char (&destination)[count], LPCSTR format_string, ...)
202+ {
203+ va_list args;
204+ va_start (args, format_string);
205+ return vsnprintf_s (destination, count, count - 1 , format_string, args);
206+ }
207+ #endif // #ifndef MASTER_GOLD
208+
209+ template <int count>
210+ inline int xr_strcpy (char (&destination)[count], LPCSTR source)
211+ {
212+ return xr_strcpy (destination, count, source);
213+ }
214+
215+ template <int count>
216+ inline int xr_strcat (char (&destination)[count], LPCSTR source)
217+ {
218+ return xr_strcat (destination, count, source);
219+ }
220+ // #endif // #ifndef _EDITOR
221+
222+ inline void MemFill32 (void * dst, u32 value, size_t dstSize)
223+ {
224+ u32 * ptr = static_cast <u32 *>(dst);
225+ u32 * end = ptr + dstSize;
226+ while (ptr != end)
227+ *ptr++ = value;
228+ }
229+
230+ XRCORE_API char * timestamp (string64& dest);
231+
232+ extern XRCORE_API u32 crc32 (const void * P, u32 len);
233+ extern XRCORE_API u32 crc32 (const void * P, u32 len, u32 starting_crc);
234+ extern XRCORE_API u32 path_crc32 (const char * path, u32 len); // ignores '/' and '\'
235+
236+
237+
238+
239+ #else // WINDOWS
240+
241+
242+
4243#include < cmath>
5244
6245#define BREAK_AT_STRCMP
@@ -341,4 +580,6 @@ extern XRCORE_API u32 crc32(const void* P, u32 len);
341580extern XRCORE_API u32 crc32 (const void * P, u32 len, u32 starting_crc);
342581extern XRCORE_API u32 path_crc32 (const char * path, u32 len); // ignores '/' and '\'
343582
344- #endif // _STD_EXT_internal
583+ #endif // WINDOWS
584+
585+ #endif // _STD_EXT_internal
0 commit comments