Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
geetee
Feb 2, 2004

>;[
There is no way &1 even compiles (right?) so I don't understand what this entire fiasco is about. The logical operator "AND" requires two ampersands, not one. I'm surprised none of your IDEs warned you about this. It's just a segfault waiting to happen. Just use modulus and move on.

Adbot
ADBOT LOVES YOU

Painless
Jan 9, 2005

Turn ons: frogs, small mammals, piles of compost
Turn offs: large birds, pitchforks
See you at the beach!

geetee posted:

There is no way &1 even compiles (right?) so I don't understand what this entire fiasco is about. The logical operator "AND" requires two ampersands, not one. I'm surprised none of your IDEs warned you about this. It's just a segfault waiting to happen. Just use modulus and move on.

Hrm good sir I think you are wrong, allow me a moment to write a two-page refutation of this complete with references (with exact page numbers) to the C99 standard, the C++98 standard, the ANSI C standard and the C++0x draft.

narbsy
Jun 2, 2007

Painless posted:

Hrm good sir I think you are wrong, allow me a moment to write a two-page refutation of this complete with references (with exact page numbers) to the C99 standard, the C++98 standard, the ANSI C standard and the C++0x draft.

One can find everything in the C++0x draft, so that may not help your case. It's "Programming with Everything but the Kitchen Sink, but We Threw One In Anyways" in one document.

Steampunk Mario
Aug 12, 2004

DIAGNOSIS ACQUIRED

geetee posted:

There is no way &1 even compiles (right?) so I don't understand what this entire fiasco is about. The logical operator "AND" requires two ampersands, not one. I'm surprised none of your IDEs warned you about this. It's just a segfault waiting to happen. Just use modulus and move on.

There's more than one kind of 'AND'... :ssh:

No Safe Word
Feb 26, 2005

Steampunk Mario posted:

There's more than one kind of 'AND'... :ssh:

*whoooosh*

edit: or "look at you, look how stupid you are" I guess

POKEMAN SAM
Jul 8, 2004

heeen posted:

Hahaha better yet, how do you define modulus for float numbers?

Ever heard of fmod?

POKEMAN SAM
Jul 8, 2004
I did get pretty carried away with the retarded examples, but it's still loving stupid to rely on the representation of something to do something mathy when you have a perfectly good/obvious/understood/optimizable way of doing it, as in this case.

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender
code:
float InvSqrt (float x){
    float xhalf = 0.5f*x;
    int i = *(int*)&x;
    i = 0x5f3759df - (i>>1);
    x = *(float*)&i;
    x = x*(1.5f - xhalf*x*x);
    return x;
}
gently caress yoooooooooouuuuuu.

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge

minato posted:

code:
float InvSqrt (float x){
    float xhalf = 0.5f*x;
    int i = *(int*)&x;
    i = 0x5f3759df - (i>>1);
    x = *(float*)&i;
    x = x*(1.5f - xhalf*x*x);
    return x;
}
gently caress yoooooooooouuuuuu.

That's not a horror, that's an extreme optimisation for an operation done a lot in many renderers. Low accuracy bitwise hack combined with one Newton-Raphson iteration.

Painless
Jan 9, 2005

Turn ons: frogs, small mammals, piles of compost
Turn offs: large birds, pitchforks
See you at the beach!

minato posted:

code:
int i = *(int*)&x;
x = *(float*)&i;

That's pretty likely to go boom on GCC FYI

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

Painless posted:

That's pretty likely to go boom on GCC FYI

Builds and runs without incident on 4.0.1 with -Wall and -pedantic.

dancavallaro
Sep 10, 2006
My title sucks

minato posted:

code:
float InvSqrt (float x){
    float xhalf = 0.5f*x;
    int i = *(int*)&x;
    i = 0x5f3759df - (i>>1);
    x = *(float*)&i;
    x = x*(1.5f - xhalf*x*x);
    return x;
}

Isn't that the Quake inverse square root function?

Presto
Nov 22, 2002

Keep calm and Harry on.

Painless posted:

That's pretty likely to go boom on GCC FYI
Should be fine, although it may bitch about violating strict aliasing rules.

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

dancavallaro posted:

Isn't that the Quake inverse square root function?

Yup.

zergstain
Dec 15, 2005

I may be a bit late posting this, but it looks like everything at work is escaped with mysql_escape_string(). I know it's deprecated and all, but I'm unable to find info on the real world security implications, or even why escaping ' isn't enough. Any examples I've ever seen rely on an unescaped '. Perhaps if I have enough evidence of what might happen, it can be changed. And no, I'm not going to rewrite it all to use whatever the gently caress it's called where you bind variables. I don't even know if mysqli is available.

ehnus
Apr 16, 2003

Now you're thinking with portals!

Zakalwe posted:

That's not a horror, that's an extreme optimisation for an operation done a lot in many renderers. Low accuracy bitwise hack combined with one Newton-Raphson iteration.

It's a coding horror for any x86 (post-1999) or PowerPC based computer.

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge

ehnus posted:

It's a coding horror for any x86 (post-1999) or PowerPC based computer.

In the context in which it was originally written it's not a coding horror which is what counts. Today of course I would (and do) use SSE for such things.

heeen
May 14, 2005

CAT NEVER STOPS

Zakalwe posted:

In the context in which it was originally written it's not a coding horror which is what counts. Today of course I would (and do) use SSE for such things.

Can you elaborate on the why and how you do things nowadays?

dancavallaro
Sep 10, 2006
My title sucks

Sartak posted:

Yup.

That's an interesting read, and so is this paper http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf for the more mathy people here.

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge

heeen posted:

Can you elaborate on the why and how you do things nowadays?

My research is on RTRT (real-time ray-tracing). Packet tracing (Google Keywords: Ingo Wald ICRT Packet Tracing) at its simplest involves tracing 4 rays at a time in SSE. Other related techniques such as MLRTA, frustum culling etc. all benefit from using SSE.

Here's a reciprocal SSE sqrt from my code.

code:
_m128 rsqrt_nr(const __m128 &a)
{
    const __m128 half = _mm_set1_ps(0.5f);
    const __m128 three = _mm_set1_ps(3.0f);
    const __m128 r = _mm_rsqrt_ps(a);
    return _mm_mul_ps(_mm_mul_ps(half, r),
           _mm_sub_ps(three,
           _mm_mul_ps(_mm_mul_ps(a, r), Ra0)));
}
I perform a simple NR on the _mm_rsqrt_ps intrinsic to gain about 22 bits of accuracy (from the original 12)

chocojosh
Jun 9, 2007

D00D.

Zakalwe posted:

That's not a horror, that's a savage optimisation for an operation done a lot in many renderers. Low accuracy bitwise hack combined with one Newton-Raphson iteration.

Fixed the quote. I do wish the code had some more spaces in it though :(

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge
btw, here's something I found out recently.

Here's that line from the above code to access the bits in a float.
code:
 int i = *(int*)&x;
I used to do that too, but you can also do this in C++

code:
int i = (int&)x;
Perhaps a bit off-topic for a coding horrors thread.

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

Zakalwe posted:

Perhaps a bit off-topic for a coding horrors thread.

Nope I'd say you're in the right place.

edit: vvvvv just funnin', man

Filburt Shellbach fucked around with this message at 20:16 on Jan 7, 2009

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge

Sartak posted:

Nope I'd say you're in the right place.

Optimisation when relevant is not a coding horror.

hexadecimal
Nov 23, 2008

by Fragmaster

Zakalwe posted:

btw, here's something I found out recently.

Here's that line from the above code to access the bits in a float.
code:
 int i = *(int*)&x;
I used to do that too, but you can also do this in C++

code:
int i = (int&)x;
Perhaps a bit off-topic for a coding horrors thread.

Can you explain why this is faster than doing int i = (int)x

Painless
Jan 9, 2005

Turn ons: frogs, small mammals, piles of compost
Turn offs: large birds, pitchforks
See you at the beach!

Presto posted:

Should be fine, although it may bitch about violating strict aliasing rules.

It won't "bitch", but it's quite possible that it will randomly fail! Just use unions drat it. Jeet chirst this is making me so angry. What nubbery..

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge

hexadecimal posted:

Can you explain why this is faster than doing int i = (int)x

Uhh because what you posted doesn't work?

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...

zergstain posted:

I may be a bit late posting this, but it looks like everything at work is escaped with mysql_escape_string(). I know it's deprecated and all, but I'm unable to find info on the real world security implications, or even why escaping ' isn't enough. Any examples I've ever seen rely on an unescaped '. Perhaps if I have enough evidence of what might happen, it can be changed. And no, I'm not going to rewrite it all to use whatever the gently caress it's called where you bind variables. I don't even know if mysqli is available.

The difference between escape and real_escape is only a problem if you're not using latin1.

If you can somehow magically escape all the ' characters assuredly, then in most cases you're safe, yes. But this is the problem with what you're saying -- the "real" coding horror:

quote:

I'm unable to find info on the real world security implications, or even why escaping ' isn't enough.

You don't know. You don't know whether or not an attack is possible. You do, however, know the best way to code it so that this entire class of attack is NOT possible.

The flaw is in your thought process. "Well, I don't know of any attacks that this doesn't prevent against, so this level of security is okay, I'll just use that." Why would you even want to think about that? Just code it the way that's not vulnerable to that class of attack AT ALL and go about your day.

dancavallaro
Sep 10, 2006
My title sucks

hexadecimal posted:

Can you explain why this is faster than doing int i = (int)x

I'm not a big C guy, so I'm kind of guessing here, but just doing (int)x will cast x to an integer, and chop off anything after the decimal. But (int&)x or *(int*)&x preserves all of the bits of the float, but as an integer. This is what you need, because the point is to be able to do bit operations on the bits of the float.

edit: In other words, the point is not to cast x to an integer, but to allow the bits of the float to be considered an integer.

Or something like that.

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge

dancavallaro posted:

I'm not a big C guy, so I'm kind of guessing here, but just doing (int)x will cast x to an integer, and chop off anything after the decimal. But (int&)x or *(int*)&x preserves all of the bits of the float, but as an integer. This is what you need, because the point is to be able to do bit operations on the bits of the float.

correct

POKEMAN SAM
Jul 8, 2004

Painless posted:

It won't "bitch", but it's quite possible that it will randomly fail! Just use unions drat it.

Where in the C standard does it specify that unioned elements will occupy the same space in memory?

hexadecimal
Nov 23, 2008

by Fragmaster

Zakalwe posted:

Uhh because what you posted doesn't work?

I just tried it and it did. x is a float and you are casting it to int i?

dancavallaro posted:

I'm not a big C guy, so I'm kind of guessing here, but just doing (int)x will cast x to an integer, and chop off anything after the decimal. But (int&)x or *(int*)&x preserves all of the bits of the float, but as an integer. This is what you need, because the point is to be able to do bit operations on the bits of the float.

edit: In other words, the point is not to cast x to an integer, but to allow the bits of the float to be considered an integer.

Or something like that.

Oh I see. Thanks. Learn something new everyday! I actually wondered before how to do this in C++ (I did it in java to switch endianess).

hexadecimal fucked around with this message at 20:19 on Jan 7, 2009

dancavallaro
Sep 10, 2006
My title sucks
edit: too late

edit2: this thread is moving way too fast. Everyone go take 5.

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge

hexadecimal posted:

I just tried it and it did

It compiles, but it doesn't do what we want to do (access the bits in a float). Taking 5 to get some food sounds like a good idea.

Mikey-San
Nov 3, 2005

I'm Edith Head!
hexadecimal.txt all up in this page

e: i kid, i kid

geetee
Feb 2, 2004

>;[

Mikey-San posted:

hexadecimal.txt all up in this page

Fixed that for you.

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge

Ugg boots posted:

Where in the C standard does it specify that unioned elements will occupy the same space in memory?

Wait, don't unions by definition start at the same point in memory. If the types have the same length they completely overlap each other. Am I missing something here?
Edit: Wagammama's ramen is delicious.

heeen
May 14, 2005

CAT NEVER STOPS

Ugg boots posted:

Where in the C standard does it specify that unioned elements will occupy the same space in memory?

Structure and union specifiers have the same form. [ . . . ] The size of a union is sufficient to contain the largest of its members. The value of at most one of the members can be stored in a union object at any time. A pointer to a union object, suitably converted, points to each of its members (or if a member is a bit-field, then to the unit in which it resides), and vice versa.
—ANSI/ISO 9899:1990 (the ANSI C standard) Section 6.5.2.1

POKEMAN SAM
Jul 8, 2004
Edit: Thanks.

Adbot
ADBOT LOVES YOU

zergstain
Dec 15, 2005

Ryouga Inverse posted:

The difference between escape and real_escape is only a problem if you're not using latin1.

If you can somehow magically escape all the ' characters assuredly, then in most cases you're safe, yes. But this is the problem with what you're saying -- the "real" coding horror:


You don't know. You don't know whether or not an attack is possible. You do, however, know the best way to code it so that this entire class of attack is NOT possible.

The flaw is in your thought process. "Well, I don't know of any attacks that this doesn't prevent against, so this level of security is okay, I'll just use that." Why would you even want to think about that? Just code it the way that's not vulnerable to that class of attack AT ALL and go about your day.

I do use real_escape when I write my own code, I just wanted to know if there was some real world examples of someone getting owned for using escape that I could use to argue the case of switching over, which should be real fast since everything is supposed to go though a quote function which does poo poo like undo magic quotes. I confirmed just now that mysqli isn't available in our php, so prepared statements (I remembered what they were called) are impossible, even if it were somehow possible for me to make a case to go rewrite thousands of lines of code.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply