Started by Jan 16 2011 11:21 AM

,
17 replies to this topic

Posted 16 January 2011 - 11:21 AM

not sure if there is a function for this

if its a positive , it makes it a negitive

if its a negitive then it makes it a positive

float num = 97.45;

num *=-1;

if its a positive , it makes it a negitive

if its a negitive then it makes it a positive

float num = 97.45;

num *=-1;

Posted 16 January 2011 - 12:04 PM

I would say its straight:

-num.

Is this worthy to worry about? I mean I've just done a test, and performance wise, difference are barely imposible to notice (testing on 1024^3 gave me ~1.10e-005 vs ~1.05e-005)

-num.

Is this worthy to worry about? I mean I've just done a test, and performance wise, difference are barely imposible to notice (testing on 1024^3 gave me ~1.10e-005 vs ~1.05e-005)

Posted 16 January 2011 - 12:19 PM

That's called "negating", not "inversing", and yes x = -x is the answer.not sure if there is a function for this

if its a positive , it makes it a negitive

if its a negitive then it makes it a positive

float num = 97.45;

num *=-1;

"In order to understand recursion, you must first understand recursion."

My website dedicated to sorting algorithms

My website dedicated to sorting algorithms

Posted 16 January 2011 - 12:28 PM

If you've got a lot of numbers you need to negate, you can batch negate them using whatever SIMD hardware you have at your disposal.

For example, with SSE:

Of course, that's probably overkill.

For example, with SSE:

size_t const count = 10000; __declspec(align(16)) float values[count] = { ... }; // values to negate __m128 zero = _mm_set_ps1(0.0f); float* p = values; // negate 16 values every iteration for (size_t i = 0, n = count / 16; i < n; p += 16, ++i) { __m128 a = _mm_load_ps(p); __m128 b = _mm_load_ps(p+4); __m128 c = _mm_load_ps(p+8); __m128 d = _mm_load_ps(p+12); a = _mm_sub_ps(zero, a); b = _mm_sub_ps(zero, <img src='http://public.gamedev.net/public/style_emoticons/<#EMO_DIR#>/cool.gif' class='bbc_emoticon' alt='B)' />; c = _mm_sub_ps(zero, c); d = _mm_sub_ps(zero, d); _mm_store_ps(p, a); _mm_store_ps(p+4, <img src='http://public.gamedev.net/public/style_emoticons/<#EMO_DIR#>/cool.gif' class='bbc_emoticon' alt='B)' />; _mm_store_ps(p+8, c); _mm_store_ps(p+12, d); } // negate remaining values for (float* p_end = values + count; p < p_end; ++p) { *p = -(*p); }

Of course, that's probably overkill.

Posted 16 January 2011 - 04:35 PM

You can check the sign bit and inverse it if necessary, but you have other things to worry about, than negating numbers don't you?

Posted 16 January 2011 - 09:42 PM

Keep it simple. x = -x; Let the compiler perform any tricks required to make it fast. Worry about making your program run correctly before worrying about making it run fast. If you think this operation is too slow, profile it. Only then worry about it if it's truly a bottleneck (it shouldn't be).

Posted 17 January 2011 - 03:13 AM

A bit nitpicking: You're right: The usual name is negation. But on the other hand, negation is a specialization of the inverse with additive math in mind. See e.g. here on wikipedia. This is a similar distinction as with the neutral element, what is 0 for addition and 1 for multiplication....

That's called "negating", not "inversing", and yes x = -x is the answer.

Posted 17 January 2011 - 10:56 AM

Most FPUs have an instruction to change the sign of a floating point number. That would be the fastest way, but isn't very portable...

Posted 17 January 2011 - 02:22 PM

Yes but that article, that function sets up some other variables and thus it is slower.

The fastest way to batch process is to use MMX for integers and SSE for floats using the CPU.

The GPU way would be way faster. Use CUDA or OpenCL.

The fastest way to batch process is to use MMX for integers and SSE for floats using the CPU.

The GPU way would be way faster. Use CUDA or OpenCL.

Sig: http://glhlib.sourceforge.net

an open source GLU replacement library. Much more modern than GLU.

float matrix[16], inverse_matrix[16];

glhLoadIdentityf2(matrix);

glhTranslatef2(matrix, 0.0, 0.0, 5.0);

glhRotateAboutXf2(matrix, angleInRadians);

glhScalef2(matrix, 1.0, 1.0, -1.0);

glhQuickInvertMatrixf2(matrix, inverse_matrix);

glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);

glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

an open source GLU replacement library. Much more modern than GLU.

float matrix[16], inverse_matrix[16];

glhLoadIdentityf2(matrix);

glhTranslatef2(matrix, 0.0, 0.0, 5.0);

glhRotateAboutXf2(matrix, angleInRadians);

glhScalef2(matrix, 1.0, 1.0, -1.0);

glhQuickInvertMatrixf2(matrix, inverse_matrix);

glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);

glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

Posted 17 January 2011 - 03:26 PM

A bit nitpicking: You're right: The usual name is negation. But on the other hand, negation is a specialization of the inverse with additive math in mind. See e.g. here on wikipedia. This is a similar distinction as with the neutral element, what is 0 for addition and 1 for multiplication....

That's called "negating", not "inversing", and yes x = -x is the answer.

I'd agree that negating is not inverting at least as far as programming is concerned. One's compliment could easily be interpreted as the "inverse" of a number in a program, which is not the same as the negation of a number.

edit:

" 'inverse' is a noun. The verb is 'invert'. The 'inverse' is the result of inverting."

oh god. You'd think with a browser that underlines everything in red people (including me) would catch such things.

/shame on family

Posted 17 January 2011 - 05:48 PM

Aarrgh.

'inverse' is a noun. The verb is 'invert'. The 'inverse' is the result of inverting.

'inverse' is a noun. The verb is 'invert'. The 'inverse' is the result of inverting.

Posted 17 January 2011 - 07:25 PM

No it should be done in the cloud with HTML and variables.Yes but that article, that function sets up some other variables and thus it is slower.This article outlines several other methods.

The fastest way to batch process is to use MMX for integers and SSE for floats using the CPU.

The GPU way would be way faster. Use CUDA or OpenCL.

Posted 17 January 2011 - 09:08 PM

No it should be done in the cloud with HTML and variables.

Yes but that article, that function sets up some other variables and thus it is slower.This article outlines several other methods.

The fastest way to batch process is to use MMX for integers and SSE for floats using the CPU.

The GPU way would be way faster. Use CUDA or OpenCL.

If you believe the buzz you could almost believe that.

Posted 18 January 2011 - 01:46 AM

I believe every gd.net meme, and anything posted on dailyWTF...If you believe the buzz you could almost believe that.No it should be done in the cloud with HTML and variables.

Yes but that article, that function sets up some other variables and thus it is slower.This article outlines several other methods.

The fastest way to batch process is to use MMX for integers and SSE for floats using the CPU.

The GPU way would be way faster. Use CUDA or OpenCL.

Posted 18 January 2011 - 01:56 AM

This article outlines several other methods.

The fastest way to batch process is to use MMX for integers and SSE for floats using the CPU.

The GPU way would be way faster. Use CUDA or OpenCL.

made me chuckle. All this hype about the cloud can get pretty silly.

Just a wannabe grinding in this game we call Life.