Hey! inline function can do it for float and double or anything with template, and don''t use C-casts! use reinterpret_cast
The C++ integrist
(just kidding)
La Mothe - Optimated vector length-allgorithm
Doesn''t that require an extra copy?
&x->p
&x->p
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
baskuenen
Obfuscate: tr.v. -cated, -cating, -cates. 1. a. To render obscure. b. To darken.
2. To confuse: his emotions obfuscated his judgment.
[LLat. obfuscare, to darken : ob(intensive) + Lat. fuscare,
to darken < fuscus, dark.] -obfuscation n. obfuscatory adj
I don''t know if you''ve checked the site. You should check it... Their laws are mainly:
So you may see that it is very fun... Some examples are even artisticly interesting
Thanks for the parenthesis. I don''t know if the compiler will accept them though, because (8+4) have no addresses... And by this same fact you cannot send constants or expressions... But here is a way (not tested though) in C++:
I love this fabs defines
Maybe a bit too many parenthesis... I just don''t want to mess with operator priority.
Now this should work, but I don''t know if the compiler will yield or collect the garbage for this...
WinNT will though.
They try to prevent death, I try to promote life.
Obfuscate: tr.v. -cated, -cating, -cates. 1. a. To render obscure. b. To darken.
2. To confuse: his emotions obfuscated his judgment.
[LLat. obfuscare, to darken : ob(intensive) + Lat. fuscare,
to darken < fuscus, dark.] -obfuscation n. obfuscatory adj
I don''t know if you''ve checked the site. You should check it... Their laws are mainly:
- To write the most Obscure/Obfuscated C program under the rules below.
- To show the importance of programming style, in an ironic way.
- To stress C compilers with unusual code.
- To illustrate some of the subtleties of the C language.
- To provide a safe forum for poor C code. :-)
So you may see that it is very fun... Some examples are even artisticly interesting
Thanks for the parenthesis. I don''t know if the compiler will accept them though, because (8+4) have no addresses... And by this same fact you cannot send constants or expressions... But here is a way (not tested though) in C++:
// Still it is NOT LISP... Please.#define fabs(x) (*((float*)(*((long*)(&((*(new float))=(x)))) &= 0x7FFFFFFF)))
I love this fabs defines
Maybe a bit too many parenthesis... I just don''t want to mess with operator priority.
Now this should work, but I don''t know if the compiler will yield or collect the garbage for this...
WinNT will though.
They try to prevent death, I try to promote life.
Now I know what I'm made of, and I'm afraid of it...
Magmai Kai Holmlor: You mean typecasting to a pointer and back to a value again? I think a good compiler will optimize this.
Poltras: Ah - Obfuscate is their name. Now I get it. But what is their URL? Could it be I missed it?
Poltras: Ah - Obfuscate is their name. Now I get it. But what is their URL? Could it be I missed it?
You can''t use reinterpret cast:
''reinterpret_cast'' : cannot convert from ''float'' to ''int''
...
the idea was to make the fabs fast... not bloated!
this *does* dereference a pointer, *p, p contains the address of x, not the value of x
All those defines use this method so they dereference a pointer as well...
Good God, your leaking!
''reinterpret_cast'' : cannot convert from ''float'' to ''int''
...
the idea was to make the fabs fast... not bloated!
quote:
float x= -10.5; int *p = (int *)&x *p &= 0x7FFFFFFF;
this *does* dereference a pointer, *p, p contains the address of x, not the value of x
All those defines use this method so they dereference a pointer as well...
quote:
#define fabs(x) (*((float*)(*((long*)(&((*(new float))=(x)))) &= 0x7FFFFFFF)))
Good God, your leaking!
//C++''ine waytemplate <class T>T fabs(T) { return((x<0)?-x:x); }
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
quote: Original post by Magmai Kai Holmlor
Poltras said:
#define fabs(x) (*((float*)(*((long*)(&((*(new float))=(x)))) &= 0x7FFFFFFF)))
Good God, your leaking!
NO!!! I'm making fun... that's different.
quote:
//C++'ine waytemplate <class T>T fabs(T) { return((x<0)?-x:x); }
And what for ANSI C??? Worse, you were talking about speed, and my solution is more speed-efficient (Anyway, who will do 30M fabs in a second?).
Your solution is not "look-what-I-did-it's-cool" enough
Just kidding.
I don't want to participate into flame wars, I just want to start them.
Edited by - Poltras on November 2, 2000 11:57:46 AM
Now I know what I'm made of, and I'm afraid of it...
quote: Original post by Poltras
// This changes to absolute the value of the parameter// and return the value.#define fabs(x) (*((float*)(*((long*)&x) &= 0x7FFFFFFF))) [/source] <hr height=1 noshade></BLOCKQUOTE></font> Your second version leaks a float every time you call it; you never match the new w/ a delete.Secondly, the above will always access potentially invalid memory addresses, as you cast a long to a float* (Bad!)Here''s a better way to do it:[source]struct fabs_func{public: fabs_func( float f ): t(f) { *reinterpret_cast<long*>(&t)&=0x7fffffff; } operator float() const { return t; } float t;};
Note that this will not work for doubles. Although it doesn''t take much to hack this for doubles.
Getting back to the original question, though, this is a pointless optimization. fabs takes 3 clocks on a 486 and 1 on a P5 and above. FP multiplication is much faster than integer multiplication. All these optimizations are pretty damn stupid.
MSN
quote: Original post by msn12b
Original post by Poltras
// This changes to absolute the value of the parameter// and return the value.#define fabs(x) (*((float*)(*((long*)&x) &= 0x7FFFFFFF))) [/source] <hr height=1 noshade></BLOCKQUOTE></font>
Your second version leaks a float every time you call it; you never match the new w/ a delete.
I know the second version will memleak 4 bytes everytimes we call it, and I warned it...
quote:
Secondly, the above will always access potentially invalid memory addresses, as you cast a long to a float* (Bad!)
Note that this will not work for doubles. Although it doesn''t take much to hack this for doubles.
mmmmh?? No... wait... you''re right! Damn!
Is there a solution? Oh yeah:
// Just for fun again (I know this is crap, but...)
#define fabs(x) (*((float*)&((*((long*)&(x)) &= 0x7FFFFFFF))))<br><br>Tadaaa! It works! <img src="wink.gif" width=15 height=15 align=middle><br><br><br><BLOCKQUOTE><font size=1>quote:<hr height=1 noshade><br>Getting back to the original question, though, this is a pointless optimization. fabs takes 3 clocks on a 486 and 1 on a P5 and above. FP multiplication is much faster than integer multiplication. All these optimizations are pretty damn stupid.<br> <hr height=1 noshade></BLOCKQUOTE></font> <br>Like I was saying, I''m only doing it for fun... and fabs takes a bit more time... it is not a single ASM instruction since you need to store and restore the value into registry.<br> </pre>
Now I know what I'm made of, and I'm afraid of it...
quote: Original post by Poltras
Like I was saying, I''m only doing it for fun... and fabs takes a bit more time... it is not a single ASM instruction since you need to store and restore the value into registry.
Hello? Are you joking or are you actually this stupid? You''re doing a FP compare immediately following the fabs()... even if you''re doing a simple fabs on a float, it is still faster to load and save from the FPU; the CPU can pair integer instructions w/ the FP instructions.
MSN
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement