mdcore
0.1.5
|
00001 /* 00002 * Copyright 1993-2010 NVIDIA Corporation. All rights reserved. 00003 * 00004 * Please refer to the NVIDIA end user license agreement (EULA) associated 00005 * with this source code for terms and conditions that govern your use of 00006 * this software. Any use, reproduction, disclosure, or distribution of 00007 * this software and related documentation outside the terms of the EULA 00008 * is strictly prohibited. 00009 * 00010 */ 00011 00012 /* 00013 This file implements common mathematical operations on vector types 00014 (float3, float4 etc.) since these are not provided as standard by CUDA. 00015 00016 The syntax is modelled on the Cg standard library. 00017 00018 This is part of the CUTIL library and is not supported by NVIDIA. 00019 00020 Thanks to Linh Hah for additions and fixes. 00021 */ 00022 00023 #ifndef CUTIL_MATH_H 00024 #define CUTIL_MATH_H 00025 00026 #include "cuda_runtime.h" 00027 00028 typedef unsigned int uint; 00029 typedef unsigned short ushort; 00030 00031 #ifndef __CUDACC__ 00032 #include <math.h> 00033 00035 // host implementations of CUDA functions 00037 00038 inline float fminf(float a, float b) 00039 { 00040 return a < b ? a : b; 00041 } 00042 00043 inline float fmaxf(float a, float b) 00044 { 00045 return a > b ? a : b; 00046 } 00047 00048 inline int max(int a, int b) 00049 { 00050 return a > b ? a : b; 00051 } 00052 00053 inline int min(int a, int b) 00054 { 00055 return a < b ? a : b; 00056 } 00057 00058 inline float rsqrtf(float x) 00059 { 00060 return 1.0f / sqrtf(x); 00061 } 00062 #endif 00063 00065 // constructors 00067 00068 inline __host__ __device__ float2 make_float2(float s) 00069 { 00070 return make_float2(s, s); 00071 } 00072 inline __host__ __device__ float2 make_float2(float3 a) 00073 { 00074 return make_float2(a.x, a.y); 00075 } 00076 inline __host__ __device__ float2 make_float2(int2 a) 00077 { 00078 return make_float2(float(a.x), float(a.y)); 00079 } 00080 inline __host__ __device__ float2 make_float2(uint2 a) 00081 { 00082 return make_float2(float(a.x), float(a.y)); 00083 } 00084 00085 inline __host__ __device__ int2 make_int2(int s) 00086 { 00087 return make_int2(s, s); 00088 } 00089 inline __host__ __device__ int2 make_int2(int3 a) 00090 { 00091 return make_int2(a.x, a.y); 00092 } 00093 inline __host__ __device__ int2 make_int2(uint2 a) 00094 { 00095 return make_int2(int(a.x), int(a.y)); 00096 } 00097 inline __host__ __device__ int2 make_int2(float2 a) 00098 { 00099 return make_int2(int(a.x), int(a.y)); 00100 } 00101 00102 inline __host__ __device__ uint2 make_uint2(uint s) 00103 { 00104 return make_uint2(s, s); 00105 } 00106 inline __host__ __device__ uint2 make_uint2(uint3 a) 00107 { 00108 return make_uint2(a.x, a.y); 00109 } 00110 inline __host__ __device__ uint2 make_uint2(int2 a) 00111 { 00112 return make_uint2(uint(a.x), uint(a.y)); 00113 } 00114 00115 inline __host__ __device__ float3 make_float3(float s) 00116 { 00117 return make_float3(s, s, s); 00118 } 00119 inline __host__ __device__ float3 make_float3(float2 a) 00120 { 00121 return make_float3(a.x, a.y, 0.0f); 00122 } 00123 inline __host__ __device__ float3 make_float3(float2 a, float s) 00124 { 00125 return make_float3(a.x, a.y, s); 00126 } 00127 inline __host__ __device__ float3 make_float3(float4 a) 00128 { 00129 return make_float3(a.x, a.y, a.z); 00130 } 00131 inline __host__ __device__ float3 make_float3(int3 a) 00132 { 00133 return make_float3(float(a.x), float(a.y), float(a.z)); 00134 } 00135 inline __host__ __device__ float3 make_float3(uint3 a) 00136 { 00137 return make_float3(float(a.x), float(a.y), float(a.z)); 00138 } 00139 00140 inline __host__ __device__ int3 make_int3(int s) 00141 { 00142 return make_int3(s, s, s); 00143 } 00144 inline __host__ __device__ int3 make_int3(int2 a) 00145 { 00146 return make_int3(a.x, a.y, 0); 00147 } 00148 inline __host__ __device__ int3 make_int3(int2 a, int s) 00149 { 00150 return make_int3(a.x, a.y, s); 00151 } 00152 inline __host__ __device__ int3 make_int3(uint3 a) 00153 { 00154 return make_int3(int(a.x), int(a.y), int(a.z)); 00155 } 00156 inline __host__ __device__ int3 make_int3(float3 a) 00157 { 00158 return make_int3(int(a.x), int(a.y), int(a.z)); 00159 } 00160 00161 inline __host__ __device__ uint3 make_uint3(uint s) 00162 { 00163 return make_uint3(s, s, s); 00164 } 00165 inline __host__ __device__ uint3 make_uint3(uint2 a) 00166 { 00167 return make_uint3(a.x, a.y, 0); 00168 } 00169 inline __host__ __device__ uint3 make_uint3(uint2 a, uint s) 00170 { 00171 return make_uint3(a.x, a.y, s); 00172 } 00173 inline __host__ __device__ uint3 make_uint3(uint4 a) 00174 { 00175 return make_uint3(a.x, a.y, a.z); 00176 } 00177 inline __host__ __device__ uint3 make_uint3(int3 a) 00178 { 00179 return make_uint3(uint(a.x), uint(a.y), uint(a.z)); 00180 } 00181 00182 inline __host__ __device__ float4 make_float4(float s) 00183 { 00184 return make_float4(s, s, s, s); 00185 } 00186 inline __host__ __device__ float4 make_float4(float3 a) 00187 { 00188 return make_float4(a.x, a.y, a.z, 0.0f); 00189 } 00190 inline __host__ __device__ float4 make_float4(float3 a, float w) 00191 { 00192 return make_float4(a.x, a.y, a.z, w); 00193 } 00194 inline __host__ __device__ float4 make_float4(int4 a) 00195 { 00196 return make_float4(float(a.x), float(a.y), float(a.z), float(a.w)); 00197 } 00198 inline __host__ __device__ float4 make_float4(uint4 a) 00199 { 00200 return make_float4(float(a.x), float(a.y), float(a.z), float(a.w)); 00201 } 00202 00203 inline __host__ __device__ int4 make_int4(int s) 00204 { 00205 return make_int4(s, s, s, s); 00206 } 00207 inline __host__ __device__ int4 make_int4(int3 a) 00208 { 00209 return make_int4(a.x, a.y, a.z, 0); 00210 } 00211 inline __host__ __device__ int4 make_int4(int3 a, int w) 00212 { 00213 return make_int4(a.x, a.y, a.z, w); 00214 } 00215 inline __host__ __device__ int4 make_int4(uint4 a) 00216 { 00217 return make_int4(int(a.x), int(a.y), int(a.z), int(a.w)); 00218 } 00219 inline __host__ __device__ int4 make_int4(float4 a) 00220 { 00221 return make_int4(int(a.x), int(a.y), int(a.z), int(a.w)); 00222 } 00223 00224 00225 inline __host__ __device__ uint4 make_uint4(uint s) 00226 { 00227 return make_uint4(s, s, s, s); 00228 } 00229 inline __host__ __device__ uint4 make_uint4(uint3 a) 00230 { 00231 return make_uint4(a.x, a.y, a.z, 0); 00232 } 00233 inline __host__ __device__ uint4 make_uint4(uint3 a, uint w) 00234 { 00235 return make_uint4(a.x, a.y, a.z, w); 00236 } 00237 inline __host__ __device__ uint4 make_uint4(int4 a) 00238 { 00239 return make_uint4(uint(a.x), uint(a.y), uint(a.z), uint(a.w)); 00240 } 00241 00243 // negate 00245 00246 inline __host__ __device__ float2 operator-(float2 &a) 00247 { 00248 return make_float2(-a.x, -a.y); 00249 } 00250 inline __host__ __device__ int2 operator-(int2 &a) 00251 { 00252 return make_int2(-a.x, -a.y); 00253 } 00254 inline __host__ __device__ float3 operator-(float3 &a) 00255 { 00256 return make_float3(-a.x, -a.y, -a.z); 00257 } 00258 inline __host__ __device__ int3 operator-(int3 &a) 00259 { 00260 return make_int3(-a.x, -a.y, -a.z); 00261 } 00262 inline __host__ __device__ float4 operator-(float4 &a) 00263 { 00264 return make_float4(-a.x, -a.y, -a.z, -a.w); 00265 } 00266 inline __host__ __device__ int4 operator-(int4 &a) 00267 { 00268 return make_int4(-a.x, -a.y, -a.z, -a.w); 00269 } 00270 00272 // addition 00274 00275 inline __host__ __device__ float2 operator+(float2 a, float2 b) 00276 { 00277 return make_float2(a.x + b.x, a.y + b.y); 00278 } 00279 inline __host__ __device__ void operator+=(float2 &a, float2 b) 00280 { 00281 a.x += b.x; a.y += b.y; 00282 } 00283 inline __host__ __device__ float2 operator+(float2 a, float b) 00284 { 00285 return make_float2(a.x + b, a.y + b); 00286 } 00287 inline __host__ __device__ float2 operator+(float b, float2 a) 00288 { 00289 return make_float2(a.x + b, a.y + b); 00290 } 00291 inline __host__ __device__ void operator+=(float2 &a, float b) 00292 { 00293 a.x += b; a.y += b; 00294 } 00295 00296 inline __host__ __device__ int2 operator+(int2 a, int2 b) 00297 { 00298 return make_int2(a.x + b.x, a.y + b.y); 00299 } 00300 inline __host__ __device__ void operator+=(int2 &a, int2 b) 00301 { 00302 a.x += b.x; a.y += b.y; 00303 } 00304 inline __host__ __device__ int2 operator+(int2 a, int b) 00305 { 00306 return make_int2(a.x + b, a.y + b); 00307 } 00308 inline __host__ __device__ int2 operator+(int b, int2 a) 00309 { 00310 return make_int2(a.x + b, a.y + b); 00311 } 00312 inline __host__ __device__ void operator+=(int2 &a, int b) 00313 { 00314 a.x += b; a.y += b; 00315 } 00316 00317 inline __host__ __device__ uint2 operator+(uint2 a, uint2 b) 00318 { 00319 return make_uint2(a.x + b.x, a.y + b.y); 00320 } 00321 inline __host__ __device__ void operator+=(uint2 &a, uint2 b) 00322 { 00323 a.x += b.x; a.y += b.y; 00324 } 00325 inline __host__ __device__ uint2 operator+(uint2 a, uint b) 00326 { 00327 return make_uint2(a.x + b, a.y + b); 00328 } 00329 inline __host__ __device__ uint2 operator+(uint b, uint2 a) 00330 { 00331 return make_uint2(a.x + b, a.y + b); 00332 } 00333 inline __host__ __device__ void operator+=(uint2 &a, uint b) 00334 { 00335 a.x += b; a.y += b; 00336 } 00337 00338 00339 inline __host__ __device__ float3 operator+(float3 a, float3 b) 00340 { 00341 return make_float3(a.x + b.x, a.y + b.y, a.z + b.z); 00342 } 00343 inline __host__ __device__ void operator+=(float3 &a, float3 b) 00344 { 00345 a.x += b.x; a.y += b.y; a.z += b.z; 00346 } 00347 inline __host__ __device__ float3 operator+(float3 a, float b) 00348 { 00349 return make_float3(a.x + b, a.y + b, a.z + b); 00350 } 00351 inline __host__ __device__ void operator+=(float3 &a, float b) 00352 { 00353 a.x += b; a.y += b; a.z += b; 00354 } 00355 00356 inline __host__ __device__ int3 operator+(int3 a, int3 b) 00357 { 00358 return make_int3(a.x + b.x, a.y + b.y, a.z + b.z); 00359 } 00360 inline __host__ __device__ void operator+=(int3 &a, int3 b) 00361 { 00362 a.x += b.x; a.y += b.y; a.z += b.z; 00363 } 00364 inline __host__ __device__ int3 operator+(int3 a, int b) 00365 { 00366 return make_int3(a.x + b, a.y + b, a.z + b); 00367 } 00368 inline __host__ __device__ void operator+=(int3 &a, int b) 00369 { 00370 a.x += b; a.y += b; a.z += b; 00371 } 00372 00373 inline __host__ __device__ uint3 operator+(uint3 a, uint3 b) 00374 { 00375 return make_uint3(a.x + b.x, a.y + b.y, a.z + b.z); 00376 } 00377 inline __host__ __device__ void operator+=(uint3 &a, uint3 b) 00378 { 00379 a.x += b.x; a.y += b.y; a.z += b.z; 00380 } 00381 inline __host__ __device__ uint3 operator+(uint3 a, uint b) 00382 { 00383 return make_uint3(a.x + b, a.y + b, a.z + b); 00384 } 00385 inline __host__ __device__ void operator+=(uint3 &a, uint b) 00386 { 00387 a.x += b; a.y += b; a.z += b; 00388 } 00389 00390 inline __host__ __device__ int3 operator+(int b, int3 a) 00391 { 00392 return make_int3(a.x + b, a.y + b, a.z + b); 00393 } 00394 inline __host__ __device__ uint3 operator+(uint b, uint3 a) 00395 { 00396 return make_uint3(a.x + b, a.y + b, a.z + b); 00397 } 00398 inline __host__ __device__ float3 operator+(float b, float3 a) 00399 { 00400 return make_float3(a.x + b, a.y + b, a.z + b); 00401 } 00402 00403 inline __host__ __device__ float4 operator+(float4 a, float4 b) 00404 { 00405 return make_float4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); 00406 } 00407 inline __host__ __device__ void operator+=(float4 &a, float4 b) 00408 { 00409 a.x += b.x; a.y += b.y; a.z += b.z; a.w += b.w; 00410 } 00411 inline __host__ __device__ float4 operator+(float4 a, float b) 00412 { 00413 return make_float4(a.x + b, a.y + b, a.z + b, a.w + b); 00414 } 00415 inline __host__ __device__ float4 operator+(float b, float4 a) 00416 { 00417 return make_float4(a.x + b, a.y + b, a.z + b, a.w + b); 00418 } 00419 inline __host__ __device__ void operator+=(float4 &a, float b) 00420 { 00421 a.x += b; a.y += b; a.z += b; a.w += b; 00422 } 00423 00424 inline __host__ __device__ int4 operator+(int4 a, int4 b) 00425 { 00426 return make_int4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); 00427 } 00428 inline __host__ __device__ void operator+=(int4 &a, int4 b) 00429 { 00430 a.x += b.x; a.y += b.y; a.z += b.z; a.w += b.w; 00431 } 00432 inline __host__ __device__ int4 operator+(int4 a, int b) 00433 { 00434 return make_int4(a.x + b, a.y + b, a.z + b, a.w + b); 00435 } 00436 inline __host__ __device__ int4 operator+(int b, int4 a) 00437 { 00438 return make_int4(a.x + b, a.y + b, a.z + b, a.w + b); 00439 } 00440 inline __host__ __device__ void operator+=(int4 &a, int b) 00441 { 00442 a.x += b; a.y += b; a.z += b; a.w += b; 00443 } 00444 00445 inline __host__ __device__ uint4 operator+(uint4 a, uint4 b) 00446 { 00447 return make_uint4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); 00448 } 00449 inline __host__ __device__ void operator+=(uint4 &a, uint4 b) 00450 { 00451 a.x += b.x; a.y += b.y; a.z += b.z; a.w += b.w; 00452 } 00453 inline __host__ __device__ uint4 operator+(uint4 a, uint b) 00454 { 00455 return make_uint4(a.x + b, a.y + b, a.z + b, a.w + b); 00456 } 00457 inline __host__ __device__ uint4 operator+(uint b, uint4 a) 00458 { 00459 return make_uint4(a.x + b, a.y + b, a.z + b, a.w + b); 00460 } 00461 inline __host__ __device__ void operator+=(uint4 &a, uint b) 00462 { 00463 a.x += b; a.y += b; a.z += b; a.w += b; 00464 } 00465 00467 // subtract 00469 00470 inline __host__ __device__ float2 operator-(float2 a, float2 b) 00471 { 00472 return make_float2(a.x - b.x, a.y - b.y); 00473 } 00474 inline __host__ __device__ void operator-=(float2 &a, float2 b) 00475 { 00476 a.x -= b.x; a.y -= b.y; 00477 } 00478 inline __host__ __device__ float2 operator-(float2 a, float b) 00479 { 00480 return make_float2(a.x - b, a.y - b); 00481 } 00482 inline __host__ __device__ float2 operator-(float b, float2 a) 00483 { 00484 return make_float2(b - a.x, b - a.y); 00485 } 00486 inline __host__ __device__ void operator-=(float2 &a, float b) 00487 { 00488 a.x -= b; a.y -= b; 00489 } 00490 00491 inline __host__ __device__ int2 operator-(int2 a, int2 b) 00492 { 00493 return make_int2(a.x - b.x, a.y - b.y); 00494 } 00495 inline __host__ __device__ void operator-=(int2 &a, int2 b) 00496 { 00497 a.x -= b.x; a.y -= b.y; 00498 } 00499 inline __host__ __device__ int2 operator-(int2 a, int b) 00500 { 00501 return make_int2(a.x - b, a.y - b); 00502 } 00503 inline __host__ __device__ int2 operator-(int b, int2 a) 00504 { 00505 return make_int2(b - a.x, b - a.y); 00506 } 00507 inline __host__ __device__ void operator-=(int2 &a, int b) 00508 { 00509 a.x -= b; a.y -= b; 00510 } 00511 00512 inline __host__ __device__ uint2 operator-(uint2 a, uint2 b) 00513 { 00514 return make_uint2(a.x - b.x, a.y - b.y); 00515 } 00516 inline __host__ __device__ void operator-=(uint2 &a, uint2 b) 00517 { 00518 a.x -= b.x; a.y -= b.y; 00519 } 00520 inline __host__ __device__ uint2 operator-(uint2 a, uint b) 00521 { 00522 return make_uint2(a.x - b, a.y - b); 00523 } 00524 inline __host__ __device__ uint2 operator-(uint b, uint2 a) 00525 { 00526 return make_uint2(b - a.x, b - a.y); 00527 } 00528 inline __host__ __device__ void operator-=(uint2 &a, uint b) 00529 { 00530 a.x -= b; a.y -= b; 00531 } 00532 00533 inline __host__ __device__ float3 operator-(float3 a, float3 b) 00534 { 00535 return make_float3(a.x - b.x, a.y - b.y, a.z - b.z); 00536 } 00537 inline __host__ __device__ void operator-=(float3 &a, float3 b) 00538 { 00539 a.x -= b.x; a.y -= b.y; a.z -= b.z; 00540 } 00541 inline __host__ __device__ float3 operator-(float3 a, float b) 00542 { 00543 return make_float3(a.x - b, a.y - b, a.z - b); 00544 } 00545 inline __host__ __device__ float3 operator-(float b, float3 a) 00546 { 00547 return make_float3(b - a.x, b - a.y, b - a.z); 00548 } 00549 inline __host__ __device__ void operator-=(float3 &a, float b) 00550 { 00551 a.x -= b; a.y -= b; a.z -= b; 00552 } 00553 00554 inline __host__ __device__ int3 operator-(int3 a, int3 b) 00555 { 00556 return make_int3(a.x - b.x, a.y - b.y, a.z - b.z); 00557 } 00558 inline __host__ __device__ void operator-=(int3 &a, int3 b) 00559 { 00560 a.x -= b.x; a.y -= b.y; a.z -= b.z; 00561 } 00562 inline __host__ __device__ int3 operator-(int3 a, int b) 00563 { 00564 return make_int3(a.x - b, a.y - b, a.z - b); 00565 } 00566 inline __host__ __device__ int3 operator-(int b, int3 a) 00567 { 00568 return make_int3(b - a.x, b - a.y, b - a.z); 00569 } 00570 inline __host__ __device__ void operator-=(int3 &a, int b) 00571 { 00572 a.x -= b; a.y -= b; a.z -= b; 00573 } 00574 00575 inline __host__ __device__ uint3 operator-(uint3 a, uint3 b) 00576 { 00577 return make_uint3(a.x - b.x, a.y - b.y, a.z - b.z); 00578 } 00579 inline __host__ __device__ void operator-=(uint3 &a, uint3 b) 00580 { 00581 a.x -= b.x; a.y -= b.y; a.z -= b.z; 00582 } 00583 inline __host__ __device__ uint3 operator-(uint3 a, uint b) 00584 { 00585 return make_uint3(a.x - b, a.y - b, a.z - b); 00586 } 00587 inline __host__ __device__ uint3 operator-(uint b, uint3 a) 00588 { 00589 return make_uint3(b - a.x, b - a.y, b - a.z); 00590 } 00591 inline __host__ __device__ void operator-=(uint3 &a, uint b) 00592 { 00593 a.x -= b; a.y -= b; a.z -= b; 00594 } 00595 00596 inline __host__ __device__ float4 operator-(float4 a, float4 b) 00597 { 00598 return make_float4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); 00599 } 00600 inline __host__ __device__ void operator-=(float4 &a, float4 b) 00601 { 00602 a.x -= b.x; a.y -= b.y; a.z -= b.z; a.w -= b.w; 00603 } 00604 inline __host__ __device__ float4 operator-(float4 a, float b) 00605 { 00606 return make_float4(a.x - b, a.y - b, a.z - b, a.w - b); 00607 } 00608 inline __host__ __device__ void operator-=(float4 &a, float b) 00609 { 00610 a.x -= b; a.y -= b; a.z -= b; a.w -= b; 00611 } 00612 00613 inline __host__ __device__ int4 operator-(int4 a, int4 b) 00614 { 00615 return make_int4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); 00616 } 00617 inline __host__ __device__ void operator-=(int4 &a, int4 b) 00618 { 00619 a.x -= b.x; a.y -= b.y; a.z -= b.z; a.w -= b.w; 00620 } 00621 inline __host__ __device__ int4 operator-(int4 a, int b) 00622 { 00623 return make_int4(a.x - b, a.y - b, a.z - b, a.w - b); 00624 } 00625 inline __host__ __device__ int4 operator-(int b, int4 a) 00626 { 00627 return make_int4(b - a.x, b - a.y, b - a.z, b - a.w); 00628 } 00629 inline __host__ __device__ void operator-=(int4 &a, int b) 00630 { 00631 a.x -= b; a.y -= b; a.z -= b; a.w -= b; 00632 } 00633 00634 inline __host__ __device__ uint4 operator-(uint4 a, uint4 b) 00635 { 00636 return make_uint4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); 00637 } 00638 inline __host__ __device__ void operator-=(uint4 &a, uint4 b) 00639 { 00640 a.x -= b.x; a.y -= b.y; a.z -= b.z; a.w -= b.w; 00641 } 00642 inline __host__ __device__ uint4 operator-(uint4 a, uint b) 00643 { 00644 return make_uint4(a.x - b, a.y - b, a.z - b, a.w - b); 00645 } 00646 inline __host__ __device__ uint4 operator-(uint b, uint4 a) 00647 { 00648 return make_uint4(b - a.x, b - a.y, b - a.z, b - a.w); 00649 } 00650 inline __host__ __device__ void operator-=(uint4 &a, uint b) 00651 { 00652 a.x -= b; a.y -= b; a.z -= b; a.w -= b; 00653 } 00654 00656 // multiply 00658 00659 inline __host__ __device__ float2 operator*(float2 a, float2 b) 00660 { 00661 return make_float2(a.x * b.x, a.y * b.y); 00662 } 00663 inline __host__ __device__ void operator*=(float2 &a, float2 b) 00664 { 00665 a.x *= b.x; a.y *= b.y; 00666 } 00667 inline __host__ __device__ float2 operator*(float2 a, float b) 00668 { 00669 return make_float2(a.x * b, a.y * b); 00670 } 00671 inline __host__ __device__ float2 operator*(float b, float2 a) 00672 { 00673 return make_float2(b * a.x, b * a.y); 00674 } 00675 inline __host__ __device__ void operator*=(float2 &a, float b) 00676 { 00677 a.x *= b; a.y *= b; 00678 } 00679 00680 inline __host__ __device__ int2 operator*(int2 a, int2 b) 00681 { 00682 return make_int2(a.x * b.x, a.y * b.y); 00683 } 00684 inline __host__ __device__ void operator*=(int2 &a, int2 b) 00685 { 00686 a.x *= b.x; a.y *= b.y; 00687 } 00688 inline __host__ __device__ int2 operator*(int2 a, int b) 00689 { 00690 return make_int2(a.x * b, a.y * b); 00691 } 00692 inline __host__ __device__ int2 operator*(int b, int2 a) 00693 { 00694 return make_int2(b * a.x, b * a.y); 00695 } 00696 inline __host__ __device__ void operator*=(int2 &a, int b) 00697 { 00698 a.x *= b; a.y *= b; 00699 } 00700 00701 inline __host__ __device__ uint2 operator*(uint2 a, uint2 b) 00702 { 00703 return make_uint2(a.x * b.x, a.y * b.y); 00704 } 00705 inline __host__ __device__ void operator*=(uint2 &a, uint2 b) 00706 { 00707 a.x *= b.x; a.y *= b.y; 00708 } 00709 inline __host__ __device__ uint2 operator*(uint2 a, uint b) 00710 { 00711 return make_uint2(a.x * b, a.y * b); 00712 } 00713 inline __host__ __device__ uint2 operator*(uint b, uint2 a) 00714 { 00715 return make_uint2(b * a.x, b * a.y); 00716 } 00717 inline __host__ __device__ void operator*=(uint2 &a, uint b) 00718 { 00719 a.x *= b; a.y *= b; 00720 } 00721 00722 inline __host__ __device__ float3 operator*(float3 a, float3 b) 00723 { 00724 return make_float3(a.x * b.x, a.y * b.y, a.z * b.z); 00725 } 00726 inline __host__ __device__ void operator*=(float3 &a, float3 b) 00727 { 00728 a.x *= b.x; a.y *= b.y; a.z *= b.z; 00729 } 00730 inline __host__ __device__ float3 operator*(float3 a, float b) 00731 { 00732 return make_float3(a.x * b, a.y * b, a.z * b); 00733 } 00734 inline __host__ __device__ float3 operator*(float b, float3 a) 00735 { 00736 return make_float3(b * a.x, b * a.y, b * a.z); 00737 } 00738 inline __host__ __device__ void operator*=(float3 &a, float b) 00739 { 00740 a.x *= b; a.y *= b; a.z *= b; 00741 } 00742 00743 inline __host__ __device__ int3 operator*(int3 a, int3 b) 00744 { 00745 return make_int3(a.x * b.x, a.y * b.y, a.z * b.z); 00746 } 00747 inline __host__ __device__ void operator*=(int3 &a, int3 b) 00748 { 00749 a.x *= b.x; a.y *= b.y; a.z *= b.z; 00750 } 00751 inline __host__ __device__ int3 operator*(int3 a, int b) 00752 { 00753 return make_int3(a.x * b, a.y * b, a.z * b); 00754 } 00755 inline __host__ __device__ int3 operator*(int b, int3 a) 00756 { 00757 return make_int3(b * a.x, b * a.y, b * a.z); 00758 } 00759 inline __host__ __device__ void operator*=(int3 &a, int b) 00760 { 00761 a.x *= b; a.y *= b; a.z *= b; 00762 } 00763 00764 inline __host__ __device__ uint3 operator*(uint3 a, uint3 b) 00765 { 00766 return make_uint3(a.x * b.x, a.y * b.y, a.z * b.z); 00767 } 00768 inline __host__ __device__ void operator*=(uint3 &a, uint3 b) 00769 { 00770 a.x *= b.x; a.y *= b.y; a.z *= b.z; 00771 } 00772 inline __host__ __device__ uint3 operator*(uint3 a, uint b) 00773 { 00774 return make_uint3(a.x * b, a.y * b, a.z * b); 00775 } 00776 inline __host__ __device__ uint3 operator*(uint b, uint3 a) 00777 { 00778 return make_uint3(b * a.x, b * a.y, b * a.z); 00779 } 00780 inline __host__ __device__ void operator*=(uint3 &a, uint b) 00781 { 00782 a.x *= b; a.y *= b; a.z *= b; 00783 } 00784 00785 inline __host__ __device__ float4 operator*(float4 a, float4 b) 00786 { 00787 return make_float4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w); 00788 } 00789 inline __host__ __device__ void operator*=(float4 &a, float4 b) 00790 { 00791 a.x *= b.x; a.y *= b.y; a.z *= b.z; a.w *= b.w; 00792 } 00793 inline __host__ __device__ float4 operator*(float4 a, float b) 00794 { 00795 return make_float4(a.x * b, a.y * b, a.z * b, a.w * b); 00796 } 00797 inline __host__ __device__ float4 operator*(float b, float4 a) 00798 { 00799 return make_float4(b * a.x, b * a.y, b * a.z, b * a.w); 00800 } 00801 inline __host__ __device__ void operator*=(float4 &a, float b) 00802 { 00803 a.x *= b; a.y *= b; a.z *= b; a.w *= b; 00804 } 00805 00806 inline __host__ __device__ int4 operator*(int4 a, int4 b) 00807 { 00808 return make_int4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w); 00809 } 00810 inline __host__ __device__ void operator*=(int4 &a, int4 b) 00811 { 00812 a.x *= b.x; a.y *= b.y; a.z *= b.z; a.w *= b.w; 00813 } 00814 inline __host__ __device__ int4 operator*(int4 a, int b) 00815 { 00816 return make_int4(a.x * b, a.y * b, a.z * b, a.w * b); 00817 } 00818 inline __host__ __device__ int4 operator*(int b, int4 a) 00819 { 00820 return make_int4(b * a.x, b * a.y, b * a.z, b * a.w); 00821 } 00822 inline __host__ __device__ void operator*=(int4 &a, int b) 00823 { 00824 a.x *= b; a.y *= b; a.z *= b; a.w *= b; 00825 } 00826 00827 inline __host__ __device__ uint4 operator*(uint4 a, uint4 b) 00828 { 00829 return make_uint4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w); 00830 } 00831 inline __host__ __device__ void operator*=(uint4 &a, uint4 b) 00832 { 00833 a.x *= b.x; a.y *= b.y; a.z *= b.z; a.w *= b.w; 00834 } 00835 inline __host__ __device__ uint4 operator*(uint4 a, uint b) 00836 { 00837 return make_uint4(a.x * b, a.y * b, a.z * b, a.w * b); 00838 } 00839 inline __host__ __device__ uint4 operator*(uint b, uint4 a) 00840 { 00841 return make_uint4(b * a.x, b * a.y, b * a.z, b * a.w); 00842 } 00843 inline __host__ __device__ void operator*=(uint4 &a, uint b) 00844 { 00845 a.x *= b; a.y *= b; a.z *= b; a.w *= b; 00846 } 00847 00849 // divide 00851 00852 inline __host__ __device__ float2 operator/(float2 a, float2 b) 00853 { 00854 return make_float2(a.x / b.x, a.y / b.y); 00855 } 00856 inline __host__ __device__ void operator/=(float2 &a, float2 b) 00857 { 00858 a.x /= b.x; a.y /= b.y; 00859 } 00860 inline __host__ __device__ float2 operator/(float2 a, float b) 00861 { 00862 return make_float2(a.x / b, a.y / b); 00863 } 00864 inline __host__ __device__ void operator/=(float2 &a, float b) 00865 { 00866 a.x /= b; a.y /= b; 00867 } 00868 inline __host__ __device__ float2 operator/(float b, float2 a) 00869 { 00870 return make_float2(b / a.x, b / a.y); 00871 } 00872 00873 inline __host__ __device__ float3 operator/(float3 a, float3 b) 00874 { 00875 return make_float3(a.x / b.x, a.y / b.y, a.z / b.z); 00876 } 00877 inline __host__ __device__ void operator/=(float3 &a, float3 b) 00878 { 00879 a.x /= b.x; a.y /= b.y; a.z /= b.z; 00880 } 00881 inline __host__ __device__ float3 operator/(float3 a, float b) 00882 { 00883 return make_float3(a.x / b, a.y / b, a.z / b); 00884 } 00885 inline __host__ __device__ void operator/=(float3 &a, float b) 00886 { 00887 a.x /= b; a.y /= b; a.z /= b; 00888 } 00889 inline __host__ __device__ float3 operator/(float b, float3 a) 00890 { 00891 return make_float3(b / a.x, b / a.y, b / a.z); 00892 } 00893 00894 inline __host__ __device__ float4 operator/(float4 a, float4 b) 00895 { 00896 return make_float4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w); 00897 } 00898 inline __host__ __device__ void operator/=(float4 &a, float4 b) 00899 { 00900 a.x /= b.x; a.y /= b.y; a.z /= b.z; a.w /= b.w; 00901 } 00902 inline __host__ __device__ float4 operator/(float4 a, float b) 00903 { 00904 return make_float4(a.x / b, a.y / b, a.z / b, a.w / b); 00905 } 00906 inline __host__ __device__ void operator/=(float4 &a, float b) 00907 { 00908 a.x /= b; a.y /= b; a.z /= b; a.w /= b; 00909 } 00910 inline __host__ __device__ float4 operator/(float b, float4 a){ 00911 return make_float4(b / a.x, b / a.y, b / a.z, b / a.w); 00912 } 00913 00915 // min 00917 00918 inline __host__ __device__ float2 fminf(float2 a, float2 b) 00919 { 00920 return make_float2(fminf(a.x,b.x), fminf(a.y,b.y)); 00921 } 00922 inline __host__ __device__ float3 fminf(float3 a, float3 b) 00923 { 00924 return make_float3(fminf(a.x,b.x), fminf(a.y,b.y), fminf(a.z,b.z)); 00925 } 00926 inline __host__ __device__ float4 fminf(float4 a, float4 b) 00927 { 00928 return make_float4(fminf(a.x,b.x), fminf(a.y,b.y), fminf(a.z,b.z), fminf(a.w,b.w)); 00929 } 00930 00931 inline __host__ __device__ int2 min(int2 a, int2 b) 00932 { 00933 return make_int2(min(a.x,b.x), min(a.y,b.y)); 00934 } 00935 inline __host__ __device__ int3 min(int3 a, int3 b) 00936 { 00937 return make_int3(min(a.x,b.x), min(a.y,b.y), min(a.z,b.z)); 00938 } 00939 inline __host__ __device__ int4 min(int4 a, int4 b) 00940 { 00941 return make_int4(min(a.x,b.x), min(a.y,b.y), min(a.z,b.z), min(a.w,b.w)); 00942 } 00943 00944 inline __host__ __device__ uint2 min(uint2 a, uint2 b) 00945 { 00946 return make_uint2(min(a.x,b.x), min(a.y,b.y)); 00947 } 00948 inline __host__ __device__ uint3 min(uint3 a, uint3 b) 00949 { 00950 return make_uint3(min(a.x,b.x), min(a.y,b.y), min(a.z,b.z)); 00951 } 00952 inline __host__ __device__ uint4 min(uint4 a, uint4 b) 00953 { 00954 return make_uint4(min(a.x,b.x), min(a.y,b.y), min(a.z,b.z), min(a.w,b.w)); 00955 } 00956 00958 // max 00960 00961 inline __host__ __device__ float2 fmaxf(float2 a, float2 b) 00962 { 00963 return make_float2(fmaxf(a.x,b.x), fmaxf(a.y,b.y)); 00964 } 00965 inline __host__ __device__ float3 fmaxf(float3 a, float3 b) 00966 { 00967 return make_float3(fmaxf(a.x,b.x), fmaxf(a.y,b.y), fmaxf(a.z,b.z)); 00968 } 00969 inline __host__ __device__ float4 fmaxf(float4 a, float4 b) 00970 { 00971 return make_float4(fmaxf(a.x,b.x), fmaxf(a.y,b.y), fmaxf(a.z,b.z), fmaxf(a.w,b.w)); 00972 } 00973 00974 inline __host__ __device__ int2 max(int2 a, int2 b) 00975 { 00976 return make_int2(max(a.x,b.x), max(a.y,b.y)); 00977 } 00978 inline __host__ __device__ int3 max(int3 a, int3 b) 00979 { 00980 return make_int3(max(a.x,b.x), max(a.y,b.y), max(a.z,b.z)); 00981 } 00982 inline __host__ __device__ int4 max(int4 a, int4 b) 00983 { 00984 return make_int4(max(a.x,b.x), max(a.y,b.y), max(a.z,b.z), max(a.w,b.w)); 00985 } 00986 00987 inline __host__ __device__ uint2 max(uint2 a, uint2 b) 00988 { 00989 return make_uint2(max(a.x,b.x), max(a.y,b.y)); 00990 } 00991 inline __host__ __device__ uint3 max(uint3 a, uint3 b) 00992 { 00993 return make_uint3(max(a.x,b.x), max(a.y,b.y), max(a.z,b.z)); 00994 } 00995 inline __host__ __device__ uint4 max(uint4 a, uint4 b) 00996 { 00997 return make_uint4(max(a.x,b.x), max(a.y,b.y), max(a.z,b.z), max(a.w,b.w)); 00998 } 00999 01001 // lerp 01002 // - linear interpolation between a and b, based on value t in [0, 1] range 01004 01005 inline __device__ __host__ float lerp(float a, float b, float t) 01006 { 01007 return a + t*(b-a); 01008 } 01009 inline __device__ __host__ float2 lerp(float2 a, float2 b, float t) 01010 { 01011 return a + t*(b-a); 01012 } 01013 inline __device__ __host__ float3 lerp(float3 a, float3 b, float t) 01014 { 01015 return a + t*(b-a); 01016 } 01017 inline __device__ __host__ float4 lerp(float4 a, float4 b, float t) 01018 { 01019 return a + t*(b-a); 01020 } 01021 01023 // clamp 01024 // - clamp the value v to be in the range [a, b] 01026 01027 inline __device__ __host__ float clamp(float f, float a, float b) 01028 { 01029 return fmaxf(a, fminf(f, b)); 01030 } 01031 inline __device__ __host__ int clamp(int f, int a, int b) 01032 { 01033 return max(a, min(f, b)); 01034 } 01035 inline __device__ __host__ uint clamp(uint f, uint a, uint b) 01036 { 01037 return max(a, min(f, b)); 01038 } 01039 01040 inline __device__ __host__ float2 clamp(float2 v, float a, float b) 01041 { 01042 return make_float2(clamp(v.x, a, b), clamp(v.y, a, b)); 01043 } 01044 inline __device__ __host__ float2 clamp(float2 v, float2 a, float2 b) 01045 { 01046 return make_float2(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y)); 01047 } 01048 inline __device__ __host__ float3 clamp(float3 v, float a, float b) 01049 { 01050 return make_float3(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b)); 01051 } 01052 inline __device__ __host__ float3 clamp(float3 v, float3 a, float3 b) 01053 { 01054 return make_float3(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z)); 01055 } 01056 inline __device__ __host__ float4 clamp(float4 v, float a, float b) 01057 { 01058 return make_float4(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b), clamp(v.w, a, b)); 01059 } 01060 inline __device__ __host__ float4 clamp(float4 v, float4 a, float4 b) 01061 { 01062 return make_float4(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z), clamp(v.w, a.w, b.w)); 01063 } 01064 01065 inline __device__ __host__ int2 clamp(int2 v, int a, int b) 01066 { 01067 return make_int2(clamp(v.x, a, b), clamp(v.y, a, b)); 01068 } 01069 inline __device__ __host__ int2 clamp(int2 v, int2 a, int2 b) 01070 { 01071 return make_int2(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y)); 01072 } 01073 inline __device__ __host__ int3 clamp(int3 v, int a, int b) 01074 { 01075 return make_int3(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b)); 01076 } 01077 inline __device__ __host__ int3 clamp(int3 v, int3 a, int3 b) 01078 { 01079 return make_int3(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z)); 01080 } 01081 inline __device__ __host__ int4 clamp(int4 v, int a, int b) 01082 { 01083 return make_int4(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b), clamp(v.w, a, b)); 01084 } 01085 inline __device__ __host__ int4 clamp(int4 v, int4 a, int4 b) 01086 { 01087 return make_int4(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z), clamp(v.w, a.w, b.w)); 01088 } 01089 01090 inline __device__ __host__ uint2 clamp(uint2 v, uint a, uint b) 01091 { 01092 return make_uint2(clamp(v.x, a, b), clamp(v.y, a, b)); 01093 } 01094 inline __device__ __host__ uint2 clamp(uint2 v, uint2 a, uint2 b) 01095 { 01096 return make_uint2(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y)); 01097 } 01098 inline __device__ __host__ uint3 clamp(uint3 v, uint a, uint b) 01099 { 01100 return make_uint3(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b)); 01101 } 01102 inline __device__ __host__ uint3 clamp(uint3 v, uint3 a, uint3 b) 01103 { 01104 return make_uint3(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z)); 01105 } 01106 inline __device__ __host__ uint4 clamp(uint4 v, uint a, uint b) 01107 { 01108 return make_uint4(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b), clamp(v.w, a, b)); 01109 } 01110 inline __device__ __host__ uint4 clamp(uint4 v, uint4 a, uint4 b) 01111 { 01112 return make_uint4(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z), clamp(v.w, a.w, b.w)); 01113 } 01114 01116 // dot product 01118 01119 inline __host__ __device__ float dot(float2 a, float2 b) 01120 { 01121 return a.x * b.x + a.y * b.y; 01122 } 01123 inline __host__ __device__ float dot(float3 a, float3 b) 01124 { 01125 return a.x * b.x + a.y * b.y + a.z * b.z; 01126 } 01127 inline __host__ __device__ float dot(float4 a, float4 b) 01128 { 01129 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; 01130 } 01131 01132 inline __host__ __device__ int dot(int2 a, int2 b) 01133 { 01134 return a.x * b.x + a.y * b.y; 01135 } 01136 inline __host__ __device__ int dot(int3 a, int3 b) 01137 { 01138 return a.x * b.x + a.y * b.y + a.z * b.z; 01139 } 01140 inline __host__ __device__ int dot(int4 a, int4 b) 01141 { 01142 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; 01143 } 01144 01145 inline __host__ __device__ uint dot(uint2 a, uint2 b) 01146 { 01147 return a.x * b.x + a.y * b.y; 01148 } 01149 inline __host__ __device__ uint dot(uint3 a, uint3 b) 01150 { 01151 return a.x * b.x + a.y * b.y + a.z * b.z; 01152 } 01153 inline __host__ __device__ uint dot(uint4 a, uint4 b) 01154 { 01155 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; 01156 } 01157 01159 // length 01161 01162 inline __host__ __device__ float length(float2 v) 01163 { 01164 return sqrtf(dot(v, v)); 01165 } 01166 inline __host__ __device__ float length(float3 v) 01167 { 01168 return sqrtf(dot(v, v)); 01169 } 01170 inline __host__ __device__ float length(float4 v) 01171 { 01172 return sqrtf(dot(v, v)); 01173 } 01174 01176 // normalize 01178 01179 inline __host__ __device__ float2 normalize(float2 v) 01180 { 01181 float invLen = rsqrtf(dot(v, v)); 01182 return v * invLen; 01183 } 01184 inline __host__ __device__ float3 normalize(float3 v) 01185 { 01186 float invLen = rsqrtf(dot(v, v)); 01187 return v * invLen; 01188 } 01189 inline __host__ __device__ float4 normalize(float4 v) 01190 { 01191 float invLen = rsqrtf(dot(v, v)); 01192 return v * invLen; 01193 } 01194 01196 // floor 01198 01199 inline __host__ __device__ float2 floorf(float2 v) 01200 { 01201 return make_float2(floorf(v.x), floorf(v.y)); 01202 } 01203 inline __host__ __device__ float3 floorf(float3 v) 01204 { 01205 return make_float3(floorf(v.x), floorf(v.y), floorf(v.z)); 01206 } 01207 inline __host__ __device__ float4 floorf(float4 v) 01208 { 01209 return make_float4(floorf(v.x), floorf(v.y), floorf(v.z), floorf(v.w)); 01210 } 01211 01213 // frac - returns the fractional portion of a scalar or each vector component 01215 01216 inline __host__ __device__ float fracf(float v) 01217 { 01218 return v - floorf(v); 01219 } 01220 inline __host__ __device__ float2 fracf(float2 v) 01221 { 01222 return make_float2(fracf(v.x), fracf(v.y)); 01223 } 01224 inline __host__ __device__ float3 fracf(float3 v) 01225 { 01226 return make_float3(fracf(v.x), fracf(v.y), fracf(v.z)); 01227 } 01228 inline __host__ __device__ float4 fracf(float4 v) 01229 { 01230 return make_float4(fracf(v.x), fracf(v.y), fracf(v.z), fracf(v.w)); 01231 } 01232 01234 // fmod 01236 01237 inline __host__ __device__ float2 fmodf(float2 a, float2 b) 01238 { 01239 return make_float2(fmodf(a.x, b.x), fmodf(a.y, b.y)); 01240 } 01241 inline __host__ __device__ float3 fmodf(float3 a, float3 b) 01242 { 01243 return make_float3(fmodf(a.x, b.x), fmodf(a.y, b.y), fmodf(a.z, b.z)); 01244 } 01245 inline __host__ __device__ float4 fmodf(float4 a, float4 b) 01246 { 01247 return make_float4(fmodf(a.x, b.x), fmodf(a.y, b.y), fmodf(a.z, b.z), fmodf(a.w, b.w)); 01248 } 01249 01251 // absolute value 01253 01254 inline __host__ __device__ float2 fabs(float2 v) 01255 { 01256 return make_float2(fabs(v.x), fabs(v.y)); 01257 } 01258 inline __host__ __device__ float3 fabs(float3 v) 01259 { 01260 return make_float3(fabs(v.x), fabs(v.y), fabs(v.z)); 01261 } 01262 inline __host__ __device__ float4 fabs(float4 v) 01263 { 01264 return make_float4(fabs(v.x), fabs(v.y), fabs(v.z), fabs(v.w)); 01265 } 01266 01267 inline __host__ __device__ int2 abs(int2 v) 01268 { 01269 return make_int2(abs(v.x), abs(v.y)); 01270 } 01271 inline __host__ __device__ int3 abs(int3 v) 01272 { 01273 return make_int3(abs(v.x), abs(v.y), abs(v.z)); 01274 } 01275 inline __host__ __device__ int4 abs(int4 v) 01276 { 01277 return make_int4(abs(v.x), abs(v.y), abs(v.z), abs(v.w)); 01278 } 01279 01281 // reflect 01282 // - returns reflection of incident ray I around surface normal N 01283 // - N should be normalized, reflected vector's length is equal to length of I 01285 01286 inline __host__ __device__ float3 reflect(float3 i, float3 n) 01287 { 01288 return i - 2.0f * n * dot(n,i); 01289 } 01290 01292 // cross product 01294 01295 inline __host__ __device__ float3 cross(float3 a, float3 b) 01296 { 01297 return make_float3(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x); 01298 } 01299 01301 // smoothstep 01302 // - returns 0 if x < a 01303 // - returns 1 if x > b 01304 // - otherwise returns smooth interpolation between 0 and 1 based on x 01306 01307 inline __device__ __host__ float smoothstep(float a, float b, float x) 01308 { 01309 float y = clamp((x - a) / (b - a), 0.0f, 1.0f); 01310 return (y*y*(3.0f - (2.0f*y))); 01311 } 01312 inline __device__ __host__ float2 smoothstep(float2 a, float2 b, float2 x) 01313 { 01314 float2 y = clamp((x - a) / (b - a), 0.0f, 1.0f); 01315 return (y*y*(make_float2(3.0f) - (make_float2(2.0f)*y))); 01316 } 01317 inline __device__ __host__ float3 smoothstep(float3 a, float3 b, float3 x) 01318 { 01319 float3 y = clamp((x - a) / (b - a), 0.0f, 1.0f); 01320 return (y*y*(make_float3(3.0f) - (make_float3(2.0f)*y))); 01321 } 01322 inline __device__ __host__ float4 smoothstep(float4 a, float4 b, float4 x) 01323 { 01324 float4 y = clamp((x - a) / (b - a), 0.0f, 1.0f); 01325 return (y*y*(make_float4(3.0f) - (make_float4(2.0f)*y))); 01326 } 01327 01328 #endif