From 99221f7d179f5f6543765486798afc22365237ec Mon Sep 17 00:00:00 2001 From: Matt Pharr Date: Thu, 1 Sep 2011 16:31:22 -0700 Subject: [PATCH] Fix a few places in examples where C reference implementaion had a double-precision fp constant undesirably causing computation to be done in double precision. Makes C scalar versions of the options pricing models, rt, and aobench 3-5% faster. Makes scalar version of noise about 15% faster. Others are unchanged. --- examples/aobench/ao_serial.cpp | 18 +++++++++--------- examples/mandelbrot/mandelbrot_serial.cpp | 2 +- .../mandelbrot_tasks/mandelbrot_serial.cpp | 2 +- examples/noise/noise_serial.cpp | 6 +++--- examples/options/options_serial.cpp | 6 +++--- examples/volume_rendering/volume_serial.cpp | 14 +++++++------- 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/examples/aobench/ao_serial.cpp b/examples/aobench/ao_serial.cpp index 0b3e2b6d..69af2eba 100644 --- a/examples/aobench/ao_serial.cpp +++ b/examples/aobench/ao_serial.cpp @@ -140,7 +140,7 @@ ray_plane_intersect(Isect &isect, Ray &ray, float d = -dot(plane.p, plane.n); float v = dot(ray.dir, plane.n); - if (fabsf(v) < 1.0e-17) + if (fabsf(v) < 1.0e-17f) return; else { float t = -(dot(ray.org, plane.n) + d) / v; @@ -183,11 +183,11 @@ orthoBasis(vec basis[3], const vec &n) { basis[2] = n; basis[1].x = 0.0; basis[1].y = 0.0; basis[1].z = 0.0; - if ((n.x < 0.6) && (n.x > -0.6)) { + if ((n.x < 0.6f) && (n.x > -0.6f)) { basis[1].x = 1.0; - } else if ((n.y < 0.6) && (n.y > -0.6)) { + } else if ((n.y < 0.6f) && (n.y > -0.6f)) { basis[1].y = 1.0; - } else if ((n.z < 0.6) && (n.z > -0.6)) { + } else if ((n.z < 0.6f) && (n.z > -0.6f)) { basis[1].z = 1.0; } else { basis[1].x = 1.0; @@ -224,7 +224,7 @@ ambient_occlusion(Isect &isect, Plane &plane, float phi = 2.0f * M_PI * drand48(); float x = cosf(phi) * theta; float y = sinf(phi) * theta; - float z = sqrtf(1.0 - theta * theta); + float z = sqrtf(1.0f - theta * theta); // local . global float rx = x * basis[0].x + y * basis[1].x + z * basis[2].x; @@ -236,14 +236,14 @@ ambient_occlusion(Isect &isect, Plane &plane, ray.dir.y = ry; ray.dir.z = rz; - occIsect.t = 1.0e+17; + occIsect.t = 1.0e+17f; occIsect.hit = 0; for (int snum = 0; snum < 3; ++snum) ray_sphere_intersect(occIsect, ray, spheres[snum]); ray_plane_intersect (occIsect, ray, plane); - if (occIsect.hit) occlusion += 1.0; + if (occIsect.hit) occlusion += 1.f; } } @@ -280,10 +280,10 @@ static void ao_scanlines(int y0, int y1, int w, int h, int nsubsamples, ray.dir.x = px; ray.dir.y = py; - ray.dir.z = -1.0; + ray.dir.z = -1.0f; vnormalize(ray.dir); - isect.t = 1.0e+17; + isect.t = 1.0e+17f; isect.hit = 0; for (int snum = 0; snum < 3; ++snum) diff --git a/examples/mandelbrot/mandelbrot_serial.cpp b/examples/mandelbrot/mandelbrot_serial.cpp index 4bea7baf..a76fb5ca 100644 --- a/examples/mandelbrot/mandelbrot_serial.cpp +++ b/examples/mandelbrot/mandelbrot_serial.cpp @@ -36,7 +36,7 @@ static int mandel(float c_re, float c_im, int count) { float z_re = c_re, z_im = c_im; int i; for (i = 0; i < count; ++i) { - if (z_re * z_re + z_im * z_im > 4.) + if (z_re * z_re + z_im * z_im > 4.f) break; float new_re = z_re*z_re - z_im*z_im; diff --git a/examples/mandelbrot_tasks/mandelbrot_serial.cpp b/examples/mandelbrot_tasks/mandelbrot_serial.cpp index 4bea7baf..a76fb5ca 100644 --- a/examples/mandelbrot_tasks/mandelbrot_serial.cpp +++ b/examples/mandelbrot_tasks/mandelbrot_serial.cpp @@ -36,7 +36,7 @@ static int mandel(float c_re, float c_im, int count) { float z_re = c_re, z_im = c_im; int i; for (i = 0; i < count; ++i) { - if (z_re * z_re + z_im * z_im > 4.) + if (z_re * z_re + z_im * z_im > 4.f) break; float new_re = z_re*z_re - z_im*z_im; diff --git a/examples/noise/noise_serial.cpp b/examples/noise/noise_serial.cpp index a988bc1a..b81c6152 100644 --- a/examples/noise/noise_serial.cpp +++ b/examples/noise/noise_serial.cpp @@ -104,7 +104,7 @@ inline float NoiseWeight(float t) { inline float Lerp(float t, float low, float high) { - return (1. - t) * low + t * high; + return (1.f - t) * low + t * high; } @@ -147,7 +147,7 @@ static float Turbulence(float x, float y, float z, int octaves) { lambda *= 1.99f; o *= omega; } - return sum * 0.5; + return sum * 0.5f; } @@ -163,7 +163,7 @@ void noise_serial(float x0, float y0, float x1, float y1, float y = y0 + j * dy; int index = (j * width + i); - output[index] = Turbulence(x, y, 0.6, 8); + output[index] = Turbulence(x, y, 0.6f, 8); } } } diff --git a/examples/options/options_serial.cpp b/examples/options/options_serial.cpp index a2689b73..1c8ed88a 100644 --- a/examples/options/options_serial.cpp +++ b/examples/options/options_serial.cpp @@ -47,7 +47,7 @@ static inline float CND(float X) { float L = fabsf(X); - float k = 1.0 / (1.0 + 0.2316419 * L); + float k = 1.f / (1.f + 0.2316419f * L); float k2 = k*k; float k3 = k2*k; float k4 = k2*k2; @@ -59,7 +59,7 @@ CND(float X) { w *= invSqrt2Pi * expf(-L * L * .5f); if (X > 0.f) - w = 1.0 - w; + w = 1.f - w; return w; } @@ -94,7 +94,7 @@ binomial_put_serial(float Sa[], float Xa[], float Ta[], float dt = T / BINOMIAL_NUM; float u = expf(v * sqrtf(dt)); - float d = 1. / u; + float d = 1.f / u; float disc = expf(r * dt); float Pu = (disc - d) / (u - d); diff --git a/examples/volume_rendering/volume_serial.cpp b/examples/volume_rendering/volume_serial.cpp index 2a697d34..bcf9e7c4 100644 --- a/examples/volume_rendering/volume_serial.cpp +++ b/examples/volume_rendering/volume_serial.cpp @@ -104,7 +104,7 @@ Inside(float3 p, float3 pMin, float3 pMax) { static bool IntersectP(const Ray &ray, float3 pMin, float3 pMax, float *hit0, float *hit1) { - float t0 = -1e30, t1 = 1e30; + float t0 = -1e30f, t1 = 1e30f; float3 tNear = (pMin - ray.origin) / ray.dir; float3 tFar = (pMax - ray.origin) / ray.dir; @@ -213,7 +213,7 @@ transmittance(float3 p0, float3 p1, float3 pMin, float tau = 0; float rayLength = sqrtf(ray.dir.x * ray.dir.x + ray.dir.y * ray.dir.y + ray.dir.z * ray.dir.z); - float stepDist = 0.2; + float stepDist = 0.2f; float stepT = stepDist / rayLength; float t = rayT0; @@ -239,8 +239,8 @@ distanceSquared(float3 a, float3 b) { static float raymarch(float density[], int nVoxels[3], const Ray &ray) { float rayT0, rayT1; - float3 pMin(.3, -.2, .3), pMax(1.8, 2.3, 1.8); - float3 lightPos(-1, 4, 1.5); + float3 pMin(.3f, -.2f, .3f), pMax(1.8f, 2.3f, 1.8f); + float3 lightPos(-1f, 4f, 1.5f); if (!IntersectP(ray, pMin, pMax, &rayT0, &rayT1)) return 0.; @@ -249,10 +249,10 @@ raymarch(float density[], int nVoxels[3], const Ray &ray) { // Parameters that define the volume scattering characteristics and // sampling rate for raymarching - float Le = .25; // Emission coefficient + float Le = .25f; // Emission coefficient float sigma_a = 10; // Absorption coefficient float sigma_s = 10; // Scattering coefficient - float stepDist = 0.025; // Ray step amount + float stepDist = 0.025f; // Ray step amount float lightIntensity = 40; // Light source intensity float tau = 0.f; // accumulated beam transmittance @@ -269,7 +269,7 @@ raymarch(float density[], int nVoxels[3], const Ray &ray) { // terminate once attenuation is high float atten = expf(-tau); - if (atten < .005) + if (atten < .005f) break; // direct lighting