My naive approach would be something like this:
const int numCountryIndicies = however many there are..;
const char* countries = "A1\0A2\0...";
return (cc < numCountryIndicies)?countries[cc*2]:"UNKNOWN";
My naive approach would be something like this:
const int numCountryIndicies = however many there are..;
const char* countries = "A1\0A2\0...";
return (cc < numCountryIndicies)?countries[cc*2]:"UNKNOWN";
I'd write something similar, more or less; Probably the following, not for optimization, but more as a matter of style:
const char countries[][3] = {
"A1",
"A0",
// [...]
};
// [...]
int total_countries = (int) (sizeof(countries) / sizeof(countries[0]));
return cc < total_countries ? countries[cc] : "UNKNOWN";
No need to hard code the length, and the cast is guaranteed to be within the bounds of an int on all platforms as long as you don't go over 2^16-1 countries.Stylistically-wise I think the best solution would be to write the array like:
const char countries[][3] = {
[0] = "A1",
[1] = "A0",
};
This way the codes are explicit when you read the code and it makes editing the array a little easier. Unfortunately gcc only warns if you set the same index twice with -Wextra, it remains silent with -Wall.Good call. It might have caught the off-by-one mistake I made (the codes start from 1 and not 0 in the blog post). Maybe even switch to using an enum as our index instead of an int.
getCountry:
mov eax, OFFSET FLAT:.LC0
cmp edi, 258
ja .L1
mov edi, edi
mov rax, QWORD PTR CSWTCH.1[0+rdi*8]
.L1:
retSo clang did this for a long time it seems.
Edited to add: I understand that it's a NOP, but why would the compiler emit one here?
[0]: https://devblogs.microsoft.com/oldnewthing/20110921-00/?p=95...
edi register is the lower 4 bytes of rdi. Instructions which write these smaller pieces zero out the unused higher bytes of the destination registers. This helps with performance because eliminates data dependencies on the old values in these higher bytes.
mov eax, OFFSET FLAT:.LC0
cmp rdi, 258
ja .L1
mov rax, QWORD PTR CSWTCH.1[0+rdi*8]
Note that in some cases the compiler can do this automatically via lifetime analysis but not in this freestanding example.I don’t know my compilers that well but if I had to guess I would say there is a good chance this will be optimized away by the compiler.
Run it on infinite multiverse, construct a mechanism to destroy the Universe every time branch predictor did not predict every single conditional correctly.
The branch predictor can select a random branch, so it doesn't even need to actually predict anything. It should use quantum noise so that it has chance of selecting different branches in different copies of the universe. There is noting to compute, quantum or otherwise.
The bomb to destroy the universe will take care of all universes where it made wrong predictions, so that is where we should concentrate our development resources.
It has more utility than just branch prediction. Imagine the bomb going off automatically whenever there starts a war.
Any copy of the universe that starts a war is automatically eliminated and this guarantees that you, as an observer, will never observe any wars.
And to minimize the risk of errors such as the "Anguila" error above.