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 */
49CVT_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 */
56CVT_FUNC(uchar)
57
58/**
59 * Convert to short.
60 *
61 * Supports 2,3,4 components of uchar, char, ushort, short, uint, int, float.
62 */
63CVT_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 */
70CVT_FUNC(ushort)
71
72/**
73 * Convert to int.
74 *
75 * Supports 2,3,4 components of uchar, char, ushort, short, uint, int, float.
76 */
77CVT_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 */
84CVT_FUNC(uint)
85
86/**
87 * Convert to float.
88 *
89 * Supports 2,3,4 components of uchar, char, ushort, short, uint, int, float.
90 */
91CVT_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 */
186extern float __attribute__((overloadable)) acos(float);
187FN_FUNC_FN(acos)
188
189/**
190 * Return the inverse hyperbolic cosine.
191 *
192 * Supports float, float2, float3, float4
193 */
194extern float __attribute__((overloadable)) acosh(float);
195FN_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);
203FN_FUNC_FN(acospi)
204
205/**
206 * Return the inverse sine.
207 *
208 * Supports float, float2, float3, float4
209 */
210extern float __attribute__((overloadable)) asin(float);
211FN_FUNC_FN(asin)
212
213/**
214 * Return the inverse hyperbolic sine.
215 *
216 * Supports float, float2, float3, float4
217 */
218extern float __attribute__((overloadable)) asinh(float);
219FN_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);
228FN_FUNC_FN(asinpi)
229
230/**
231 * Return the inverse tangent.
232 *
233 * Supports float, float2, float3, float4
234 */
235extern float __attribute__((overloadable)) atan(float);
236FN_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 */
247extern float __attribute__((overloadable)) atan2(float y, float x);
248FN_FUNC_FN_FN(atan2)
249
250/**
251 * Return the inverse hyperbolic tangent.
252 *
253 * Supports float, float2, float3, float4
254 */
255extern float __attribute__((overloadable)) atanh(float);
256FN_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);
264FN_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);
276FN_FUNC_FN_FN(atan2pi)
277
278
279/**
280 * Return the cube root.
281 *
282 * Supports float, float2, float3, float4.
283 */
284extern float __attribute__((overloadable)) cbrt(float);
285FN_FUNC_FN(cbrt)
286
287/**
288 * Return the smallest integer not less than a value.
289 *
290 * Supports float, float2, float3, float4.
291 */
292extern float __attribute__((overloadable)) ceil(float);
293FN_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 */
304extern float __attribute__((overloadable)) copysign(float x, float y);
305FN_FUNC_FN_FN(copysign)
306
307/**
308 * Return the cosine.
309 *
310 * Supports float, float2, float3, float4.
311 */
312extern float __attribute__((overloadable)) cos(float);
313FN_FUNC_FN(cos)
314
315/**
316 * Return the hypebolic cosine.
317 *
318 * Supports float, float2, float3, float4.
319 */
320extern float __attribute__((overloadable)) cosh(float);
321FN_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);
329FN_FUNC_FN(cospi)
330
331/**
332 * Return the complementary error function.
333 *
334 * Supports float, float2, float3, float4.
335 */
336extern float __attribute__((overloadable)) erfc(float);
337FN_FUNC_FN(erfc)
338
339/**
340 * Return the error function.
341 *
342 * Supports float, float2, float3, float4.
343 */
344extern float __attribute__((overloadable)) erf(float);
345FN_FUNC_FN(erf)
346
347/**
348 * Return e ^ value.
349 *
350 * Supports float, float2, float3, float4.
351 */
352extern float __attribute__((overloadable)) exp(float);
353FN_FUNC_FN(exp)
354
355/**
356 * Return 2 ^ value.
357 *
358 * Supports float, float2, float3, float4.
359 */
360extern float __attribute__((overloadable)) exp2(float);
361FN_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 */
369extern float __attribute__((overloadable)) pow(float x, float y);
370FN_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);
378FN_FUNC_FN(exp10)
379
380/**
381 * Return (e ^ value) - 1.
382 *
383 * Supports float, float2, float3, float4.
384 */
385extern float __attribute__((overloadable)) expm1(float);
386FN_FUNC_FN(expm1)
387
388/**
389 * Return the absolute value of a value.
390 *
391 * Supports float, float2, float3, float4.
392 */
393extern float __attribute__((overloadable)) fabs(float);
394FN_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 */
402extern float __attribute__((overloadable)) fdim(float, float);
403FN_FUNC_FN_FN(fdim)
404
405/**
406 * Return the smallest integer not greater than a value.
407 *
408 * Supports float, float2, float3, float4.
409 */
410extern float __attribute__((overloadable)) floor(float);
411FN_FUNC_FN(floor)
412
413/**
414 * Return a*b + c.
415 *
416 * Supports float, float2, float3, float4.
417 */
418extern float __attribute__((overloadable)) fma(float a, float b, float c);
419FN_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 */
428extern float __attribute__((overloadable)) fmax(float x, float y);
429FN_FUNC_FN_FN(fmax);
430FN_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 */
438extern float __attribute__((overloadable)) fmin(float x, float y);
439FN_FUNC_FN_FN(fmin);
440FN_FUNC_FN_F(fmin);
441
442/**
443 * Return the remainder from x / y
444 *
445 * Supports float, float2, float3, float4.
446 */
447extern float __attribute__((overloadable)) fmod(float x, float y);
448FN_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);
457FN_FUNC_FN_PFN(fract)
458
459/**
460 * Return fractional part of v
461 *
462 * Supports float, float2, float3, float4.
463 */
464static inline float __attribute__((overloadable)) fract(float v) {
465    float unused;
466    return fract(v, &unused);
467}
468
469static inline float2 __attribute__((overloadable)) fract(float2 v) {
470    float2 unused;
471    return fract(v, &unused);
472}
473
474static inline float3 __attribute__((overloadable)) fract(float3 v) {
475    float3 unused;
476    return fract(v, &unused);
477}
478
479static 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 */
490extern float __attribute__((overloadable)) frexp(float v, int *iptr);
491FN_FUNC_FN_PIN(frexp)
492
493/**
494 * Return sqrt(x*x + y*y)
495 *
496 * Supports float, float2, float3, float4.
497 */
498extern float __attribute__((overloadable)) hypot(float x, float y);
499FN_FUNC_FN_FN(hypot)
500
501/**
502 * Return the integer exponent of a value
503 *
504 * Supports 1,2,3,4 components
505 */
506extern int __attribute__((overloadable)) ilogb(float);
507IN_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 */
515extern float __attribute__((overloadable)) ldexp(float x, int y);
516FN_FUNC_FN_IN(ldexp)
517FN_FUNC_FN_I(ldexp)
518
519/**
520 * Return the log gamma
521 *
522 * Supports 1,2,3,4 components
523 */
524extern float __attribute__((overloadable)) lgamma(float);
525FN_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 */
533extern float __attribute__((overloadable)) lgamma(float x, int* y);
534FN_FUNC_FN_PIN(lgamma)
535
536/**
537 * Return the natural logarithm
538 *
539 * Supports 1,2,3,4 components
540 */
541extern float __attribute__((overloadable)) log(float);
542FN_FUNC_FN(log)
543
544/**
545 * Return the base 10 logarithm
546 *
547 * Supports 1,2,3,4 components
548 */
549extern float __attribute__((overloadable)) log10(float);
550FN_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);
558FN_FUNC_FN(log2)
559
560/**
561 * Return the natural logarithm of (v + 1.0f)
562 *
563 * Supports 1,2,3,4 components
564 */
565extern float __attribute__((overloadable)) log1p(float v);
566FN_FUNC_FN(log1p)
567
568/**
569 * Compute the exponent of the value.
570 *
571 * Supports 1,2,3,4 components
572 */
573extern float __attribute__((overloadable)) logb(float);
574FN_FUNC_FN(logb)
575
576/**
577 * Compute (a * b) + c
578 *
579 * Supports 1,2,3,4 components
580 */
581extern float __attribute__((overloadable)) mad(float a, float b, float c);
582FN_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 */
592extern float __attribute__((overloadable)) modf(float x, float *iret);
593FN_FUNC_FN_PFN(modf);
594
595extern 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 */
602extern float __attribute__((overloadable)) nextafter(float x, float y);
603FN_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);
611FN_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);
620FN_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 */
627extern float __attribute__((overloadable)) remainder(float x, float y);
628FN_FUNC_FN_FN(remainder)
629
630// document once we know the precision of bionic
631extern float __attribute__((overloadable)) remquo(float, float, int *);
632FN_FUNC_FN_FN_PIN(remquo)
633
634/**
635 * Round to the nearest integral value.
636 *
637 * Supports 1,2,3,4 components
638 */
639extern float __attribute__((overloadable)) rint(float);
640FN_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);
648FN_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 */
655extern float __attribute__((overloadable)) round(float);
656FN_FUNC_FN(round)
657
658/**
659 * Return the square root of a value.
660 *
661 * Supports 1,2,3,4 components
662 */
663extern float __attribute__((overloadable)) sqrt(float);
664FN_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);
672FN_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 */
680extern float __attribute__((overloadable)) sin(float v);
681FN_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);
693FN_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 */
700extern float __attribute__((overloadable)) sinh(float);
701FN_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);
709FN_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 */
717extern float __attribute__((overloadable)) tan(float v);
718FN_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 */
726extern float __attribute__((overloadable)) tanh(float);
727FN_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);
735FN_FUNC_FN(tanpi)
736
737/**
738 * Compute the gamma function of a value.
739 *
740 * Supports 1,2,3,4 components
741 */
742extern float __attribute__((overloadable)) tgamma(float);
743FN_FUNC_FN(tgamma)
744
745/**
746 * Round to integral using truncation.
747 *
748 * Supports 1,2,3,4 components
749 */
750extern float __attribute__((overloadable)) trunc(float);
751FN_FUNC_FN(trunc)
752
753
754#define XN_FUNC_YN(typeout, fnc, typein)                                \
755extern 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)          \
761XN_FUNC_YN(uchar, fnc, char)      \
762XN_FUNC_YN(ushort, fnc, short)    \
763XN_FUNC_YN(uint, fnc, int)
764
765#define IN_FUNC_IN(fnc)           \
766XN_FUNC_YN(uchar, fnc, uchar)     \
767XN_FUNC_YN(char, fnc, char)       \
768XN_FUNC_YN(ushort, fnc, ushort)   \
769XN_FUNC_YN(short, fnc, short)     \
770XN_FUNC_YN(uint, fnc, uint)       \
771XN_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)   \
785XN_FUNC_XN_XN_BODY(uchar, fnc, body)    \
786XN_FUNC_XN_XN_BODY(char, fnc, body)     \
787XN_FUNC_XN_XN_BODY(ushort, fnc, body)   \
788XN_FUNC_XN_XN_BODY(short, fnc, body)    \
789XN_FUNC_XN_XN_BODY(uint, fnc, body)     \
790XN_FUNC_XN_XN_BODY(int, fnc, body)      \
791XN_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 */
798UIN_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 */
805IN_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 */
812IN_FUNC_IN_IN_BODY(min, (v1 < v2 ? v1 : v2))
813FN_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 */
820IN_FUNC_IN_IN_BODY(max, (v1 > v2 ? v1 : v2))
821FN_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);
831FN_FUNC_FN_FN_FN(clamp)
832FN_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);
840FN_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);
848FN_FUNC_FN_FN_FN(mix)
849FN_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);
857FN_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);
868FN_FUNC_FN_FN(step)
869FN_FUNC_FN_F(step)
870
871// not implemented
872extern float __attribute__((overloadable)) smoothstep(float, float, float);
873extern float2 __attribute__((overloadable)) smoothstep(float2, float2, float2);
874extern float3 __attribute__((overloadable)) smoothstep(float3, float3, float3);
875extern float4 __attribute__((overloadable)) smoothstep(float4, float4, float4);
876extern float2 __attribute__((overloadable)) smoothstep(float, float, float2);
877extern float3 __attribute__((overloadable)) smoothstep(float, float, float3);
878extern 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);
890FN_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);
906F_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);
914F_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);
922F_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);
930FN_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);
942FN_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);
950FN_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);
958FN_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);
966F_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);
974F_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);
982F_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);
1000FN_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);
1010FN_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);
1020FN_FUNC_FN(native_exp10)
1021
1022
1023_RS_RUNTIME float __attribute__((overloadable)) native_log2(float v);
1024FN_FUNC_FN(native_log2)
1025
1026_RS_RUNTIME float __attribute__((overloadable)) native_log(float v);
1027FN_FUNC_FN(native_log)
1028
1029_RS_RUNTIME float __attribute__((overloadable)) native_log10(float v);
1030FN_FUNC_FN(native_log10)
1031
1032
1033_RS_RUNTIME float __attribute__((overloadable)) native_powr(float v, float y);
1034FN_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