Skip to content
This repository was archived by the owner on Dec 11, 2025. It is now read-only.

Commit c44d91c

Browse files
author
Alexey Panteleev
committed
Merged pull request "Q2PRO sync: Clamping & Conversions": #504
2 parents d7541ad + 05404c8 commit c44d91c

58 files changed

Lines changed: 316 additions & 303 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

inc/common/msg.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ static inline int MSG_PackSolid16(const vec3_t mins, const vec3_t maxs)
189189
int zd = -mins[2] / 8;
190190
int zu = (maxs[2] + 32) / 8;
191191

192-
clamp(x, 1, 31);
193-
clamp(zd, 1, 31);
194-
clamp(zu, 1, 63);
192+
x = Q_clip(x, 1, 31);
193+
zd = Q_clip(zd, 1, 31);
194+
zu = Q_clip(zu, 1, 63);
195195

196196
return (zu << 10) | (zd << 5) | x;
197197
}
@@ -202,9 +202,9 @@ static inline uint32_t MSG_PackSolid32_Ver1(const vec3_t mins, const vec3_t maxs
202202
int zd = -mins[2];
203203
int zu = maxs[2] + 32768;
204204

205-
clamp(x, 1, 255);
206-
clamp(zd, 0, 255);
207-
clamp(zu, 0, 65535);
205+
x = Q_clip(x, 1, 255);
206+
zd = Q_clip_uint8(zd);
207+
zu = Q_clip_uint16(zu);
208208

209209
return ((uint32_t)zu << 16) | (zd << 8) | x;
210210
}
@@ -216,10 +216,10 @@ static inline uint32_t MSG_PackSolid32_Ver2(const vec3_t mins, const vec3_t maxs
216216
int zd = -mins[2];
217217
int zu = maxs[2] + 32;
218218

219-
clamp(x, 1, 255);
220-
clamp(y, 1, 255);
221-
clamp(zd, 0, 255);
222-
clamp(zu, 0, 255);
219+
x = Q_clip(x, 1, 255);
220+
y = Q_clip(y, 1, 255);
221+
zd = Q_clip_uint8(zd);
222+
zu = Q_clip_uint8(zu);
223223

224224
return MakeLittleLong(x, y, zd, zu);
225225
}

inc/shared/shared.h

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,65 @@ void Q_srand(uint32_t seed);
354354
uint32_t Q_rand(void);
355355
uint32_t Q_rand_uniform(uint32_t n);
356356

357-
#define clamp(a,b,c) ((a)<(b)?(a)=(b):(a)>(c)?(a)=(c):(a))
358-
#define cclamp(a,b,c) ((b)>(c)?clamp(a,c,b):clamp(a,b,c))
359-
360357
static inline int Q_clip(int a, int b, int c)
361358
{
362-
return clamp(a, b, c);
359+
if (a < b)
360+
return b;
361+
if (a > c)
362+
return c;
363+
return a;
364+
}
365+
366+
static inline float Q_clipf(float a, float b, float c)
367+
{
368+
#if defined(__GNUC__) && defined(__SSE__)
369+
__asm__("maxss %1, %0 \n\t"
370+
"minss %2, %0 \n\t"
371+
: "+&x"(a) : "xm"(b), "xm"(c));
372+
return a;
373+
#else
374+
if (a < b)
375+
return b;
376+
if (a > c)
377+
return c;
378+
return a;
379+
#endif
380+
}
381+
382+
static inline float Q_circ_clipf(float a, float b, float c)
383+
{
384+
return b > c ? Q_clipf(a, c, b) : Q_clipf(a, b, c);
385+
}
386+
387+
static inline int8_t Q_clip_int8(int a)
388+
{
389+
return ((a + 0x80U) & ~0xFF) ? (a >> 31) ^ 0x7F : a;
390+
}
391+
392+
static inline int16_t Q_clip_int16(int a)
393+
{
394+
return ((a + 0x8000U) & ~0xFFFF) ? (a >> 31) ^ 0x7FFF : a;
395+
}
396+
397+
static inline int32_t Q_clip_int32(int64_t a)
398+
{
399+
return ((a + 0x80000000ULL) & ~0xFFFFFFFFULL) ? (a >> 63) ^ 0x7FFFFFFF : a;
400+
}
401+
402+
#ifdef _LP64
403+
#define Q_clipl_int32(a) Q_clip_int32(a)
404+
#else
405+
#define Q_clipl_int32(a) (a)
406+
#endif
407+
408+
static inline uint8_t Q_clip_uint8(int a)
409+
{
410+
return (a & ~0xFF) ? ~a >> 31 : a;
411+
}
412+
413+
static inline uint16_t Q_clip_uint16(int a)
414+
{
415+
return (a & ~0xFFFF) ? ~a >> 31 : a;
363416
}
364417

365418
#ifndef max
@@ -484,6 +537,14 @@ char *Q_strchrnul(const char *s, int c);
484537
void *Q_memccpy(void *dst, const void *src, int c, size_t size);
485538
size_t Q_strnlen(const char *s, size_t maxlen);
486539

540+
#ifdef _WIN32
541+
#define Q_atoi(s) atoi(s)
542+
#else
543+
int Q_atoi(const char *s);
544+
#endif
545+
546+
#define Q_atof(s) strtof(s, NULL)
547+
487548
char *COM_SkipPath(const char *pathname);
488549
size_t COM_StripExtension(char *out, const char *in, size_t size);
489550
void COM_FilePath(const char *in, char *out, size_t size);

src/client/ascii.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -111,39 +111,39 @@ static void TH_DrawLayoutString(char *dst, const char *s)
111111
if (token[0] == 'x') {
112112
if (token[1] == 'l') {
113113
token = COM_Parse(&s);
114-
x = atoi(token) / 8;
114+
x = Q_atoi(token) / 8;
115115
continue;
116116
}
117117

118118
if (token[1] == 'r') {
119119
token = COM_Parse(&s);
120-
x = TH_WIDTH + atoi(token) / 8;
120+
x = TH_WIDTH + Q_atoi(token) / 8;
121121
continue;
122122
}
123123

124124
if (token[1] == 'v') {
125125
token = COM_Parse(&s);
126-
x = TH_WIDTH / 2 - 20 + atoi(token) / 8;
126+
x = TH_WIDTH / 2 - 20 + Q_atoi(token) / 8;
127127
continue;
128128
}
129129
}
130130

131131
if (token[0] == 'y') {
132132
if (token[1] == 't') {
133133
token = COM_Parse(&s);
134-
y = atoi(token) / 8;
134+
y = Q_atoi(token) / 8;
135135
continue;
136136
}
137137

138138
if (token[1] == 'b') {
139139
token = COM_Parse(&s);
140-
y = TH_HEIGHT + atoi(token) / 8;
140+
y = TH_HEIGHT + Q_atoi(token) / 8;
141141
continue;
142142
}
143143

144144
if (token[1] == 'v') {
145145
token = COM_Parse(&s);
146-
y = TH_HEIGHT / 2 - 15 + atoi(token) / 8;
146+
y = TH_HEIGHT / 2 - 15 + Q_atoi(token) / 8;
147147
continue;
148148
}
149149
}
@@ -160,25 +160,25 @@ static void TH_DrawLayoutString(char *dst, const char *s)
160160
int score, ping, time;
161161

162162
token = COM_Parse(&s);
163-
x = TH_WIDTH / 2 - 20 + atoi(token) / 8;
163+
x = TH_WIDTH / 2 - 20 + Q_atoi(token) / 8;
164164
token = COM_Parse(&s);
165-
y = TH_HEIGHT / 2 - 15 + atoi(token) / 8;
165+
y = TH_HEIGHT / 2 - 15 + Q_atoi(token) / 8;
166166

167167
token = COM_Parse(&s);
168-
value = atoi(token);
168+
value = Q_atoi(token);
169169
if (value < 0 || value >= MAX_CLIENTS) {
170170
Com_Error(ERR_DROP, "%s: invalid client index", __func__);
171171
}
172172
ci = &cl.clientinfo[value];
173173

174174
token = COM_Parse(&s);
175-
score = atoi(token);
175+
score = Q_atoi(token);
176176

177177
token = COM_Parse(&s);
178-
ping = atoi(token);
178+
ping = Q_atoi(token);
179179

180180
token = COM_Parse(&s);
181-
time = atoi(token);
181+
time = Q_atoi(token);
182182

183183
len = strlen(ci->name);
184184
TH_DrawString(dst, x + 4, y, ci->name, len);
@@ -196,22 +196,22 @@ static void TH_DrawLayoutString(char *dst, const char *s)
196196
int score, ping;
197197

198198
token = COM_Parse(&s);
199-
x = TH_WIDTH / 2 - 20 + atoi(token) / 8;
199+
x = TH_WIDTH / 2 - 20 + Q_atoi(token) / 8;
200200
token = COM_Parse(&s);
201-
y = TH_HEIGHT / 2 - 15 + atoi(token) / 8;
201+
y = TH_HEIGHT / 2 - 15 + Q_atoi(token) / 8;
202202

203203
token = COM_Parse(&s);
204-
value = atoi(token);
204+
value = Q_atoi(token);
205205
if (value < 0 || value >= MAX_CLIENTS) {
206206
Com_Error(ERR_DROP, "%s: invalid client index", __func__);
207207
}
208208
ci = &cl.clientinfo[value];
209209

210210
token = COM_Parse(&s);
211-
score = atoi(token);
211+
score = Q_atoi(token);
212212

213213
token = COM_Parse(&s);
214-
ping = atoi(token);
214+
ping = Q_atoi(token);
215215
if (ping > 999)
216216
ping = 999;
217217

@@ -230,9 +230,9 @@ static void TH_DrawLayoutString(char *dst, const char *s)
230230
if (!strcmp(token, "num")) {
231231
// draw a number
232232
token = COM_Parse(&s);
233-
width = atoi(token);
233+
width = Q_atoi(token);
234234
token = COM_Parse(&s);
235-
value = atoi(token);
235+
value = Q_atoi(token);
236236
if (value < 0 || value >= MAX_STATS) {
237237
Com_Error(ERR_DROP, "%s: invalid stat index", __func__);
238238
}
@@ -243,7 +243,7 @@ static void TH_DrawLayoutString(char *dst, const char *s)
243243

244244
if (!strcmp(token, "stat_string")) {
245245
token = COM_Parse(&s);
246-
index = atoi(token);
246+
index = Q_atoi(token);
247247
if (index < 0 || index >= MAX_STATS) {
248248
Com_Error(ERR_DROP, "%s: invalid string index", __func__);
249249
}
@@ -272,7 +272,7 @@ static void TH_DrawLayoutString(char *dst, const char *s)
272272

273273
if (!strcmp(token, "if")) {
274274
token = COM_Parse(&s);
275-
value = atoi(token);
275+
value = Q_atoi(token);
276276
if (value < 0 || value >= MAX_STATS) {
277277
Com_Error(ERR_DROP, "%s: invalid stat index", __func__);
278278
}

src/client/console.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,16 +366,12 @@ If the line width has changed, reformat the buffer.
366366
*/
367367
void Con_CheckResize(void)
368368
{
369-
int width;
370-
371369
con.scale = R_ClampScale(con_scale);
372370

373371
con.vidWidth = Q_rint(r_config.width * con.scale);
374372
con.vidHeight = Q_rint(r_config.height * con.scale);
375373

376-
width = con.vidWidth / CHAR_WIDTH - 2;
377-
378-
con.linewidth = clamp(width, 0, CON_LINEWIDTH);
374+
con.linewidth = Q_clip(con.vidWidth / CHAR_WIDTH - 2, 0, CON_LINEWIDTH);
379375
con.prompt.inputLine.visibleChars = con.linewidth;
380376
con.prompt.widthInChars = con.linewidth;
381377
con.chatPrompt.inputLine.visibleChars = con.linewidth;

src/client/demo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ static void CL_Seek_f(void)
981981
return;
982982
}
983983

984-
clamp(percent, 0, 100);
984+
percent = Q_clipf(percent, 0, 100);
985985
dest = cls.demo.file_offset + cls.demo.file_size * percent / 100;
986986

987987
byte_seek = true;

src/client/entities.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,8 +1379,7 @@ void CL_AddTestModel(void)
13791379
VectorCopy(cl_testmodel_position, entity.origin);
13801380
VectorCopy(cl_testmodel_position, entity.oldorigin);
13811381

1382-
entity.alpha = cl_testalpha->value;
1383-
clamp(entity.alpha, 0.f, 1.f);
1382+
entity.alpha = Q_clipf(cl_testalpha->value, 0.f, 1.f);
13841383
if (entity.alpha < 1.f)
13851384
entity.flags |= RF_TRANSLUCENT;
13861385

src/client/input.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ static void KeyDown(kbutton_t *b)
249249

250250
c = Cmd_Argv(1);
251251
if (c[0])
252-
k = atoi(c);
252+
k = Q_atoi(c);
253253
else
254254
k = -1; // typed manually at the console for continuous down
255255

@@ -270,7 +270,7 @@ static void KeyDown(kbutton_t *b)
270270

271271
// save timestamp
272272
c = Cmd_Argv(2);
273-
b->downtime = atoi(c);
273+
b->downtime = Q_atoi(c);
274274
if (!b->downtime) {
275275
b->downtime = com_eventTime - 100;
276276
}
@@ -286,7 +286,7 @@ static void KeyUp(kbutton_t *b)
286286

287287
c = Cmd_Argv(1);
288288
if (c[0])
289-
k = atoi(c);
289+
k = Q_atoi(c);
290290
else {
291291
// typed manually at the console, assume for unsticking, so clear all
292292
b->down[0] = b->down[1] = 0;
@@ -308,7 +308,7 @@ static void KeyUp(kbutton_t *b)
308308

309309
// save timestamp
310310
c = Cmd_Argv(2);
311-
uptime = atoi(c);
311+
uptime = Q_atoi(c);
312312
if (!uptime) {
313313
b->msec += 10;
314314
} else if (uptime > b->downtime) {
@@ -384,7 +384,7 @@ static void IN_UseUp(void)
384384

385385
static void IN_Impulse(void)
386386
{
387-
in_impulse = atoi(Cmd_Argv(1));
387+
in_impulse = Q_atoi(Cmd_Argv(1));
388388
}
389389

390390
static void IN_CenterView(void)
@@ -415,7 +415,6 @@ Returns the fraction of the frame that the key was down
415415
static float CL_KeyState(kbutton_t *key)
416416
{
417417
unsigned msec = key->msec;
418-
float val;
419418

420419
if (key->state & 1) {
421420
// still down
@@ -429,9 +428,7 @@ static float CL_KeyState(kbutton_t *key)
429428
return (float)(key->state & 1);
430429
}
431430

432-
val = (float)msec / cl.cmd.msec;
433-
434-
return clamp(val, 0, 1);
431+
return Q_clipf((float)msec / cl.cmd.msec, 0, 1);
435432
}
436433

437434
//==========================================================================
@@ -565,11 +562,11 @@ static void CL_BaseMove(vec3_t move)
565562

566563
static void CL_ClampSpeed(vec3_t move)
567564
{
568-
float speed = 400; // default (maximum) running speed
565+
const float speed = 400; // default (maximum) running speed
569566

570-
clamp(move[0], -speed, speed);
571-
clamp(move[1], -speed, speed);
572-
clamp(move[2], -speed, speed);
567+
move[0] = Q_clipf(move[0], -speed, speed);
568+
move[1] = Q_clipf(move[1], -speed, speed);
569+
move[2] = Q_clipf(move[2], -speed, speed);
573570
}
574571

575572
static void CL_ClampPitch(void)
@@ -584,7 +581,7 @@ static void CL_ClampPitch(void)
584581
if (angle > 180)
585582
angle -= 360; // wrapped
586583

587-
clamp(angle, -89, 89);
584+
angle = Q_clipf(angle, -89, 89);
588585
cl.viewangles[PITCH] = angle - pitch;
589586
}
590587

0 commit comments

Comments
 (0)