rs_cl.c revision ba92a7085bbb8916334a6571ff33355873883173
1#include "rs_types.rsh" 2 3extern float2 __attribute__((overloadable)) convert_float2(int2 c); 4extern float3 __attribute__((overloadable)) convert_float3(int3 c); 5extern float4 __attribute__((overloadable)) convert_float4(int4 c); 6 7extern int2 __attribute__((overloadable)) convert_int2(float2 c); 8extern int3 __attribute__((overloadable)) convert_int3(float3 c); 9extern int4 __attribute__((overloadable)) convert_int4(float4 c); 10 11 12extern float __attribute__((overloadable)) fmin(float v, float v2); 13extern float2 __attribute__((overloadable)) fmin(float2 v, float v2); 14extern float3 __attribute__((overloadable)) fmin(float3 v, float v2); 15extern float4 __attribute__((overloadable)) fmin(float4 v, float v2); 16 17extern float __attribute__((overloadable)) fmax(float v, float v2); 18extern float2 __attribute__((overloadable)) fmax(float2 v, float v2); 19extern float3 __attribute__((overloadable)) fmax(float3 v, float v2); 20extern float4 __attribute__((overloadable)) fmax(float4 v, float v2); 21 22// Float ops, 6.11.2 23 24#define FN_FUNC_FN(fnc) \ 25extern float2 __attribute__((overloadable)) fnc(float2 v) { \ 26 float2 r; \ 27 r.x = fnc(v.x); \ 28 r.y = fnc(v.y); \ 29 return r; \ 30} \ 31extern float3 __attribute__((overloadable)) fnc(float3 v) { \ 32 float3 r; \ 33 r.x = fnc(v.x); \ 34 r.y = fnc(v.y); \ 35 r.z = fnc(v.z); \ 36 return r; \ 37} \ 38extern float4 __attribute__((overloadable)) fnc(float4 v) { \ 39 float4 r; \ 40 r.x = fnc(v.x); \ 41 r.y = fnc(v.y); \ 42 r.z = fnc(v.z); \ 43 r.w = fnc(v.w); \ 44 return r; \ 45} 46 47#define IN_FUNC_FN(fnc) \ 48extern int2 __attribute__((overloadable)) fnc(float2 v) { \ 49 int2 r; \ 50 r.x = fnc(v.x); \ 51 r.y = fnc(v.y); \ 52 return r; \ 53} \ 54extern int3 __attribute__((overloadable)) fnc(float3 v) { \ 55 int3 r; \ 56 r.x = fnc(v.x); \ 57 r.y = fnc(v.y); \ 58 r.z = fnc(v.z); \ 59 return r; \ 60} \ 61extern int4 __attribute__((overloadable)) fnc(float4 v) { \ 62 int4 r; \ 63 r.x = fnc(v.x); \ 64 r.y = fnc(v.y); \ 65 r.z = fnc(v.z); \ 66 r.w = fnc(v.w); \ 67 return r; \ 68} 69 70#define FN_FUNC_FN_FN(fnc) \ 71extern float2 __attribute__((overloadable)) fnc(float2 v1, float2 v2) { \ 72 float2 r; \ 73 r.x = fnc(v1.x, v2.x); \ 74 r.y = fnc(v1.y, v2.y); \ 75 return r; \ 76} \ 77extern float3 __attribute__((overloadable)) fnc(float3 v1, float3 v2) { \ 78 float3 r; \ 79 r.x = fnc(v1.x, v2.x); \ 80 r.y = fnc(v1.y, v2.y); \ 81 r.z = fnc(v1.z, v2.z); \ 82 return r; \ 83} \ 84extern float4 __attribute__((overloadable)) fnc(float4 v1, float4 v2) { \ 85 float4 r; \ 86 r.x = fnc(v1.x, v2.x); \ 87 r.y = fnc(v1.y, v2.y); \ 88 r.z = fnc(v1.z, v2.z); \ 89 r.w = fnc(v1.w, v2.w); \ 90 return r; \ 91} 92 93#define FN_FUNC_FN_F(fnc) \ 94extern float2 __attribute__((overloadable)) fnc(float2 v1, float v2) { \ 95 float2 r; \ 96 r.x = fnc(v1.x, v2); \ 97 r.y = fnc(v1.y, v2); \ 98 return r; \ 99} \ 100extern float3 __attribute__((overloadable)) fnc(float3 v1, float v2) { \ 101 float3 r; \ 102 r.x = fnc(v1.x, v2); \ 103 r.y = fnc(v1.y, v2); \ 104 r.z = fnc(v1.z, v2); \ 105 return r; \ 106} \ 107extern float4 __attribute__((overloadable)) fnc(float4 v1, float v2) { \ 108 float4 r; \ 109 r.x = fnc(v1.x, v2); \ 110 r.y = fnc(v1.y, v2); \ 111 r.z = fnc(v1.z, v2); \ 112 r.w = fnc(v1.w, v2); \ 113 return r; \ 114} 115 116#define FN_FUNC_FN_IN(fnc) \ 117extern float2 __attribute__((overloadable)) fnc(float2 v1, int2 v2) { \ 118 float2 r; \ 119 r.x = fnc(v1.x, v2.x); \ 120 r.y = fnc(v1.y, v2.y); \ 121 return r; \ 122} \ 123extern float3 __attribute__((overloadable)) fnc(float3 v1, int3 v2) { \ 124 float3 r; \ 125 r.x = fnc(v1.x, v2.x); \ 126 r.y = fnc(v1.y, v2.y); \ 127 r.z = fnc(v1.z, v2.z); \ 128 return r; \ 129} \ 130extern float4 __attribute__((overloadable)) fnc(float4 v1, int4 v2) { \ 131 float4 r; \ 132 r.x = fnc(v1.x, v2.x); \ 133 r.y = fnc(v1.y, v2.y); \ 134 r.z = fnc(v1.z, v2.z); \ 135 r.w = fnc(v1.w, v2.w); \ 136 return r; \ 137} 138 139#define FN_FUNC_FN_I(fnc) \ 140extern float2 __attribute__((overloadable)) fnc(float2 v1, int v2) { \ 141 float2 r; \ 142 r.x = fnc(v1.x, v2); \ 143 r.y = fnc(v1.y, v2); \ 144 return r; \ 145} \ 146extern float3 __attribute__((overloadable)) fnc(float3 v1, int v2) { \ 147 float3 r; \ 148 r.x = fnc(v1.x, v2); \ 149 r.y = fnc(v1.y, v2); \ 150 r.z = fnc(v1.z, v2); \ 151 return r; \ 152} \ 153extern float4 __attribute__((overloadable)) fnc(float4 v1, int v2) { \ 154 float4 r; \ 155 r.x = fnc(v1.x, v2); \ 156 r.y = fnc(v1.y, v2); \ 157 r.z = fnc(v1.z, v2); \ 158 r.w = fnc(v1.w, v2); \ 159 return r; \ 160} 161 162#define FN_FUNC_FN_PFN(fnc) \ 163extern float2 __attribute__((overloadable)) \ 164 fnc(float2 v1, float2 *v2) { \ 165 float2 r; \ 166 float t[2]; \ 167 r.x = fnc(v1.x, &t[0]); \ 168 r.y = fnc(v1.y, &t[1]); \ 169 v2->x = t[0]; \ 170 v2->y = t[1]; \ 171 return r; \ 172} \ 173extern float3 __attribute__((overloadable)) \ 174 fnc(float3 v1, float3 *v2) { \ 175 float3 r; \ 176 float t[3]; \ 177 r.x = fnc(v1.x, &t[0]); \ 178 r.y = fnc(v1.y, &t[1]); \ 179 r.z = fnc(v1.z, &t[2]); \ 180 v2->x = t[0]; \ 181 v2->y = t[1]; \ 182 v2->z = t[2]; \ 183 return r; \ 184} \ 185extern float4 __attribute__((overloadable)) \ 186 fnc(float4 v1, float4 *v2) { \ 187 float4 r; \ 188 float t[4]; \ 189 r.x = fnc(v1.x, &t[0]); \ 190 r.y = fnc(v1.y, &t[1]); \ 191 r.z = fnc(v1.z, &t[2]); \ 192 r.w = fnc(v1.w, &t[3]); \ 193 v2->x = t[0]; \ 194 v2->y = t[1]; \ 195 v2->z = t[2]; \ 196 v2->w = t[3]; \ 197 return r; \ 198} 199 200#define FN_FUNC_FN_PIN(fnc) \ 201extern float2 __attribute__((overloadable)) fnc(float2 v1, int2 *v2) { \ 202 float2 r; \ 203 int t[2]; \ 204 r.x = fnc(v1.x, &t[0]); \ 205 r.y = fnc(v1.y, &t[1]); \ 206 v2->x = t[0]; \ 207 v2->y = t[1]; \ 208 return r; \ 209} \ 210extern float3 __attribute__((overloadable)) fnc(float3 v1, int3 *v2) { \ 211 float3 r; \ 212 int t[3]; \ 213 r.x = fnc(v1.x, &t[0]); \ 214 r.y = fnc(v1.y, &t[1]); \ 215 r.z = fnc(v1.z, &t[2]); \ 216 v2->x = t[0]; \ 217 v2->y = t[1]; \ 218 v2->z = t[2]; \ 219 return r; \ 220} \ 221extern float4 __attribute__((overloadable)) fnc(float4 v1, int4 *v2) { \ 222 float4 r; \ 223 int t[4]; \ 224 r.x = fnc(v1.x, &t[0]); \ 225 r.y = fnc(v1.y, &t[1]); \ 226 r.z = fnc(v1.z, &t[2]); \ 227 r.w = fnc(v1.w, &t[3]); \ 228 v2->x = t[0]; \ 229 v2->y = t[1]; \ 230 v2->z = t[2]; \ 231 v2->w = t[3]; \ 232 return r; \ 233} 234 235#define FN_FUNC_FN_FN_FN(fnc) \ 236extern float2 __attribute__((overloadable)) \ 237 fnc(float2 v1, float2 v2, float2 v3) { \ 238 float2 r; \ 239 r.x = fnc(v1.x, v2.x, v3.x); \ 240 r.y = fnc(v1.y, v2.y, v3.y); \ 241 return r; \ 242} \ 243extern float3 __attribute__((overloadable)) \ 244 fnc(float3 v1, float3 v2, float3 v3) { \ 245 float3 r; \ 246 r.x = fnc(v1.x, v2.x, v3.x); \ 247 r.y = fnc(v1.y, v2.y, v3.y); \ 248 r.z = fnc(v1.z, v2.z, v3.z); \ 249 return r; \ 250} \ 251extern float4 __attribute__((overloadable)) \ 252 fnc(float4 v1, float4 v2, float4 v3) { \ 253 float4 r; \ 254 r.x = fnc(v1.x, v2.x, v3.x); \ 255 r.y = fnc(v1.y, v2.y, v3.y); \ 256 r.z = fnc(v1.z, v2.z, v3.z); \ 257 r.w = fnc(v1.w, v2.w, v3.w); \ 258 return r; \ 259} 260 261#define FN_FUNC_FN_FN_PIN(fnc) \ 262extern float2 __attribute__((overloadable)) \ 263 fnc(float2 v1, float2 v2, int2 *v3) { \ 264 float2 r; \ 265 int t[2]; \ 266 r.x = fnc(v1.x, v2.x, &t[0]); \ 267 r.y = fnc(v1.y, v2.y, &t[1]); \ 268 v3->x = t[0]; \ 269 v3->y = t[1]; \ 270 return r; \ 271} \ 272extern float3 __attribute__((overloadable)) \ 273 fnc(float3 v1, float3 v2, int3 *v3) { \ 274 float3 r; \ 275 int t[3]; \ 276 r.x = fnc(v1.x, v2.x, &t[0]); \ 277 r.y = fnc(v1.y, v2.y, &t[1]); \ 278 r.z = fnc(v1.z, v2.z, &t[2]); \ 279 v3->x = t[0]; \ 280 v3->y = t[1]; \ 281 v3->z = t[2]; \ 282 return r; \ 283} \ 284extern float4 __attribute__((overloadable)) \ 285 fnc(float4 v1, float4 v2, int4 *v3) { \ 286 float4 r; \ 287 int t[4]; \ 288 r.x = fnc(v1.x, v2.x, &t[0]); \ 289 r.y = fnc(v1.y, v2.y, &t[1]); \ 290 r.z = fnc(v1.z, v2.z, &t[2]); \ 291 r.w = fnc(v1.w, v2.w, &t[3]); \ 292 v3->x = t[0]; \ 293 v3->y = t[1]; \ 294 v3->z = t[2]; \ 295 v3->w = t[3]; \ 296 return r; \ 297} 298 299static const int iposinf = 0x7f800000; 300static const int ineginf = 0xff800000; 301 302static const float posinf() { 303 float f = *((float*)&iposinf); 304 return f; 305} 306 307static const float neginf() { 308 float f = *((float*)&ineginf); 309 return f; 310} 311 312static bool isinf(float f) { 313 int i = *((int*)(void*)&f); 314 return (i == iposinf) || (i == ineginf); 315} 316 317static bool isnan(float f) { 318 int i = *((int*)(void*)&f); 319 return (((i & 0x7f800000) == 0x7f800000) && (i & 0x007fffff)); 320} 321 322static bool isposzero(float f) { 323 int i = *((int*)(void*)&f); 324 return (i == 0x00000000); 325} 326 327static bool isnegzero(float f) { 328 int i = *((int*)(void*)&f); 329 return (i == 0x80000000); 330} 331 332static bool iszero(float f) { 333 return isposzero(f) || isnegzero(f); 334} 335 336 337extern float __attribute__((overloadable)) acos(float); 338FN_FUNC_FN(acos) 339 340extern float __attribute__((overloadable)) acosh(float); 341FN_FUNC_FN(acosh) 342 343 344extern float __attribute__((overloadable)) acospi(float v) { 345 return acos(v) / M_PI; 346} 347FN_FUNC_FN(acospi) 348 349extern float __attribute__((overloadable)) asin(float); 350FN_FUNC_FN(asin) 351 352extern float __attribute__((overloadable)) asinh(float); 353FN_FUNC_FN(asinh) 354 355extern float __attribute__((overloadable)) asinpi(float v) { 356 return asin(v) / M_PI; 357} 358FN_FUNC_FN(asinpi) 359 360extern float __attribute__((overloadable)) atan(float); 361FN_FUNC_FN(atan) 362 363extern float __attribute__((overloadable)) atan2(float, float); 364FN_FUNC_FN_FN(atan2) 365 366extern float __attribute__((overloadable)) atanh(float); 367FN_FUNC_FN(atanh) 368 369extern float __attribute__((overloadable)) atanpi(float v) { 370 return atan(v) / M_PI; 371} 372FN_FUNC_FN(atanpi) 373 374 375extern float __attribute__((overloadable)) atan2pi(float y, float x) { 376 return atan2(y, x) / M_PI; 377} 378FN_FUNC_FN_FN(atan2pi) 379 380extern float __attribute__((overloadable)) cbrt(float); 381FN_FUNC_FN(cbrt) 382 383extern float __attribute__((overloadable)) ceil(float); 384FN_FUNC_FN(ceil) 385 386extern float __attribute__((overloadable)) copysign(float, float); 387FN_FUNC_FN_FN(copysign) 388 389extern float __attribute__((overloadable)) cos(float); 390FN_FUNC_FN(cos) 391 392extern float __attribute__((overloadable)) cosh(float); 393FN_FUNC_FN(cosh) 394 395extern float __attribute__((overloadable)) cospi(float v) { 396 return cos(v * M_PI); 397} 398FN_FUNC_FN(cospi) 399 400extern float __attribute__((overloadable)) erfc(float); 401FN_FUNC_FN(erfc) 402 403extern float __attribute__((overloadable)) erf(float); 404FN_FUNC_FN(erf) 405 406extern float __attribute__((overloadable)) exp(float); 407FN_FUNC_FN(exp) 408 409extern float __attribute__((overloadable)) exp2(float); 410FN_FUNC_FN(exp2) 411 412extern float __attribute__((overloadable)) pow(float, float); 413 414extern float __attribute__((overloadable)) exp10(float v) { 415 return exp2(v * 3.321928095f); 416} 417FN_FUNC_FN(exp10) 418 419extern float __attribute__((overloadable)) expm1(float); 420FN_FUNC_FN(expm1) 421 422extern float __attribute__((overloadable)) fabs(float v) { 423 int i = *((int*)(void*)&v) & 0x7fffffff; 424 return *((float*)(void*)&i); 425} 426FN_FUNC_FN(fabs) 427 428extern float __attribute__((overloadable)) fdim(float, float); 429FN_FUNC_FN_FN(fdim) 430 431extern float __attribute__((overloadable)) floor(float); 432FN_FUNC_FN(floor) 433 434extern float __attribute__((overloadable)) fma(float, float, float); 435FN_FUNC_FN_FN_FN(fma) 436 437extern float __attribute__((overloadable)) fmin(float, float); 438 439extern float __attribute__((overloadable)) fmod(float, float); 440FN_FUNC_FN_FN(fmod) 441 442extern float __attribute__((overloadable)) fract(float v, float *iptr) { 443 int i = (int)floor(v); 444 if (iptr) { 445 iptr[0] = i; 446 } 447 return fmin(v - i, 0x1.fffffep-1f); 448} 449FN_FUNC_FN_PFN(fract) 450 451extern float __attribute__((overloadable)) frexp(float, int *); 452FN_FUNC_FN_PIN(frexp) 453 454extern float __attribute__((overloadable)) hypot(float, float); 455FN_FUNC_FN_FN(hypot) 456 457extern int __attribute__((overloadable)) ilogb(float); 458IN_FUNC_FN(ilogb) 459 460extern float __attribute__((overloadable)) ldexp(float, int); 461FN_FUNC_FN_IN(ldexp) 462FN_FUNC_FN_I(ldexp) 463 464extern float __attribute__((overloadable)) lgamma(float); 465FN_FUNC_FN(lgamma) 466extern float __attribute__((overloadable)) lgamma(float, int*); 467FN_FUNC_FN_PIN(lgamma) 468 469extern float __attribute__((overloadable)) log(float); 470FN_FUNC_FN(log) 471 472extern float __attribute__((overloadable)) log10(float); 473FN_FUNC_FN(log10) 474 475 476extern float __attribute__((overloadable)) log2(float v) { 477 return log10(v) * 3.321928095f; 478} 479FN_FUNC_FN(log2) 480 481extern float __attribute__((overloadable)) log1p(float); 482FN_FUNC_FN(log1p) 483 484extern float __attribute__((overloadable)) logb(float); 485FN_FUNC_FN(logb) 486 487extern float __attribute__((overloadable)) mad(float a, float b, float c) { 488 return a * b + c; 489} 490extern float2 __attribute__((overloadable)) mad(float2 a, float2 b, float2 c) { 491 return a * b + c; 492} 493extern float3 __attribute__((overloadable)) mad(float3 a, float3 b, float3 c) { 494 return a * b + c; 495} 496extern float4 __attribute__((overloadable)) mad(float4 a, float4 b, float4 c) { 497 return a * b + c; 498} 499 500extern float __attribute__((overloadable)) modf(float, float *); 501FN_FUNC_FN_PFN(modf); 502 503extern float __attribute__((overloadable)) nan(uint v) { 504 float f[1]; 505 uint32_t *ip = (uint32_t *)f; 506 *ip = v | 0x7fc00000; 507 return f[0]; 508} 509 510extern float __attribute__((overloadable)) nextafter(float, float); 511FN_FUNC_FN_FN(nextafter) 512 513FN_FUNC_FN_FN(pow) 514 515extern float __attribute__((overloadable)) pown(float v, int p) { 516 /* The mantissa of a float has fewer bits than an int (24 effective vs. 31). 517 * For very large ints, we'll lose whether the exponent is even or odd, making 518 * the selection of a correct sign incorrect. We correct this. Use copysign 519 * to handle the negative zero case. 520 */ 521 float sign = (p & 0x1) ? copysign(1.f, v) : 1.f; 522 float f = pow(v, (float)p); 523 return copysign(f, sign); 524} 525FN_FUNC_FN_IN(pown) 526 527extern float __attribute__((overloadable)) powr(float v, float p) { 528 return pow(v, p); 529} 530extern float2 __attribute__((overloadable)) powr(float2 v, float2 p) { 531 return pow(v, p); 532} 533extern float3 __attribute__((overloadable)) powr(float3 v, float3 p) { 534 return pow(v, p); 535} 536extern float4 __attribute__((overloadable)) powr(float4 v, float4 p) { 537 return pow(v, p); 538} 539 540extern float __attribute__((overloadable)) remainder(float, float); 541FN_FUNC_FN_FN(remainder) 542 543extern float __attribute__((overloadable)) remquo(float, float, int *); 544FN_FUNC_FN_FN_PIN(remquo) 545 546extern float __attribute__((overloadable)) rint(float); 547FN_FUNC_FN(rint) 548 549extern float __attribute__((overloadable)) rootn(float v, int r) { 550 if (r == 0) { 551 return nan(0); 552 } 553 554 if (iszero(v)) { 555 if (r < 0) { 556 if (r & 1) { 557 return copysign(posinf(), v); 558 } else { 559 return posinf(); 560 } 561 } else { 562 if (r & 1) { 563 return copysign(0.f, v); 564 } else { 565 return 0.f; 566 } 567 } 568 } 569 570 if (!isinf(v) && !isnan(v) && (v < 0.f)) { 571 if (r & 1) { 572 return (-1.f * pow(-1.f * v, 1.f / r)); 573 } else { 574 return nan(0); 575 } 576 } 577 578 return pow(v, 1.f / r); 579} 580FN_FUNC_FN_IN(rootn); 581 582extern float __attribute__((overloadable)) round(float); 583FN_FUNC_FN(round) 584 585 586extern float __attribute__((overloadable)) sqrt(float); 587extern float __attribute__((overloadable)) rsqrt(float v) { 588 return 1.f / sqrt(v); 589} 590 591#if !defined(__i386__) 592FN_FUNC_FN(sqrt) 593#endif // !defined(__i386__) 594 595FN_FUNC_FN(rsqrt) 596 597extern float __attribute__((overloadable)) sin(float); 598FN_FUNC_FN(sin) 599 600extern float __attribute__((overloadable)) sincos(float v, float *cosptr) { 601 *cosptr = cos(v); 602 return sin(v); 603} 604extern float2 __attribute__((overloadable)) sincos(float2 v, float2 *cosptr) { 605 *cosptr = cos(v); 606 return sin(v); 607} 608extern float3 __attribute__((overloadable)) sincos(float3 v, float3 *cosptr) { 609 *cosptr = cos(v); 610 return sin(v); 611} 612extern float4 __attribute__((overloadable)) sincos(float4 v, float4 *cosptr) { 613 *cosptr = cos(v); 614 return sin(v); 615} 616 617extern float __attribute__((overloadable)) sinh(float); 618FN_FUNC_FN(sinh) 619 620extern float __attribute__((overloadable)) sinpi(float v) { 621 return sin(v * M_PI); 622} 623FN_FUNC_FN(sinpi) 624 625extern float __attribute__((overloadable)) tan(float); 626FN_FUNC_FN(tan) 627 628extern float __attribute__((overloadable)) tanh(float); 629FN_FUNC_FN(tanh) 630 631extern float __attribute__((overloadable)) tanpi(float v) { 632 return tan(v * M_PI); 633} 634FN_FUNC_FN(tanpi) 635 636 637extern float __attribute__((overloadable)) tgamma(float); 638FN_FUNC_FN(tgamma) 639 640extern float __attribute__((overloadable)) trunc(float); 641FN_FUNC_FN(trunc) 642 643// Int ops (partial), 6.11.3 644 645#define XN_FUNC_YN(typeout, fnc, typein) \ 646extern typeout __attribute__((overloadable)) fnc(typein); \ 647extern typeout##2 __attribute__((overloadable)) fnc(typein##2 v) { \ 648 typeout##2 r; \ 649 r.x = fnc(v.x); \ 650 r.y = fnc(v.y); \ 651 return r; \ 652} \ 653extern typeout##3 __attribute__((overloadable)) fnc(typein##3 v) { \ 654 typeout##3 r; \ 655 r.x = fnc(v.x); \ 656 r.y = fnc(v.y); \ 657 r.z = fnc(v.z); \ 658 return r; \ 659} \ 660extern typeout##4 __attribute__((overloadable)) fnc(typein##4 v) { \ 661 typeout##4 r; \ 662 r.x = fnc(v.x); \ 663 r.y = fnc(v.y); \ 664 r.z = fnc(v.z); \ 665 r.w = fnc(v.w); \ 666 return r; \ 667} 668 669 670#define UIN_FUNC_IN(fnc) \ 671XN_FUNC_YN(uchar, fnc, char) \ 672XN_FUNC_YN(ushort, fnc, short) \ 673XN_FUNC_YN(uint, fnc, int) 674 675#define IN_FUNC_IN(fnc) \ 676XN_FUNC_YN(uchar, fnc, uchar) \ 677XN_FUNC_YN(char, fnc, char) \ 678XN_FUNC_YN(ushort, fnc, ushort) \ 679XN_FUNC_YN(short, fnc, short) \ 680XN_FUNC_YN(uint, fnc, uint) \ 681XN_FUNC_YN(int, fnc, int) 682 683 684#define XN_FUNC_XN_XN_BODY(type, fnc, body) \ 685extern type __attribute__((overloadable)) \ 686 fnc(type v1, type v2) { \ 687 return body; \ 688} \ 689extern type##2 __attribute__((overloadable)) \ 690 fnc(type##2 v1, type##2 v2) { \ 691 type##2 r; \ 692 r.x = fnc(v1.x, v2.x); \ 693 r.y = fnc(v1.y, v2.y); \ 694 return r; \ 695} \ 696extern type##3 __attribute__((overloadable)) \ 697 fnc(type##3 v1, type##3 v2) { \ 698 type##3 r; \ 699 r.x = fnc(v1.x, v2.x); \ 700 r.y = fnc(v1.y, v2.y); \ 701 r.z = fnc(v1.z, v2.z); \ 702 return r; \ 703} \ 704extern type##4 __attribute__((overloadable)) \ 705 fnc(type##4 v1, type##4 v2) { \ 706 type##4 r; \ 707 r.x = fnc(v1.x, v2.x); \ 708 r.y = fnc(v1.y, v2.y); \ 709 r.z = fnc(v1.z, v2.z); \ 710 r.w = fnc(v1.w, v2.w); \ 711 return r; \ 712} 713 714#define IN_FUNC_IN_IN_BODY(fnc, body) \ 715XN_FUNC_XN_XN_BODY(uchar, fnc, body) \ 716XN_FUNC_XN_XN_BODY(char, fnc, body) \ 717XN_FUNC_XN_XN_BODY(ushort, fnc, body) \ 718XN_FUNC_XN_XN_BODY(short, fnc, body) \ 719XN_FUNC_XN_XN_BODY(uint, fnc, body) \ 720XN_FUNC_XN_XN_BODY(int, fnc, body) \ 721XN_FUNC_XN_XN_BODY(float, fnc, body) 722 723 724/** 725 * abs 726 */ 727extern uint32_t __attribute__((overloadable)) abs(int32_t v) { 728 if (v < 0) 729 return -v; 730 return v; 731} 732extern uint16_t __attribute__((overloadable)) abs(int16_t v) { 733 if (v < 0) 734 return -v; 735 return v; 736} 737extern uint8_t __attribute__((overloadable)) abs(int8_t v) { 738 if (v < 0) 739 return -v; 740 return v; 741} 742 743/** 744 * clz 745 * __builtin_clz only accepts a 32-bit unsigned int, so every input will be 746 * expanded to 32 bits. For our smaller data types, we need to subtract off 747 * these unused top bits (that will be always be composed of zeros). 748 */ 749extern uint32_t __attribute__((overloadable)) clz(uint32_t v) { 750 return __builtin_clz(v); 751} 752extern uint16_t __attribute__((overloadable)) clz(uint16_t v) { 753 return __builtin_clz(v) - 16; 754} 755extern uint8_t __attribute__((overloadable)) clz(uint8_t v) { 756 return __builtin_clz(v) - 24; 757} 758extern int32_t __attribute__((overloadable)) clz(int32_t v) { 759 return __builtin_clz(v); 760} 761extern int16_t __attribute__((overloadable)) clz(int16_t v) { 762 return __builtin_clz(((uint32_t)v) & 0x0000ffff) - 16; 763} 764extern int8_t __attribute__((overloadable)) clz(int8_t v) { 765 return __builtin_clz(((uint32_t)v) & 0x000000ff) - 24; 766} 767 768 769UIN_FUNC_IN(abs) 770IN_FUNC_IN(clz) 771 772 773// 6.11.4 774 775 776extern float __attribute__((overloadable)) degrees(float radians) { 777 return radians * (180.f / M_PI); 778} 779extern float2 __attribute__((overloadable)) degrees(float2 radians) { 780 return radians * (180.f / M_PI); 781} 782extern float3 __attribute__((overloadable)) degrees(float3 radians) { 783 return radians * (180.f / M_PI); 784} 785extern float4 __attribute__((overloadable)) degrees(float4 radians) { 786 return radians * (180.f / M_PI); 787} 788 789extern float __attribute__((overloadable)) mix(float start, float stop, float amount) { 790 return start + (stop - start) * amount; 791} 792extern float2 __attribute__((overloadable)) mix(float2 start, float2 stop, float2 amount) { 793 return start + (stop - start) * amount; 794} 795extern float3 __attribute__((overloadable)) mix(float3 start, float3 stop, float3 amount) { 796 return start + (stop - start) * amount; 797} 798extern float4 __attribute__((overloadable)) mix(float4 start, float4 stop, float4 amount) { 799 return start + (stop - start) * amount; 800} 801extern float2 __attribute__((overloadable)) mix(float2 start, float2 stop, float amount) { 802 return start + (stop - start) * amount; 803} 804extern float3 __attribute__((overloadable)) mix(float3 start, float3 stop, float amount) { 805 return start + (stop - start) * amount; 806} 807extern float4 __attribute__((overloadable)) mix(float4 start, float4 stop, float amount) { 808 return start + (stop - start) * amount; 809} 810 811extern float __attribute__((overloadable)) radians(float degrees) { 812 return degrees * (M_PI / 180.f); 813} 814extern float2 __attribute__((overloadable)) radians(float2 degrees) { 815 return degrees * (M_PI / 180.f); 816} 817extern float3 __attribute__((overloadable)) radians(float3 degrees) { 818 return degrees * (M_PI / 180.f); 819} 820extern float4 __attribute__((overloadable)) radians(float4 degrees) { 821 return degrees * (M_PI / 180.f); 822} 823 824extern float __attribute__((overloadable)) step(float edge, float v) { 825 return (v < edge) ? 0.f : 1.f; 826} 827extern float2 __attribute__((overloadable)) step(float2 edge, float2 v) { 828 float2 r; 829 r.x = (v.x < edge.x) ? 0.f : 1.f; 830 r.y = (v.y < edge.y) ? 0.f : 1.f; 831 return r; 832} 833extern float3 __attribute__((overloadable)) step(float3 edge, float3 v) { 834 float3 r; 835 r.x = (v.x < edge.x) ? 0.f : 1.f; 836 r.y = (v.y < edge.y) ? 0.f : 1.f; 837 r.z = (v.z < edge.z) ? 0.f : 1.f; 838 return r; 839} 840extern float4 __attribute__((overloadable)) step(float4 edge, float4 v) { 841 float4 r; 842 r.x = (v.x < edge.x) ? 0.f : 1.f; 843 r.y = (v.y < edge.y) ? 0.f : 1.f; 844 r.z = (v.z < edge.z) ? 0.f : 1.f; 845 r.w = (v.w < edge.w) ? 0.f : 1.f; 846 return r; 847} 848extern float2 __attribute__((overloadable)) step(float2 edge, float v) { 849 float2 r; 850 r.x = (v < edge.x) ? 0.f : 1.f; 851 r.y = (v < edge.y) ? 0.f : 1.f; 852 return r; 853} 854extern float3 __attribute__((overloadable)) step(float3 edge, float v) { 855 float3 r; 856 r.x = (v < edge.x) ? 0.f : 1.f; 857 r.y = (v < edge.y) ? 0.f : 1.f; 858 r.z = (v < edge.z) ? 0.f : 1.f; 859 return r; 860} 861extern float4 __attribute__((overloadable)) step(float4 edge, float v) { 862 float4 r; 863 r.x = (v < edge.x) ? 0.f : 1.f; 864 r.y = (v < edge.y) ? 0.f : 1.f; 865 r.z = (v < edge.z) ? 0.f : 1.f; 866 r.w = (v < edge.w) ? 0.f : 1.f; 867 return r; 868} 869extern float2 __attribute__((overloadable)) step(float edge, float2 v) { 870 float2 r; 871 r.x = (v.x < edge) ? 0.f : 1.f; 872 r.y = (v.y < edge) ? 0.f : 1.f; 873 return r; 874} 875extern float3 __attribute__((overloadable)) step(float edge, float3 v) { 876 float3 r; 877 r.x = (v.x < edge) ? 0.f : 1.f; 878 r.y = (v.y < edge) ? 0.f : 1.f; 879 r.z = (v.z < edge) ? 0.f : 1.f; 880 return r; 881} 882extern float4 __attribute__((overloadable)) step(float edge, float4 v) { 883 float4 r; 884 r.x = (v.x < edge) ? 0.f : 1.f; 885 r.y = (v.y < edge) ? 0.f : 1.f; 886 r.z = (v.z < edge) ? 0.f : 1.f; 887 r.w = (v.w < edge) ? 0.f : 1.f; 888 return r; 889} 890 891extern float __attribute__((overloadable)) smoothstep(float, float, float); 892extern float2 __attribute__((overloadable)) smoothstep(float2, float2, float2); 893extern float3 __attribute__((overloadable)) smoothstep(float3, float3, float3); 894extern float4 __attribute__((overloadable)) smoothstep(float4, float4, float4); 895extern float2 __attribute__((overloadable)) smoothstep(float, float, float2); 896extern float3 __attribute__((overloadable)) smoothstep(float, float, float3); 897extern float4 __attribute__((overloadable)) smoothstep(float, float, float4); 898 899extern float __attribute__((overloadable)) sign(float v) { 900 if (v > 0) return 1.f; 901 if (v < 0) return -1.f; 902 return v; 903} 904FN_FUNC_FN(sign) 905 906 907// 6.11.5 908extern float3 __attribute__((overloadable)) cross(float3 lhs, float3 rhs) { 909 float3 r; 910 r.x = lhs.y * rhs.z - lhs.z * rhs.y; 911 r.y = lhs.z * rhs.x - lhs.x * rhs.z; 912 r.z = lhs.x * rhs.y - lhs.y * rhs.x; 913 return r; 914} 915 916extern float4 __attribute__((overloadable)) cross(float4 lhs, float4 rhs) { 917 float4 r; 918 r.x = lhs.y * rhs.z - lhs.z * rhs.y; 919 r.y = lhs.z * rhs.x - lhs.x * rhs.z; 920 r.z = lhs.x * rhs.y - lhs.y * rhs.x; 921 r.w = 0.f; 922 return r; 923} 924 925#if !defined(__i386__) 926 927extern float __attribute__((overloadable)) dot(float lhs, float rhs) { 928 return lhs * rhs; 929} 930extern float __attribute__((overloadable)) dot(float2 lhs, float2 rhs) { 931 return lhs.x*rhs.x + lhs.y*rhs.y; 932} 933extern float __attribute__((overloadable)) dot(float3 lhs, float3 rhs) { 934 return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z; 935} 936extern float __attribute__((overloadable)) dot(float4 lhs, float4 rhs) { 937 return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z + lhs.w*rhs.w; 938} 939 940extern float __attribute__((overloadable)) length(float v) { 941 return fabs(v); 942} 943extern float __attribute__((overloadable)) length(float2 v) { 944 return sqrt(v.x*v.x + v.y*v.y); 945} 946extern float __attribute__((overloadable)) length(float3 v) { 947 return sqrt(v.x*v.x + v.y*v.y + v.z*v.z); 948} 949extern float __attribute__((overloadable)) length(float4 v) { 950 return sqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w); 951} 952 953#else 954 955extern float __attribute__((overloadable)) length(float v); 956extern float __attribute__((overloadable)) length(float2 v); 957extern float __attribute__((overloadable)) length(float3 v); 958extern float __attribute__((overloadable)) length(float4 v); 959 960#endif 961 962extern float __attribute__((overloadable)) distance(float lhs, float rhs) { 963 return length(lhs - rhs); 964} 965extern float __attribute__((overloadable)) distance(float2 lhs, float2 rhs) { 966 return length(lhs - rhs); 967} 968extern float __attribute__((overloadable)) distance(float3 lhs, float3 rhs) { 969 return length(lhs - rhs); 970} 971extern float __attribute__((overloadable)) distance(float4 lhs, float4 rhs) { 972 return length(lhs - rhs); 973} 974 975/* For the normalization functions, vectors of length 0 should simply be 976 * returned (i.e. all the components of that vector are 0). 977 */ 978extern float __attribute__((overloadable)) normalize(float v) { 979 if (v == 0.0f) { 980 return 0.0f; 981 } else if (v < 0.0f) { 982 return -1.0f; 983 } else { 984 return 1.0f; 985 } 986} 987extern float2 __attribute__((overloadable)) normalize(float2 v) { 988 float l = length(v); 989 return l == 0.0f ? v : v / l; 990} 991extern float3 __attribute__((overloadable)) normalize(float3 v) { 992 float l = length(v); 993 return l == 0.0f ? v : v / l; 994} 995extern float4 __attribute__((overloadable)) normalize(float4 v) { 996 float l = length(v); 997 return l == 0.0f ? v : v / l; 998} 999 1000extern float __attribute__((overloadable)) half_sqrt(float v) { 1001 return sqrt(v); 1002} 1003FN_FUNC_FN(half_sqrt) 1004 1005extern float __attribute__((overloadable)) fast_length(float v) { 1006 return fabs(v); 1007} 1008extern float __attribute__((overloadable)) fast_length(float2 v) { 1009 return half_sqrt(v.x*v.x + v.y*v.y); 1010} 1011extern float __attribute__((overloadable)) fast_length(float3 v) { 1012 return half_sqrt(v.x*v.x + v.y*v.y + v.z*v.z); 1013} 1014extern float __attribute__((overloadable)) fast_length(float4 v) { 1015 return half_sqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w); 1016} 1017 1018extern float __attribute__((overloadable)) fast_distance(float lhs, float rhs) { 1019 return fast_length(lhs - rhs); 1020} 1021extern float __attribute__((overloadable)) fast_distance(float2 lhs, float2 rhs) { 1022 return fast_length(lhs - rhs); 1023} 1024extern float __attribute__((overloadable)) fast_distance(float3 lhs, float3 rhs) { 1025 return fast_length(lhs - rhs); 1026} 1027extern float __attribute__((overloadable)) fast_distance(float4 lhs, float4 rhs) { 1028 return fast_length(lhs - rhs); 1029} 1030 1031extern float __attribute__((overloadable)) half_rsqrt(float); 1032 1033/* For the normalization functions, vectors of length 0 should simply be 1034 * returned (i.e. all the components of that vector are 0). 1035 */ 1036extern float __attribute__((overloadable)) fast_normalize(float v) { 1037 if (v == 0.0f) { 1038 return 0.0f; 1039 } else if (v < 0.0f) { 1040 return -1.0f; 1041 } else { 1042 return 1.0f; 1043 } 1044} 1045// If the length is 0, then rlength should be NaN. 1046extern float2 __attribute__((overloadable)) fast_normalize(float2 v) { 1047 float rlength = half_rsqrt(v.x*v.x + v.y*v.y); 1048 return (rlength == rlength) ? v * rlength : v; 1049} 1050extern float3 __attribute__((overloadable)) fast_normalize(float3 v) { 1051 float rlength = half_rsqrt(v.x*v.x + v.y*v.y + v.z*v.z); 1052 return (rlength == rlength) ? v * rlength : v; 1053} 1054extern float4 __attribute__((overloadable)) fast_normalize(float4 v) { 1055 float rlength = half_rsqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w); 1056 return (rlength == rlength) ? v * rlength : v; 1057} 1058 1059extern float __attribute__((overloadable)) half_recip(float v) { 1060 return 1.f / v; 1061} 1062 1063/* 1064extern float __attribute__((overloadable)) approx_atan(float x) { 1065 if (x == 0.f) 1066 return 0.f; 1067 if (x < 0.f) 1068 return -1.f * approx_atan(-1.f * x); 1069 if (x > 1.f) 1070 return M_PI_2 - approx_atan(approx_recip(x)); 1071 return x * approx_recip(1.f + 0.28f * x*x); 1072} 1073FN_FUNC_FN(approx_atan) 1074*/ 1075 1076typedef union 1077{ 1078 float fv; 1079 int32_t iv; 1080} ieee_float_shape_type; 1081 1082/* Get a 32 bit int from a float. */ 1083 1084#define GET_FLOAT_WORD(i,d) \ 1085do { \ 1086 ieee_float_shape_type gf_u; \ 1087 gf_u.fv = (d); \ 1088 (i) = gf_u.iv; \ 1089} while (0) 1090 1091/* Set a float from a 32 bit int. */ 1092 1093#define SET_FLOAT_WORD(d,i) \ 1094do { \ 1095 ieee_float_shape_type sf_u; \ 1096 sf_u.iv = (i); \ 1097 (d) = sf_u.fv; \ 1098} while (0) 1099 1100 1101 1102// Valid -125 to 125 1103extern float __attribute__((overloadable)) native_exp2(float v) { 1104 int32_t iv = (int)v; 1105 int32_t x = iv + (iv >> 31); // ~floor(v) 1106 float r = (v - x); 1107 1108 float fo; 1109 SET_FLOAT_WORD(fo, (x + 127) << 23); 1110 1111 r *= 0.694f; // ~ log(e) / log(2) 1112 float r2 = r*r; 1113 float adj = 1.f + r + (r2 * 0.5f) + (r2*r * 0.166666f) + (r2*r2 * 0.0416666f); 1114 return fo * adj; 1115} 1116 1117extern float2 __attribute__((overloadable)) native_exp2(float2 v) { 1118 int2 iv = convert_int2(v); 1119 int2 x = iv + (iv >> (int2)31);//floor(v); 1120 float2 r = (v - convert_float2(x)); 1121 1122 x += 127; 1123 1124 float2 fo = (float2)(x << (int2)23); 1125 1126 r *= 0.694f; // ~ log(e) / log(2) 1127 float2 r2 = r*r; 1128 float2 adj = 1.f + r + (r2 * 0.5f) + (r2*r * 0.166666f) + (r2*r2 * 0.0416666f); 1129 return fo * adj; 1130} 1131 1132extern float4 __attribute__((overloadable)) native_exp2(float4 v) { 1133 int4 iv = convert_int4(v); 1134 int4 x = iv + (iv >> (int4)31);//floor(v); 1135 float4 r = (v - convert_float4(x)); 1136 1137 x += 127; 1138 1139 float4 fo = (float4)(x << (int4)23); 1140 1141 r *= 0.694f; // ~ log(e) / log(2) 1142 float4 r2 = r*r; 1143 float4 adj = 1.f + r + (r2 * 0.5f) + (r2*r * 0.166666f) + (r2*r2 * 0.0416666f); 1144 return fo * adj; 1145} 1146 1147extern float3 __attribute__((overloadable)) native_exp2(float3 v) { 1148 float4 t = 1.f; 1149 t.xyz = v; 1150 return native_exp2(t).xyz; 1151} 1152 1153 1154extern float __attribute__((overloadable)) native_exp(float v) { 1155 return native_exp2(v * 1.442695041f); 1156} 1157extern float2 __attribute__((overloadable)) native_exp(float2 v) { 1158 return native_exp2(v * 1.442695041f); 1159} 1160extern float3 __attribute__((overloadable)) native_exp(float3 v) { 1161 return native_exp2(v * 1.442695041f); 1162} 1163extern float4 __attribute__((overloadable)) native_exp(float4 v) { 1164 return native_exp2(v * 1.442695041f); 1165} 1166 1167extern float __attribute__((overloadable)) native_exp10(float v) { 1168 return native_exp2(v * 3.321928095f); 1169} 1170extern float2 __attribute__((overloadable)) native_exp10(float2 v) { 1171 return native_exp2(v * 3.321928095f); 1172} 1173extern float3 __attribute__((overloadable)) native_exp10(float3 v) { 1174 return native_exp2(v * 3.321928095f); 1175} 1176extern float4 __attribute__((overloadable)) native_exp10(float4 v) { 1177 return native_exp2(v * 3.321928095f); 1178} 1179 1180extern float __attribute__((overloadable)) native_log2(float v) { 1181 int32_t ibits; 1182 GET_FLOAT_WORD(ibits, v); 1183 1184 int32_t e = (ibits >> 23) & 0xff; 1185 1186 ibits &= 0x7fffff; 1187 ibits |= 127 << 23; 1188 1189 float ir; 1190 SET_FLOAT_WORD(ir, ibits); 1191 1192 ir -= 1.5f; 1193 float ir2 = ir*ir; 1194 float adj2 = 0.405465108f + // -0.00009f + 1195 (0.666666667f * ir) - 1196 (0.222222222f * ir2) + 1197 (0.098765432f * ir*ir2) - 1198 (0.049382716f * ir2*ir2) + 1199 (0.026337449f * ir*ir2*ir2) - 1200 (0.014631916f * ir2*ir2*ir2); 1201 adj2 *= (1.f / 0.693147181f); 1202 1203 return (float)(e - 127) + adj2; 1204} 1205extern float2 __attribute__((overloadable)) native_log2(float2 v) { 1206 float2 v2 = {native_log2(v.x), native_log2(v.y)}; 1207 return v2; 1208} 1209extern float3 __attribute__((overloadable)) native_log2(float3 v) { 1210 float3 v2 = {native_log2(v.x), native_log2(v.y), native_log2(v.z)}; 1211 return v2; 1212} 1213extern float4 __attribute__((overloadable)) native_log2(float4 v) { 1214 float4 v2 = {native_log2(v.x), native_log2(v.y), native_log2(v.z), native_log2(v.w)}; 1215 return v2; 1216} 1217 1218extern float __attribute__((overloadable)) native_log(float v) { 1219 return native_log2(v) * (1.f / 1.442695041f); 1220} 1221extern float2 __attribute__((overloadable)) native_log(float2 v) { 1222 return native_log2(v) * (1.f / 1.442695041f); 1223} 1224extern float3 __attribute__((overloadable)) native_log(float3 v) { 1225 return native_log2(v) * (1.f / 1.442695041f); 1226} 1227extern float4 __attribute__((overloadable)) native_log(float4 v) { 1228 return native_log2(v) * (1.f / 1.442695041f); 1229} 1230 1231extern float __attribute__((overloadable)) native_log10(float v) { 1232 return native_log2(v) * (1.f / 3.321928095f); 1233} 1234extern float2 __attribute__((overloadable)) native_log10(float2 v) { 1235 return native_log2(v) * (1.f / 3.321928095f); 1236} 1237extern float3 __attribute__((overloadable)) native_log10(float3 v) { 1238 return native_log2(v) * (1.f / 3.321928095f); 1239} 1240extern float4 __attribute__((overloadable)) native_log10(float4 v) { 1241 return native_log2(v) * (1.f / 3.321928095f); 1242} 1243 1244 1245extern float __attribute__((overloadable)) native_powr(float v, float y) { 1246 float v2 = native_log2(v); 1247 v2 = fmax(v2, -125.f); 1248 return native_exp2(v2 * y); 1249} 1250extern float2 __attribute__((overloadable)) native_powr(float2 v, float2 y) { 1251 float2 v2 = native_log2(v); 1252 v2 = fmax(v2, -125.f); 1253 return native_exp2(v2 * y); 1254} 1255extern float3 __attribute__((overloadable)) native_powr(float3 v, float3 y) { 1256 float3 v2 = native_log2(v); 1257 v2 = fmax(v2, -125.f); 1258 return native_exp2(v2 * y); 1259} 1260extern float4 __attribute__((overloadable)) native_powr(float4 v, float4 y) { 1261 float4 v2 = native_log2(v); 1262 v2 = fmax(v2, -125.f); 1263 return native_exp2(v2 * y); 1264} 1265 1266 1267#undef FN_FUNC_FN 1268#undef IN_FUNC_FN 1269#undef FN_FUNC_FN_FN 1270#undef FN_FUNC_FN_F 1271#undef FN_FUNC_FN_IN 1272#undef FN_FUNC_FN_I 1273#undef FN_FUNC_FN_PFN 1274#undef FN_FUNC_FN_PIN 1275#undef FN_FUNC_FN_FN_FN 1276#undef FN_FUNC_FN_FN_PIN 1277#undef XN_FUNC_YN 1278#undef UIN_FUNC_IN 1279#undef IN_FUNC_IN 1280#undef XN_FUNC_XN_XN_BODY 1281#undef IN_FUNC_IN_IN_BODY 1282