My rudimentary understanding is that the key here is the "union" declaration. Union is like a stingy struct that is only able to store one value of its members at a time. If long is stored as a 32-bit value and double is stored as a 64-bit value, the entire union structure will take up 64 bits, swapping between the double and long as necessary. This means that both values would have the same allocated 64-bit memory space.
The initial value of the double does not necessarily matter, but it's important to remember how a double is stored. The first bit is the sign and the second part consists of 11 bits which represent the exponent == e. Finally, the third part consists of 52 bits for the fraction value == f. The way we obtain a decimal value is by computing -> (sign) * 1.f * 2e.
Alright! Now that we have all of the important details covered, we can dig into what the code actually does. First we assign a value to x.f, taking up 64-bits. We now add a ridiculously large number to x.i:
4503599627370496d = leading zeroes...10000000000000000000000000000000000000000000000000000b
Since union takes up the same 64-bit slot and both x.i and x.f share it, we're not really doing anything fancy here besides just adding this large binary number to what is already stored in that particular part of the memory (currently x.f). This behaviour is considered undefined as x.i is only supposed to be a 32-bit value (might even be compiler or system-specific), but for the sake of this example, we're only really concerned about the fact that x.i is using up the same memory space as x.f is.
This large binary value is tailored to increase the exponent part of the double by 1. This results in our output number being (sign) * 1.f * 2e+1 which is exactly the same as just multiplying the initial number by 2.
Be careful though!! Printing x.f at the very end is still considered undefined behaviour because we were referring to x.i just before it. Luckily, in our case, we aren't doing anything too drastic and just abusing the fact that x.i and x.f share the same memory slot.
I hope this explanation was clear enough. I tested it out a bit before and it seems to make enough sense so I figured I would share if not for any other reason in order to provoke someone smarter into finding the courage to correct me.
I'd just like to confirm that this behavior is compiler specific. I've tested this with MSVC 14.16 in default debug configuration and as expected it behaves differently.
When adding our large number to x.i (long), the compiler will strip 32 most significant bits, to fit long. This will result in adding 0 to x.i and a final result will remain 15.2.
This is actually a really misleading explaination - this ONLY works for specific numbers due to how floats are stored in memory. You absolutely cannot correctly operate on arbitrary floats by treating them as integers.
My point is that your specific value c only works for this specific case - you can't add c to a different number d and have a deterministic outcome for any value of d.
Also keep in mind that tricks like this, when used correctly, can massively increase the speed of certain calculations. Consider the case of Quake's fast inverse square root. The more obvious way involves finding a square root and then taking its reciprocal, which are both computationally expensive. But this method uses only multiplication and bit shifting, and is way, way faster.
EDIT: OK so this specific hack is now obsolete. Still, the point is that sometimes these things serve a specific purpose beyond just wankery.
I read an old story from the original Macintosh dev team that has one of my favorite of these. They wanted to draw circles and ovals quickly, so they needed to solve x2 + y2 = r2 quickly. But the 68000 CPU didn't have hardware multiplication, so this formula was very slow. So the genius who wrote all the graphics drawing routines took advantage of this: the sequence of squares is the sequence of the sums of consecutive odd numbers. So
int square(int x)
{
int result = 0;
int y = 1;
if(x < 0) {x = -x};
for(int i = 0; i < x; i++)
{
result += y;
y += 2;
}
return result;
}
This was hastily written on a phone, and it's the first C I've written in a couple years, might be buggy. :)
32 bit multiplication took like 40 cycles. With small enough value this might be quicker. If you need a series of square numbers, this would definitely be quicker.
Sure it was slow, but usable for the generic case. Bit shifting and other tricks was of course faster for specific cases, but saying it wasn’t supported is simply not true.
Good insight! It's useful primarily for graphing circles. It allows you to determine when the line of the curve goes up or down a pixel, so it's not really just 42 you want, but n2 for integer n from 0 to 4, for example. Then this formula allows you to get the squares leading up to 42 for pretty cheap.
also I doubt I optimized that very well. I had a second go of it that doubled x (just a bit shift, so still efficient) then used that as the threshold for y to end the loop, eliminating the i counter, but I lost it and didn't feel like writing it again.
edit: I looked at what you're saying again and you already optimized that bit for me, it sounds like.
So after kicking your code around the office on slack, there were some questions, so I posted the insight above which was brought up by a coworker. Eventually someone pasted this:
bool inCircle(int x, int y, int r)
{
int x2=0;
int y2=0;
int r2=0;
if(x < 0) {x = -x};
if(y < 0) {y = -y};
if(r < 0) {r = -r};
int maxI = max(x, y, z);
int z = 1;
int result =0;
for(int i = 1; i <= maxI; i++)
{
result+=z;
if(i==x){x2=result;}
if(i==y){y2=result;}
if(i==r){r2=result;}
z += 2;
}
return x2+y2<r2;
}
and he is doing exactly what you said above! so this leads to O(5n) operations where n is the max of x, y, r. I think this is still more expensive than finding them independently which would be 3 O(n) operations, or O(3n). I know that in school they teach me that O(3n) == O(n), but I think it's clear that this is different. So then we started discussing things like
"okay well is an if statement the same cost as an add statement?"
and then we all looked at each other like "who fucking knows" and moved on.
Sorry I nitpicked your thing, just was something cool that sort of took off in the office.
The fast inverse square root was necessary as hardware didn't have any square root support, and only an approximate solution was necessary.
modern architectures have hardware inverse square root approximations (generally I believe the precise hardware square root implementations use the approximation hardware)
Is there any use of std::cout << each time writing strbefore cout instead of just writing using namespace std; at the start so u dont have to type it again? I’m new to programming and I barely know C++ but wondering why you prefer std:: each time instead of just typing using namespace std at start to save type and space?
Well, it's Undefined Behaviour (in C++, but not in C, it's "type punning"), so it might not work... Of course, since most compiler vendors want to keep C-compiled-as-C++ working and use a lot of common code in the compiler for the two languages it'll probably work in most environments.
15
u/banquuuooo Apr 11 '19
Why does this work?
38
u/[deleted] Apr 11 '19
Though it is incorrect because it wraps infinity to negative zero. This is the biggest problem here ;)
It’s worth noting however that the correct implementation of floating point negation is to explicitly flip/clear the sign bit, eg
-some_float
On x86 and others is turned into
movl $reg, [some float] xorl $reg, (1<<31)Not x87 though because it isn’t particularly cooperative with registers :)
11
u/ANXtreme Apr 11 '19
Is there any use of std::cout <<
each time writing strbefore cout instead of just writing using namespace std; at the start so u dont have to type it again? I’m new to programming and I barely know C++ but wondering why you prefer std:: each time instead of just typing using namespace std at start to save type and space?
3
u/mallardtheduck Apr 12 '19
Well, it's Undefined Behaviour (in C++, but not in C, it's "type punning"), so it might not work... Of course, since most compiler vendors want to keep C-compiled-as-C++ working and use a lot of common code in the compiler for the two languages it'll probably work in most environments.
1
u/[deleted] Apr 12 '19
No this is how you double a float: