1- /***
1+ /*
22*
3- * Copyright (c) 1996-2002, Valve LLC. All rights reserved.
4- *
5- * This product contains software technology licensed from Id
6- * Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
7- * All Rights Reserved.
3+ * This program is free software; you can redistribute it and/or modify it
4+ * under the terms of the GNU General Public License as published by the
5+ * Free Software Foundation; either version 2 of the License, or (at
6+ * your option) any later version.
87*
9- * Use, distribution, and modification of this source code and/or resulting
10- * object code is restricted to non-commercial enhancements to products from
11- * Valve LLC. All other use, distribution, or modification is prohibited
12- * without written permission from Valve LLC .
8+ * This program is distributed in the hope that it will be useful, but
9+ * WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+ * General Public License for more details .
1312*
14- ****/
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program; if not, write to the Free Software Foundation,
15+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16+ *
17+ * In addition, as a special exception, the author gives permission to
18+ * link the code of this program with the Half-Life Game Engine ("HL
19+ * Engine") and Modified Game Libraries ("MODs") developed by Valve,
20+ * L.L.C ("Valve"). You must obey the GNU General Public License in all
21+ * respects for all of the code used other than the HL Engine and MODs
22+ * from Valve. If you modify this file, you may extend this exception
23+ * to your version of the file, but you are not obligated to do so. If
24+ * you do not wish to do so, delete this exception statement from your
25+ * version.
26+ *
27+ */
1528
29+ #ifndef MATHLIB_H
30+ #define MATHLIB_H
31+ #ifdef _WIN32
1632#pragma once
33+ #endif
1734
18-
19- /* <42b7f> ../common/mathlib.h:3 */
2035typedef float vec_t ;
2136
22- /* <42b91> ../common/mathlib.h:6 */
23- #ifndef DID_VEC3_T_DEFINE
37+ #if !defined DID_VEC3_T_DEFINE && !defined vec3_t
2438#define DID_VEC3_T_DEFINE
2539typedef vec_t vec3_t [3 ];
2640#endif
2741
28- /* <80013> ../common/mathlib.h:8 */
2942typedef vec_t vec4_t [4 ];
43+ typedef int fixed16_t ;
3044
31- /* <42bac> ../common/mathlib.h:18 */
32- typedef int fixed16_t ; /* size: 4 */
33-
34- /* <42bb7> ../common/mathlib.h:60 */
3545typedef union DLONG_u
3646{
3747 int i[2 ];
@@ -41,6 +51,50 @@ typedef union DLONG_u
4151
4252#define M_PI 3.14159265358979323846
4353
54+ #ifdef __cplusplus
55+ #ifdef min
56+ #undef min
57+ #endif
58+
59+ #ifdef max
60+ #undef max
61+ #endif
62+
63+ #ifdef clamp
64+ #undef clamp
65+ #endif
66+
67+ template <typename T>
68+ inline T min (T a, T b) {
69+ return (a < b) ? a : b;
70+ }
71+
72+ template <typename T>
73+ inline T max (T a, T b) {
74+ return (a < b) ? b : a;
75+ }
76+
77+ template <typename T>
78+ inline T clamp (T a, T min, T max) {
79+ return (a > max) ? max : (a < min) ? min : a;
80+ }
81+
82+ template <typename T>
83+ inline T bswap (T s) {
84+ switch (sizeof (T)) {
85+ #ifdef _WIN32
86+ case 2 : {auto res = _byteswap_ushort (*(uint16 *)&s); return *(T *)&res;}
87+ case 4 : {auto res = _byteswap_ulong (*(uint32 *)(&s)); return *(T *)&res;}
88+ case 8 : {auto res = _byteswap_uint64 (*(uint64 *)&s); return *(T *)&res;}
89+ #else
90+ case 2 : {auto res = _bswap16 (*(uint16 *)&s); return *(T *)&res;}
91+ case 4 : {auto res = _bswap (*(uint32 *)&s); return *(T *)&res;}
92+ case 8 : {auto res = _bswap64 (*(uint64 *)&s); return *(T *)&res;}
93+ #endif
94+ default : return s;
95+ }
96+ }
97+ #else // __cplusplus
4498#ifndef max
4599#define max (a,b ) (((a) > (b)) ? (a) : (b))
46100#endif
@@ -50,3 +104,6 @@ typedef union DLONG_u
50104#endif
51105
52106#define clamp (val, min, max ) (((val) > (max)) ? (max) : (((val) < (min)) ? (min) : (val)))
107+ #endif // __cplusplus
108+
109+ #endif // MATHLIB_H
0 commit comments