# Does ARB Fragment Program have a sqrt function?

This topic is 5219 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I don't see anything on a sqrt() in the arg fragment program? Is this correct? I also don't see anything for division? I am assuming this because divison is slower than multiplying? So what do you suggest I use in place of the divison? Basically this is what I am trying to do in my fragment program....
nx = 2*r-1;
ny = 2*g-1;
nz = 2*b-1

d = sqrt(nx*nx + ny*ny + nz*nz);

nx /=d;
ny /=d;
nz /=d;

r = (nx + 1)/2;
g = (ny + 1)/2;
b = (nz + 1)/2;


I am having to do a lot of explicit calls in my fragment program which seems stupid? In GLSlang the above code would be close to what I would do or not? due to its like C syntax? This what I am doing currently in my fragment program and seems like there is a more effecient way to do it...
MUL nx, two, normalmap.r;
MUL ny, two, normalmap.g;
MUL nz, two, normalmap.b;
SUB nx, nx, 1;
SUB ny, ny, 1;
SUB nz, nz, 1;

#sqrt here?



##### Share on other sites
It doesn't have a square root function, but it does have pow.

##### Share on other sites
It has both a square root and a division opcode: RSQ (reciprocal sqrt) and RCP. RSQ is usually used to normalize vectors, but if you really need a standalone sqrt(), you could RCP it back. You divide by multiplying with the reciprocal.

##### Share on other sites
Huh, interesting.... would RSQ + RCP actually be faster than POW? (I'd benchmark it myself, but I get the feeling you already know [wink])

##### Share on other sites
To get the square root of a number n, first calculate RSQ(n) and then multiply it by the original n:

sqrt(n) = n * (1.0 / sqrt(n))

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633768
• Total Posts
3013741
×