Home | History | Annotate | Download | only in include
      1 /*
      2  * Copyright (C) 2011-2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 /** @file rs_cl.rsh
     18  *  \brief Basic math functions
     19  *
     20  *
     21  */
     22 
     23 #ifndef __RS_CL_RSH__
     24 #define __RS_CL_RSH__
     25 
     26 // Conversions
     27 #define CVT_FUNC_2(typeout, typein)                             \
     28 _RS_RUNTIME typeout##2 __attribute__((overloadable))            \
     29         convert_##typeout##2(typein##2 v);                      \
     30 _RS_RUNTIME typeout##3 __attribute__((overloadable))            \
     31         convert_##typeout##3(typein##3 v);                      \
     32 _RS_RUNTIME typeout##4 __attribute__((overloadable))            \
     33         convert_##typeout##4(typein##4 v);
     34 
     35 
     36 #define CVT_FUNC(type)  CVT_FUNC_2(type, uchar)     \
     37                         CVT_FUNC_2(type, char)      \
     38                         CVT_FUNC_2(type, ushort)    \
     39                         CVT_FUNC_2(type, short)     \
     40                         CVT_FUNC_2(type, uint)      \
     41                         CVT_FUNC_2(type, int)       \
     42                         CVT_FUNC_2(type, float)
     43 
     44 /**
     45  * Convert to char.
     46  *
     47  * Supports 2,3,4 components of uchar, char, ushort, short, uint, int, float.
     48  */
     49 CVT_FUNC(char)
     50 
     51 /**
     52  * Convert to unsigned char.
     53  *
     54  * Supports 2,3,4 components of uchar, char, ushort, short, uint, int, float.
     55  */
     56 CVT_FUNC(uchar)
     57 
     58 /**
     59  * Convert to short.
     60  *
     61  * Supports 2,3,4 components of uchar, char, ushort, short, uint, int, float.
     62  */
     63 CVT_FUNC(short)
     64 
     65 /**
     66  * Convert to unsigned short.
     67  *
     68  * Supports 2,3,4 components of uchar, char, ushort, short, uint, int, float.
     69  */
     70 CVT_FUNC(ushort)
     71 
     72 /**
     73  * Convert to int.
     74  *
     75  * Supports 2,3,4 components of uchar, char, ushort, short, uint, int, float.
     76  */
     77 CVT_FUNC(int)
     78 
     79 /**
     80  * Convert to unsigned int.
     81  *
     82  * Supports 2,3,4 components of uchar, char, ushort, short, uint, int, float.
     83  */
     84 CVT_FUNC(uint)
     85 
     86 /**
     87  * Convert to float.
     88  *
     89  * Supports 2,3,4 components of uchar, char, ushort, short, uint, int, float.
     90  */
     91 CVT_FUNC(float)
     92 
     93 // Float ops, 6.11.2
     94 
     95 #define FN_FUNC_FN(fnc)                                         \
     96 _RS_RUNTIME float2 __attribute__((overloadable)) fnc(float2 v); \
     97 _RS_RUNTIME float3 __attribute__((overloadable)) fnc(float3 v); \
     98 _RS_RUNTIME float4 __attribute__((overloadable)) fnc(float4 v);
     99 
    100 #define F_FUNC_FN(fnc)                                          \
    101 _RS_RUNTIME float __attribute__((overloadable)) fnc(float2 v);  \
    102 _RS_RUNTIME float __attribute__((overloadable)) fnc(float3 v);  \
    103 _RS_RUNTIME float __attribute__((overloadable)) fnc(float4 v);
    104 
    105 #define IN_FUNC_FN(fnc)                                         \
    106 _RS_RUNTIME int2 __attribute__((overloadable)) fnc(float2 v);   \
    107 _RS_RUNTIME int3 __attribute__((overloadable)) fnc(float3 v);   \
    108 _RS_RUNTIME int4 __attribute__((overloadable)) fnc(float4 v);
    109 
    110 #define FN_FUNC_FN_FN(fnc)                                                  \
    111 _RS_RUNTIME float2 __attribute__((overloadable)) fnc(float2 v1, float2 v2); \
    112 _RS_RUNTIME float3 __attribute__((overloadable)) fnc(float3 v1, float3 v2); \
    113 _RS_RUNTIME float4 __attribute__((overloadable)) fnc(float4 v1, float4 v2);
    114 
    115 #define F_FUNC_FN_FN(fnc)                                                   \
    116 _RS_RUNTIME float __attribute__((overloadable)) fnc(float2 v1, float2 v2);  \
    117 _RS_RUNTIME float __attribute__((overloadable)) fnc(float3 v1, float3 v2);  \
    118 _RS_RUNTIME float __attribute__((overloadable)) fnc(float4 v1, float4 v2);
    119 
    120 #define FN_FUNC_FN_F(fnc)                                                   \
    121 _RS_RUNTIME float2 __attribute__((overloadable)) fnc(float2 v1, float v2);  \
    122 _RS_RUNTIME float3 __attribute__((overloadable)) fnc(float3 v1, float v2);  \
    123 _RS_RUNTIME float4 __attribute__((overloadable)) fnc(float4 v1, float v2);
    124 
    125 #define FN_FUNC_FN_IN(fnc)                                                  \
    126 _RS_RUNTIME float2 __attribute__((overloadable)) fnc(float2 v1, int2 v2);   \
    127 _RS_RUNTIME float3 __attribute__((overloadable)) fnc(float3 v1, int3 v2);   \
    128 _RS_RUNTIME float4 __attribute__((overloadable)) fnc(float4 v1, int4 v2);   \
    129 
    130 #define FN_FUNC_FN_I(fnc)                                                   \
    131 _RS_RUNTIME float2 __attribute__((overloadable)) fnc(float2 v1, int v2);    \
    132 _RS_RUNTIME float3 __attribute__((overloadable)) fnc(float3 v1, int v2);    \
    133 _RS_RUNTIME float4 __attribute__((overloadable)) fnc(float4 v1, int v2);
    134 
    135 #define FN_FUNC_FN_PFN(fnc)                         \
    136 _RS_RUNTIME float2 __attribute__((overloadable))    \
    137         fnc(float2 v1, float2 *v2);                 \
    138 _RS_RUNTIME float3 __attribute__((overloadable))    \
    139         fnc(float3 v1, float3 *v2);                 \
    140 _RS_RUNTIME float4 __attribute__((overloadable))    \
    141         fnc(float4 v1, float4 *v2);
    142 
    143 #define FN_FUNC_FN_PIN(fnc)                                                 \
    144 _RS_RUNTIME float2 __attribute__((overloadable)) fnc(float2 v1, int2 *v2);  \
    145 _RS_RUNTIME float3 __attribute__((overloadable)) fnc(float3 v1, int3 *v2);  \
    146 _RS_RUNTIME float4 __attribute__((overloadable)) fnc(float4 v1, int4 *v2);
    147 
    148 #define FN_FUNC_FN_FN_FN(fnc)                       \
    149 _RS_RUNTIME float2 __attribute__((overloadable))    \
    150         fnc(float2 v1, float2 v2, float2 v3);       \
    151 _RS_RUNTIME float3 __attribute__((overloadable))    \
    152         fnc(float3 v1, float3 v2, float3 v3);       \
    153 _RS_RUNTIME float4 __attribute__((overloadable))    \
    154         fnc(float4 v1, float4 v2, float4 v3);
    155 
    156 #define FN_FUNC_FN_FN_F(fnc)                        \
    157 _RS_RUNTIME float2 __attribute__((overloadable))    \
    158         fnc(float2 v1, float2 v2, float v3);        \
    159 _RS_RUNTIME float3 __attribute__((overloadable))    \
    160         fnc(float3 v1, float3 v2, float v3);        \
    161 _RS_RUNTIME float4 __attribute__((overloadable))    \
    162         fnc(float4 v1, float4 v2, float v3);
    163 
    164 #define FN_FUNC_FN_F_F(fnc)                         \
    165 _RS_RUNTIME float2 __attribute__((overloadable))    \
    166         fnc(float2 v1, float v2, float v3);         \
    167 _RS_RUNTIME float3 __attribute__((overloadable))    \
    168         fnc(float3 v1, float v2, float v3);         \
    169 _RS_RUNTIME float4 __attribute__((overloadable))    \
    170         fnc(float4 v1, float v2, float v3);
    171 
    172 #define FN_FUNC_FN_FN_PIN(fnc)                      \
    173 _RS_RUNTIME float2 __attribute__((overloadable))    \
    174         fnc(float2 v1, float2 v2, int2 *v3);        \
    175 _RS_RUNTIME float3 __attribute__((overloadable))    \
    176         fnc(float3 v1, float3 v2, int3 *v3);        \
    177 _RS_RUNTIME float4 __attribute__((overloadable))    \
    178         fnc(float4 v1, float4 v2, int4 *v3);
    179 
    180 
    181 /**
    182  * Return the inverse cosine.
    183  *
    184  * Supports float, float2, float3, float4
    185  */
    186 extern float __attribute__((overloadable)) acos(float);
    187 FN_FUNC_FN(acos)
    188 
    189 /**
    190  * Return the inverse hyperbolic cosine.
    191  *
    192  * Supports float, float2, float3, float4
    193  */
    194 extern float __attribute__((overloadable)) acosh(float);
    195 FN_FUNC_FN(acosh)
    196 
    197 /**
    198  * Return the inverse cosine divided by PI.
    199  *
    200  * Supports float, float2, float3, float4
    201  */
    202 _RS_RUNTIME float __attribute__((overloadable)) acospi(float v);
    203 FN_FUNC_FN(acospi)
    204 
    205 /**
    206  * Return the inverse sine.
    207  *
    208  * Supports float, float2, float3, float4
    209  */
    210 extern float __attribute__((overloadable)) asin(float);
    211 FN_FUNC_FN(asin)
    212 
    213 /**
    214  * Return the inverse hyperbolic sine.
    215  *
    216  * Supports float, float2, float3, float4
    217  */
    218 extern float __attribute__((overloadable)) asinh(float);
    219 FN_FUNC_FN(asinh)
    220 
    221 
    222 /**
    223  * Return the inverse sine divided by PI.
    224  *
    225  * Supports float, float2, float3, float4
    226  */
    227 _RS_RUNTIME float __attribute__((overloadable)) asinpi(float v);
    228 FN_FUNC_FN(asinpi)
    229 
    230 /**
    231  * Return the inverse tangent.
    232  *
    233  * Supports float, float2, float3, float4
    234  */
    235 extern float __attribute__((overloadable)) atan(float);
    236 FN_FUNC_FN(atan)
    237 
    238 /**
    239  * Return the inverse tangent of y / x.
    240  *
    241  * Supports float, float2, float3, float4.  Both arguments must be of the same
    242  * type.
    243  *
    244  * @param y
    245  * @param x
    246  */
    247 extern float __attribute__((overloadable)) atan2(float y, float x);
    248 FN_FUNC_FN_FN(atan2)
    249 
    250 /**
    251  * Return the inverse hyperbolic tangent.
    252  *
    253  * Supports float, float2, float3, float4
    254  */
    255 extern float __attribute__((overloadable)) atanh(float);
    256 FN_FUNC_FN(atanh)
    257 
    258 /**
    259  * Return the inverse tangent divided by PI.
    260  *
    261  * Supports float, float2, float3, float4
    262  */
    263 _RS_RUNTIME float __attribute__((overloadable)) atanpi(float v);
    264 FN_FUNC_FN(atanpi)
    265 
    266 /**
    267  * Return the inverse tangent of y / x, divided by PI.
    268  *
    269  * Supports float, float2, float3, float4.  Both arguments must be of the same
    270  * type.
    271  *
    272  * @param y
    273  * @param x
    274  */
    275 _RS_RUNTIME float __attribute__((overloadable)) atan2pi(float y, float x);
    276 FN_FUNC_FN_FN(atan2pi)
    277 
    278 
    279 /**
    280  * Return the cube root.
    281  *
    282  * Supports float, float2, float3, float4.
    283  */
    284 extern float __attribute__((overloadable)) cbrt(float);
    285 FN_FUNC_FN(cbrt)
    286 
    287 /**
    288  * Return the smallest integer not less than a value.
    289  *
    290  * Supports float, float2, float3, float4.
    291  */
    292 extern float __attribute__((overloadable)) ceil(float);
    293 FN_FUNC_FN(ceil)
    294 
    295 /**
    296  * Copy the sign bit from y to x.
    297  *
    298  * Supports float, float2, float3, float4.  Both arguments must be of the same
    299  * type.
    300  *
    301  * @param x
    302  * @param y
    303  */
    304 extern float __attribute__((overloadable)) copysign(float x, float y);
    305 FN_FUNC_FN_FN(copysign)
    306 
    307 /**
    308  * Return the cosine.
    309  *
    310  * Supports float, float2, float3, float4.
    311  */
    312 extern float __attribute__((overloadable)) cos(float);
    313 FN_FUNC_FN(cos)
    314 
    315 /**
    316  * Return the hypebolic cosine.
    317  *
    318  * Supports float, float2, float3, float4.
    319  */
    320 extern float __attribute__((overloadable)) cosh(float);
    321 FN_FUNC_FN(cosh)
    322 
    323 /**
    324  * Return the cosine of the value * PI.
    325  *
    326  * Supports float, float2, float3, float4.
    327  */
    328 _RS_RUNTIME float __attribute__((overloadable)) cospi(float v);
    329 FN_FUNC_FN(cospi)
    330 
    331 /**
    332  * Return the complementary error function.
    333  *
    334  * Supports float, float2, float3, float4.
    335  */
    336 extern float __attribute__((overloadable)) erfc(float);
    337 FN_FUNC_FN(erfc)
    338 
    339 /**
    340  * Return the error function.
    341  *
    342  * Supports float, float2, float3, float4.
    343  */
    344 extern float __attribute__((overloadable)) erf(float);
    345 FN_FUNC_FN(erf)
    346 
    347 /**
    348  * Return e ^ value.
    349  *
    350  * Supports float, float2, float3, float4.
    351  */
    352 extern float __attribute__((overloadable)) exp(float);
    353 FN_FUNC_FN(exp)
    354 
    355 /**
    356  * Return 2 ^ value.
    357  *
    358  * Supports float, float2, float3, float4.
    359  */
    360 extern float __attribute__((overloadable)) exp2(float);
    361 FN_FUNC_FN(exp2)
    362 
    363 /**
    364  * Return x ^ y.
    365  *
    366  * Supports float, float2, float3, float4. Both arguments must be of the same
    367  * type.
    368  */
    369 extern float __attribute__((overloadable)) pow(float x, float y);
    370 FN_FUNC_FN_FN(pow)
    371 
    372 /**
    373  * Return 10 ^ value.
    374  *
    375  * Supports float, float2, float3, float4.
    376  */
    377 _RS_RUNTIME float __attribute__((overloadable)) exp10(float v);
    378 FN_FUNC_FN(exp10)
    379 
    380 /**
    381  * Return (e ^ value) - 1.
    382  *
    383  * Supports float, float2, float3, float4.
    384  */
    385 extern float __attribute__((overloadable)) expm1(float);
    386 FN_FUNC_FN(expm1)
    387 
    388 /**
    389  * Return the absolute value of a value.
    390  *
    391  * Supports float, float2, float3, float4.
    392  */
    393 extern float __attribute__((overloadable)) fabs(float);
    394 FN_FUNC_FN(fabs)
    395 
    396 /**
    397  * Return the positive difference between two values.
    398  *
    399  * Supports float, float2, float3, float4.  Both arguments must be of the same
    400  * type.
    401  */
    402 extern float __attribute__((overloadable)) fdim(float, float);
    403 FN_FUNC_FN_FN(fdim)
    404 
    405 /**
    406  * Return the smallest integer not greater than a value.
    407  *
    408  * Supports float, float2, float3, float4.
    409  */
    410 extern float __attribute__((overloadable)) floor(float);
    411 FN_FUNC_FN(floor)
    412 
    413 /**
    414  * Return a*b + c.
    415  *
    416  * Supports float, float2, float3, float4.
    417  */
    418 extern float __attribute__((overloadable)) fma(float a, float b, float c);
    419 FN_FUNC_FN_FN_FN(fma)
    420 
    421 /**
    422  * Return (x < y ? y : x)
    423  *
    424  * Supports float, float2, float3, float4.
    425  * @param x: may be float, float2, float3, float4
    426  * @param y: may be float or vector.  If vector must match type of x.
    427  */
    428 extern float __attribute__((overloadable)) fmax(float x, float y);
    429 FN_FUNC_FN_FN(fmax);
    430 FN_FUNC_FN_F(fmax);
    431 
    432 /**
    433  * Return (x > y ? y : x)
    434  *
    435  * @param x: may be float, float2, float3, float4
    436  * @param y: may be float or vector.  If vector must match type of x.
    437  */
    438 extern float __attribute__((overloadable)) fmin(float x, float y);
    439 FN_FUNC_FN_FN(fmin);
    440 FN_FUNC_FN_F(fmin);
    441 
    442 /**
    443  * Return the remainder from x / y
    444  *
    445  * Supports float, float2, float3, float4.
    446  */
    447 extern float __attribute__((overloadable)) fmod(float x, float y);
    448 FN_FUNC_FN_FN(fmod)
    449 
    450 /**
    451  * Return fractional part of v
    452  *
    453  * @param iptr  iptr[0] will be set to the floor of the input value.
    454  * Supports float, float2, float3, float4.
    455  */
    456 _RS_RUNTIME float __attribute__((overloadable)) fract(float v, float *iptr);
    457 FN_FUNC_FN_PFN(fract)
    458 
    459 /**
    460  * Return fractional part of v
    461  *
    462  * Supports float, float2, float3, float4.
    463  */
    464 static inline float __attribute__((overloadable)) fract(float v) {
    465     float unused;
    466     return fract(v, &unused);
    467 }
    468 
    469 static inline float2 __attribute__((overloadable)) fract(float2 v) {
    470     float2 unused;
    471     return fract(v, &unused);
    472 }
    473 
    474 static inline float3 __attribute__((overloadable)) fract(float3 v) {
    475     float3 unused;
    476     return fract(v, &unused);
    477 }
    478 
    479 static inline float4 __attribute__((overloadable)) fract(float4 v) {
    480     float4 unused;
    481     return fract(v, &unused);
    482 }
    483 
    484 /**
    485  * Return the mantissa and place the exponent into iptr[0]
    486  *
    487  * @param v Supports float, float2, float3, float4.
    488  * @param iptr  Must have the same vector size as v.
    489  */
    490 extern float __attribute__((overloadable)) frexp(float v, int *iptr);
    491 FN_FUNC_FN_PIN(frexp)
    492 
    493 /**
    494  * Return sqrt(x*x + y*y)
    495  *
    496  * Supports float, float2, float3, float4.
    497  */
    498 extern float __attribute__((overloadable)) hypot(float x, float y);
    499 FN_FUNC_FN_FN(hypot)
    500 
    501 /**
    502  * Return the integer exponent of a value
    503  *
    504  * Supports 1,2,3,4 components
    505  */
    506 extern int __attribute__((overloadable)) ilogb(float);
    507 IN_FUNC_FN(ilogb)
    508 
    509 /**
    510  * Return (x * 2^y)
    511  *
    512  * @param x Supports 1,2,3,4 components
    513  * @param y Supports single component or matching vector.
    514  */
    515 extern float __attribute__((overloadable)) ldexp(float x, int y);
    516 FN_FUNC_FN_IN(ldexp)
    517 FN_FUNC_FN_I(ldexp)
    518 
    519 /**
    520  * Return the log gamma
    521  *
    522  * Supports 1,2,3,4 components
    523  */
    524 extern float __attribute__((overloadable)) lgamma(float);
    525 FN_FUNC_FN(lgamma)
    526 
    527 /**
    528  * Return the log gamma and sign
    529  *
    530  * @param x Supports 1,2,3,4 components
    531  * @param y Supports matching vector.
    532  */
    533 extern float __attribute__((overloadable)) lgamma(float x, int* y);
    534 FN_FUNC_FN_PIN(lgamma)
    535 
    536 /**
    537  * Return the natural logarithm
    538  *
    539  * Supports 1,2,3,4 components
    540  */
    541 extern float __attribute__((overloadable)) log(float);
    542 FN_FUNC_FN(log)
    543 
    544 /**
    545  * Return the base 10 logarithm
    546  *
    547  * Supports 1,2,3,4 components
    548  */
    549 extern float __attribute__((overloadable)) log10(float);
    550 FN_FUNC_FN(log10)
    551 
    552 /**
    553  * Return the base 2 logarithm
    554  *
    555  * Supports 1,2,3,4 components
    556  */
    557 _RS_RUNTIME float __attribute__((overloadable)) log2(float v);
    558 FN_FUNC_FN(log2)
    559 
    560 /**
    561  * Return the natural logarithm of (v + 1.0f)
    562  *
    563  * Supports 1,2,3,4 components
    564  */
    565 extern float __attribute__((overloadable)) log1p(float v);
    566 FN_FUNC_FN(log1p)
    567 
    568 /**
    569  * Compute the exponent of the value.
    570  *
    571  * Supports 1,2,3,4 components
    572  */
    573 extern float __attribute__((overloadable)) logb(float);
    574 FN_FUNC_FN(logb)
    575 
    576 /**
    577  * Compute (a * b) + c
    578  *
    579  * Supports 1,2,3,4 components
    580  */
    581 extern float __attribute__((overloadable)) mad(float a, float b, float c);
    582 FN_FUNC_FN_FN_FN(mad)
    583 
    584 /**
    585  * Return the integral and fractional components of a number.
    586  * Supports 1,2,3,4 components
    587  *
    588  * @param x Source value
    589  * @param iret iret[0] will be set to the integral portion of the number.
    590  * @return The floating point portion of the value.
    591  */
    592 extern float __attribute__((overloadable)) modf(float x, float *iret);
    593 FN_FUNC_FN_PFN(modf);
    594 
    595 extern float __attribute__((overloadable)) nan(uint);
    596 
    597 /**
    598  * Return the next floating point number from x towards y.
    599  *
    600  * Supports 1,2,3,4 components
    601  */
    602 extern float __attribute__((overloadable)) nextafter(float x, float y);
    603 FN_FUNC_FN_FN(nextafter)
    604 
    605 /**
    606  * Return (v ^ p).
    607  *
    608  * Supports 1,2,3,4 components
    609  */
    610 _RS_RUNTIME float __attribute__((overloadable)) pown(float v, int p);
    611 FN_FUNC_FN_IN(pown)
    612 
    613 /**
    614  * Return (v ^ p).
    615  * @param v must be greater than 0.
    616  *
    617  * Supports 1,2,3,4 components
    618  */
    619 _RS_RUNTIME float __attribute__((overloadable)) powr(float v, float p);
    620 FN_FUNC_FN_FN(powr)
    621 
    622 /**
    623  * Return round x/y to the nearest integer then compute the remander.
    624  *
    625  * Supports 1,2,3,4 components
    626  */
    627 extern float __attribute__((overloadable)) remainder(float x, float y);
    628 FN_FUNC_FN_FN(remainder)
    629 
    630 // document once we know the precision of bionic
    631 extern float __attribute__((overloadable)) remquo(float, float, int *);
    632 FN_FUNC_FN_FN_PIN(remquo)
    633 
    634 /**
    635  * Round to the nearest integral value.
    636  *
    637  * Supports 1,2,3,4 components
    638  */
    639 extern float __attribute__((overloadable)) rint(float);
    640 FN_FUNC_FN(rint)
    641 
    642 /**
    643  * Compute the Nth root of a value.
    644  *
    645  * Supports 1,2,3,4 components
    646  */
    647 _RS_RUNTIME float __attribute__((overloadable)) rootn(float v, int n);
    648 FN_FUNC_FN_IN(rootn)
    649 
    650 /**
    651  * Round to the nearest integral value.  Half values are rounded away from zero.
    652  *
    653  * Supports 1,2,3,4 components
    654  */
    655 extern float __attribute__((overloadable)) round(float);
    656 FN_FUNC_FN(round)
    657 
    658 /**
    659  * Return the square root of a value.
    660  *
    661  * Supports 1,2,3,4 components
    662  */
    663 extern float __attribute__((overloadable)) sqrt(float);
    664 FN_FUNC_FN(sqrt)
    665 
    666 /**
    667  * Return (1 / sqrt(value)).
    668  *
    669  * Supports 1,2,3,4 components
    670  */
    671 _RS_RUNTIME float __attribute__((overloadable)) rsqrt(float v);
    672 FN_FUNC_FN(rsqrt)
    673 
    674 /**
    675  * Return the sine of a value specified in radians.
    676  *
    677  * @param v The incoming value in radians
    678  * Supports 1,2,3,4 components
    679  */
    680 extern float __attribute__((overloadable)) sin(float v);
    681 FN_FUNC_FN(sin)
    682 
    683 /**
    684  * Return the sine and cosine of a value.
    685  *
    686  * @return sine
    687  * @param v The incoming value in radians
    688  * @param *cosptr cosptr[0] will be set to the cosine value.
    689  *
    690  * Supports 1,2,3,4 components
    691  */
    692 _RS_RUNTIME float __attribute__((overloadable)) sincos(float v, float *cosptr);
    693 FN_FUNC_FN_PFN(sincos);
    694 
    695 /**
    696  * Return the hyperbolic sine of a value specified in radians.
    697  *
    698  * Supports 1,2,3,4 components
    699  */
    700 extern float __attribute__((overloadable)) sinh(float);
    701 FN_FUNC_FN(sinh)
    702 
    703 /**
    704  * Return the sin(v * PI).
    705  *
    706  * Supports 1,2,3,4 components
    707  */
    708 _RS_RUNTIME float __attribute__((overloadable)) sinpi(float v);
    709 FN_FUNC_FN(sinpi)
    710 
    711 /**
    712  * Return the tangent of a value.
    713  *
    714  * Supports 1,2,3,4 components
    715  * @param v The incoming value in radians
    716  */
    717 extern float __attribute__((overloadable)) tan(float v);
    718 FN_FUNC_FN(tan)
    719 
    720 /**
    721  * Return the hyperbolic tangent of a value.
    722  *
    723  * Supports 1,2,3,4 components
    724  * @param v The incoming value in radians
    725  */
    726 extern float __attribute__((overloadable)) tanh(float);
    727 FN_FUNC_FN(tanh)
    728 
    729 /**
    730  * Return tan(v * PI)
    731  *
    732  * Supports 1,2,3,4 components
    733  */
    734 _RS_RUNTIME float __attribute__((overloadable)) tanpi(float v);
    735 FN_FUNC_FN(tanpi)
    736 
    737 /**
    738  * Compute the gamma function of a value.
    739  *
    740  * Supports 1,2,3,4 components
    741  */
    742 extern float __attribute__((overloadable)) tgamma(float);
    743 FN_FUNC_FN(tgamma)
    744 
    745 /**
    746  * Round to integral using truncation.
    747  *
    748  * Supports 1,2,3,4 components
    749  */
    750 extern float __attribute__((overloadable)) trunc(float);
    751 FN_FUNC_FN(trunc)
    752 
    753 
    754 #define XN_FUNC_YN(typeout, fnc, typein)                                \
    755 extern typeout __attribute__((overloadable)) fnc(typein);               \
    756 _RS_RUNTIME typeout##2 __attribute__((overloadable)) fnc(typein##2 v);  \
    757 _RS_RUNTIME typeout##3 __attribute__((overloadable)) fnc(typein##3 v);  \
    758 _RS_RUNTIME typeout##4 __attribute__((overloadable)) fnc(typein##4 v);
    759 
    760 #define UIN_FUNC_IN(fnc)          \
    761 XN_FUNC_YN(uchar, fnc, char)      \
    762 XN_FUNC_YN(ushort, fnc, short)    \
    763 XN_FUNC_YN(uint, fnc, int)
    764 
    765 #define IN_FUNC_IN(fnc)           \
    766 XN_FUNC_YN(uchar, fnc, uchar)     \
    767 XN_FUNC_YN(char, fnc, char)       \
    768 XN_FUNC_YN(ushort, fnc, ushort)   \
    769 XN_FUNC_YN(short, fnc, short)     \
    770 XN_FUNC_YN(uint, fnc, uint)       \
    771 XN_FUNC_YN(int, fnc, int)
    772 
    773 
    774 #define XN_FUNC_XN_XN_BODY(type, fnc, body)         \
    775 _RS_RUNTIME type __attribute__((overloadable))      \
    776         fnc(type v1, type v2);                      \
    777 _RS_RUNTIME type##2 __attribute__((overloadable))   \
    778         fnc(type##2 v1, type##2 v2);                \
    779 _RS_RUNTIME type##3 __attribute__((overloadable))   \
    780         fnc(type##3 v1, type##3 v2);                \
    781 _RS_RUNTIME type##4 __attribute__((overloadable))   \
    782         fnc(type##4 v1, type##4 v2);
    783 
    784 #define IN_FUNC_IN_IN_BODY(fnc, body)   \
    785 XN_FUNC_XN_XN_BODY(uchar, fnc, body)    \
    786 XN_FUNC_XN_XN_BODY(char, fnc, body)     \
    787 XN_FUNC_XN_XN_BODY(ushort, fnc, body)   \
    788 XN_FUNC_XN_XN_BODY(short, fnc, body)    \
    789 XN_FUNC_XN_XN_BODY(uint, fnc, body)     \
    790 XN_FUNC_XN_XN_BODY(int, fnc, body)      \
    791 XN_FUNC_XN_XN_BODY(float, fnc, body)
    792 
    793 /**
    794  * Return the absolute value of a value.
    795  *
    796  * Supports 1,2,3,4 components of char, short, int.
    797  */
    798 UIN_FUNC_IN(abs)
    799 
    800 /**
    801  * Return the number of leading 0-bits in a value.
    802  *
    803  * Supports 1,2,3,4 components of uchar, char, ushort, short, uint, int.
    804  */
    805 IN_FUNC_IN(clz)
    806 
    807 /**
    808  * Return the minimum of two values.
    809  *
    810  * Supports 1,2,3,4 components of uchar, char, ushort, short, uint, int, float.
    811  */
    812 IN_FUNC_IN_IN_BODY(min, (v1 < v2 ? v1 : v2))
    813 FN_FUNC_FN_F(min)
    814 
    815 /**
    816  * Return the maximum of two values.
    817  *
    818  * Supports 1,2,3,4 components of uchar, char, ushort, short, uint, int, float.
    819  */
    820 IN_FUNC_IN_IN_BODY(max, (v1 > v2 ? v1 : v2))
    821 FN_FUNC_FN_F(max)
    822 
    823 /**
    824  *  Clamp a value to a specified high and low bound.
    825  *
    826  * @param amount value to be clamped.  Supports 1,2,3,4 components
    827  * @param low Lower bound, must be scalar or matching vector.
    828  * @param high High bound, must match type of low
    829  */
    830 _RS_RUNTIME float __attribute__((overloadable)) clamp(float amount, float low, float high);
    831 FN_FUNC_FN_FN_FN(clamp)
    832 FN_FUNC_FN_F_F(clamp)
    833 
    834 /**
    835  * Convert from radians to degrees.
    836  *
    837  * Supports 1,2,3,4 components
    838  */
    839 _RS_RUNTIME float __attribute__((overloadable)) degrees(float radians);
    840 FN_FUNC_FN(degrees)
    841 
    842 /**
    843  * return start + ((stop - start) * amount);
    844  *
    845  * Supports 1,2,3,4 components
    846  */
    847 _RS_RUNTIME float __attribute__((overloadable)) mix(float start, float stop, float amount);
    848 FN_FUNC_FN_FN_FN(mix)
    849 FN_FUNC_FN_FN_F(mix)
    850 
    851 /**
    852  * Convert from degrees to radians.
    853  *
    854  * Supports 1,2,3,4 components
    855  */
    856 _RS_RUNTIME float __attribute__((overloadable)) radians(float degrees);
    857 FN_FUNC_FN(radians)
    858 
    859 /**
    860  * if (v < edge)
    861  *     return 0.f;
    862  * else
    863  *     return 1.f;
    864  *
    865  * Supports 1,2,3,4 components
    866  */
    867 _RS_RUNTIME float __attribute__((overloadable)) step(float edge, float v);
    868 FN_FUNC_FN_FN(step)
    869 FN_FUNC_FN_F(step)
    870 
    871 // not implemented
    872 extern float __attribute__((overloadable)) smoothstep(float, float, float);
    873 extern float2 __attribute__((overloadable)) smoothstep(float2, float2, float2);
    874 extern float3 __attribute__((overloadable)) smoothstep(float3, float3, float3);
    875 extern float4 __attribute__((overloadable)) smoothstep(float4, float4, float4);
    876 extern float2 __attribute__((overloadable)) smoothstep(float, float, float2);
    877 extern float3 __attribute__((overloadable)) smoothstep(float, float, float3);
    878 extern float4 __attribute__((overloadable)) smoothstep(float, float, float4);
    879 
    880 /**
    881  * Return the sign of a value.
    882  *
    883  * if (v < 0) return -1.f;
    884  * else if (v > 0) return 1.f;
    885  * else return 0.f;
    886  *
    887  * Supports 1,2,3,4 components
    888  */
    889 _RS_RUNTIME float __attribute__((overloadable)) sign(float v);
    890 FN_FUNC_FN(sign)
    891 
    892 /**
    893  * Compute the cross product of two vectors.
    894  *
    895  * Supports 3,4 components
    896  */
    897 _RS_RUNTIME float3 __attribute__((overloadable)) cross(float3 lhs, float3 rhs);
    898 _RS_RUNTIME float4 __attribute__((overloadable)) cross(float4 lhs, float4 rhs);
    899 
    900 /**
    901  * Compute the dot product of two vectors.
    902  *
    903  * Supports 1,2,3,4 components
    904  */
    905 _RS_RUNTIME float __attribute__((overloadable)) dot(float lhs, float rhs);
    906 F_FUNC_FN_FN(dot)
    907 
    908 /**
    909  * Compute the length of a vector.
    910  *
    911  * Supports 1,2,3,4 components
    912  */
    913 _RS_RUNTIME float __attribute__((overloadable)) length(float v);
    914 F_FUNC_FN(length)
    915 
    916 /**
    917  * Compute the distance between two points.
    918  *
    919  * Supports 1,2,3,4 components
    920  */
    921 _RS_RUNTIME float __attribute__((overloadable)) distance(float lhs, float rhs);
    922 F_FUNC_FN_FN(distance)
    923 
    924 /**
    925  * Normalize a vector.
    926  *
    927  * Supports 1,2,3,4 components
    928  */
    929 _RS_RUNTIME float __attribute__((overloadable)) normalize(float v);
    930 FN_FUNC_FN(normalize)
    931 
    932 
    933 // New approx API functions
    934 #if (defined(RS_VERSION) && (RS_VERSION >= 17))
    935 
    936 /**
    937  * Return the approximate reciprocal of a value.
    938  *
    939  * Supports 1,2,3,4 components
    940  */
    941 _RS_RUNTIME float __attribute__((overloadable)) half_recip(float);
    942 FN_FUNC_FN(half_recip)
    943 
    944 /**
    945  * Return the approximate square root of a value.
    946  *
    947  * Supports 1,2,3,4 components
    948  */
    949 _RS_RUNTIME float __attribute__((overloadable)) half_sqrt(float);
    950 FN_FUNC_FN(half_sqrt)
    951 
    952 /**
    953  * Return the approximate value of (1 / sqrt(value)).
    954  *
    955  * Supports 1,2,3,4 components
    956  */
    957 _RS_RUNTIME float __attribute__((overloadable)) half_rsqrt(float v);
    958 FN_FUNC_FN(half_rsqrt)
    959 
    960 /**
    961  * Compute the approximate length of a vector.
    962  *
    963  * Supports 1,2,3,4 components
    964  */
    965 _RS_RUNTIME float __attribute__((overloadable)) fast_length(float v);
    966 F_FUNC_FN(fast_length)
    967 
    968 /**
    969  * Compute the approximate distance between two points.
    970  *
    971  * Supports 1,2,3,4 components
    972  */
    973 _RS_RUNTIME float __attribute__((overloadable)) fast_distance(float lhs, float rhs);
    974 F_FUNC_FN_FN(fast_distance)
    975 
    976 /**
    977  * Approximately normalize a vector.
    978  *
    979  * Supports 1,2,3,4 components
    980  */
    981 _RS_RUNTIME float __attribute__((overloadable)) fast_normalize(float v);
    982 F_FUNC_FN(fast_normalize)
    983 
    984 #endif  // (defined(RS_VERSION) && (RS_VERSION >= 17))
    985 
    986 
    987 
    988 #if (defined(RS_VERSION) && (RS_VERSION >= 18))
    989 // Fast native math functions.
    990 
    991 
    992 /**
    993  * Fast approximate exp2
    994  * valid for inputs -125.f to 125.f
    995  * Max 8192 ulps of error
    996  *
    997  * Supports 1,2,3,4 components
    998  */
    999 _RS_RUNTIME float __attribute__((overloadable)) native_exp2(float v);
   1000 FN_FUNC_FN(native_exp2)
   1001 
   1002 /**
   1003  * Fast approximate exp
   1004  * valid for inputs -86.f to 86.f
   1005  * Max 8192 ulps of error
   1006  *
   1007  * Supports 1,2,3,4 components
   1008  */
   1009 _RS_RUNTIME float __attribute__((overloadable)) native_exp(float v);
   1010 FN_FUNC_FN(native_exp)
   1011 
   1012 /**
   1013  * Fast approximate exp10
   1014  * valid for inputs -37.f to 37.f
   1015  * Max 8192 ulps of error
   1016  *
   1017  * Supports 1,2,3,4 components
   1018  */
   1019 _RS_RUNTIME float __attribute__((overloadable)) native_exp10(float v);
   1020 FN_FUNC_FN(native_exp10)
   1021 
   1022 
   1023 _RS_RUNTIME float __attribute__((overloadable)) native_log2(float v);
   1024 FN_FUNC_FN(native_log2)
   1025 
   1026 _RS_RUNTIME float __attribute__((overloadable)) native_log(float v);
   1027 FN_FUNC_FN(native_log)
   1028 
   1029 _RS_RUNTIME float __attribute__((overloadable)) native_log10(float v);
   1030 FN_FUNC_FN(native_log10)
   1031 
   1032 
   1033 _RS_RUNTIME float __attribute__((overloadable)) native_powr(float v, float y);
   1034 FN_FUNC_FN_FN(native_powr)
   1035 
   1036 
   1037 #endif  // (defined(RS_VERSION) && (RS_VERSION >= 18))
   1038 
   1039 
   1040 #undef CVT_FUNC
   1041 #undef CVT_FUNC_2
   1042 #undef FN_FUNC_FN
   1043 #undef F_FUNC_FN
   1044 #undef IN_FUNC_FN
   1045 #undef FN_FUNC_FN_FN
   1046 #undef F_FUNC_FN_FN
   1047 #undef FN_FUNC_FN_F
   1048 #undef FN_FUNC_FN_IN
   1049 #undef FN_FUNC_FN_I
   1050 #undef FN_FUNC_FN_PFN
   1051 #undef FN_FUNC_FN_PIN
   1052 #undef FN_FUNC_FN_FN_FN
   1053 #undef FN_FUNC_FN_FN_F
   1054 #undef FN_FUNC_FN_F_F
   1055 #undef FN_FUNC_FN_FN_PIN
   1056 #undef XN_FUNC_YN
   1057 #undef UIN_FUNC_IN
   1058 #undef IN_FUNC_IN
   1059 #undef XN_FUNC_XN_XN_BODY
   1060 #undef IN_FUNC_IN_IN_BODY
   1061 
   1062 #endif
   1063