Skip to content

Commit

Permalink
Merge remote-tracking branch 'MissLavander/master'
Browse files Browse the repository at this point in the history
Merge pull request bunder#1 from 20kdc/master
ApplyAbbreviations: Work around GCC optimizations being weird.
  • Loading branch information
fhomolka committed Aug 10, 2023
2 parents 251b839 + a5f65a6 commit 37bb903
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions im_mip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,16 @@ rgb_image_c *MIP_LoadImage(const char *filename)
return img;
}


static bool ReplacePrefix(char *name, const char *prefix, char ch)
// Last character of prefix is the character it represents.
static bool ReplacePrefix(char *name, const char *prefix)
{
if (strncmp(name, prefix, strlen(prefix)) != 0)
int prefix_len = strlen(prefix) - 1;
if (strncmp(name, prefix, prefix_len) != 0)
return false;

*name++ = ch;
*name++ = prefix[prefix_len];

int move_chars = strlen(prefix) - 1;
int move_chars = prefix_len - 1;

if (move_chars > 1)
{
Expand All @@ -102,6 +103,14 @@ static bool ReplacePrefix(char *name, const char *prefix, char ch)
return true;
}

static const char * abbr_prefixes[] = {
"star_*",
"plus_+",
"minu_-",
"divd_/",
NULL
};

static void ApplyAbbreviations(char *name, bool *fullbright)
{
// make it lower case
Expand All @@ -112,14 +121,19 @@ static void ApplyAbbreviations(char *name, bool *fullbright)

if (len >= 6)
{
ReplacePrefix(name, "star_", '*')
|| ReplacePrefix(name, "plus_", '+')
|| ReplacePrefix(name, "minu_", '-')
|| ReplacePrefix(name, "divd_", '/');
// This used to be a series of logical ORs.
// The problem is, at least as of GCC 9.3.0, the side-effects are "lost".
// So the second strlen never happened.
for (const char ** prefix = abbr_prefixes; *prefix; prefix++)
{
if (ReplacePrefix(name, *prefix))
{
len = strlen(name);
break;
}
}
}

len = strlen(name);

if (len >= 5 && memcmp(name+len-4, "_fbr", 4) == 0)
{
*fullbright = true;
Expand Down

0 comments on commit 37bb903

Please sign in to comment.