Browse Source

use double precision numbers in love.math.noise

megagrump 3 years ago
parent
commit
136c3cd277

+ 82 - 82
src/libraries/noise1234/noise1234.cpp

@@ -106,47 +106,47 @@ unsigned char Noise1234::perm[] = {151,160,137,91,90,15,
  * float SLnoise = (Noise1234::noise(x,y,z) + 1.0) * 0.5;
  */
 
-float  Noise1234::grad( int hash, float x ) {
+double  Noise1234::grad( int hash, double x ) {
     int h = hash & 15;
-    float grad = 1.0 + (h & 7);  // Gradient value 1.0, 2.0, ..., 8.0
+    double grad = 1.0 + (h & 7);   // Gradient value 1.0, 2.0, ..., 8.0
     if (h&8) grad = -grad;         // and a random sign for the gradient
     return ( grad * x );           // Multiply the gradient with the distance
 }
 
-float  Noise1234::grad( int hash, float x, float y ) {
-    int h = hash & 7;      // Convert low 3 bits of hash code
-    float u = h<4 ? x : y;  // into 8 simple gradient directions,
-    float v = h<4 ? y : x;  // and compute the dot product with (x,y).
+double  Noise1234::grad( int hash, double x, double y ) {
+    int h = hash & 7;        // Convert low 3 bits of hash code
+    double u = h<4 ? x : y;  // into 8 simple gradient directions,
+    double v = h<4 ? y : x;  // and compute the dot product with (x,y).
     return ((h&1)? -u : u) + ((h&2)? -2.0*v : 2.0*v);
 }
 
-float  Noise1234::grad( int hash, float x, float y , float z ) {
-    int h = hash & 15;     // Convert low 4 bits of hash code into 12 simple
-    float u = h<8 ? x : y; // gradient directions, and compute dot product.
-    float v = h<4 ? y : h==12||h==14 ? x : z; // Fix repeats at h = 12 to 15
+double  Noise1234::grad( int hash, double x, double y , double z ) {
+    int h = hash & 15;      // Convert low 4 bits of hash code into 12 simple
+    double u = h<8 ? x : y; // gradient directions, and compute dot product.
+    double v = h<4 ? y : h==12||h==14 ? x : z; // Fix repeats at h = 12 to 15
     return ((h&1)? -u : u) + ((h&2)? -v : v);
 }
 
-float  Noise1234::grad( int hash, float x, float y, float z, float t ) {
-    int h = hash & 31;      // Convert low 5 bits of hash code into 32 simple
-    float u = h<24 ? x : y; // gradient directions, and compute dot product.
-    float v = h<16 ? y : z;
-    float w = h<8 ? z : t;
+double  Noise1234::grad( int hash, double x, double y, double z, double t ) {
+    int h = hash & 31;       // Convert low 5 bits of hash code into 32 simple
+    double u = h<24 ? x : y; // gradient directions, and compute dot product.
+    double v = h<16 ? y : z;
+    double w = h<8 ? z : t;
     return ((h&1)? -u : u) + ((h&2)? -v : v) + ((h&4)? -w : w);
 }
 
 //---------------------------------------------------------------------
 /** 1D float Perlin noise, SL "noise()"
  */
-float Noise1234::noise( float x )
+float Noise1234::noise( double x )
 {
     int ix0, ix1;
-    float fx0, fx1;
-    float s, n0, n1;
+    double fx0, fx1;
+    double s, n0, n1;
 
     ix0 = FASTFLOOR( x ); // Integer part of x
-    fx0 = x - ix0;       // Fractional part of x
-    fx1 = fx0 - 1.0f;
+    fx0 = x - ix0;        // Fractional part of x
+    fx1 = fx0 - 1.0;
     ix1 = ( ix0+1 ) & 0xff;
     ix0 = ix0 & 0xff;    // Wrap to 0..255
 
@@ -160,17 +160,17 @@ float Noise1234::noise( float x )
 //---------------------------------------------------------------------
 /** 1D float Perlin periodic noise, SL "pnoise()"
  */
-float Noise1234::pnoise( float x, int px )
+float Noise1234::pnoise( double x, int px )
 {
     int ix0, ix1;
-    float fx0, fx1;
-    float s, n0, n1;
+    double fx0, fx1;
+    double s, n0, n1;
 
     ix0 = FASTFLOOR( x ); // Integer part of x
-    fx0 = x - ix0;       // Fractional part of x
-    fx1 = fx0 - 1.0f;
+    fx0 = x - ix0;        // Fractional part of x
+    fx1 = fx0 - 1.0;
     ix1 = (( ix0 + 1 ) % px) & 0xff; // Wrap to 0..px-1 *and* wrap to 0..255
-    ix0 = ( ix0 % px ) & 0xff;      // (because px might be greater than 256)
+    ix0 = ( ix0 % px ) & 0xff;       // (because px might be greater than 256)
 
     s = FADE( fx0 );
 
@@ -183,23 +183,23 @@ float Noise1234::pnoise( float x, int px )
 //---------------------------------------------------------------------
 /** 2D float Perlin noise.
  */
-float Noise1234::noise( float x, float y )
+float Noise1234::noise( double x, double y )
 {
     int ix0, iy0, ix1, iy1;
-    float fx0, fy0, fx1, fy1;
-    float s, t, nx0, nx1, n0, n1;
+    double fx0, fy0, fx1, fy1;
+    double s, t, nx0, nx1, n0, n1;
 
     ix0 = FASTFLOOR( x ); // Integer part of x
     iy0 = FASTFLOOR( y ); // Integer part of y
     fx0 = x - ix0;        // Fractional part of x
     fy0 = y - iy0;        // Fractional part of y
-    fx1 = fx0 - 1.0f;
-    fy1 = fy0 - 1.0f;
+    fx1 = fx0 - 1.0;
+    fy1 = fy0 - 1.0;
     ix1 = (ix0 + 1) & 0xff;  // Wrap to 0..255
     iy1 = (iy0 + 1) & 0xff;
     ix0 = ix0 & 0xff;
     iy0 = iy0 & 0xff;
-    
+
     t = FADE( fy0 );
     s = FADE( fx0 );
 
@@ -217,23 +217,23 @@ float Noise1234::noise( float x, float y )
 //---------------------------------------------------------------------
 /** 2D float Perlin periodic noise.
  */
-float Noise1234::pnoise( float x, float y, int px, int py )
+float Noise1234::pnoise( double x, double y, int px, int py )
 {
     int ix0, iy0, ix1, iy1;
-    float fx0, fy0, fx1, fy1;
-    float s, t, nx0, nx1, n0, n1;
+    double fx0, fy0, fx1, fy1;
+    double s, t, nx0, nx1, n0, n1;
 
     ix0 = FASTFLOOR( x ); // Integer part of x
     iy0 = FASTFLOOR( y ); // Integer part of y
     fx0 = x - ix0;        // Fractional part of x
     fy0 = y - iy0;        // Fractional part of y
-    fx1 = fx0 - 1.0f;
-    fy1 = fy0 - 1.0f;
+    fx1 = fx0 - 1.0;
+    fy1 = fy0 - 1.0;
     ix1 = (( ix0 + 1 ) % px) & 0xff;  // Wrap to 0..px-1 and wrap to 0..255
     iy1 = (( iy0 + 1 ) % py) & 0xff;  // Wrap to 0..py-1 and wrap to 0..255
     ix0 = ( ix0 % px ) & 0xff;
     iy0 = ( iy0 % py ) & 0xff;
-    
+
     t = FADE( fy0 );
     s = FADE( fx0 );
 
@@ -252,12 +252,12 @@ float Noise1234::pnoise( float x, float y, int px, int py )
 //---------------------------------------------------------------------
 /** 3D float Perlin noise.
  */
-float Noise1234::noise( float x, float y, float z )
+float Noise1234::noise( double x, double y, double z )
 {
     int ix0, iy0, ix1, iy1, iz0, iz1;
-    float fx0, fy0, fz0, fx1, fy1, fz1;
-    float s, t, r;
-    float nxy0, nxy1, nx0, nx1, n0, n1;
+    double fx0, fy0, fz0, fx1, fy1, fz1;
+    double s, t, r;
+    double nxy0, nxy1, nx0, nx1, n0, n1;
 
     ix0 = FASTFLOOR( x ); // Integer part of x
     iy0 = FASTFLOOR( y ); // Integer part of y
@@ -265,16 +265,16 @@ float Noise1234::noise( float x, float y, float z )
     fx0 = x - ix0;        // Fractional part of x
     fy0 = y - iy0;        // Fractional part of y
     fz0 = z - iz0;        // Fractional part of z
-    fx1 = fx0 - 1.0f;
-    fy1 = fy0 - 1.0f;
-    fz1 = fz0 - 1.0f;
+    fx1 = fx0 - 1.0;
+    fy1 = fy0 - 1.0;
+    fz1 = fz0 - 1.0;
     ix1 = ( ix0 + 1 ) & 0xff; // Wrap to 0..255
     iy1 = ( iy0 + 1 ) & 0xff;
     iz1 = ( iz0 + 1 ) & 0xff;
     ix0 = ix0 & 0xff;
     iy0 = iy0 & 0xff;
     iz0 = iz0 & 0xff;
-    
+
     r = FADE( fz0 );
     t = FADE( fy0 );
     s = FADE( fx0 );
@@ -298,19 +298,19 @@ float Noise1234::noise( float x, float y, float z )
     nx1 = LERP( r, nxy0, nxy1 );
 
     n1 = LERP( t, nx0, nx1 );
-    
+
     return 0.936f * ( LERP( s, n0, n1 ) );
 }
 
 //---------------------------------------------------------------------
 /** 3D float Perlin periodic noise.
  */
-float Noise1234::pnoise( float x, float y, float z, int px, int py, int pz )
+float Noise1234::pnoise( double x, double y, double z, int px, int py, int pz )
 {
     int ix0, iy0, ix1, iy1, iz0, iz1;
-    float fx0, fy0, fz0, fx1, fy1, fz1;
-    float s, t, r;
-    float nxy0, nxy1, nx0, nx1, n0, n1;
+    double fx0, fy0, fz0, fx1, fy1, fz1;
+    double s, t, r;
+    double nxy0, nxy1, nx0, nx1, n0, n1;
 
     ix0 = FASTFLOOR( x ); // Integer part of x
     iy0 = FASTFLOOR( y ); // Integer part of y
@@ -318,16 +318,16 @@ float Noise1234::pnoise( float x, float y, float z, int px, int py, int pz )
     fx0 = x - ix0;        // Fractional part of x
     fy0 = y - iy0;        // Fractional part of y
     fz0 = z - iz0;        // Fractional part of z
-    fx1 = fx0 - 1.0f;
-    fy1 = fy0 - 1.0f;
-    fz1 = fz0 - 1.0f;
+    fx1 = fx0 - 1.0;
+    fy1 = fy0 - 1.0;
+    fz1 = fz0 - 1.0;
     ix1 = (( ix0 + 1 ) % px ) & 0xff; // Wrap to 0..px-1 and wrap to 0..255
     iy1 = (( iy0 + 1 ) % py ) & 0xff; // Wrap to 0..py-1 and wrap to 0..255
     iz1 = (( iz0 + 1 ) % pz ) & 0xff; // Wrap to 0..pz-1 and wrap to 0..255
     ix0 = ( ix0 % px ) & 0xff;
     iy0 = ( iy0 % py ) & 0xff;
     iz0 = ( iz0 % pz ) & 0xff;
-    
+
     r = FADE( fz0 );
     t = FADE( fy0 );
     s = FADE( fx0 );
@@ -351,7 +351,7 @@ float Noise1234::pnoise( float x, float y, float z, int px, int py, int pz )
     nx1 = LERP( r, nxy0, nxy1 );
 
     n1 = LERP( t, nx0, nx1 );
-    
+
     return 0.936f * ( LERP( s, n0, n1 ) );
 }
 
@@ -360,12 +360,12 @@ float Noise1234::pnoise( float x, float y, float z, int px, int py, int pz )
 /** 4D float Perlin noise.
  */
 
-float Noise1234::noise( float x, float y, float z, float w )
+float Noise1234::noise( double x, double y, double z, double w )
 {
     int ix0, iy0, iz0, iw0, ix1, iy1, iz1, iw1;
-    float fx0, fy0, fz0, fw0, fx1, fy1, fz1, fw1;
-    float s, t, r, q;
-    float nxyz0, nxyz1, nxy0, nxy1, nx0, nx1, n0, n1;
+    double fx0, fy0, fz0, fw0, fx1, fy1, fz1, fw1;
+    double s, t, r, q;
+    double nxyz0, nxyz1, nxy0, nxy1, nx0, nx1, n0, n1;
 
     ix0 = FASTFLOOR( x ); // Integer part of x
     iy0 = FASTFLOOR( y ); // Integer part of y
@@ -375,10 +375,10 @@ float Noise1234::noise( float x, float y, float z, float w )
     fy0 = y - iy0;        // Fractional part of y
     fz0 = z - iz0;        // Fractional part of z
     fw0 = w - iw0;        // Fractional part of w
-    fx1 = fx0 - 1.0f;
-    fy1 = fy0 - 1.0f;
-    fz1 = fz0 - 1.0f;
-    fw1 = fw0 - 1.0f;
+    fx1 = fx0 - 1.0;
+    fy1 = fy0 - 1.0;
+    fz1 = fz0 - 1.0;
+    fw1 = fw0 - 1.0;
     ix1 = ( ix0 + 1 ) & 0xff;  // Wrap to 0..255
     iy1 = ( iy0 + 1 ) & 0xff;
     iz1 = ( iz0 + 1 ) & 0xff;
@@ -396,17 +396,17 @@ float Noise1234::noise( float x, float y, float z, float w )
     nxyz0 = grad(perm[ix0 + perm[iy0 + perm[iz0 + perm[iw0]]]], fx0, fy0, fz0, fw0);
     nxyz1 = grad(perm[ix0 + perm[iy0 + perm[iz0 + perm[iw1]]]], fx0, fy0, fz0, fw1);
     nxy0 = LERP( q, nxyz0, nxyz1 );
-        
+
     nxyz0 = grad(perm[ix0 + perm[iy0 + perm[iz1 + perm[iw0]]]], fx0, fy0, fz1, fw0);
     nxyz1 = grad(perm[ix0 + perm[iy0 + perm[iz1 + perm[iw1]]]], fx0, fy0, fz1, fw1);
     nxy1 = LERP( q, nxyz0, nxyz1 );
-        
+
     nx0 = LERP ( r, nxy0, nxy1 );
 
     nxyz0 = grad(perm[ix0 + perm[iy1 + perm[iz0 + perm[iw0]]]], fx0, fy1, fz0, fw0);
     nxyz1 = grad(perm[ix0 + perm[iy1 + perm[iz0 + perm[iw1]]]], fx0, fy1, fz0, fw1);
     nxy0 = LERP( q, nxyz0, nxyz1 );
-        
+
     nxyz0 = grad(perm[ix0 + perm[iy1 + perm[iz1 + perm[iw0]]]], fx0, fy1, fz1, fw0);
     nxyz1 = grad(perm[ix0 + perm[iy1 + perm[iz1 + perm[iw1]]]], fx0, fy1, fz1, fw1);
     nxy1 = LERP( q, nxyz0, nxyz1 );
@@ -418,7 +418,7 @@ float Noise1234::noise( float x, float y, float z, float w )
     nxyz0 = grad(perm[ix1 + perm[iy0 + perm[iz0 + perm[iw0]]]], fx1, fy0, fz0, fw0);
     nxyz1 = grad(perm[ix1 + perm[iy0 + perm[iz0 + perm[iw1]]]], fx1, fy0, fz0, fw1);
     nxy0 = LERP( q, nxyz0, nxyz1 );
-        
+
     nxyz0 = grad(perm[ix1 + perm[iy0 + perm[iz1 + perm[iw0]]]], fx1, fy0, fz1, fw0);
     nxyz1 = grad(perm[ix1 + perm[iy0 + perm[iz1 + perm[iw1]]]], fx1, fy0, fz1, fw1);
     nxy1 = LERP( q, nxyz0, nxyz1 );
@@ -428,7 +428,7 @@ float Noise1234::noise( float x, float y, float z, float w )
     nxyz0 = grad(perm[ix1 + perm[iy1 + perm[iz0 + perm[iw0]]]], fx1, fy1, fz0, fw0);
     nxyz1 = grad(perm[ix1 + perm[iy1 + perm[iz0 + perm[iw1]]]], fx1, fy1, fz0, fw1);
     nxy0 = LERP( q, nxyz0, nxyz1 );
-        
+
     nxyz0 = grad(perm[ix1 + perm[iy1 + perm[iz1 + perm[iw0]]]], fx1, fy1, fz1, fw0);
     nxyz1 = grad(perm[ix1 + perm[iy1 + perm[iz1 + perm[iw1]]]], fx1, fy1, fz1, fw1);
     nxy1 = LERP( q, nxyz0, nxyz1 );
@@ -444,13 +444,13 @@ float Noise1234::noise( float x, float y, float z, float w )
 /** 4D float Perlin periodic noise.
  */
 
-float Noise1234::pnoise( float x, float y, float z, float w,
+float Noise1234::pnoise( double x, double y, double z, double w,
                             int px, int py, int pz, int pw )
 {
     int ix0, iy0, iz0, iw0, ix1, iy1, iz1, iw1;
-    float fx0, fy0, fz0, fw0, fx1, fy1, fz1, fw1;
-    float s, t, r, q;
-    float nxyz0, nxyz1, nxy0, nxy1, nx0, nx1, n0, n1;
+    double fx0, fy0, fz0, fw0, fx1, fy1, fz1, fw1;
+    double s, t, r, q;
+    double nxyz0, nxyz1, nxy0, nxy1, nx0, nx1, n0, n1;
 
     ix0 = FASTFLOOR( x ); // Integer part of x
     iy0 = FASTFLOOR( y ); // Integer part of y
@@ -460,10 +460,10 @@ float Noise1234::pnoise( float x, float y, float z, float w,
     fy0 = y - iy0;        // Fractional part of y
     fz0 = z - iz0;        // Fractional part of z
     fw0 = w - iw0;        // Fractional part of w
-    fx1 = fx0 - 1.0f;
-    fy1 = fy0 - 1.0f;
-    fz1 = fz0 - 1.0f;
-    fw1 = fw0 - 1.0f;
+    fx1 = fx0 - 1.0;
+    fy1 = fy0 - 1.0;
+    fz1 = fz0 - 1.0;
+    fw1 = fw0 - 1.0;
     ix1 = (( ix0 + 1 ) % px ) & 0xff;  // Wrap to 0..px-1 and wrap to 0..255
     iy1 = (( iy0 + 1 ) % py ) & 0xff;  // Wrap to 0..py-1 and wrap to 0..255
     iz1 = (( iz0 + 1 ) % pz ) & 0xff;  // Wrap to 0..pz-1 and wrap to 0..255
@@ -481,17 +481,17 @@ float Noise1234::pnoise( float x, float y, float z, float w,
     nxyz0 = grad(perm[ix0 + perm[iy0 + perm[iz0 + perm[iw0]]]], fx0, fy0, fz0, fw0);
     nxyz1 = grad(perm[ix0 + perm[iy0 + perm[iz0 + perm[iw1]]]], fx0, fy0, fz0, fw1);
     nxy0 = LERP( q, nxyz0, nxyz1 );
-        
+
     nxyz0 = grad(perm[ix0 + perm[iy0 + perm[iz1 + perm[iw0]]]], fx0, fy0, fz1, fw0);
     nxyz1 = grad(perm[ix0 + perm[iy0 + perm[iz1 + perm[iw1]]]], fx0, fy0, fz1, fw1);
     nxy1 = LERP( q, nxyz0, nxyz1 );
-        
+
     nx0 = LERP ( r, nxy0, nxy1 );
 
     nxyz0 = grad(perm[ix0 + perm[iy1 + perm[iz0 + perm[iw0]]]], fx0, fy1, fz0, fw0);
     nxyz1 = grad(perm[ix0 + perm[iy1 + perm[iz0 + perm[iw1]]]], fx0, fy1, fz0, fw1);
     nxy0 = LERP( q, nxyz0, nxyz1 );
-        
+
     nxyz0 = grad(perm[ix0 + perm[iy1 + perm[iz1 + perm[iw0]]]], fx0, fy1, fz1, fw0);
     nxyz1 = grad(perm[ix0 + perm[iy1 + perm[iz1 + perm[iw1]]]], fx0, fy1, fz1, fw1);
     nxy1 = LERP( q, nxyz0, nxyz1 );
@@ -503,7 +503,7 @@ float Noise1234::pnoise( float x, float y, float z, float w,
     nxyz0 = grad(perm[ix1 + perm[iy0 + perm[iz0 + perm[iw0]]]], fx1, fy0, fz0, fw0);
     nxyz1 = grad(perm[ix1 + perm[iy0 + perm[iz0 + perm[iw1]]]], fx1, fy0, fz0, fw1);
     nxy0 = LERP( q, nxyz0, nxyz1 );
-        
+
     nxyz0 = grad(perm[ix1 + perm[iy0 + perm[iz1 + perm[iw0]]]], fx1, fy0, fz1, fw0);
     nxyz1 = grad(perm[ix1 + perm[iy0 + perm[iz1 + perm[iw1]]]], fx1, fy0, fz1, fw1);
     nxy1 = LERP( q, nxyz0, nxyz1 );
@@ -513,7 +513,7 @@ float Noise1234::pnoise( float x, float y, float z, float w,
     nxyz0 = grad(perm[ix1 + perm[iy1 + perm[iz0 + perm[iw0]]]], fx1, fy1, fz0, fw0);
     nxyz1 = grad(perm[ix1 + perm[iy1 + perm[iz0 + perm[iw1]]]], fx1, fy1, fz0, fw1);
     nxy0 = LERP( q, nxyz0, nxyz1 );
-        
+
     nxyz0 = grad(perm[ix1 + perm[iy1 + perm[iz1 + perm[iw0]]]], fx1, fy1, fz1, fw0);
     nxyz1 = grad(perm[ix1 + perm[iy1 + perm[iz1 + perm[iw1]]]], fx1, fy1, fz1, fw1);
     nxy1 = LERP( q, nxyz0, nxyz1 );

+ 12 - 16
src/libraries/noise1234/noise1234.h

@@ -20,10 +20,6 @@
  * This is a clean, fast, modern and free Perlin noise class in C++.
  * Being a stand-alone class with no external dependencies, it is
  * highly reusable without source code modifications.
- *
- * Note:
- * Replacing the "float" type with "double" can actually make this run faster
- * on some platforms. A templatized version of Noise1234 could be useful.
  */
 
 class Noise1234 {
@@ -34,24 +30,24 @@ class Noise1234 {
 
 /** 1D, 2D, 3D and 4D float Perlin noise, SL "noise()"
  */
-    static float noise( float x );
-    static float noise( float x, float y );
-    static float noise( float x, float y, float z );
-    static float noise( float x, float y, float z, float w );
+    static float noise( double x );
+    static float noise( double x, double y );
+    static float noise( double x, double y, double z );
+    static float noise( double x, double y, double z, double w );
 
 /** 1D, 2D, 3D and 4D float Perlin periodic noise, SL "pnoise()"
  */
-    static float pnoise( float x, int px );
-    static float pnoise( float x, float y, int px, int py );
-    static float pnoise( float x, float y, float z, int px, int py, int pz );
-    static float pnoise( float x, float y, float z, float w,
+    static float pnoise( double x, int px );
+    static float pnoise( double x, double y, int px, int py );
+    static float pnoise( double x, double y, double z, int px, int py, int pz );
+    static float pnoise( double x, double y, double z, double w,
                               int px, int py, int pz, int pw );
 
   private:
     static unsigned char perm[];
-    static float  grad( int hash, float x );
-    static float  grad( int hash, float x, float y );
-    static float  grad( int hash, float x, float y , float z );
-    static float  grad( int hash, float x, float y, float z, float t );
+    static double  grad( int hash, double x );
+    static double  grad( int hash, double x, double y );
+    static double  grad( int hash, double x, double y , double z );
+    static double  grad( int hash, double x, double y, double z, double t );
 
 };

+ 34 - 36
src/libraries/noise1234/simplexnoise1234.cpp

@@ -85,7 +85,7 @@ unsigned char SimplexNoise1234::perm[512] = {151,160,137,91,90,15,
   129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
   251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
   49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
-  138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180 
+  138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
 };
 
 //---------------------------------------------------------------------
@@ -103,37 +103,35 @@ unsigned char SimplexNoise1234::perm[512] = {151,160,137,91,90,15,
  * float SLnoise = (SimplexNoise1234::noise(x,y,z) + 1.0) * 0.5;
  */
 
-float  SimplexNoise1234::grad( int hash, float x ) {
+double  SimplexNoise1234::grad( int hash, double x ) {
     int h = hash & 15;
-    float grad = 1.0f + (h & 7);   // Gradient value 1.0, 2.0, ..., 8.0
+    double grad = 1.0 + (h & 7);   // Gradient value 1.0, 2.0, ..., 8.0
     if (h&8) grad = -grad;         // Set a random sign for the gradient
     return ( grad * x );           // Multiply the gradient with the distance
 }
 
-float  SimplexNoise1234::grad( int hash, float x, float y ) {
+double  SimplexNoise1234::grad( int hash, double x, double y ) {
     int h = hash & 7;      // Convert low 3 bits of hash code
-    float u = h<4 ? x : y;  // into 8 simple gradient directions,
-    float v = h<4 ? y : x;  // and compute the dot product with (x,y).
-    return ((h&1)? -u : u) + ((h&2)? -2.0f*v : 2.0f*v);
+    double u = h<4 ? x : y;  // into 8 simple gradient directions,
+    double v = h<4 ? y : x;  // and compute the dot product with (x,y).
+    return ((h&1)? -u : u) + ((h&2)? -2.0*v : 2.0*v);
 }
 
 // 1D simplex noise
-float SimplexNoise1234::noise(float x) {
+float SimplexNoise1234::noise(double x) {
 
   int i0 = FASTFLOOR(x);
   int i1 = i0 + 1;
-  float x0 = x - i0;
-  float x1 = x0 - 1.0f;
+  double x0 = x - i0;
+  double x1 = x0 - 1.0;
 
-  float n0, n1;
+  double n0, n1;
 
-  float t0 = 1.0f - x0*x0;
-//  if(t0 < 0.0f) t0 = 0.0f;
+  double t0 = 1.0 - x0*x0;
   t0 *= t0;
   n0 = t0 * t0 * grad(perm[i0 & 0xff], x0);
 
-  float t1 = 1.0f - x1*x1;
-//  if(t1 < 0.0f) t1 = 0.0f;
+  double t1 = 1.0 - x1*x1;
   t1 *= t1;
   n1 = t1 * t1 * grad(perm[i1 & 0xff], x1);
   // The maximum value of this noise is 8*(3/4)^4 = 2.53125
@@ -143,25 +141,25 @@ float SimplexNoise1234::noise(float x) {
 }
 
 // 2D simplex noise
-float SimplexNoise1234::noise(float x, float y) {
+float SimplexNoise1234::noise(double x, double y) {
 
 #define F2 0.366025403 // F2 = 0.5*(sqrt(3.0)-1.0)
 #define G2 0.211324865 // G2 = (3.0-Math.sqrt(3.0))/6.0
 
-    float n0, n1, n2; // Noise contributions from the three corners
+    double n0, n1, n2; // Noise contributions from the three corners
 
     // Skew the input space to determine which simplex cell we're in
-    float s = (x+y)*F2; // Hairy factor for 2D
-    float xs = x + s;
-    float ys = y + s;
+    double s = (x+y)*F2; // Hairy factor for 2D
+    double xs = x + s;
+    double ys = y + s;
     int i = FASTFLOOR(xs);
     int j = FASTFLOOR(ys);
 
-    float t = (float)(i+j)*G2;
-    float X0 = i-t; // Unskew the cell origin back to (x,y) space
-    float Y0 = j-t;
-    float x0 = x-X0; // The x,y distances from the cell origin
-    float y0 = y-Y0;
+    double t = (i+j)*G2;
+    double X0 = i-t; // Unskew the cell origin back to (x,y) space
+    double Y0 = j-t;
+    double x0 = x-X0; // The x,y distances from the cell origin
+    double y0 = y-Y0;
 
     // For the 2D case, the simplex shape is an equilateral triangle.
     // Determine which simplex we are in.
@@ -173,32 +171,32 @@ float SimplexNoise1234::noise(float x, float y) {
     // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
     // c = (3-sqrt(3))/6
 
-    float x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
-    float y1 = y0 - j1 + G2;
-    float x2 = x0 - 1.0f + 2.0f * G2; // Offsets for last corner in (x,y) unskewed coords
-    float y2 = y0 - 1.0f + 2.0f * G2;
+    double x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
+    double y1 = y0 - j1 + G2;
+    double x2 = x0 - 1.0 + 2.0 * G2; // Offsets for last corner in (x,y) unskewed coords
+    double y2 = y0 - 1.0 + 2.0 * G2;
 
     // Wrap the integer indices at 256, to avoid indexing perm[] out of bounds
     int ii = i & 0xff;
     int jj = j & 0xff;
 
     // Calculate the contribution from the three corners
-    float t0 = 0.5f - x0*x0-y0*y0;
-    if(t0 < 0.0f) n0 = 0.0f;
+    double t0 = 0.5 - x0*x0-y0*y0;
+    if(t0 < 0.0) n0 = 0.0;
     else {
       t0 *= t0;
-      n0 = t0 * t0 * grad(perm[ii+perm[jj]], x0, y0); 
+      n0 = t0 * t0 * grad(perm[ii+perm[jj]], x0, y0);
     }
 
-    float t1 = 0.5f - x1*x1-y1*y1;
-    if(t1 < 0.0f) n1 = 0.0f;
+    double t1 = 0.5 - x1*x1-y1*y1;
+    if(t1 < 0.0) n1 = 0.0;
     else {
       t1 *= t1;
       n1 = t1 * t1 * grad(perm[ii+i1+perm[jj+j1]], x1, y1);
     }
 
-    float t2 = 0.5f - x2*x2-y2*y2;
-    if(t2 < 0.0f) n2 = 0.0f;
+    double t2 = 0.5 - x2*x2-y2*y2;
+    if(t2 < 0.0) n2 = 0.0;
     else {
       t2 *= t2;
       n2 = t2 * t2 * grad(perm[ii+1+perm[jj+1]], x2, y2);

+ 4 - 9
src/libraries/noise1234/simplexnoise1234.h

@@ -25,11 +25,6 @@
  * This is a clean, fast, modern and free Perlin Simplex noise class in C++.
  * Being a stand-alone class with no external dependencies, it is
  * highly reusable without source code modifications.
- *
- *
- * Note:
- * Replacing the "float" type with "double" can actually make this run faster
- * on some platforms. A templatized version of SimplexNoise1234 could be useful.
  */
 
 class SimplexNoise1234 {
@@ -40,12 +35,12 @@ class SimplexNoise1234 {
 
 /** 1D and 2D float Perlin noise
  */
-    static float noise( float x );
-    static float noise( float x, float y );
+    static float noise( double x );
+    static float noise( double x, double y );
 
   private:
     static unsigned char perm[];
-    static float  grad( int hash, float x );
-    static float  grad( int hash, float x, float y );
+    static double  grad( int hash, double x );
+    static double  grad( int hash, double x, double y );
 
 };

+ 4 - 4
src/modules/math/MathModule.h

@@ -132,12 +132,12 @@ private:
 }; // Math
 
 
-static inline float noise1(float x)
+static inline float noise1(double x)
 {
 	return SimplexNoise1234::noise(x) * 0.5f + 0.5f;
 }
 
-static inline float noise2(float x, float y)
+static inline float noise2(double x, double y)
 {
 	return SimplexNoise1234::noise(x, y) * 0.5f + 0.5f;
 }
@@ -145,12 +145,12 @@ static inline float noise2(float x, float y)
 // Perlin noise is used instead of Simplex noise in the 3D and 4D cases to avoid
 // patent issues.
 
-static inline float noise3(float x, float y, float z)
+static inline float noise3(double x, double y, double z)
 {
 	return Noise1234::noise(x, y, z) * 0.5f + 0.5f;
 }
 
-static inline float noise4(float x, float y, float z, float w)
+static inline float noise4(double x, double y, double z, double w)
 {
 	return Noise1234::noise(x, y, z, w) * 0.5f + 0.5f;
 }

+ 6 - 6
src/modules/math/wrap_Math.cpp

@@ -319,10 +319,10 @@ int w_linearToGamma(lua_State *L)
 int w_noise(lua_State *L)
 {
 	int nargs = std::min(std::max(lua_gettop(L), 1), 4);
-	float args[4];
+	double args[4];
 
 	for (int i = 0; i < nargs; i++)
-		args[i] = (float) luaL_checknumber(L, i + 1);
+		args[i] = luaL_checknumber(L, i + 1);
 
 	float val = 0.0f;
 
@@ -349,10 +349,10 @@ int w_noise(lua_State *L)
 // C functions in a struct, necessary for the FFI versions of math functions.
 struct FFI_Math
 {
-	float (*noise1)(float x);
-	float (*noise2)(float x, float y);
-	float (*noise3)(float x, float y, float z);
-	float (*noise4)(float x, float y, float z, float w);
+	float (*noise1)(double x);
+	float (*noise2)(double x, double y);
+	float (*noise3)(double x, double y, double z);
+	float (*noise4)(double x, double y, double z, double w);
 
 	float (*gammaToLinear)(float c);
 	float (*linearToGamma)(float c);

+ 4 - 4
src/modules/math/wrap_Math.lua

@@ -93,10 +93,10 @@ if not status then return end
 pcall(ffi.cdef, [[
 typedef struct FFI_Math
 {
-	float (*noise1)(float x);
-	float (*noise2)(float x, float y);
-	float (*noise3)(float x, float y, float z);
-	float (*noise4)(float x, float y, float z, float w);
+	float (*noise1)(double x);
+	float (*noise2)(double x, double y);
+	float (*noise3)(double x, double y, double z);
+	float (*noise4)(double x, double y, double z, double w);
 
 	float (*gammaToLinear)(float c);
 	float (*linearToGamma)(float c);