# Floating-point precision problems

**URL:** <https://forums.developer.nvidia.com/t/floating-point-precision-problems/20318>\
**Category:** CUDA Programming and Performance\
**Created:** [December 23, 2010, 8:16pm UTC](https://forums.developer.nvidia.com/t/floating-point-precision-problems/20318 "2010-12-23T20:16:37Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxest](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@maxest](https://forums.developer.nvidia.com/u/maxest)\
**Post date:** [December 23, 2010, 8:16pm UTC](https://forums.developer.nvidia.com/t/floating-point-precision-problems/20318/1 "2010-12-23T20:16:37Z")

</div>

Hey,

I’ve experienced some serious floating-point problems. Here’s the thing: I have a software renderer. To speed up pixel processing I decided to employ CUDA. My renderer is a traditional object-order renderer like OpenGL and I even use almost the same view/projection matrices as in OpenGL. In the attached pictures you can see a dramatic difference in image quality on CPU and on GPU (using CUDA) using the same perspctive matrix having near plane set to 0.5 and far plane to 1000. CPU does its job really well on 32-bit floats whereas CUDA have really serious problems. Where do these come from? I haven’t experienced similar difficulties when I used OpenGL or Direct3D  
 ![gpu.png](https://global.discourse-cdn.com/nvidia/original/2X/a/ac0e88358a0af7dd7f23e80a974ead57b96f50c3.png)  
 ![cpu.png](https://global.discourse-cdn.com/nvidia/original/2X/f/fa11c80befa3f0660470aa279952cdd5bbbcbc4b.png)

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [December 23, 2010, 11:41pm UTC](https://forums.developer.nvidia.com/t/floating-point-precision-problems/20318/2 "2010-12-23T23:41:57Z")

</div>

It is difficult to answer this without any source code. One difference that comes to mind though is that pre-Fermi GPUs don’t support denormals and always flush to zero.

---

<div class="post-metadata">

**Author:** ![maxest](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@maxest](https://forums.developer.nvidia.com/u/maxest)\
**Post date:** [December 24, 2010, 10:32am UTC](https://forums.developer.nvidia.com/t/floating-point-precision-problems/20318/3 "2010-12-24T10:32:32Z")

</div>

To be honest I dont think any piece of code could change anything. I execute the _exact_ same code on both CPU and CUDA on the exact same type of memory (unsigned char for color buffer, float for depth buffer). But if this might really help, here it is (a function that processes one pixel of a triangle (the triangle’s attributes are passed through arguments of the function)):

```auto
__global__ void rasterize(byte *colorBuffer, float *depthBuffer, [long list of other attributes])

{

	int y = minY + threadIdx.y + blockIdx.y*blockDim.y;

	int x = minX + threadIdx.x + blockIdx.x*blockDim.x;

	if (x >= minX && x <= maxX && y >= minY && y <= maxY)

	{

		// affine barycentric weights

		float a = implicitLine_device((float)x, (float)y, v1.position, v2.position) * one_over_v0ToLine12;

		float b = implicitLine_device((float)x, (float)y, v2.position, v0.position) * one_over_v1ToLine20;

		float c = implicitLine_device((float)x, (float)y, v0.position, v1.position) * one_over_v2ToLine01;

		// if pixel (x, y) is inside the triangle or on one of its edges

		if (a >= 0 && b >= 0 && c >= 0)

		{

			// this prevents from drawing the same pixel twice (belonging to adjacent triangles)

			if ( ( (a == 0) && (dot12 > 0) ) ||

				 ( (b == 0) && (dot20 > 0) ) ||

				 ( (c == 0) && (dot01 > 0) ) )

			{

				return;

			}

			int pixelIndex = y*width + x;

			float z_affine = a*v0.position.z + b*v1.position.z + c*v2.position.z;

			if (z_affine < depthBuffer[pixelIndex] && z_affine <= 1.0f)

			{

				// perspective-correct barycentric weights

				float d = a*one_over_h0 + b*one_over_h1 + c*one_over_h2;

				d = 1.0f / d;

				a *= d*one_over_h0;

				b *= d*one_over_h1;

				c *= d*one_over_h2;

				// attributes interpolation

				vec3 color_persp = a*v0.color + b*v1.color + c*v2.color;

				vec2 texCoord_persp = a*v0.texCoord + b*v1.texCoord + c*v2.texCoord;

				// run pixel shader

				vec3 pixelColor = color_persp;

				// clamp bytes to 255

				byte red = (byte)(255.0f * MIN(pixelColor.x, 1.0f));

				byte green = (byte)(255.0f * MIN(pixelColor.y, 1.0f));

				byte blue = (byte)(255.0f * MIN(pixelColor.z, 1.0f));

				// update buffers

				colorBuffer[4*pixelIndex + 0] = blue;

				colorBuffer[4*pixelIndex + 1] = green;

				colorBuffer[4*pixelIndex + 2] = red;

				depthBuffer[pixelIndex] = z_affine;

			}

		}

	}

}

```

byte == unsigned char

---

<div class="post-metadata">

**Author:** ![Lev](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Lev](https://forums.developer.nvidia.com/u/Lev)\
**Post date:** [December 24, 2010, 1:18pm UTC](https://forums.developer.nvidia.com/t/floating-point-precision-problems/20318/4 "2010-12-24T13:18:14Z")

</div>

> [@](#):
>
> To be honest I dont think any piece of code could change anything. I execute the _exact_ same code on both CPU and CUDA on the exact same type of memory (unsigned char for color buffer, float for depth buffer). But if this might really help, here it is (a function that processes one pixel of a triangle (the triangle’s attributes are passed through arguments of the function)):
> 
> ```auto
> __global__ void rasterize(byte *colorBuffer, float *depthBuffer, [long list of other attributes])
> 
> {
> 
> int y = minY + threadIdx.y + blockIdx.y*blockDim.y;
> 
> int x = minX + threadIdx.x + blockIdx.x*blockDim.x;
> 
> if (x >= minX && x <= maxX && y >= minY && y <= maxY)
> 
> {
> 
> // affine barycentric weights
> 
> float a = implicitLine_device((float)x, (float)y, v1.position, v2.position) * one_over_v0ToLine12;
> 
> float b = implicitLine_device((float)x, (float)y, v2.position, v0.position) * one_over_v1ToLine20;
> 
> float c = implicitLine_device((float)x, (float)y, v0.position, v1.position) * one_over_v2ToLine01;
> 
> // if pixel (x, y) is inside the triangle or on one of its edges
> 
> if (a >= 0 && b >= 0 && c >= 0)
> 
> {
> 
> // this prevents from drawing the same pixel twice (belonging to adjacent triangles)
> 
> if ( ( (a == 0) && (dot12 > 0) ) ||
> 
> ( (b == 0) && (dot20 > 0) ) ||
> 
> ( (c == 0) && (dot01 > 0) ) )
> 
> {
> 
> return;
> 
> }
> 
> int pixelIndex = y*width + x;
> 
> float z_affine = a*v0.position.z + b*v1.position.z + c*v2.position.z;
> 
> if (z_affine < depthBuffer[pixelIndex] && z_affine <= 1.0f)
> 
> {
> 
> // perspective-correct barycentric weights
> 
> float d = a*one_over_h0 + b*one_over_h1 + c*one_over_h2;
> 
> d = 1.0f / d;
> 
> a *= d*one_over_h0;
> 
> b *= d*one_over_h1;
> 
> c *= d*one_over_h2;
> 
> // attributes interpolation
> 
> vec3 color_persp = a*v0.color + b*v1.color + c*v2.color;
> 
> vec2 texCoord_persp = a*v0.texCoord + b*v1.texCoord + c*v2.texCoord;
> 
> // run pixel shader
> 
> vec3 pixelColor = color_persp;
> 
> // clamp bytes to 255
> 
> byte red = (byte)(255.0f * MIN(pixelColor.x, 1.0f));
> 
> byte green = (byte)(255.0f * MIN(pixelColor.y, 1.0f));
> 
> byte blue = (byte)(255.0f * MIN(pixelColor.z, 1.0f));
> 
> // update buffers
> 
> colorBuffer[4*pixelIndex + 0] = blue;
> 
> colorBuffer[4*pixelIndex + 1] = green;
> 
> colorBuffer[4*pixelIndex + 2] = red;
> 
> depthBuffer[pixelIndex] = z_affine;
> 
> }
> 
> }
> 
> }
> 
> }
> 
> ```
> 
> byte == unsigned char

you probably use -fast\_math switch, use full precision functions for division and square roots if you have them.

---

<div class="post-metadata">

**Author:** ![maxest](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@maxest](https://forums.developer.nvidia.com/u/maxest)\
**Post date:** [December 24, 2010, 1:27pm UTC](https://forums.developer.nvidia.com/t/floating-point-precision-problems/20318/5 "2010-12-24T13:27:33Z")

</div>

> [@](#):
>
> you probably use -fast\_math switch, use full precision functions for division and square roots if you have them.

I don’t think I use such a thing. In the CUDA Build Rule (from CUDA Toolkit) there is some “Use fast math” switch which is set to “false”. However, it is available only on the “Host” page so I don’t think it has any meaning to code being executed on GPU. Here’s what the build rule produces:

```auto
"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v3.2\bin\nvcc.exe" -gencode=arch=compute_10,code=\"sm_10,compute_10\" -gencode=arch=compute_20,code=\"sm_20,compute_20\" --machine 32 -ccbin "C:\Program Files\Microsoft Visual Studio 9.0\VC\bin" -Xcompiler "/EHsc /W3 /nologo /O2 /Zi /MD " -I"..\..\..\..\include" -I"..\..\..\..\..\BlossomEngine\common\include" -I"..\..\..\..\..\BlossomEngine\math\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v3.2\include" -maxrregcount=32 --compile -o "Release/renderer.cu.obj" renderer.cu

```

---

<div class="post-metadata">

**Author:** ![Lev](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Lev](https://forums.developer.nvidia.com/u/Lev)\
**Post date:** [December 24, 2010, 3:42pm UTC](https://forums.developer.nvidia.com/t/floating-point-precision-problems/20318/6 "2010-12-24T15:42:38Z")

</div>

> [@](#):
>
> I don’t think I use such a thing. In the CUDA Build Rule (from CUDA Toolkit) there is some “Use fast math” switch which is set to “false”. However, it is available only on the “Host” page so I don’t think it has any meaning to code being executed on GPU. Here’s what the build rule produces:
> 
> ```auto
> "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v3.2\bin\nvcc.exe" -gencode=arch=compute_10,code=\"sm_10,compute_10\" -gencode=arch=compute_20,code=\"sm_20,compute_20\" --machine 32 -ccbin "C:\Program Files\Microsoft Visual Studio 9.0\VC\bin" -Xcompiler "/EHsc /W3 /nologo /O2 /Zi /MD " -I"..\..\..\..\include" -I"..\..\..\..\..\BlossomEngine\common\include" -I"..\..\..\..\..\BlossomEngine\math\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v3.2\include" -maxrregcount=32 --compile -o "Release/renderer.cu.obj" renderer.cu
> 
> ```

try add clamping from zero.

---

<div class="post-metadata">

**Author:** ![maxest](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@maxest](https://forums.developer.nvidia.com/u/maxest)\
**Post date:** [December 25, 2010, 1:52pm UTC](https://forums.developer.nvidia.com/t/floating-point-precision-problems/20318/7 "2010-12-25T13:52:37Z")

</div>

Could you be more specific, please? :) You mean some compiler’s option? I don’t see any such in the compiler’s documentation

---

<div class="post-metadata">

**Author:** ![Lev](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Lev](https://forums.developer.nvidia.com/u/Lev)\
**Post date:** [December 25, 2010, 2:50pm UTC](https://forums.developer.nvidia.com/t/floating-point-precision-problems/20318/8 "2010-12-25T14:50:32Z")

</div>

> [@](#):
>
> Could you be more specific, please? :) You mean some compiler’s option? I don’t see any such in the compiler’s documentation

I mean clamp bytes to 0…255, you only check upper side.

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [December 27, 2010, 2:31pm UTC](https://forums.developer.nvidia.com/t/floating-point-precision-problems/20318/9 "2010-12-27T14:31:27Z")

</div>

What happens if you comment out this part?

```auto
// this prevents from drawing the same pixel twice (belonging to adjacent triangles)

			if ( ( (a == 0) && (dot12 > 0) ) ||

				 ( (b == 0) && (dot20 > 0) ) ||

				 ( (c == 0) && (dot01 > 0) ) )

			{

				return;

			}

```

Testing floating point variables for exact equality almost always is the wrong thing to do. And flush-to-zero could explain why this would trigger more often on the GPU than on the CPU.

---

<div class="post-metadata">

**Author:** ![maxest](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@maxest](https://forums.developer.nvidia.com/u/maxest)\
**Post date:** [January 7, 2011, 2:34pm UTC](https://forums.developer.nvidia.com/t/floating-point-precision-problems/20318/10 "2011-01-07T14:34:49Z")

</div>

Unfortunately, it does not help. I have totally run out of ideas. I really do exactly the same things on both CPU and GPU. Have you not experienced floating-point problems on GPU? Because it must be it - floats near the far plane all are tightly packed (as depth-buffer values form a sort of 1/z function).

---

<div class="post-metadata">

**Author:** ![Lev](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Lev](https://forums.developer.nvidia.com/u/Lev)\
**Post date:** [January 7, 2011, 3:51pm UTC](https://forums.developer.nvidia.com/t/floating-point-precision-problems/20318/11 "2011-01-07T15:51:11Z")

</div>

> [@](#):
>
> Unfortunately, it does not help. I have totally run out of ideas. I really do exactly the same things on both CPU and GPU. Have you not experienced floating-point problems on GPU? Because it must be it - floats near the far plane all are tightly packed (as depth-buffer values form a sort of 1/z function).

try to debug your code at those points and see what is going on.

---

<div class="post-metadata">

**Author:** ![satyam\_shivam](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@satyam\_shivam](https://forums.developer.nvidia.com/u/satyam_shivam)\
**Post date:** [January 7, 2011, 6:59pm UTC](https://forums.developer.nvidia.com/t/floating-point-precision-problems/20318/12 "2011-01-07T18:59:26Z")

</div>

Hi,

try replacing 0 with 0.f in the following lines:

```
if (a >= 0 && b >= 0 && c >= 0)
      
if ( ( (a == 0) && (dot12 > 0) ) || ( (b == 0) && (dot20 > 0) ) || ( (c == 0) && (dot01 > 0) ) )

```

It will be more helpful if you could paste the entire code, if you can !

---

<div class="post-metadata">

**Author:** ![maxest](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@maxest](https://forums.developer.nvidia.com/u/maxest)\
**Post date:** [January 7, 2011, 7:24pm UTC](https://forums.developer.nvidia.com/t/floating-point-precision-problems/20318/13 "2011-01-07T19:24:11Z")

</div>

I replaced - no change in speed.  
How can I poste more code if since have posted the whole global function? :)

---

<div class="post-metadata">

**Author:** ![njuffa](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@njuffa](https://forums.developer.nvidia.com/u/njuffa)\
**Post date:** [January 7, 2011, 7:45pm UTC](https://forums.developer.nvidia.com/t/floating-point-precision-problems/20318/14 "2011-01-07T19:45:24Z")

</div>

The code posted does not seem to be the complete code, so by necessity the following discussion will have to remain somewhat speculative. I assume it has been established that the inputs to the function shown are identical in the CPU and GPU versions, and that data corruption (e.g. due to out of bounds accesses, race conditions) has been ruled out both by code inspection and testing.

It is not clear to me how sensitive the computation is to small numerical differences, but there are multiple reasons why intermediate GPU and CPU results may be slightly different. This in turn may cause differences in the output if such sensitivity exists. BTW, is this running on an sm\_1x or sm\_2x platform?

(1) On sm\_1x platforms, and on sm\_2x platforms with -ftz=true, very small numbers (so called denormals) are flushed to zero when running on the GPU. While such an FTZ mode also exists in SSE, it is unlikely that it is turned on by default, and the host compiler may not even target SSE. In my experience, this is an infrequent issue (with a few exceptions, code is not usually prone to frequent underflows).

(2) On sm\_1x platforms, and on sm\_2x platforms with -prec-div=false, the following results in an approximate, not IEEE-rounded reciprocal:

```auto
d = 1.0f / d;

```

On the CPU, an IEEE-rounded reciprocal would likely be generated here. For the sake of experiment, you could replace this as follows to force a reciprocal with IEEE round to-nearest-or-even on the GPU:

```auto
d = __frcp_rn(d);

```

(3) There are multiple opportunities in this code for the compiler to generate either FMADs (on sm\_1x), or FFMAs (on sm\_2x), which are merged multiply-and-add operations. For example, the following expression naturally lends itself to the use of FMAD/FMA:

```auto
vec3 color_persp = a*v0.color + b*v1.color + c*v2.color;

```

The results from either FMAD or FFMA are not generally identical to a sequence of FMUL followed by FADD, as would be used on the CPU. FFMA on sm\_2x follows the IEEE-754 (2008) specification for fused multiply-add and will in general provide better accuracy than the combination of FMUL and FADD. To force the use of separate FMUL and FADD, one can use the \_\_fmul\_rn(), \_\_fadd\_rn() device functions, the use of which prevents the compiler from merging these operations into FMAD/FFMA.

(4) Even though the code uses float variables throughout, depending on how it is compiled on the CPU, much of the CPU computation may actually be performed at higher precision. In particular this would likely be the case if the compiler targets the x87 FPU [i.e. not SSE], in which case most floating-point operations would map to in-register computation that is performed with double-precision (on Windows) or extended-precision (on Linux).

While there are host compiler flags designed to reduce “excess precision” being carried in temporary variables (I think /Op for MSVC, -ffloat-store for gcc), the only way to test that hypothesis rigorously on the CPU is to declare all float variables volatile, and also break up the computations into individual operations. For example, this

```auto
float z_affine = a*v0.position.z + b*v1.position.z + c*v2.position.z;

```

would be turned into this:

```auto
volatile float z_affine;

z_affine = a*v0.position.z;

z_affine += b*v1.position.z;

z_affine += c*v2.position.z;

```

In my personal experience, significant numerical differences observed between CPU and GPU computation on floats are often due to issue (4), but combinations of multiple of the issues mentioned above are also not uncommon.

---

<div class="post-metadata">

**Author:** ![cbuchner1](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/cbuchner1/32/14168_2.png) [@cbuchner1](https://forums.developer.nvidia.com/u/cbuchner1)\
**Post date:** [January 7, 2011, 8:29pm UTC](https://forums.developer.nvidia.com/t/floating-point-precision-problems/20318/15 "2011-01-07T20:29:16Z")

</div>

> [@](#):
>
> In my personal experience, significant numerical differences observed between CPU and GPU computation on floats are often due to issue (4), but combinations of multiple of the issues mentioned above are also not uncommon.

Wow, great explanations here. Thanks for taking the time to summarize all this.
