38
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 :)
36
Apr 11 '19 edited Apr 11 '19
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.
18
u/modulusshift Apr 11 '19 edited Apr 12 '19
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. :)
Edit: source, worth a read!
4
u/Farull Apr 12 '19
The 68000 supported 32-bit hardware multiplication just fine through the mul operator. The 8-bit 6800 or 6502 did not however.
5
Apr 12 '19
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.
1
u/modulusshift Apr 12 '19 edited Apr 12 '19
Not to mention it could be done while putting the resulting pixels on the screen instead of waiting for a result. I apologize for the error though.
1
u/Farull Apr 12 '19
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.
3
u/brendanrivers Apr 12 '19
why is this better than just doing
4^2 = 4 + 4 + 4 +4?
the snippet above does this in 2x as many additions:
4^2 = 1 + 3 + 5 + 7
and it also has to do y+2 each time, so it's twice as hard
3
u/modulusshift Apr 12 '19 edited Apr 12 '19
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.
2
u/brendanrivers Apr 12 '19
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.
3
u/modulusshift Apr 12 '19
Lol no problem sounds like a fun time! Did you share around the link to the original story? https://www.folklore.org/StoryView.py?story=Round_Rects_Are_Everywhere.txt
To be clear, the code in question was handwritten 68k assembly with both size and speed constraints if that helps.
10
Apr 11 '19
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)
2
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
Apr 12 '19
No this is how you double a float:
float f = 15.2f;
double f_doubled = static_cast<double>(f);
15
u/banquuuooo Apr 11 '19
Why does this work?