125 {
126 const uint64_t F2 = 1572067135;
127 const uint64_t G2 = 907633384;
128
129
130 uint32_t s = (((uint64_t)
x + (uint64_t)
y) * F2) >> 32;
131 uint32_t i = ((
x>>1) + (s>>1)) >> 11;
132 uint32_t j = ((
y>>1) + (s>>1)) >> 11;
133
134 uint64_t t = ((uint64_t)i + (uint64_t)j) * G2;
135 uint64_t X0 = ((uint64_t)i<<32) - t;
136 uint64_t Y0 = ((uint64_t)j<<32) - t;
137 int32_t x0 = ((uint64_t)
x<<2) - (X0>>18);
138 int32_t y0 = ((uint64_t)
y<<2) - (Y0>>18);
139
140
141
142 uint32_t i1, j1;
143 if (x0 > y0) {
144 i1 = 1;
145 j1 = 0;
146 } else {
147 i1 = 0;
148 j1 = 1;
149 }
150
151
152
153
154
155 int32_t x1 = x0 - ((int32_t)i1<<14) + (int32_t)(G2>>18);
156 int32_t y1 = y0 - ((int32_t)j1<<14) + (int32_t)(G2>>18);
157 int32_t x2 = x0 - (1 << 14) + ((int32_t)(2*G2)>>18);
158 int32_t y2 = y0 - (1 << 14) + ((int32_t)(2*G2)>>18);
159
160 int32_t n0 = 0, n1 = 0, n2 = 0;
161
162
163 int32_t t0 = (((int32_t)1 << 27) - x0*x0 - y0*y0) >> 12;
164 if (t0 > 0) {
165 t0 = (t0 * t0) >> 16;
166 t0 = (t0 * t0) >> 16;
167 n0 = t0 *
grad(
P((i+(uint32_t)(
P(j&0xff)))&0xff), x0, y0);
168 }
169
170 int32_t t1 = (((int32_t)1 << 27) - x1*x1 - y1*y1) >> 12;
171 if (t1 > 0) {
172 t1 = (t1 * t1) >> 16;
173 t1 = (t1 * t1) >> 16;
174 n1 = t1 *
grad(
P((i+i1+(uint32_t)(
P((j+j1)&0xff)))&0xff), x1, y1);
175 }
176
177 int32_t t2 = (((int32_t)1 << 27) - x2*x2 - y2*y2) >> 12;
178 if (t2 > 0) {
179 t2 = (t2 * t2) >> 16;
180 t2 = (t2 * t2) >> 16;
181 n2 = t2 *
grad(
P((i+1+(uint32_t)(
P((j+1)&0xff)))&0xff), x2, y2);
182 }
183
184
185
186 int32_t n = n0 + n1 + n2;
187 n = ((n >> 8) * 23163) >> 16;
188 return (uint16_t)n + 0x8000;
189}
static int32_t grad(uint8_t hash, int32_t x)