33 return _mm_cvtsd_f64(_mm_floor_sd(_mm_set_sd(0.0), _mm_set_sd(val)));
38 return _mm_cvtsd_f64(_mm_round_sd(_mm_set_sd(0.0), _mm_set_sd(val), _MM_FROUND_TO_NEAREST_INT));
46double s_curve(
double t) {
return t * t * t * (t * (6 * t - 15) + 10); }
53 for (
int k = 0; k < d; k++) {
54 static const uint32_t M = 1664525, C = 1013904223;
55 seed = seed * M +
index[k] + C;
59 seed ^= (seed << 7) & 0x9d2c5680UL;
60 seed ^= (seed << 15) & 0xefc60000UL;
63 return (((seed & 0xff0000) >> 4) + (seed & 0xff)) & 0xff;
75 for (
int k = 0; k < d; k++) {
76 static const uint32_t M = 1664525, C = 1013904223;
77 u1.i = u1.i * M +
index[k] + C;
81 u1.i ^= (u1.i << 7) & 0x9d2c5680U;
82 u1.i ^= (u1.i << 15) & 0xefc60000U;
86 u2.c[2] = p[u1.c[1] + u2.c[3]];
87 u2.c[1] = p[u1.c[2] + u2.c[2]];
88 u2.c[0] = p[u1.c[3] + u2.c[1]];
94template <
int d,
class T,
bool periodic>
99 for (
int k = 0; k < d; k++) {
103 index[k] %= period[k];
106 weights[0][k] = X[k] -
f;
107 weights[1][k] = weights[0][k] - 1;
110 const int num = 1 << d;
112 for (
int dummy = 0; dummy < num; dummy++) {
115 for (
int k = 0; k < d; k++) {
116 offset[k] = ((dummy & (1 << k)) != 0);
117 latticeIndex[k] =
index[k] + offset[k];
120 int lookup = hashReduceChar<d>(latticeIndex);
122 for (
int k = 0; k < d; k++) {
124 double weight = weights[offset[k]][k];
125 val += grad * weight;
131 for (
int k = 0; k < d; k++) alphas[k] =
s_curve(weights[0][k]);
133 for (
int newd = d - 1; newd >= 0; newd--) {
134 int newnum = 1 << newd;
135 int k = (d - newd - 1);
137 T beta = T(1) - alphas[k];
138 for (
int dummy = 0; dummy < newnum; dummy++) {
139 int index = dummy * (1 << (d - newd));
140 int otherIndex =
index + (1 << k);
142 vals[
index] = beta * vals[
index] + alpha * vals[otherIndex];
153template <
int d_in,
int d_out,
class T>
155 uint32_t
index[d_in];
157 for (
int k = 0; k < d_in; k++)
index[k] = uint32_t(
floorSSE(in[k]));
159 out[dim] = hashReduce<d_in>(
index) * (1.0 / 0xffffffffu);
160 if (++dim >= d_out)
break;
161 for (
int k = 0; k < d_in; k++)
index[k] += 1000;
166template <
int d_in,
int d_out,
class T>
169 for (
int i = 0; i < d_in; i++) P[i] = in[i];
173 out[i] = noiseHelper<d_in, T, false>(P);
174 if (++i >= d_out)
break;
175 for (
int k = 0; k < d_out; k++) P[k] += (T)1000;
180template <
int d_in,
int d_out,
class T>
181void PNoise(
const T* in,
const int* period, T* out) {
183 for (
int i = 0; i < d_in; i++) P[i] = in[i];
187 out[i] = noiseHelper<d_in, T, true>(P, period);
188 if (++i >= d_out)
break;
189 for (
int k = 0; k < d_out; k++) P[k] += (T)1000;
195template <
int d_in,
int d_out,
bool turbulence,
class T>
196void FBM(
const T* in, T* out,
int octaves, T lacunarity, T gain) {
198 for (
int i = 0; i < d_in; i++) P[i] = in[i];
201 for (
int k = 0; k < d_out; k++) out[k] = 0;
204 T localResult[d_out];
205 Noise<d_in, d_out>(P, localResult);
207 for (
int k = 0; k < d_out; k++) out[k] += fabs(localResult[k]) * scale;
209 for (
int k = 0; k < d_out; k++) out[k] += localResult[k] * scale;
210 if (++octave >= octaves)
break;
212 for (
int k = 0; k < d_in; k++) {
238int main(
int argc,
char* argv[]) {
241 for (
int i = 0; i < 10000000; i++) {
242 T foo[3] = {.3, .3, .3};
244 sum += SeExpr2::noiseHelper<3, T, false>(foo);
int main(int argc, char *argv[])
template void FBM< 3, 3, true, double >(const double *, double *, int, double, double)
template void Noise< 4, 3, double >(const double *, double *)
template void FBM< 3, 1, false, double >(const double *, double *, int, double, double)
template void FBM< 4, 3, false, double >(const double *, double *, int, double, double)
template void CellNoise< 3, 3, double >(const double *, double *)
uint32_t hashReduce(uint32_t index[d])
Does a hash reduce to an integer.
T noiseHelper(const T *X, const int *period=0)
Noise with d_in dimensional domain, 1 dimensional abcissa.
template void FBM< 3, 3, false, double >(const double *, double *, int, double, double)
template void Noise< 3, 3, double >(const double *, double *)
template void CellNoise< 3, 1, double >(const double *, double *)
template void PNoise< 3, 1, double >(const double *, const int *, double *)
void PNoise(const T *in, const int *period, T *out)
Periodic Noise with d_in dimensional domain, d_out dimensional abcissa.
unsigned char hashReduceChar(int index[d])
Does a hash reduce to a character.
template void Noise< 1, 1, double >(const double *, double *)
void Noise(const T *in, T *out)
Noise with d_in dimensional domain, d_out dimensional abcissa.
template void Noise< 4, 1, double >(const double *, double *)
void FBM(const T *in, T *out, int octaves, T lacunarity, T gain)
Fractional Brownian Motion. If turbulence is true then turbulence computed.
template void Noise< 2, 1, double >(const double *, double *)
double turbulence(int n, const Vec3d *args)
template void FBM< 3, 1, true, double >(const double *, double *, int, double, double)
void CellNoise(const T *in, T *out)
Computes cellular noise (non-interpolated piecewise constant cell random values)
template void Noise< 3, 1, double >(const double *, double *)
template void FBM< 4, 1, false, double >(const double *, double *, int, double, double)
double s_curve(double t)
This is the Quintic interpolant from Perlin's Improved Noise Paper.
The result is computed int int< br >< div style="margin-left: 40px;"> Picks values randomly between loRange and hiRange based on supplied index(which is automatically hashed).  
with numParticles numAttributes A variable block contains variable names and types but doesn t care what the values are< pre > void f(const std::string &s, MyParticleData *p, int outputDim=3)