192 {
193
194 const uint64_t F3 = 1431655764;
195 const uint64_t G3 = 715827884;
196
197
198 uint32_t s = (((uint64_t)
x + (uint64_t)
y + (uint64_t)
z) * F3) >> 32;
199 uint32_t i = ((
x>>1) + (s>>1)) >> 11;
200 uint32_t j = ((
y>>1) + (s>>1)) >> 11;
201 uint32_t k = ((
z>>1) + (s>>1)) >> 11;
202
203 uint64_t t = ((uint64_t)i + (uint64_t)j + (uint64_t)k) * G3;
204 uint64_t X0 = ((uint64_t)i<<32) - t;
205 uint64_t Y0 = ((uint64_t)j<<32) - t;
206 uint64_t Z0 = ((uint64_t)k<<32) - t;
207 int32_t x0 = ((uint64_t)
x<<2) - (X0>>18);
208 int32_t y0 = ((uint64_t)
y<<2) - (Y0>>18);
209 int32_t z0 = ((uint64_t)
z<<2) - (Z0>>18);
210
211
212
213 uint32_t i1, j1, k1;
214 uint32_t i2, j2, k2;
215
216
217 if (x0 >= y0) {
218 if (y0 >= z0) {
219 i1 = 1;
220 j1 = 0;
221 k1 = 0;
222 i2 = 1;
223 j2 = 1;
224 k2 = 0;
225 } else if (x0 >= z0) {
226 i1 = 1;
227 j1 = 0;
228 k1 = 0;
229 i2 = 1;
230 j2 = 0;
231 k2 = 1;
232 } else {
233 i1 = 0;
234 j1 = 0;
235 k1 = 1;
236 i2 = 1;
237 j2 = 0;
238 k2 = 1;
239 }
240 } else {
241 if (y0 < z0) {
242 i1 = 0;
243 j1 = 0;
244 k1 = 1;
245 i2 = 0;
246 j2 = 1;
247 k2 = 1;
248 } else if (x0 < z0) {
249 i1 = 0;
250 j1 = 1;
251 k1 = 0;
252 i2 = 0;
253 j2 = 1;
254 k2 = 1;
255 } else {
256 i1 = 0;
257 j1 = 1;
258 k1 = 0;
259 i2 = 1;
260 j2 = 1;
261 k2 = 0;
262 }
263 }
264
265
266
267
268
269
270 int32_t x1 = x0 - ((int32_t)i1<<14) + ((int32_t)(G3>>18));
271 int32_t y1 = y0 - ((int32_t)j1<<14) + ((int32_t)(G3>>18));
272 int32_t z1 = z0 - ((int32_t)k1<<14) + ((int32_t)(G3>>18));
273 int32_t x2 = x0 - ((int32_t)i2<<14) + ((int32_t)(2*G3)>>18);
274 int32_t y2 = y0 - ((int32_t)j2<<14) + ((int32_t)(2*G3)>>18);
275 int32_t z2 = z0 - ((int32_t)k2<<14) + ((int32_t)(2*G3)>>18);
276 int32_t x3 = x0 - (1 << 14) + (int32_t)((3*G3)>>18);
277 int32_t y3 = y0 - (1 << 14) + (int32_t)((3*G3)>>18);
278 int32_t z3 = z0 - (1 << 14) + (int32_t)((3*G3)>>18);
279
280
281 int32_t n0 = 0, n1 = 0, n2 = 0, n3 = 0;
282 const int32_t fix0_6 = 161061274;
283
284 int32_t t0 = (fix0_6 - x0*x0 - y0*y0 - z0*z0) >> 12;
285 if (t0 > 0) {
286 t0 = (t0 * t0) >> 16;
287 t0 = (t0 * t0) >> 16;
288
289 n0 = t0 *
grad(
P((i+(uint32_t)
P((j+(uint32_t)
P(k&0xff))&0xff))&0xff), x0, y0, z0);
290 }
291
292 int32_t t1 = (fix0_6 - x1*x1 - y1*y1 - z1*z1) >> 12;
293 if (t1 > 0) {
294 t1 = (t1 * t1) >> 16;
295 t1 = (t1 * t1) >> 16;
296
297 n1 = t1 *
grad(
P((i+i1+(uint32_t)
P((j+j1+(uint32_t)
P((k+k1)&0xff))&0xff))&0xff), x1, y1, z1);
298 }
299
300 int32_t t2 = (fix0_6 - x2*x2 - y2*y2 - z2*z2) >> 12;
301 if (t2 > 0) {
302 t2 = (t2 * t2) >> 16;
303 t2 = (t2 * t2) >> 16;
304
305 n2 = t2 *
grad(
P((i+i2+(uint32_t)
P((j+j2+(uint32_t)
P((k+k2)&0xff))&0xff))&0xff), x2, y2, z2);
306 }
307
308 int32_t t3 = (fix0_6 - x3*x3 - y3*y3 - z3*z3) >> 12;
309 if (t3 > 0) {
310 t3 = (t3 * t3) >> 16;
311 t3 = (t3 * t3) >> 16;
312
313 n3 = t3 *
grad(
P((i+1+(uint32_t)
P((j+1+(uint32_t)
P((k+1)&0xff))&0xff))&0xff), x3, y3, z3);
314 }
315
316
317
318 int32_t n = n0 + n1 + n2 + n3;
319 n = ((n >> 8) * 16748) >> 16 ;
320 return (uint16_t)n + 0x8000;
321}
static int32_t grad(uint8_t hash, int32_t x)