Bitwise operations on GPUs

Started by
4 comments, last by egwenejs 19 years, 2 months ago
Has anyone tried to emulate bitwise operation on the GPU ? I know that GLSL and Cg do not support bitwise operations. So how to get around this problem ? Can we use the preprocessor because the preprocessor expressions do allow bitwise operators ? Has anyone tired this ? The 'abs' & 'mod' functions in GLSL: 1) float abs(float x) --> Returns x if x >= 0; otherwise it returns x. "OpenGL Shading Language" - page 122. Ok. Is it not returning x with the same sign ? What is the use of this function ? Isn't it supposed to get rid of the -ve sign if x < 0 ? 2) float mod(float x, float y) --> Returns x - y * floor(x/y); Somebody please explain it to me what is this function doing ? Is there a builtin function for sgn(x) = 1 if x > 0; otherwise 0; Thanks! [Edited by - dimensionX on February 8, 2005 5:37:24 PM]
Advertisement
1) of course the abs(x) function returns the negated value of x if negative. If you've read a spec that says something else, this is obviously a typo.

2) the mod function returns the remainder of the division, just like the Pascal's mod function or C's % function, except the fact that this version also works with floating-point numbers.
Examples :
mod(10,3) == 1
mod(10,5) == 0
mod(6.5, 3) == 0.5
This function ensures that for any x,y pair, the result of mod(x,y) is in the [0,y[ range : 0 <= mod(x,y) < y.
A typical example of this property is with y=2*PI : it transforms any angle value x from the ]-oo, +oo[ range to the [0,2*PI] range.

As for bitwise operations, you can actually use the logical operation functionality glLogicOp, available since OpenGL 1.0, but it's fairly limited and rarely accelerated in hardware for all operations.
Quote:Is there a builtin function for sgn(x) = 1 if x > 0; otherwise 0;


genType step (genType edge, genType x)
genType step (float edge, genType x)
Returns 0.0 if x < edge, otherwise it returns 1.0
Vincoof,

I know what the spec says about 'abs(x)'. What is the use of this function if it returns the same (value and sign) as the input 'x' ?
Quote:Original post by dimensionX
I know what the spec says about 'abs(x)'. What is the use of this function if it returns the same (value and sign) as the input 'x' ?


That's not what it does. It returns the absolute value of x. And in case you don't know, that means if x is positive it returns x, if it is negative it returns -x. What vincoof was saying is that if you've read somewhere that it just returns x, then it's obviously a typo.

ie:
Quote:1) float abs(float x) --> Returns x if x >= 0; otherwise it returns x. "OpenGL Shading Language" - page 122.


That is obviously a typo and should be "otherwise it returns -x."
Usually you when you take the absolute value of something its called the magnitude of that something. The last time I used abs () in a program it was to take the sqrt() of negative numbers when I was rolling my complex number class.

This topic is closed to new replies.

Advertisement