12

Last semester we had to develop a raytracer for a course at school. School being done, I tried to fiddle a bit with it.

I wanted to see what would change if I changed all floating point calculations from double to float (all calculations being done with doubles). So I changed every variable to type float, and simply cast each double returned from the Math methods to float. Testing my raytracer on a couple of scenes showed a pretty decent performance increase.

Searching around the web I found various reasons why float could be faster, but also some saying that double could be as fast, or even faster in a 64-bit environment. The thing is, I am running in a 64-bit environment and JVM. What would be the reason for this increased performance?

Now, I'm reading the PBRT book and planning to rewrite a raytracer from scratch following this. Would choosing float for all floating point calculations pose any problems? I didn't notice any during my tests, but perhaps for certain scenes the lower precision might (intersection testing seems like where it could pose a possible problem)? Or perhaps a tradeoff, like using double for the critical tests and float for less critical calculations? I would have to use an other Math library to get rid of the casting, would I be going the float way.

Koekje
  • 677
  • 1
  • 6
  • 17
  • I believe `double d = 1d` is two instructions, while `float f = 1f` is one. Even on 64 bit machines. Mess around with a 64bit JVM. – jn1kk Jun 25 '15 at 16:08
  • "I am running in a 64-bit environment" A 64-bit OS doesn't count as a 64-bit environment. Your compiler and other tools (such as a JVM if you are doing Java) also have to be 64-bit. – Code-Apprentice Jun 25 '15 at 16:08
  • 1
    possible duplicate of [Is float slower than double? Does 64 bit program run faster than 32 bit program?](http://stackoverflow.com/questions/5738448/is-float-slower-than-double-does-64-bit-program-run-faster-than-32-bit-program) – Raman Shrivastava Jun 25 '15 at 16:10
  • Can you put some code please! – hagrawal7777 Jun 25 '15 at 16:11
  • 3
    @Code-Apprentice for Java there is no real distinction between 32/64 bit compiling I think? I am running it on a 64-bit version of the JVM though. – Koekje Jun 25 '15 at 16:21
  • @hagrawal I think it's pretty difficult, I would have to strip down the tracer a lot? – Koekje Jun 25 '15 at 16:22
  • @Koekje I don't know if there are any differences between the byte codes with 32-bit javac vs 64-bit. It's very possible that there are not and the difference only occurs as the JVM translates the bytecodes into machine code and executes. This certainly would be an interesting area of Java to investigate. – Code-Apprentice Jun 25 '15 at 16:25
  • @Code-Apprentice Minus the compiler bit. There is no such thing as a 32/64 bit compiler in Java, neither is there a 32/64 bit class file format. – biziclop Jun 25 '15 at 16:25
  • @biziclop I have no idea what you even mean by "32/64 bit compiler". Are you saying there is no difference between the compiler in the 32-bit JDK and the one in the 64-bit JDK? (p.s. I didn't introduce the "32/64" notation into this conversation. – Code-Apprentice Jun 25 '15 at 16:29
  • @Code-Apprentice Yes, there is no difference. They produce exactly the same class file. The class file format is defined in absolute field widths. Moreover, 32 and 64 bit JVMs, though they behave very differently internally, particularly in terms of performance, adhere to the same language specification and provide exactly the same guarantees. – biziclop Jun 25 '15 at 17:09
  • @Code-Apprentice The one important exception is native libraries, if you've built an application that uses its own so/dll libraries, those need to be compiled for the specific target platform. A 32 bit dll will not be loaded by a 64 bit JVM. – biziclop Jun 25 '15 at 17:10

2 Answers2

9

There is pretty much no difference between float and double when it comes to speed of calculation, as far as desktop processors are the platform. The difference can only come from the increased memory bandwidth requirements because doubles require twice as much space.

Its different for GPU based calculations, those are more tailored for float and e.g. Nvidia GPU's break down considerably for double.

I'd go with a mixed approach; store data like polygons with float precision, but do all calculations in double. Small memory footprint, high precision - win-win.

Durandal
  • 19,919
  • 4
  • 36
  • 70
2

I'm answering so late that this is probably irrelevant to you, but perhaps helpful to others. I'm also working on a ray tracer in Go as a personal project (also using the PBR book), and had the same question...32 or 64 bit floats. I think I'm going to go with 32 bit mainly for one reason: SIMDizing ray-triangle intersection tests. No matter which instruction set your processor can use (eg SSE4, AVX2, AVX512), it can perform 2x the amount of 32-bit fp ops per instruction vs 64-bit.

Benny Jobigan
  • 5,078
  • 2
  • 31
  • 41