Prefer pre-inc/dec unless post-inc/dec are necessary

This commit is contained in:
Rangi42
2025-07-19 15:04:08 -04:00
parent bf69043a1d
commit 14f5e16ae8
19 changed files with 111 additions and 118 deletions

View File

@@ -71,7 +71,7 @@ static inline int ctz(unsigned int x) {
while (!(x & 1)) {
x >>= 1;
cnt++;
++cnt;
}
return cnt;
}
@@ -81,7 +81,7 @@ static inline int clz(unsigned int x) {
while (x <= UINT_MAX / 2) {
x <<= 1;
cnt++;
++cnt;
}
return cnt;
}

View File

@@ -238,13 +238,13 @@ size_t charmap_ConvertNext(std::string_view &input, std::vector<int32_t> *output
break;
}
inputIdx++; // Consume that char
++inputIdx; // Consume that char
if (charmap.nodes[nodeIdx].isTerminal()) {
matchIdx = nodeIdx; // This node matches, register it
rewindDistance = 0; // If no longer match is found, rewind here
} else {
rewindDistance++;
++rewindDistance;
}
}
@@ -271,7 +271,7 @@ size_t charmap_ConvertNext(std::string_view &input, std::vector<int32_t> *output
codepointLen = 1;
break;
}
codepointLen++;
++codepointLen;
if (state == UTF8_ACCEPT) {
break;
}

View File

@@ -189,7 +189,7 @@ bool yywrap() {
}
}
// Advance to the next iteration
fileInfoIters.front()++;
++fileInfoIters.front();
// If this wasn't the last iteration, wrap instead of popping
if (fileInfoIters.front() <= context.nbReptIters) {
lexer_RestartRept(context.fileInfo->lineNo);

View File

@@ -373,7 +373,7 @@ void LexerState::clear(uint32_t lineNo_) {
}
static void nextLine() {
lexerState->lineNo++;
++lexerState->lineNo;
}
uint32_t lexer_GetIFDepth() {
@@ -501,7 +501,7 @@ LexerState::~LexerState() {
bool Expansion::advance() {
assume(offset <= size());
offset++;
++offset;
return offset > size();
}
@@ -511,12 +511,11 @@ BufferedContent::~BufferedContent() {
void BufferedContent::advance() {
assume(offset < std::size(buf));
offset++;
if (offset == std::size(buf)) {
if (++offset == std::size(buf)) {
offset = 0; // Wrap around if necessary
}
if (size > 0) {
size--;
--size;
}
}
@@ -810,11 +809,11 @@ static int peek() {
return c;
}
lexerState->macroArgScanDistance++; // Do not consider again
++lexerState->macroArgScanDistance; // Do not consider again
if (c == '\\' && !lexerState->disableMacroArgs) {
// If character is a backslash, check for a macro arg
lexerState->macroArgScanDistance++;
++lexerState->macroArgScanDistance;
c = lexerState->peekCharAhead();
if (isMacroChar(c)) {
shiftChar();
@@ -854,10 +853,10 @@ static void shiftChar() {
if (lexerState->captureBuf) {
lexerState->captureBuf->push_back(peek());
}
lexerState->captureSize++;
++lexerState->captureSize;
}
lexerState->macroArgScanDistance--;
--lexerState->macroArgScanDistance;
for (;;) {
if (!lexerState->expansions.empty()) {
@@ -871,7 +870,7 @@ static void shiftChar() {
} else {
// Advance within the file contents
if (std::holds_alternative<ViewedContent>(lexerState->content)) {
std::get<ViewedContent>(lexerState->content).offset++;
++std::get<ViewedContent>(lexerState->content).offset;
} else {
std::get<BufferedContent>(lexerState->content).advance();
}
@@ -1008,7 +1007,7 @@ static std::string readAnonLabelRef(char c) {
// We come here having already peeked at one char, so no need to do it again
do {
shiftChar();
n++;
++n;
} while (peek() == c);
return sym_MakeAnonLabelName(n, c == '-');
@@ -1273,7 +1272,7 @@ static uint32_t readGfxConstant() {
bitPlaneUpper = bitPlaneUpper << 1 | (pixel >> 1);
}
if (width < 9) {
width++;
++width;
}
}
@@ -2124,13 +2123,13 @@ static Token yylex_RAW() {
case '(': // Open parentheses inside macro args
if (parenDepth < UINT_MAX) {
parenDepth++;
++parenDepth;
}
goto append;
case ')': // Close parentheses inside macro args
if (parenDepth > 0) {
parenDepth--;
--parenDepth;
}
goto append;
@@ -2358,11 +2357,11 @@ static Token yylex_SKIP_TO_ENDR() {
switch (readIdentifier(c, false).type) {
case T_(POP_FOR):
case T_(POP_REPT):
depth++;
++depth;
break;
case T_(POP_ENDR):
depth--;
--depth;
// `lexer_CaptureRept` has already guaranteed that the `ENDR`s are balanced
assume(depth > 0);
break;
@@ -2483,12 +2482,12 @@ Capture lexer_CaptureRept() {
switch (readIdentifier(c, false).type) {
case T_(POP_REPT):
case T_(POP_FOR):
depth++;
++depth;
break; // Ignore the rest of that line
case T_(POP_ENDR):
if (depth) {
depth--;
--depth;
break; // Ignore the rest of that line
}
endCapture(capture);

View File

@@ -293,7 +293,7 @@ int main(int argc, char *argv[]) {
case 'Q':
precisionArg = musl_optarg;
if (precisionArg[0] == '.') {
precisionArg++;
++precisionArg;
}
precision = strtoul(precisionArg, &endptr, 0);

View File

@@ -91,7 +91,7 @@ void opt_Parse(char const *s) {
case 'Q':
precisionArg = &s[1];
if (precisionArg[0] == '.') {
precisionArg++;
++precisionArg;
}
if (strlen(precisionArg) <= 2) {
int result;

View File

@@ -2830,7 +2830,7 @@ static size_t strlenUTF8(std::string const &str, bool printErrors) {
state = UTF8_ACCEPT;
// fallthrough
case UTF8_ACCEPT:
len++;
++len;
break;
}
}
@@ -2840,7 +2840,7 @@ static size_t strlenUTF8(std::string const &str, bool printErrors) {
if (printErrors) {
error("STRLEN: Incomplete UTF-8 character");
}
len++;
++len;
}
return len;
@@ -2861,10 +2861,10 @@ static std::string strsliceUTF8(std::string const &str, uint32_t start, uint32_t
state = UTF8_ACCEPT;
// fallthrough
case UTF8_ACCEPT:
curIdx++;
++curIdx;
break;
}
index++;
++index;
}
// An index 1 past the end of the string is allowed, but will trigger the
@@ -2887,16 +2887,16 @@ static std::string strsliceUTF8(std::string const &str, uint32_t start, uint32_t
state = UTF8_ACCEPT;
// fallthrough
case UTF8_ACCEPT:
curIdx++;
++curIdx;
break;
}
index++;
++index;
}
// Check for partial code point.
if (state != UTF8_ACCEPT) {
error("STRSLICE: Incomplete UTF-8 character");
curIdx++;
++curIdx;
}
if (curIdx < stop) {
@@ -2925,10 +2925,10 @@ static std::string strsubUTF8(std::string const &str, uint32_t pos, uint32_t len
state = UTF8_ACCEPT;
// fallthrough
case UTF8_ACCEPT:
curPos++;
++curPos;
break;
}
index++;
++index;
}
// A position 1 past the end of the string is allowed, but will trigger the
@@ -2950,16 +2950,16 @@ static std::string strsubUTF8(std::string const &str, uint32_t pos, uint32_t len
state = UTF8_ACCEPT;
// fallthrough
case UTF8_ACCEPT:
curLen++;
++curLen;
break;
}
index++;
++index;
}
// Check for partial code point.
if (state != UTF8_ACCEPT) {
error("STRSUB: Incomplete UTF-8 character");
curLen++;
++curLen;
}
if (curLen < len) {
@@ -3147,7 +3147,7 @@ static std::string
fmt.appendString(str, std::get<std::string>(args[argIndex]));
}
argIndex++;
++argIndex;
}
if (argIndex < args.size()) {

View File

@@ -98,7 +98,7 @@ Section *sect_FindSectionByName(std::string const &name) {
#define sectError(...) \
do { \
error(__VA_ARGS__); \
nbSectErrors++; \
++nbSectErrors; \
} while (0)
static unsigned int mergeSectUnion(

View File

@@ -545,7 +545,7 @@ Symbol *sym_AddAnonLabel() {
}
std::string anon = sym_MakeAnonLabelName(0, true); // The direction is important!
anonLabelID++;
++anonLabelID;
return addLabel(anon);
}
@@ -565,8 +565,8 @@ std::string sym_MakeAnonLabelName(uint32_t ofs, bool neg) {
id = anonLabelID - ofs;
}
} else {
ofs--; // We're referencing symbols that haven't been created yet...
if (ofs > UINT32_MAX - anonLabelID) {
// We're referencing symbols that haven't been created yet...
if (--ofs > UINT32_MAX - anonLabelID) {
// LCOV_EXCL_START
error(
"Reference to anonymous label %" PRIu32 " after, when only %" PRIu32

View File

@@ -81,8 +81,7 @@ static void printDiag(
static void incrementErrors() {
// This intentionally makes 0 act as "unlimited" (or at least "limited to sizeof(unsigned)")
nbErrors++;
if (nbErrors == maxErrors) {
if (++nbErrors == maxErrors) {
fprintf(
stderr,
"Assembly aborted after the maximum of %u error%s! (configure with "

View File

@@ -71,7 +71,7 @@ std::pair<WarningState, std::optional<uint8_t>> getInitialWarningState(std::stri
}
param = param * 10 + (*ptr - '0');
ptr++;
++ptr;
} while (*ptr);
// If we reached the end of the string, truncate it at the '='

26
src/extern/getopt.cpp vendored
View File

@@ -57,7 +57,7 @@ static int getopt(int argc, char *argv[], char const *optstring) {
}
if (!musl_optpos) {
musl_optpos++;
++musl_optpos;
}
k = mbtowc(&c, argv[musl_optind] + musl_optpos, MB_LEN_MAX);
if (k < 0) {
@@ -68,12 +68,12 @@ static int getopt(int argc, char *argv[], char const *optstring) {
musl_optpos += k;
if (!argv[musl_optind][musl_optpos]) {
musl_optind++;
++musl_optind;
musl_optpos = 0;
}
if (optstring[0] == '-' || optstring[0] == '+') {
optstring++;
++optstring;
}
i = 0;
@@ -83,7 +83,7 @@ static int getopt(int argc, char *argv[], char const *optstring) {
if (l > 0) {
i += l;
} else {
i++;
++i;
}
} while (l && d != c);
@@ -185,11 +185,11 @@ static int musl_getopt_long_core(
opt = start;
if (*opt == '-') {
opt++;
++opt;
}
while (*opt && *opt != '=' && *opt == *name) {
name++;
opt++;
++name;
++opt;
}
if (*opt && *opt != '=') {
continue;
@@ -200,7 +200,7 @@ static int musl_getopt_long_core(
cnt = 1;
break;
}
cnt++;
++cnt;
}
if (cnt == 1 && longonly && arg - start == mblen(start, MB_LEN_MAX)) {
int l = arg - start;
@@ -209,10 +209,10 @@ static int musl_getopt_long_core(
int j = 0;
while (j < l && start[j] == optstring[i + j]) {
j++;
++j;
}
if (j == l) {
cnt++;
++cnt;
break;
}
}
@@ -220,7 +220,7 @@ static int musl_getopt_long_core(
if (cnt == 1) {
i = match;
opt = arg;
musl_optind++;
++musl_optind;
if (*opt == '=') {
if (!longopts[i].has_arg) {
musl_optopt = longopts[i].val;
@@ -254,7 +254,7 @@ static int musl_getopt_long_core(
);
return '?';
}
musl_optind++;
++musl_optind;
}
if (idx) {
*idx = i;
@@ -275,7 +275,7 @@ static int musl_getopt_long_core(
strlen(argv[musl_optind] + 2)
);
}
musl_optind++;
++musl_optind;
return '?';
}
}

View File

@@ -90,7 +90,7 @@ static void error(char const *fmt, ...) {
putc('\n', stderr);
if (nbErrors != UINT32_MAX) {
nbErrors++;
++nbErrors;
}
}
@@ -104,7 +104,7 @@ static void fatal(char const *fmt, ...) {
putc('\n', stderr);
if (nbErrors != UINT32_MAX) {
nbErrors++;
++nbErrors;
}
}
@@ -206,6 +206,18 @@ static void printAcceptedMBCNames() {
static uint8_t tpp1Rev[2];
static void skipWhitespace(char const *&ptr) {
while (*ptr == ' ' || *ptr == '\t') {
++ptr;
}
}
static void skipMBCSpace(char const *&ptr) {
while (*ptr == ' ' || *ptr == '\t' || *ptr == '_') {
++ptr;
}
}
static bool readMBCSlice(char const *&name, char const *expected) {
while (*expected) {
char c = *name++;
@@ -237,7 +249,7 @@ static MbcType parseMBC(char const *name) {
int base = 0;
if (name[0] == '$') {
name++;
++name;
base = 16;
}
// Parse number, and return it as-is (unless it's too large)
@@ -257,10 +269,7 @@ static MbcType parseMBC(char const *name) {
uint16_t mbc;
char const *ptr = name;
// Trim off leading whitespace
while (*ptr == ' ' || *ptr == '\t') {
ptr++;
}
skipWhitespace(ptr); // Trim off leading whitespace
#define tryReadSlice(expected) \
do { \
@@ -274,11 +283,9 @@ static MbcType parseMBC(char const *name) {
case 'r':
tryReadSlice("OM");
// Handle optional " ONLY"
while (*ptr == ' ' || *ptr == '\t' || *ptr == '_') {
ptr++;
}
skipMBCSpace(ptr);
if (*ptr == 'O' || *ptr == 'o') {
ptr++;
++ptr;
tryReadSlice("NLY");
}
mbc = ROM;
@@ -351,9 +358,7 @@ static MbcType parseMBC(char const *name) {
case 'P': {
tryReadSlice("P1");
// Parse version
while (*ptr == ' ' || *ptr == '_') {
ptr++;
}
skipMBCSpace(ptr);
// Major
char *endptr;
unsigned long val = strtoul(ptr, &endptr, 10);
@@ -420,23 +425,18 @@ static MbcType parseMBC(char const *name) {
// clang-format on
for (;;) {
// Trim off trailing whitespace
while (*ptr == ' ' || *ptr == '\t' || *ptr == '_') {
ptr++;
}
skipWhitespace(ptr); // Trim off trailing whitespace
// If done, start processing "features"
if (!*ptr) {
break;
}
// We expect a '+' at this point
skipMBCSpace(ptr);
if (*ptr++ != '+') {
return MBC_BAD;
}
// Trim off leading whitespace
while (*ptr == ' ' || *ptr == '\t' || *ptr == '_') {
ptr++;
}
skipMBCSpace(ptr);
switch (*ptr++) {
case 'B': // BATTERY
@@ -502,7 +502,7 @@ static MbcType parseMBC(char const *name) {
case MBC1:
case MMM01:
if (features == RAM) {
mbc++;
++mbc;
} else if (features == (RAM | BATTERY)) {
mbc += 2;
} else if (features) {
@@ -534,7 +534,7 @@ static MbcType parseMBC(char const *name) {
MBC3_TIMER_BATTERY + 1 == MBC3_TIMER_RAM_BATTERY, "Enum sanity check failed!"
);
if (features == RAM) {
mbc++;
++mbc;
} else if (features == (RAM | BATTERY)) {
mbc += 2;
} else if (features) {
@@ -552,7 +552,7 @@ static MbcType parseMBC(char const *name) {
static_assert(MBC5_RUMBLE + 1 == MBC5_RUMBLE_RAM, "Enum sanity check failed!");
static_assert(MBC5_RUMBLE + 2 == MBC5_RUMBLE_RAM_BATTERY, "Enum sanity check failed!");
if (features == RAM) {
mbc++;
++mbc;
} else if (features == (RAM | BATTERY)) {
mbc += 2;
} else if (features) {
@@ -604,10 +604,7 @@ static MbcType parseMBC(char const *name) {
break;
}
// Trim off trailing whitespace
while (*ptr == ' ' || *ptr == '\t') {
ptr++;
}
skipWhitespace(ptr); // Trim off trailing whitespace
// If there is still something past the whitespace, error out
if (*ptr) {
@@ -1050,7 +1047,7 @@ static void
fatal("\"%s\" has more than 65536 banks", name);
return;
}
nbBanks++;
++nbBanks;
// Update global checksum, too
for (uint16_t i = 0; i < bankLen; i++) {
@@ -1375,7 +1372,7 @@ int main(int argc, char *argv[]) {
default:
warnx("Ignoring '%c' in fix spec", *musl_optarg);
}
musl_optarg++;
++musl_optarg;
}
break;

View File

@@ -37,6 +37,12 @@ void requireZeroErrors() {
}
}
static void incrementErrors() {
if (nbErrors != std::numeric_limits<decltype(nbErrors)>::max()) {
++nbErrors;
}
}
void error(char const *fmt, ...) {
va_list ap;
fputs("error: ", stderr);
@@ -45,9 +51,7 @@ void error(char const *fmt, ...) {
va_end(ap);
putc('\n', stderr);
if (nbErrors != std::numeric_limits<decltype(nbErrors)>::max()) {
nbErrors++;
}
incrementErrors();
}
[[noreturn]]
@@ -59,10 +63,7 @@ void fatal(char const *fmt, ...) {
va_end(ap);
putc('\n', stderr);
if (nbErrors != std::numeric_limits<decltype(nbErrors)>::max()) {
nbErrors++;
}
incrementErrors();
giveUp();
}
@@ -89,9 +90,7 @@ void warning(WarningID id, char const *fmt, ...) {
va_end(ap);
putc('\n', stderr);
if (nbErrors != std::numeric_limits<decltype(nbErrors)>::max()) {
nbErrors++;
}
incrementErrors();
break;
}
}

View File

@@ -58,7 +58,7 @@ static void assignSection(Section &section, MemoryLocation const &location) {
next->bank = location.bank;
}
nbSectionsToAssign--;
--nbSectionsToAssign;
out_AddSection(section);
}
@@ -151,7 +151,7 @@ static ssize_t getPlacement(Section const &section, MemoryLocation &location) {
location.address += section.alignMask + 1 + section.alignOfs;
} else {
// Any location is fine, so, next free block
spaceIdx++;
++spaceIdx;
if (spaceIdx < bankMem.size()) {
location.address = bankMem[spaceIdx].address;
}
@@ -161,7 +161,7 @@ static ssize_t getPlacement(Section const &section, MemoryLocation &location) {
// go forwards until that is no longer the case.
while (spaceIdx < bankMem.size()
&& location.address >= bankMem[spaceIdx].address + bankMem[spaceIdx].size) {
spaceIdx++;
++spaceIdx;
}
// Try again with the new location/free space combo
@@ -174,7 +174,7 @@ static ssize_t getPlacement(Section const &section, MemoryLocation &location) {
return -1;
} else if (scrambleROMX && section.type == SECTTYPE_ROMX && location.bank <= scrambleROMX) {
if (location.bank > typeInfo.firstBank) {
location.bank--;
--location.bank;
} else if (scrambleROMX < typeInfo.lastBank) {
location.bank = scrambleROMX + 1;
} else {
@@ -183,7 +183,7 @@ static ssize_t getPlacement(Section const &section, MemoryLocation &location) {
} else if (scrambleWRAMX && section.type == SECTTYPE_WRAMX
&& location.bank <= scrambleWRAMX) {
if (location.bank > typeInfo.firstBank) {
location.bank--;
--location.bank;
} else if (scrambleWRAMX < typeInfo.lastBank) {
location.bank = scrambleWRAMX + 1;
} else {
@@ -191,14 +191,14 @@ static ssize_t getPlacement(Section const &section, MemoryLocation &location) {
}
} else if (scrambleSRAM && section.type == SECTTYPE_SRAM && location.bank <= scrambleSRAM) {
if (location.bank > typeInfo.firstBank) {
location.bank--;
--location.bank;
} else if (scrambleSRAM < typeInfo.lastBank) {
location.bank = scrambleSRAM + 1;
} else {
return -1;
}
} else if (location.bank < typeInfo.lastBank) {
location.bank++;
++location.bank;
} else {
return -1;
}
@@ -355,11 +355,11 @@ static void categorizeSection(Section &section) {
// Insert section while keeping the list sorted by decreasing size
auto pos = sections.begin();
while (pos != sections.end() && (*pos)->size > section.size) {
pos++;
++pos;
}
sections.insert(pos, &section);
nbSectionsToAssign++;
++nbSectionsToAssign;
}
void assign_AssignSections() {
@@ -395,8 +395,7 @@ void assign_AssignSections() {
constraints--) {
for (Section *section : unassignedSections[constraints]) {
fprintf(stderr, "%c \"%s\"", nbSections == 0 ? ';' : ',', section->name.c_str());
nbSections++;
if (nbSections == 10) {
if (++nbSections == 10) {
goto max_out; // Can't `break` out of a nested loop
}
}

View File

@@ -209,7 +209,7 @@ static void parseScrambleSpec(char const *spec) {
}
if (*spec == '=') {
spec++; // `strtoul` will skip the whitespace on its own
++spec; // `strtoul` will skip the whitespace on its own
unsigned long limit;
char *endptr;

View File

@@ -527,7 +527,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) {
sym_AddSymbol(symbol);
if (std::holds_alternative<Label>(symbol.data)) {
nbSymPerSect[std::get<Label>(symbol.data).sectionID]++;
++nbSymPerSect[std::get<Label>(symbol.data).sectionID];
}
}

View File

@@ -90,7 +90,7 @@ void out_AddSection(Section const &section) {
// Insert section while keeping the list sorted by increasing org
auto pos = bankSections.begin();
while (pos != bankSections.end() && (*pos)->org < section.org) {
pos++;
++pos;
}
bankSections.insert(pos, &section);
}
@@ -178,7 +178,7 @@ static void
// Output padding up to the next SECTION
while (offset + baseOffset < section->org) {
putc(getNextFillByte(), outputFile);
offset++;
++offset;
}
// Output the section itself
@@ -196,7 +196,7 @@ static void
if (!disablePadding) {
while (offset < size) {
putc(getNextFillByte(), outputFile);
offset++;
++offset;
}
}
}
@@ -419,7 +419,7 @@ uint16_t forEachSection(SortedSections const &sectList, F callback) {
: zeroLenSection;
used += (*pickedSection)->size;
callback(**pickedSection);
pickedSection++;
++pickedSection;
}
return used;
}

View File

@@ -52,7 +52,7 @@ static void printDiag(
static void incrementErrors() {
if (nbErrors != UINT32_MAX) {
nbErrors++;
++nbErrors;
}
}