use C11 _Generic macros for q_min, q_max and CLAMP, if available.

.. if not, then use GCC expression macros for them, if available.
This commit is contained in:
Ozkan Sezer 2022-06-12 14:32:28 +03:00
parent 2012cb5484
commit 02b953e993
20 changed files with 110 additions and 40 deletions

View file

@ -53,6 +53,7 @@ CPUFLAGS=
LDFLAGS =
DFLAGS ?=
CFLAGS ?= -Wall -Wno-trigraphs
CFLAGS += $(call check_gcc,-std=gnu11,)
CFLAGS += $(CPUFLAGS)
ifneq ($(DEBUG),0)
DFLAGS += -DDEBUG
@ -61,6 +62,7 @@ do_strip=
else
DFLAGS += -DNDEBUG
CFLAGS += -O2
# -fno-asynchronous-unwind-tables
CFLAGS += $(call check_gcc,-fweb,)
CFLAGS += $(call check_gcc,-frename-registers,)
cmd_strip=$(STRIP) $(1)

View file

@ -84,11 +84,11 @@ CFLAGS +=-mmacosx-version-min=11.0
LDFLAGS +=-mmacosx-version-min=11.0
USE_RPATH=1
endif
CFLAGS += $(call check_gcc,-std=gnu11,)
CFLAGS += $(CPUFLAGS)
ifeq ($(USE_RPATH),1)
LDFLAGS+=-Wl,-rpath,@executable_path/../Frameworks
endif
ifneq ($(DEBUG),0)
DFLAGS += -DDEBUG
CFLAGS += -g
@ -96,6 +96,7 @@ do_strip=
else
DFLAGS += -DNDEBUG
CFLAGS += -O2
# -fno-asynchronous-unwind-tables
CFLAGS += $(call check_gcc,-fweb,)
CFLAGS += $(call check_gcc,-frename-registers,)
cmd_strip=$(STRIP) $(1)
@ -339,4 +340,3 @@ debug:
clean:
rm -f $(shell find . \( -name '*~' -o -name '#*#' -o -name '*.o' -o -name '*.res' -o -name $(DEFAULT_TARGET) \) -print)

View file

@ -50,8 +50,8 @@ CPUFLAGS=
LDFLAGS = -m32 -mwindows -static-libgcc
DFLAGS ?=
CFLAGS ?= -m32 -Wall -Wno-trigraphs
CFLAGS += $(call check_gcc,-std=gnu11,)
CFLAGS += $(CPUFLAGS)
ifneq ($(DEBUG),0)
DFLAGS += -DDEBUG
CFLAGS += -g
@ -59,6 +59,7 @@ do_strip=
else
DFLAGS += -DNDEBUG
CFLAGS += -O2
# -fno-asynchronous-unwind-tables
CFLAGS += $(call check_gcc,-fweb,)
CFLAGS += $(call check_gcc,-frename-registers,)
cmd_strip=$(STRIP) $(1)
@ -296,4 +297,3 @@ debug:
clean:
rm -f $(shell find . \( -name '*~' -o -name '#*#' -o -name '*.o' -o -name '*.res' -o -name $(DEFAULT_TARGET) \) -print)

View file

@ -48,8 +48,8 @@ CPUFLAGS=
LDFLAGS = -m64 -mwindows -static-libgcc
DFLAGS ?=
CFLAGS ?= -m64 -Wall -Wno-trigraphs
CFLAGS += $(call check_gcc,-std=gnu11,)
CFLAGS += $(CPUFLAGS)
ifneq ($(DEBUG),0)
DFLAGS += -DDEBUG
CFLAGS += -g
@ -57,6 +57,7 @@ do_strip=
else
DFLAGS += -DNDEBUG
CFLAGS += -O2
# -fno-asynchronous-unwind-tables
CFLAGS += $(call check_gcc,-fweb,)
CFLAGS += $(call check_gcc,-frename-registers,)
cmd_strip=$(STRIP) $(1)
@ -289,4 +290,3 @@ debug:
clean:
rm -f $(shell find . \( -name '*~' -o -name '#*#' -o -name '*.o' -o -name '*.res' -o -name $(DEFAULT_TARGET) \) -print)

View file

@ -41,11 +41,76 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#undef min
#undef max
#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \
(defined(__cplusplus) && (__cplusplus >= 201103L))
#define GENERIC_TYPES(x, separator) \
x(int, i) separator \
x(unsigned int, u) separator \
x(long, l) separator \
x(unsigned long, ul) separator \
x(long long, ll) separator \
x(unsigned long long, ull) separator \
x(float, f) separator \
x(double, d)
#define COMMA ,
#define NO_COMMA
#define IMPL_GENERIC_FUNCS(type, suffix) \
static inline type q_min_##suffix (type a, type b) { \
return (a < b) ? a : b; \
} \
static inline type q_max_##suffix (type a, type b) { \
return (a > b) ? a : b; \
} \
static inline type clamp_##suffix (type minval, type val, type maxval) { \
return (val < minval) ? minval : ((val > maxval) ? maxval : val); \
}
GENERIC_TYPES (IMPL_GENERIC_FUNCS, NO_COMMA)
#define SELECT_Q_MIN(type, suffix) type: q_min_##suffix
#define q_min(a, b) _Generic((a) + (b), GENERIC_TYPES (SELECT_Q_MIN, COMMA))(a, b)
#define SELECT_Q_MAX(type, suffix) type: q_max_##suffix
#define q_max(a, b) _Generic((a) + (b), GENERIC_TYPES (SELECT_Q_MAX, COMMA))(a, b)
#define SELECT_CLAMP(type, suffix) type: clamp_##suffix
#define CLAMP(minval, val, maxval) _Generic((minval) + (val) + (maxval), \
GENERIC_TYPES (SELECT_CLAMP, COMMA))(minval, val, maxval)
#elif defined(__GNUC__)
/* min and max macros with type checking -- based on tyrquake. */
#define q_max(a,b) ({ \
const __typeof(a) a_ = (a); \
const __typeof(b) b_ = (b); \
(void)(&a_ == &b_); \
(a_ > b_) ? a_ : b_; \
})
#define q_min(a,b) ({ \
const __typeof(a) a_ = (a); \
const __typeof(b) b_ = (b); \
(void)(&a_ == &b_); \
(a_ < b_) ? a_ : b_; \
})
#define CLAMP(_minval, x, _maxval) ({ \
const __typeof(x) x_ = (x); \
const __typeof(_minval) valmin_ = (_minval);\
const __typeof(_maxval) valmax_ = (_maxval);\
(void)(&x_ == &valmin_); \
(void)(&x_ == &valmax_); \
(x_ < valmin_) ? valmin_ : \
(x_ > valmax_) ? valmax_ : x_; \
})
#else
#define q_min(a, b) (((a) < (b)) ? (a) : (b))
#define q_max(a, b) (((a) > (b)) ? (a) : (b))
#define CLAMP(_minval, x, _maxval) \
((x) < (_minval) ? (_minval) : \
(x) > (_maxval) ? (_maxval) : (x))
#endif
typedef struct sizebuf_s
{

View file

@ -559,13 +559,13 @@ void Draw_ConsoleBackground (void)
pic->width = vid.conwidth;
pic->height = vid.conheight;
alpha = (con_forcedup) ? 1.0 : scr_conalpha.value;
alpha = (con_forcedup) ? 1.0f : scr_conalpha.value;
GL_SetCanvas (CANVAS_CONSOLE); //in case this is called from weird places
if (alpha > 0.0)
if (alpha > 0.0f)
{
if (alpha < 1.0)
if (alpha < 1.0f)
{
glEnable (GL_BLEND);
glColor4f (1,1,1,alpha);
@ -575,7 +575,7 @@ void Draw_ConsoleBackground (void)
Draw_Pic (0, 0, pic);
if (alpha < 1.0)
if (alpha < 1.0f)
{
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnable (GL_ALPHA_TEST);
@ -703,14 +703,14 @@ void GL_SetCanvas (canvastype newcanvas)
glViewport (glx, gly, glwidth, glheight);
break;
case CANVAS_MENU:
s = q_min((float)glwidth / 320.0, (float)glheight / 200.0);
s = CLAMP (1.0, scr_menuscale.value, s);
s = q_min((float)glwidth / 320.0f, (float)glheight / 200.0f);
s = CLAMP (1.0f, scr_menuscale.value, s);
// ericw -- doubled width to 640 to accommodate long keybindings
glOrtho (0, 640, 200, 0, -99999, 99999);
glViewport (glx + (glwidth - 320*s) / 2, gly + (glheight - 200*s) / 2, 640*s, 200*s);
break;
case CANVAS_SBAR:
s = CLAMP (1.0, scr_sbarscale.value, (float)glwidth / 320.0);
s = CLAMP (1.0f, scr_sbarscale.value, (float)glwidth / 320.0f);
if (cl.gametype == GAME_DEATHMATCH)
{
glOrtho (0, glwidth / s, 48, 0, -99999, 99999);
@ -727,7 +727,7 @@ void GL_SetCanvas (canvastype newcanvas)
glViewport (glx, gly+glheight-gl_warpimagesize, gl_warpimagesize, gl_warpimagesize);
break;
case CANVAS_CROSSHAIR: //0,0 is center of viewport
s = CLAMP (1.0, scr_crosshairscale.value, 10.0);
s = CLAMP (1.0f, scr_crosshairscale.value, 10.0f);
glOrtho (scr_vrect.width/-2/s, scr_vrect.width/2/s, scr_vrect.height/2/s, scr_vrect.height/-2/s, -99999, 99999);
glViewport (scr_vrect.x, glheight - scr_vrect.y - scr_vrect.height, scr_vrect.width & ~1, scr_vrect.height & ~1);
break;

View file

@ -508,7 +508,7 @@ void Mod_LoadTextures (lump_t *l)
if (((byte*)(mt+1) + pixels) > (mod_base + l->fileofs + l->filelen))
{
Con_DPrintf("Texture %s extends past end of lump\n", mt->name);
pixels = q_max(0, (mod_base + l->fileofs + l->filelen) - (byte*)(mt+1));
pixels = q_max(0L, (long)((mod_base + l->fileofs + l->filelen) - (byte*)(mt+1)));
}
tx->update_warp = false; //johnfitz

View file

@ -233,7 +233,7 @@ void GLSLGamma_GammaCorrect (void)
// draw the texture back to the framebuffer with a fragment shader
GL_UseProgramFunc (r_gamma_program);
GL_Uniform1fFunc (gammaLoc, vid_gamma.value);
GL_Uniform1fFunc (contrastLoc, q_min(2.0, q_max(1.0, vid_contrast.value)));
GL_Uniform1fFunc (contrastLoc, q_min(2.0f, q_max(1.0f, vid_contrast.value)));
GL_Uniform1iFunc (textureLoc, 0); // use texture unit 0
glDisable (GL_ALPHA_TEST);
@ -662,7 +662,7 @@ R_EmitWirePoint -- johnfitz -- draws a wireframe cross shape for point entities
*/
void R_EmitWirePoint (vec3_t origin)
{
int size=8;
const int size = 8;
glBegin (GL_LINES);
glVertex3f (origin[0]-size, origin[1], origin[2]);

View file

@ -307,7 +307,7 @@ static void SCR_CalcRefdef (void)
//johnfitz -- rewrote this section
size = scr_viewsize.value;
scale = CLAMP (1.0, scr_sbarscale.value, (float)glwidth / 320.0);
scale = CLAMP (1.0f, scr_sbarscale.value, (float)glwidth / 320.0f);
if (size >= 120 || cl.intermission || scr_sbaralpha.value < 1) //johnfitz -- scr_sbaralpha.value
sb_lines = 0;
@ -316,12 +316,12 @@ static void SCR_CalcRefdef (void)
else
sb_lines = 48 * scale;
size = q_min(scr_viewsize.value, 100) / 100;
size = q_min(scr_viewsize.value, 100.f) / 100;
//johnfitz
//johnfitz -- rewrote this section
r_refdef.vrect.width = q_max(glwidth * size, 96); //no smaller than 96, for icons
r_refdef.vrect.height = q_min(glheight * size, glheight - sb_lines); //make room for sbar
r_refdef.vrect.width = q_max(glwidth * size, 96.0f); //no smaller than 96, for icons
r_refdef.vrect.height = q_min((int)(glheight * size), glheight - sb_lines); //make room for sbar
r_refdef.vrect.x = (glwidth - r_refdef.vrect.width)/2;
r_refdef.vrect.y = (glheight - sb_lines - r_refdef.vrect.height)/2;
//johnfitz

View file

@ -800,7 +800,7 @@ void Sky_DrawSkyBox (void)
c = Fog_GetColor();
glEnable (GL_BLEND);
glDisable (GL_TEXTURE_2D);
glColor4f (c[0],c[1],c[2], CLAMP(0.0,skyfog,1.0));
glColor4f (c[0],c[1],c[2], CLAMP(0.0f,skyfog,1.0f));
glBegin (GL_QUADS);
Sky_EmitSkyBoxVertex (skymins[0][i], skymins[1][i], i);
@ -951,7 +951,7 @@ void Sky_DrawFaceQuad (glpoly_t *p)
c = Fog_GetColor();
glEnable (GL_BLEND);
glDisable (GL_TEXTURE_2D);
glColor4f (c[0],c[1],c[2], CLAMP(0.0,skyfog,1.0));
glColor4f (c[0],c[1],c[2], CLAMP(0.0f,skyfog,1.0f));
glBegin (GL_QUADS);
for (i=0, v=p->verts[0] ; i<4 ; i++, v+=VERTEXSIZE)

View file

@ -679,7 +679,8 @@ int TexMgr_SafeTextureSize (int s)
p = TexMgr_Pad(p);
if (p < s) s = p;
}
s = q_min(gl_hardware_maxsize, s);
if (s > gl_hardware_maxsize)
s = gl_hardware_maxsize;
return s;
}

View file

@ -577,7 +577,7 @@ qboolean Host_FilterTime (float time)
realtime += time;
//johnfitz -- max fps cvar
maxfps = CLAMP (10.0, host_maxfps.value, 1000.0);
maxfps = CLAMP (10.f, host_maxfps.value, 1000.f);
if (!cls.timedemo && realtime - oldrealtime < 1.0/maxfps)
return false; // framerate is too high
//johnfitz

View file

@ -1008,7 +1008,9 @@ static void Host_SavegameComment (char *text)
if (p1 != NULL) *p1 = 0;
if (p2 != NULL) *p2 = 0;
memcpy (text, cl.levelname, q_min(strlen(cl.levelname),22)); //johnfitz -- only copy 22 chars.
i = (int) strlen(cl.levelname);
if (i > 22) i = 22;
memcpy (text, cl.levelname, (size_t)i);
sprintf (kills,"kills:%3i/%3i", cl.stats[STAT_MONSTERS], cl.stats[STAT_TOTALMONSTERS]);
memcpy (text+22, kills, strlen(kills));
// convert space to _ to make stdio happy

View file

@ -80,8 +80,8 @@ char *PL_GetClipboardData (void)
* such as an ip address, etc: do chop the size
* here, otherwise we may experience Z_Malloc()
* failures and all other not-oh-so-fun stuff. */
size = q_min(MAX_CLIPBOARDTXT, size);
data = (char *) Z_Malloc(size);
size = q_min((size_t)(MAX_CLIPBOARDTXT), size);
data = (char *) Z_Malloc((int)size);
q_strlcpy (data, cliptext, size);
}
#endif

View file

@ -56,8 +56,8 @@ char *PL_GetClipboardData (void)
NSString* clipboardString = [pasteboard stringForType: NSPasteboardTypeString];
if (clipboardString != NULL && [clipboardString length] > 0) {
size_t sz = [clipboardString length] + 1;
sz = q_min(MAX_CLIPBOARDTXT, sz);
data = (char *) Z_Malloc(sz);
sz = q_min((size_t)(MAX_CLIPBOARDTXT), sz);
data = (char *) Z_Malloc((int)sz);
#if (MAC_OS_X_VERSION_MIN_REQUIRED < 1040) /* for ppc builds targeting 10.3 and older */
q_strlcpy (data, [clipboardString cString], sz);
#else

View file

@ -95,8 +95,8 @@ char *PL_GetClipboardData (void)
* such as an ip address, etc: do chop the size
* here, otherwise we may experience Z_Malloc()
* failures and all other not-oh-so-fun stuff. */
size = q_min(MAX_CLIPBOARDTXT, size);
data = (char *) Z_Malloc(size);
size = q_min((size_t)(MAX_CLIPBOARDTXT), size);
data = (char *) Z_Malloc((int)size);
q_strlcpy (data, cliptext, size);
GlobalUnlock (hClipboardData);
}

View file

@ -924,7 +924,7 @@ const char *ED_ParseEdict (const char *data, edict_t *ent)
//johnfitz -- hack to support .alpha even when progs.dat doesn't know about it
if (!strcmp(keyname, "alpha"))
ent->alpha = ENTALPHA_ENCODE(atof(com_token));
ent->alpha = ENTALPHA_ENCODE(Q_atof(com_token));
//johnfitz
key = ED_FindField (keyname);

View file

@ -131,7 +131,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define ENTALPHA_DEFAULT 0 //entity's alpha is "default" (i.e. water obeys r_wateralpha) -- must be zero so zeroed out memory works
#define ENTALPHA_ZERO 1 //entity is invisible (lowest possible alpha)
#define ENTALPHA_ONE 255 //entity is fully opaque (highest possible alpha)
#define ENTALPHA_ENCODE(a) (((a)==0)?ENTALPHA_DEFAULT:Q_rint(CLAMP(1,(a)*254.0f+1,255))) //server convert to byte to send to client
#define ENTALPHA_ENCODE(a) (((a)==0)?ENTALPHA_DEFAULT:Q_rint(CLAMP(1.0f,(a)*254.0f+1,255.0f))) //server convert to byte to send to client
#define ENTALPHA_DECODE(a) (((a)==ENTALPHA_DEFAULT)?1.0f:((float)(a)-1)/(254)) //client convert to float for rendering
#define ENTALPHA_TOSAVE(a) (((a)==ENTALPHA_DEFAULT)?0.0f:(((a)==ENTALPHA_ZERO)?-1.0f:((float)(a)-1)/(254))) //server convert to float for savegame
//johnfitz

View file

@ -459,9 +459,9 @@ void R_SetupAliasFrame (aliashdr_t *paliashdr, int frame, lerpdata_t *lerpdata)
if (r_lerpmodels.value && !(e->model->flags & MOD_NOLERP && r_lerpmodels.value != 2))
{
if (e->lerpflags & LERP_FINISH && numposes == 1)
lerpdata->blend = CLAMP (0, (cl.time - e->lerpstart) / (e->lerpfinish - e->lerpstart), 1);
lerpdata->blend = CLAMP (0.0f, (float)(cl.time - e->lerpstart) / (e->lerpfinish - e->lerpstart), 1.0f);
else
lerpdata->blend = CLAMP (0, (cl.time - e->lerpstart) / e->lerptime, 1);
lerpdata->blend = CLAMP (0.0f, (float)(cl.time - e->lerpstart) / e->lerptime, 1.0f);
lerpdata->pose1 = e->previouspose;
lerpdata->pose2 = e->currentpose;
}
@ -507,9 +507,9 @@ void R_SetupEntityTransform (entity_t *e, lerpdata_t *lerpdata)
if (r_lerpmove.value && e != &cl.viewent && e->lerpflags & LERP_MOVESTEP)
{
if (e->lerpflags & LERP_FINISH)
blend = CLAMP (0, (cl.time - e->movelerpstart) / (e->lerpfinish - e->movelerpstart), 1);
blend = CLAMP (0.0f, (float)(cl.time - e->movelerpstart) / (e->lerpfinish - e->movelerpstart), 1.0f);
else
blend = CLAMP (0, (cl.time - e->movelerpstart) / 0.1, 1);
blend = CLAMP (0.0f, (float)(cl.time - e->movelerpstart) / 0.1f, 1.0f);
//translation
VectorSubtract (e->currentorigin, e->previousorigin, d);

View file

@ -322,7 +322,7 @@ void Sbar_DrawScrollString (int x, int y, int width, const char *str)
float scale;
int len, ofs, left;
scale = CLAMP (1.0, scr_sbarscale.value, (float)glwidth / 320.0);
scale = CLAMP (1.0f, scr_sbarscale.value, (float)glwidth / 320.0f);
left = x * scale;
if (cl.gametype != GAME_DEATHMATCH)
left += (((float)glwidth - 320.0 * scale) / 2);
@ -1191,7 +1191,7 @@ void Sbar_MiniDeathmatchOverlay (void)
float scale; //johnfitz
scoreboard_t *s;
scale = CLAMP (1.0, scr_sbarscale.value, (float)glwidth / 320.0); //johnfitz
scale = CLAMP (1.0f, scr_sbarscale.value, (float)glwidth / 320.0f); //johnfitz
//MAX_SCOREBOARDNAME = 32, so total width for this overlay plus sbar is 632, but we can cut off some i guess
if (glwidth/scale < 512 || scr_viewsize.value >= 120) //johnfitz -- test should consider scr_sbarscale