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