17 #if _MINICAD_EXTERNAL_ || _GSWINSDK_
18 #if GS_INTERFACE_FACTORING
61 #define CLASSIC_ONLY(x) x
63 #define CLASSIC_ONLY(x)
69 #define MAC_WIN(m, w) w
70 #ifndef GS_LITTLE_ENDIAN
71 #define GS_LITTLE_ENDIAN 1
74 #define GS_BIG_ENDIAN 0
79 #define MAC_WIN(m, w) m
89inline int16_t min(int16_t a, int16_t b) {
return a > b ? b : a; }
90inline int32_t min(int32_t a, int32_t b) {
return a > b ? b : a; }
91inline int64_t min(int64_t a, int64_t b) {
return a > b ? b : a; }
92inline size_t min(
size_t a,
size_t b) {
return a > b ? b : a; }
94inline uint32_t min(uint32_t a, uint32_t b) {
return a > b ? b : a; }
96inline float min(
float a,
float b) {
return a > b ? b : a; }
97inline double min(
double a,
double b) {
return a > b ? b : a; }
102inline int16_t max(int16_t a, int16_t b) {
return b > a ? b : a; }
103inline int32_t max(int32_t a, int32_t b) {
return b > a ? b : a; }
104inline int64_t max(int64_t a, int64_t b) {
return b > a ? b : a; }
105inline size_t max(
size_t a,
size_t b) {
return b > a ? b : a; }
106inline float max(
float a,
float b) {
return b > a ? b : a; }
107inline double max(
double a,
double b) {
return b > a ? b : a; }
112inline int32_t min(int16_t a, int32_t b) {
return min((int32_t) a, b); }
113inline int32_t min(int32_t a, int16_t b) {
return min(a, (int32_t) b); }
114inline int64_t min(int64_t a, int32_t b) {
return min(a, (int64_t) b); }
115inline int64_t min(int32_t a, int64_t b) {
return min((int64_t) a, b); }
117inline int32_t max(int16_t a, int32_t b) {
return max((int32_t) a, b); }
118inline int32_t max(int32_t a, int16_t b) {
return max(a, (int32_t) b); }
119inline int64_t max(int64_t a, int32_t b) {
return max(a, (int64_t) b); }
120inline int64_t max(int32_t a, int64_t b) {
return max((int64_t) a, b); }
126template <
class T1,
class T2,
class T3>
127inline T1 PIN(
const T1& v,
const T2& lo,
const T3& hi)
128 {
return (v < (T1)lo) ? (T1)lo : (v >(T1) hi) ? (T1)hi : v; }
146 Sint32 result = (v < lo) ? lo : (v > hi) ? hi : v;
147 outWasPinned = (v == result) ?
false : true;
156#define CAST(Type, thing) (*((Type *) &(thing)))
157#define CAST_SHORT(x) (*((short *) &(x)))
158#define CAST_LONG(x) (*((Sint32 *) &(x)))
165 struct {
short lo, hi; } shortWord;
168 struct {
short hi, lo; } shortWord;
173inline const short HiWrd(
const Sint32& x) {
return ((
const HiLo *) &x)->shortWord.hi; }
174inline const short LoWrd(
const Sint32& x) {
return ((
const HiLo *) &x)->shortWord.lo; }
175inline short& HiWrd(
Sint32& x) {
return ((HiLo *) &x)->shortWord.hi; }
176inline short& LoWrd(
Sint32& x) {
return ((HiLo *) &x)->shortWord.lo; }
182template<
class A,
class B,
class C>
183inline bool inrange(A value, B lowerBound, C upperBound)
184 {
return (value >= lowerBound && value <= upperBound); }
186inline void ClipTop(
short &x,
short upperBound) {
if (x > upperBound) x = upperBound; }
187inline void ClipTop(
Sint32 &x,
Sint32 upperBound) {
if (x > upperBound) x = upperBound; }
188inline void ClipBottom(
short &x,
short lowerBound) {
if (x < lowerBound) x = lowerBound; }
189inline void ClipBottom(
Sint32 &x,
Sint32 lowerBound) {
if (x < lowerBound) x = lowerBound; }
191inline void ClipNum(
short &x,
short lowerBound,
short upperBound)
192 {
if (x < lowerBound) x = lowerBound;
else if (x > upperBound) x = upperBound; }
195 {
if (x < lowerBound) x = lowerBound;
else if (x > upperBound) x = upperBound; }
197inline void ClipNum(
double &x,
double lowerBound,
double upperBound)
198 {
if (x < lowerBound) x = lowerBound;
else if (x > upperBound) x = upperBound; }
204inline void TranslateBitPosition32(
Sint32 *pBit)
209 if (*pBit < 8) *pBit += 24;
210 else if (*pBit < 16) *pBit += 8;
211 else if (*pBit < 24) *pBit -= 8;
221inline void pbitclear(
Sint32 &x,
Sint32 bit) { x &= ~(1L << (bit&31)); }
222inline Bool8 pbittest(
Sint32 x,
Sint32 bit) {
return (x & (1L << (bit&31))) != 0; }
223inline void pbitset(
Sint32 &x,
Sint32 bit) { x |= (1L << (bit&31)); }
226inline void _pbitclear(
Sint32 &x,
Sint32 bit) { TranslateBitPosition32(&bit); pbitclear(x,bit); }
227inline Bool8 _pbittest(
Sint32 x,
Sint32 bit) { TranslateBitPosition32(&bit);
return pbittest(x,bit); }
228inline void _pbitset(
Sint32 &x,
Sint32 bit) { TranslateBitPosition32(&bit); pbitset(x,bit); }
233 #define CHAR0xFF ('\xFF')
235 #define CHAR0xFF (0xFF)
243typedef unsigned short Set16;
244typedef unsigned char Set8;
250template<
class X,
class T>
inline bool setmember(X x, T a, T b)
251 {
return (x == a || x == b); }
252template<
class X,
class T>
inline bool setmember(X x, T a, T b, T c)
253 {
return (x == a || x == b || x == c); }
254template<
class X,
class T>
inline bool setmember(X x, T a, T b, T c, T d)
255 {
return (x == a || x == b || x == c || x == d); }
256template<
class X,
class T>
inline bool setmember(X x, T a, T b, T c, T d, T e)
257 {
return (x == a || x == b || x == c || x == d || x == e); }
258template<
class X,
class T>
inline bool setmember(X x, T a, T b, T c, T d, T e, T f)
259 {
return (x == a || x == b || x == c || x == d || x == e || x == f); }
260template<
class X,
class T>
inline bool setmember(X x, T a, T b, T c, T d, T e, T f, T g)
261 {
return (x == a || x == b || x == c || x == d || x == e || x == f || x == g); }
262template<
class X,
class T>
inline bool setmember(X x, T a, T b, T c, T d, T e, T f, T g, T h)
263 {
return (x == a || x == b || x == c || x == d || x == e || x == f || x == g || x == h); }
264template<
class X,
class T>
inline bool setmember(X x, T a, T b, T c, T d, T e, T f, T g, T h, T i)
265 {
return (x == a || x == b || x == c || x == d || x == e || x == f || x == g || x == h || x == i); }
266template<
class X,
class T>
inline bool setmember(X x, T a, T b, T c, T d, T e, T f, T g, T h, T i, T j)
267 {
return (x == a || x == b || x == c || x == d || x == e || x == f || x == g || x == h || x == i || x == j); }
268template<
class X,
class T>
inline bool setmember(X x, T a, T b, T c, T d, T e, T f, T g, T h, T i, T j, T k)
269 {
return (x == a || x == b || x == c || x == d || x == e || x == f || x == g || x == h || x == i || x == j || x == k); }
270template<
class X,
class T>
inline bool setmember(X x, T a, T b, T c, T d, T e, T f, T g, T h, T i, T j, T k, T l)
271 {
return (x == a || x == b || x == c || x == d || x == e || x == f || x == g || x == h || x == i || x == j || x == k || x == l); }
272template<
class X,
class T>
inline bool setmember(X x, T a, T b, T c, T d, T e, T f, T g, T h, T i, T j, T k, T l, T m)
273 {
return (x == a || x == b || x == c || x == d || x == e || x == f || x == g || x == h || x == i || x == j || x == k || x == l || x == m); }
274template<
class X,
class T>
inline bool setmember(X x, T a, T b, T c, T d, T e, T f, T g, T h, T i, T j, T k, T l, T m, T n)
275 {
return (x == a || x == b || x == c || x == d || x == e || x == f || x == g || x == h || x == i || x == j || x == k || x == l || x == m || x == n); }
276template<
class X,
class T>
inline bool setmember(X x, T a, T b, T c, T d, T e, T f, T g, T h, T i, T j, T k, T l, T m, T n, T o)
277 {
return (x == a || x == b || x == c || x == d || x == e || x == f || x == g || x == h || x == i || x == j || x == k || x == l || x == m || x == n || x == o); }
283 enum { numLongs = 64 } ;
284 enum { maxElement = numLongs * 32 - 1 } ;
288 IntegerSet(
short i0,
short i1 = -1,
short i2 = -1,
short i3 = -1,
289 short i4 = -1,
short i5 = -1,
short i6 = -1,
short i7 = -1,
290 short i8 = -1,
short i9 = -1);
291 IntegerSet(
const IntegerSet &s);
293 Bool8 contains(
short i)
const;
294 void operator +=(
short i);
295 void operator +=(
const IntegerSet &s);
296 void operator -=(
short i);
297 void operator -=(
const IntegerSet &s);
298 IntegerSet& operator =(
const IntegerSet &s);
308inline bool TernaryXOR(
bool a,
bool b,
bool c)
310 return (!a && (b ^ c)) || (a && !(b || c));
319enum EByteSwapDirection {
327inline void ByteSwapDouble(
double *pld)
330 char *pb1 = (
char *) pld;
331 char *pb2 = pb1 +
sizeof(
Real64) - 1;
343inline void ByteSwapFloat(
float *pf)
346 char *pb1 = (
char *) pf;
347 char *pb2 = pb1 +
sizeof(float) - 1;
357inline void ByteSwapUint64(
Uint64 *pld)
360 char *pb1 = (
char *) pld;
361 char *pb2 = pb1 +
sizeof(
Uint64) - 1;
371inline void ByteSwapUint32(
Uint32 *pld)
374 char *pb1 = (
char *) pld;
375 char *pb2 = pb1 +
sizeof(
Uint32) - 1;
385inline void ByteSwapLONG(
Sint32 *pl)
388 char* p1 = (
char*)pl;
402#if GS_MAC && GS_PLATFORM32
404inline void ByteSwapLONG(
long *pl)
406 ByteSwapLONG((
Sint32 *)pl);
411inline void ByteSwapSHORT(
Sint16 *ps)
413 char *p1 = (
char*)ps;
420inline void ByteSwapDWORD(
Uint32 *pl)
422 ByteSwapLONG((
Sint32 *)pl);
425#if GS_MAC && GS_PLATFORM32
427inline void ByteSwapDWORD(
unsigned long *pl)
429 ByteSwapLONG((
Sint32 *)pl);
433inline void ByteSwapWORD(
Uint16 *ps)
435 ByteSwapSHORT((
Sint16 *)ps);
438inline Uint32 TruncatePointer(
const void* ptr)
520#define force_inline __forceinline
524#define force_inline inline
529force_inline
double fastFabs(
double d) {
return (d >= 0) ? d : -d; }
532template <
typename PinType,
typename FromType>
536FromType PinTo(
const TProgrammer& TEST_ONLY(toWhom),
const FromType fromValue)
558 const size_t kMaxByteSize = 8;
559 size_t pinTypeSize =
sizeof(PinType);
560 size_t fromTypeSize =
sizeof(FromType);
561 bool mayPinValue =
true;
567 if (pinTypeSize > kMaxByteSize ||
568 fromTypeSize > kMaxByteSize) {
572 std::ostringstream outStream;
574 outStream <<
"PINNING WITH LARGER THAN SUPPORTED TYPE. PLEASE STOP AND EVALUATE." << std::endl;
575 outStream << std::endl;
576 outStream <<
"Pinning has occured where the size of one or both " << std::endl;
577 outStream <<
"of the source and pin types is greater " << std::endl;
578 outStream <<
"than the pinning function's maximum supported size " << std::endl;
579 outStream <<
"of " << kMaxByteSize <<
" bytes." << std::endl;
580 outStream << std::endl;
581 outStream <<
"Please remove the use of this pinning function," << std::endl;
582 outStream <<
"or submit a bug that includes steps on how to reproduce this assert." << std::endl;
583 outStream << std::endl << std::ends;
585 DSTOP((toWhom,
"%s", outStream.str().c_str()));
590 else if (
typeid(FromType) ==
typeid(PinType)) {
594 std::ostringstream outStream;
596 outStream <<
"PINNING BETWEEN IDENTICAL TYPES. PLEASE STOP AND EVALUATE." << std::endl;
597 outStream << std::endl;
598 outStream <<
"Pinning has occurred where no pin should be required." << std::endl;
599 outStream << std::endl;
600 outStream <<
"Please remove the use of this pinning function," << std::endl;
601 outStream <<
"or submit a bug that includes steps on how to reproduce this assert." << std::endl;
602 outStream << std::endl << std::ends;
604 DSTOP((toWhom,
"%s", outStream.str().c_str()));
609 else if (!(std::numeric_limits<FromType>::is_signed && !std::numeric_limits<PinType>::is_signed) &&
610 sizeof(PinType) >
sizeof(FromType)) {
614 std::ostringstream outStream;
616 outStream <<
"PINNING TO LARGER TYPE. PLEASE STOP AND EVALUATE." << std::endl;
617 outStream << std::endl;
618 outStream <<
"Pinning has occured where no pin should be required." << std::endl;
619 outStream << std::endl;
620 outStream <<
"Please remove the use of this pinning function," << std::endl;
621 outStream <<
"or submit a bug that includes steps on how to reproduce this assert." << std::endl;
622 outStream << std::endl << std::ends;
624 DSTOP((toWhom,
"%s", outStream.str().c_str()));
628 maxPinValue = std::numeric_limits<PinType>::max ();
630 if (std::numeric_limits<PinType>::is_integer) {
631 minPinValue = std::numeric_limits<PinType>::min ();
634 minPinValue = -std::numeric_limits<PinType>::max ();
638 fromValue > maxPinValue) {
639 toValue = maxPinValue;
641 else if (mayPinValue &&
642 fromValue < minPinValue) {
643 toValue = minPinValue;
657template <
typename RealType,
typename OtherType>
661bool DemotionDoubleTest(
const RealType& realValue,
const OtherType& otherValue)
664 #pragma warning ( push )
665 #pragma warning( disable : 4244 )
669 #pragma warning ( pop )
675template <
typename ToType,
typename FromType>
679ToType DemoteTo(
const TProgrammer& TEST_ONLY(toWhom),
const FromType fromValue)
704 toValue = (ToType) fromValue;
710 const size_t kMaxByteSize = 8;
711 size_t toTypeSize =
sizeof(ToType);
712 size_t fromTypeSize =
sizeof(FromType);
713 bool castingResultsInLossOfData =
false;
717 if (toTypeSize > kMaxByteSize ||
718 fromTypeSize > kMaxByteSize) {
719 std::ostringstream outStream;
721 outStream <<
"CASTING WITH LARGER THAN SUPPORTED TYPE. PLEASE STOP AND EVALUATE." << std::endl;
722 outStream << std::endl;
723 outStream <<
"Casting has occured where the size of one or both " << std::endl;
724 outStream <<
"of the source and destination types is greater " << std::endl;
725 outStream <<
"than the casting function's maximum supported size " << std::endl;
726 outStream <<
"of " << kMaxByteSize <<
" bytes." << std::endl;
727 outStream << std::endl;
728 outStream <<
"Please remove the use of this casting function," << std::endl;
729 outStream <<
"or submit a bug that includes steps on how to reproduce this assert." << std::endl;
730 outStream << std::endl;
731 outStream <<
"Cast of:" << std::endl;
732 outStream << std::endl;
733 outStream <<
" " <<
typeid(FromType).name() <<
" to " <<
typeid(ToType).name() << std::endl;
734 outStream << std::endl << std::ends;
736 DSTOP((toWhom,
"%s", outStream.str().c_str()));
738 else if ((
typeid(FromType) ==
typeid(ToType)) && (
sizeof(
SintptrT) ==
sizeof(FromType)) && (
sizeof(
Sint32) ==
sizeof(ToType))) {
744 else if (
typeid(FromType) ==
typeid(ToType)) {
745 std::ostringstream outStream;
747 outStream <<
"CASTING BETWEEN IDENTICAL TYPES. PLEASE STOP AND EVALUATE." << std::endl;
748 outStream << std::endl;
749 outStream <<
"Casting has occured where no cast should be required." << std::endl;
750 outStream << std::endl;
751 outStream <<
"Please remove the use of this casting function," << std::endl;
752 outStream <<
"or submit a bug that includes steps on how to reproduce this assert." << std::endl;
753 outStream << std::endl;
754 outStream <<
"Cast of:" << std::endl;
755 outStream << std::endl;
756 outStream <<
" " <<
typeid(FromType).name() <<
" to " <<
typeid(ToType).name() << std::endl;
757 outStream << std::endl << std::ends;
759 DSTOP((toWhom,
"%s", outStream.str().c_str()));
763 else if (!(std::numeric_limits<FromType>::is_signed && !std::numeric_limits<ToType>::is_signed) &&
764 sizeof(ToType) >
sizeof(FromType)) {
765 std::ostringstream outStream;
767 outStream <<
"CASTING RESULTS IN PROMOTION. PLEASE STOP AND EVALUATE." << std::endl;
768 outStream << std::endl;
769 outStream <<
"Casting has occured where no cast should be required." << std::endl;
770 outStream << std::endl;
771 outStream <<
"Please remove the use of this casting function," << std::endl;
772 outStream <<
"or submit a bug that includes steps on how to reproduce this assert." << std::endl;
773 outStream << std::endl;
774 outStream <<
"Cast of:" << std::endl;
775 outStream << std::endl;
776 outStream <<
" " <<
typeid(FromType).name() <<
" to " <<
typeid(ToType).name() << std::endl;
777 outStream << std::endl << std::ends;
779 DSTOP((toWhom,
"%s", outStream.str().c_str()));
784 if (std::numeric_limits<FromType>::is_integer || std::numeric_limits<ToType>::is_integer) {
785 if (fromValue != toValue) {
786 castingResultsInLossOfData =
true;
791 else if (std::numeric_limits<FromType>::is_integer && !std::numeric_limits<ToType>::is_integer && DemotionDoubleTest (toValue, fromValue)) {
792 castingResultsInLossOfData =
true;
794 else if (std::numeric_limits<ToType>::is_integer && !std::numeric_limits<FromType>::is_integer && DemotionDoubleTest (fromValue, toValue)) {
795 castingResultsInLossOfData =
true;
798 if (castingResultsInLossOfData) {
799 std::ostringstream outStream;
801 outStream <<
"CASTING RESULTS IN LOSS OF DATA. PLEASE STOP AND EVALUATE." << std::endl;
802 outStream << std::endl;
803 outStream <<
"Casting has caused a loss of data." << std::endl;
804 outStream << std::endl;
805 outStream <<
"Please evaluate this cast to determine what may be done to fix this error," << std::endl;
806 outStream <<
"or submit a bug that includes steps on how to reproduce this assert." << std::endl;
807 outStream << std::endl;
808 outStream <<
"Cast of:" << std::endl;
809 outStream << std::endl;
810 outStream <<
" " <<
typeid(FromType).name() <<
" to " <<
typeid(ToType).name() << std::endl;
811 outStream << std::endl;
812 outStream <<
"Has changed value from:" << std::endl;
813 outStream << std::endl;
814 outStream <<
" " << fromValue <<
" to " << toValue << std::endl << std::ends;
816 DSTOP((toWhom,
"%s", outStream.str().c_str()));
824template <
typename ToType,
typename FromType>
828ToType CastToPointer(
const TProgrammer& TEST_ONLY(toWhom),
const FromType fromValue){
831 toValue =
reinterpret_cast<ToType
>(
static_cast<uintptr_t
>(fromValue));
835template <
typename ToType,
typename FromType>
839ToType CastFromPointer(
const TProgrammer& TEST_ONLY(toWhom),
const FromType fromValue){
842 toValue =
static_cast<ToType
>(
reinterpret_cast<uintptr_t
>(fromValue));
unsigned char Bool8
Definition GSTypes.h:79
uint16_t Uint16
Definition GSTypes.h:26
intptr_t SintptrT
Definition GSTypes.h:98
uintptr_t UintptrT
Definition GSTypes.h:99
int32_t Sint32
Definition GSTypes.h:36
int16_t Sint16
Definition GSTypes.h:35
uint64_t Uint64
Definition GSTypes.h:28
double Real64
Definition GSTypes.h:66
unsigned char Boolean
Definition GSTypes.h:111
uint32_t Uint32
Definition GSTypes.h:27
#define DSTOP(params)
Definition StdAfx.h:38
Boolean DoublesAreNotNearlyEqual(double n1, double n2)
Definition MCFloat.h:488