[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [39742] trunk/blender/source/blender/ blenlib/intern/noise.c: remove implicit casts to doubles in the noise code, also use floating point math functions rather then double since the noise functions range is already limited by casting to ints in many places .

Campbell Barton ideasman42 at gmail.com
Sun Aug 28 04:54:26 CEST 2011


Revision: 39742
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=39742
Author:   campbellbarton
Date:     2011-08-28 02:54:26 +0000 (Sun, 28 Aug 2011)
Log Message:
-----------
remove implicit casts to doubles in the noise code, also use floating point math functions rather then double since the noise functions range is already limited by casting to ints in many places.
- gives a very small speedup.

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/intern/noise.c

Modified: trunk/blender/source/blender/blenlib/intern/noise.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/noise.c	2011-08-28 02:04:40 UTC (rev 39741)
+++ trunk/blender/source/blender/blenlib/intern/noise.c	2011-08-28 02:54:26 UTC (rev 39742)
@@ -251,7 +251,7 @@
 /* for use with BLI_gNoise()/BLI_gTurbulence(), returns unsigned improved perlin noise */
 static float newPerlinU(float x, float y, float z)
 {
-	return (0.5+0.5*newPerlin(x, y, z));
+	return (0.5f+0.5f*newPerlin(x, y, z));
 }
 
 
@@ -278,12 +278,12 @@
 	cn1=ox*ox; cn2=oy*oy; cn3=oz*oz;
 	cn4=jx*jx; cn5=jy*jy; cn6=jz*jz;
 
-	cn1= 1.0-3.0*cn1+2.0*cn1*ox;
-	cn2= 1.0-3.0*cn2+2.0*cn2*oy;
-	cn3= 1.0-3.0*cn3+2.0*cn3*oz;
-	cn4= 1.0-3.0*cn4-2.0*cn4*jx;
-	cn5= 1.0-3.0*cn5-2.0*cn5*jy;
-	cn6= 1.0-3.0*cn6-2.0*cn6*jz;
+	cn1= 1.0f-3.0f*cn1+2.0f*cn1*ox;
+	cn2= 1.0f-3.0f*cn2+2.0f*cn2*oy;
+	cn3= 1.0f-3.0f*cn3+2.0f*cn3*oz;
+	cn4= 1.0f-3.0f*cn4-2.0f*cn4*jx;
+	cn5= 1.0f-3.0f*cn5-2.0f*cn5*jy;
+	cn6= 1.0f-3.0f*cn6-2.0f*cn6*jz;
 
 	b00= hash[ hash[ix & 255]+(iy & 255)];
 	b10= hash[ hash[(ix+1) & 255]+(iy & 255)];
@@ -325,23 +325,23 @@
 		h=hashvectf+ 3*hash[b21+b11];
 		n+= i*(h[0]*jx+h[1]*jy+h[2]*jz);
 
-	if(n<0.0) n=0.0; else if(n>1.0) n=1.0;
+	if(n<0.0f) n=0.0f; else if(n>1.0f) n=1.0f;
 	return n;
 }
 
 /* as orgBlenderNoise(), returning signed noise */
 static float orgBlenderNoiseS(float x, float y, float z)
 {
-	return (2.0*orgBlenderNoise(x, y, z)-1.0);
+	return (2.0f*orgBlenderNoise(x, y, z)-1.0f);
 }
 
 /* separated from orgBlenderNoise above, with scaling */
 float BLI_hnoise(float noisesize, float x, float y, float z)
 {
-	if(noisesize==0.0) return 0.0;
-	x= (1.0+x)/noisesize;
-	y= (1.0+y)/noisesize;
-	z= (1.0+z)/noisesize;
+	if(noisesize==0.0f) return 0.0f;
+	x= (1.0f+x)/noisesize;
+	y= (1.0f+y)/noisesize;
+	z= (1.0f+z)/noisesize;
 	return orgBlenderNoise(x, y, z);
 }
 
@@ -357,7 +357,7 @@
 	
 		s+= d*BLI_hnoise(noisesize*d, x, y, z);
 		div+= d;
-		d*= 0.5;
+		d*= 0.5f;
 
 		nr--;
 	}
@@ -368,13 +368,13 @@
 {
 	float s, d= 0.5, div=1.0;
 
-	s= fabs( (-1.0+2.0*BLI_hnoise(noisesize, x, y, z)));
+	s= fabsf( (-1.0f+2.0f*BLI_hnoise(noisesize, x, y, z)));
 	
 	while(nr>0) {
 	
-		s+= fabs(d* (-1.0+2.0*BLI_hnoise(noisesize*d, x, y, z)));
+		s+= fabsf(d* (-1.0f+2.0f*BLI_hnoise(noisesize*d, x, y, z)));
 		div+= d;
-		d*= 0.5;
+		d*= 0.5f;
 		
 		nr--;
 	}
@@ -917,11 +917,11 @@
 #define DOT(a,b) (a[0] * b[0] + a[1] * b[1] + a[2] * b[2])
 
 #define setup(i,b0,b1,r0,r1) \
-		t = vec[i] + 10000.; \
+		t = vec[i] + 10000.0f; \
 		b0 = ((int)t) & 255; \
 		b1 = (b0+1) & 255; \
 		r0 = t - floorf(t); \
-		r1 = r0 - 1.;
+		r1 = r0 - 1.0f;
 
 
 static float noise3_perlin(float vec[3])
@@ -945,7 +945,7 @@
 
 #define at(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
 
-#define surve(t) ( t * t * (3. - 2. * t) )
+#define surve(t) ( t * t * (3.0f - 2.0f * t) )
 
 /* lerp moved to improved perlin above */
 
@@ -982,7 +982,7 @@
 
 	d = lerp(sy, a, b);          /* interpolate in y at hi x */
 
-	return 1.5 * lerp(sz, c, d); /* interpolate in z */
+	return 1.5f * lerp(sz, c, d); /* interpolate in z */
 }
 
 #if 0
@@ -996,7 +996,7 @@
 
 	t = 0;
 	for (freq = lofreq ; freq < hifreq ; freq *= 2.) {
-		t += fabs(noise3_perlin(p)) / freq;
+		t += fabsf(noise3_perlin(p)) / freq;
 		p[0] *= 2.;
 		p[1] *= 2.;
 		p[2] *= 2.;
@@ -1024,7 +1024,7 @@
 	v[0] = x;
 	v[1] = y;
 	v[2] = z;
-	return (0.5+0.5*noise3_perlin(v));
+	return (0.5f+0.5f*noise3_perlin(v));
 }
 
 /* *************** CALL AS: *************** */
@@ -1061,18 +1061,18 @@
 /* distance squared */
 static float dist_Squared(float x, float y, float z, float e) { (void)e; return (x*x + y*y + z*z); }
 /* real distance */
-static float dist_Real(float x, float y, float z, float e) { (void)e; return sqrt(x*x + y*y + z*z); }
+static float dist_Real(float x, float y, float z, float e) { (void)e; return sqrtf(x*x + y*y + z*z); }
 /* manhattan/taxicab/cityblock distance */
-static float dist_Manhattan(float x, float y, float z, float e) { (void)e; return (fabs(x) + fabs(y) + fabs(z)); }
+static float dist_Manhattan(float x, float y, float z, float e) { (void)e; return (fabsf(x) + fabsf(y) + fabsf(z)); }
 /* Chebychev */
 static float dist_Chebychev(float x, float y, float z, float e)
 {
 	float t;
 	(void)e;
 
-	x = fabs(x);
-	y = fabs(y);
-	z = fabs(z);
+	x = fabsf(x);
+	y = fabsf(y);
+	z = fabsf(z);
 	t = (x>y)?x:y;
 	return ((z>t)?z:t);
 }
@@ -1080,7 +1080,7 @@
 /* minkovsky preset exponent 0.5 */
 static float dist_MinkovskyH(float x, float y, float z, float e)
 {
-	float d = sqrt(fabs(x)) + sqrt(fabs(y)) + sqrt(fabs(z));
+	float d = sqrtf(fabsf(x)) + sqrtf(fabsf(y)) + sqrtf(fabsf(z));
 	(void)e;
 	return (d*d);
 }
@@ -1092,13 +1092,13 @@
 	x *= x;
 	y *= y;
 	z *= z;
-	return sqrt(sqrt(x*x + y*y + z*z));
+	return sqrtf(sqrtf(x*x + y*y + z*z));
 }
 
 /* Minkovsky, general case, slow, maybe too slow to be useful */
 static float dist_Minkovsky(float x, float y, float z, float e)
 {
-	return pow(pow(fabs(x), e) + pow(fabs(y), e) + pow(fabs(z), e), 1.0/e);
+	return powf(powf(fabsf(x), e) + powf(fabsf(y), e) + powf(fabsf(z), e), 1.0f/e);
 }
 
 
@@ -1224,35 +1224,35 @@
 {
 	float da[4], pa[12];
 	voronoi(x, y, z, da, pa, 1, 0);
-	return (2.0*da[0]-1.0);
+	return (2.0f*da[0]-1.0f);
 }
 
 static float voronoi_F2S(float x, float y, float z)
 {
 	float da[4], pa[12];
 	voronoi(x, y, z, da, pa, 1, 0);
-	return (2.0*da[1]-1.0);
+	return (2.0f*da[1]-1.0f);
 }
 
 static float voronoi_F3S(float x, float y, float z)
 {
 	float da[4], pa[12];
 	voronoi(x, y, z, da, pa, 1, 0);
-	return (2.0*da[2]-1.0);
+	return (2.0f*da[2]-1.0f);
 }
 
 static float voronoi_F4S(float x, float y, float z)
 {
 	float da[4], pa[12];
 	voronoi(x, y, z, da, pa, 1, 0);
-	return (2.0*da[3]-1.0);
+	return (2.0f*da[3]-1.0f);
 }
 
 static float voronoi_F1F2S(float x, float y, float z)
 {
 	float da[4], pa[12];
 	voronoi(x, y, z, da, pa, 1, 0);
-	return (2.0*(da[1]-da[0])-1.0);
+	return (2.0f*(da[1]-da[0])-1.0f);
 }
 
 /* Crackle type pattern, just a scale/clamp of F2-F1 */
@@ -1260,7 +1260,7 @@
 {
 	float t = 10*voronoi_F1F2(x, y, z);
 	if (t>1.f) return 1.f;
-	return (2.0*t-1.0);
+	return (2.0f*t-1.0f);
 }
 
 
@@ -1280,13 +1280,13 @@
 	int zi = (int)(floor(z));
 	unsigned int n = xi + yi*1301 + zi*314159;
 	n ^= (n<<13);
-	return ((float)(n*(n*n*15731 + 789221) + 1376312589) / 4294967296.0);
+	return ((float)(n*(n*n*15731 + 789221) + 1376312589) / 4294967296.0f);
 }
 
 /* idem, signed */
 float cellNoise(float x, float y, float z)
 {
-	return (2.0*cellNoiseU(x, y, z)-1.0);
+	return (2.0f*cellNoiseU(x, y, z)-1.0f);
 }
 
 /* returns a vector/point/color in ca, using point hasharray directly */
@@ -1349,14 +1349,14 @@
 		}
 	}
 
-	if (noisesize!=0.0) {
-		noisesize = 1.0/noisesize;
+	if (noisesize!=0.0f) {
+		noisesize = 1.0f/noisesize;
 		x *= noisesize;
 		y *= noisesize;
 		z *= noisesize;
 	}
 	
-	if (hard) return fabs(2.0*noisefunc(x, y, z)-1.0);
+	if (hard) return fabsf(2.0f*noisefunc(x, y, z)-1.0f);
 	return noisefunc(x, y, z);
 }
 
@@ -1403,17 +1403,17 @@
 			z += 1;
 	}
 
-	if (noisesize!=0.0) {
-		noisesize = 1.0/noisesize;
+	if (noisesize!=0.0f) {
+		noisesize = 1.0f/noisesize;
 		x *= noisesize;
 		y *= noisesize;
 		z *= noisesize;
 	}
 
 	sum = 0;
-	for (i=0;i<=oct;i++, amp*=0.5, fscale*=2) {
+	for (i=0;i<=oct;i++, amp*=0.5f, fscale*=2.0f) {
 		t = noisefunc(fscale*x, fscale*y, fscale*z);
-		if (hard) t = fabs(2.0*t-1.0);
+		if (hard) t = fabsf(2.0f*t-1.0f);
 		sum += t * amp;
 	}
 	
@@ -1439,7 +1439,7 @@
  */
 float mg_fBm(float x, float y, float z, float H, float lacunarity, float octaves, int noisebasis)
 {
-	float	rmd, value=0.0, pwr=1.0, pwHL=pow(lacunarity, -H);
+	float	rmd, value=0.0, pwr=1.0, pwHL=powf(lacunarity, -H);
 	int	i;
 
 	float (*noisefunc)(float, float, float);
@@ -1485,7 +1485,7 @@
 		z *= lacunarity;
 	}
 
-	rmd = octaves - floor(octaves);
+	rmd = octaves - floorf(octaves);
 	if (rmd!=0.f) value += rmd * noisefunc(x, y, z) * pwr;
 
 	return value;
@@ -1508,9 +1508,9 @@
 	* I modified it to something that made sense to me, so it might be wrong... */
 float mg_MultiFractal(float x, float y, float z, float H, float lacunarity, float octaves, int noisebasis)
 {
-	float	rmd, value=1.0, pwr=1.0, pwHL=pow(lacunarity, -H);
+	float	rmd, value=1.0, pwr=1.0, pwHL=powf(lacunarity, -H);
 	int i;
-	
+
 	float (*noisefunc)(float, float, float);
 	switch (noisebasis) {
 		case 1:
@@ -1547,14 +1547,14 @@
 	}
 
 	for (i=0; i<(int)octaves; i++) {
-		value *= (pwr * noisefunc(x, y, z) + 1.0);
+		value *= (pwr * noisefunc(x, y, z) + 1.0f);
 		pwr *= pwHL;
 		x *= lacunarity;
 		y *= lacunarity;
 		z *= lacunarity;
 	}
-	rmd = octaves - floor(octaves);
-	if (rmd!=0.0) value *= (rmd * noisefunc(x, y, z) * pwr + 1.0);
+	rmd = octaves - floorf(octaves);
+	if (rmd!=0.0f) value *= (rmd * noisefunc(x, y, z) * pwr + 1.0f);
 
 	return value;
 
@@ -1574,7 +1574,7 @@
 {
 	float	value, increment, rmd;
 	int i;
-	float pwHL = pow(lacunarity, -H);
+	float pwHL = powf(lacunarity, -H);
 	float pwr = pwHL;	/* starts with i=1 instead of 0 */
 
 	float (*noisefunc)(float, float, float);
@@ -1627,8 +1627,8 @@
 		z *= lacunarity;
 	}
 
-	rmd = octaves - floor(octaves);
-	if (rmd!=0.0) {
+	rmd = octaves - floorf(octaves);
+	if (rmd!=0.0f) {
 		increment = (noisefunc(x, y, z) + offset) * pwr * value;
 		value += rmd * increment;
 	}
@@ -1647,7 +1647,7 @@
 {
 	float result, signal, weight, rmd;
 	int i;
-	float pwHL = pow(lacunarity, -H);
+	float pwHL = powf(lacunarity, -H);
 	float pwr = pwHL;	/* starts with i=1 instead of 0 */
 	float (*noisefunc)(float, float, float);
 
@@ -1691,8 +1691,8 @@
 	y *= lacunarity;
 	z *= lacunarity;
 
-	for (i=1; (weight>0.001) && (i<(int)octaves); i++) {
-		if (weight>1.0)  weight=1.0;
+	for (i=1; (weight>0.001f) && (i<(int)octaves); i++) {
+		if (weight>1.0f)  weight=1.0f;
 		signal = (noisefunc(x, y, z) + offset) * pwr;
 		pwr *= pwHL;
 		result += weight * signal;
@@ -1702,7 +1702,7 @@
 		z *= lacunarity;
 	}
 
-	rmd = octaves - floor(octaves);
+	rmd = octaves - floorf(octaves);
 	if (rmd!=0.f) result += rmd * ((noisefunc(x, y, z) + offset) * pwr);
 
 	return result;
@@ -1722,7 +1722,7 @@
 {
 	float result, signal, weight;
 	int	i;
-	float pwHL = pow(lacunarity, -H);
+	float pwHL = powf(lacunarity, -H);
 	float pwr = pwHL;	/* starts with i=1 instead of 0 */
 	
 	float (*noisefunc)(float, float, float);
@@ -1760,7 +1760,7 @@
 		}
 	}
 
-	signal = offset - fabs(noisefunc(x, y, z));
+	signal = offset - fabsf(noisefunc(x, y, z));
 	signal *= signal;
 	result = signal;
 
@@ -1770,8 +1770,8 @@
 		y *= lacunarity;

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list