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/**
452 * Return fractional part of v
453 *
454 * @param iptr  iptr[0] will be set to the floor of the input value.
455 * Supports float, float2, float3, float4.
456 */
457_RS_RUNTIME float __attribute__((overloadable)) fract(float v, float *iptr);
458FN_FUNC_FN_PFN(fract)
459
460/**
461 * Return the mantissa and place the exponent into iptr[0]
462 *
463 * @param v Supports float, float2, float3, float4.
464 * @param iptr  Must have the same vector size as v.
465 */
466extern float __attribute__((overloadable)) frexp(float v, int *iptr);
467FN_FUNC_FN_PIN(frexp)
468
469/**
470 * Return sqrt(x*x + y*y)
471 *
472 * Supports float, float2, float3, float4.
473 */
474extern float __attribute__((overloadable)) hypot(float x, float y);
475FN_FUNC_FN_FN(hypot)
476
477/**
478 * Return the integer exponent of a value
479 *
480 * Supports 1,2,3,4 components
481 */
482extern int __attribute__((overloadable)) ilogb(float);
483IN_FUNC_FN(ilogb)
484
485/**
486 * Return (x * 2^y)
487 *
488 * @param x Supports 1,2,3,4 components
489 * @param y Supports single component or matching vector.
490 */
491extern float __attribute__((overloadable)) ldexp(float x, int y);
492FN_FUNC_FN_IN(ldexp)
493FN_FUNC_FN_I(ldexp)
494
495/**
496 * Return the log gamma
497 *
498 * Supports 1,2,3,4 components
499 */
500extern float __attribute__((overloadable)) lgamma(float);
501FN_FUNC_FN(lgamma)
502
503/**
504 * Return the log gamma and sign
505 *
506 * @param x Supports 1,2,3,4 components
507 * @param y Supports matching vector.
508 */
509extern float __attribute__((overloadable)) lgamma(float x, int* y);
510FN_FUNC_FN_PIN(lgamma)
511
512/**
513 * Return the natural logarithm
514 *
515 * Supports 1,2,3,4 components
516 */
517extern float __attribute__((overloadable)) log(float);
518FN_FUNC_FN(log)
519
520/**
521 * Return the base 10 logarithm
522 *
523 * Supports 1,2,3,4 components
524 */
525extern float __attribute__((overloadable)) log10(float);
526FN_FUNC_FN(log10)
527
528/**
529 * Return the base 2 logarithm
530 *
531 * Supports 1,2,3,4 components
532 */
533_RS_RUNTIME float __attribute__((overloadable)) log2(float v);
534FN_FUNC_FN(log2)
535
536/**
537 * Return the natural logarithm of (v + 1.0f)
538 *
539 * Supports 1,2,3,4 components
540 */
541extern float __attribute__((overloadable)) log1p(float v);
542FN_FUNC_FN(log1p)
543
544/**
545 * Compute the exponent of the value.
546 *
547 * Supports 1,2,3,4 components
548 */
549extern float __attribute__((overloadable)) logb(float);
550FN_FUNC_FN(logb)
551
552/**
553 * Compute (a * b) + c
554 *
555 * Supports 1,2,3,4 components
556 */
557extern float __attribute__((overloadable)) mad(float a, float b, float c);
558FN_FUNC_FN_FN_FN(mad)
559
560/**
561 * Return the integral and fractional components of a number.
562 * Supports 1,2,3,4 components
563 *
564 * @param x Source value
565 * @param iret iret[0] will be set to the integral portion of the number.
566 * @return The floating point portion of the value.
567 */
568extern float __attribute__((overloadable)) modf(float x, float *iret);
569FN_FUNC_FN_PFN(modf);
570
571extern float __attribute__((overloadable)) nan(uint);
572
573/**
574 * Return the next floating point number from x towards y.
575 *
576 * Supports 1,2,3,4 components
577 */
578extern float __attribute__((overloadable)) nextafter(float x, float y);
579FN_FUNC_FN_FN(nextafter)
580
581/**
582 * Return (v ^ p).
583 *
584 * Supports 1,2,3,4 components
585 */
586_RS_RUNTIME float __attribute__((overloadable)) pown(float v, int p);
587FN_FUNC_FN_IN(pown)
588
589/**
590 * Return (v ^ p).
591 * @param v must be greater than 0.
592 *
593 * Supports 1,2,3,4 components
594 */
595_RS_RUNTIME float __attribute__((overloadable)) powr(float v, float p);
596FN_FUNC_FN_FN(powr)
597
598/**
599 * Return round x/y to the nearest integer then compute the remander.
600 *
601 * Supports 1,2,3,4 components
602 */
603extern float __attribute__((overloadable)) remainder(float x, float y);
604FN_FUNC_FN_FN(remainder)
605
606// document once we know the precision of bionic
607extern float __attribute__((overloadable)) remquo(float, float, int *);
608FN_FUNC_FN_FN_PIN(remquo)
609
610/**
611 * Round to the nearest integral value.
612 *
613 * Supports 1,2,3,4 components
614 */
615extern float __attribute__((overloadable)) rint(float);
616FN_FUNC_FN(rint)
617
618/**
619 * Compute the Nth root of a value.
620 *
621 * Supports 1,2,3,4 components
622 */
623_RS_RUNTIME float __attribute__((overloadable)) rootn(float v, int n);
624FN_FUNC_FN_IN(rootn)
625
626/**
627 * Round to the nearest integral value.  Half values are rounded away from zero.
628 *
629 * Supports 1,2,3,4 components
630 */
631extern float __attribute__((overloadable)) round(float);
632FN_FUNC_FN(round)
633
634/**
635 * Return the square root of a value.
636 *
637 * Supports 1,2,3,4 components
638 */
639extern float __attribute__((overloadable)) sqrt(float);
640FN_FUNC_FN(sqrt)
641
642/**
643 * Return (1 / sqrt(value)).
644 *
645 * Supports 1,2,3,4 components
646 */
647_RS_RUNTIME float __attribute__((overloadable)) rsqrt(float v);
648FN_FUNC_FN(rsqrt)
649
650/**
651 * Return the sine of a value specified in radians.
652 *
653 * @param v The incoming value in radians
654 * Supports 1,2,3,4 components
655 */
656extern float __attribute__((overloadable)) sin(float v);
657FN_FUNC_FN(sin)
658
659/**
660 * Return the sine and cosine of a value.
661 *
662 * @return sine
663 * @param v The incoming value in radians
664 * @param *cosptr cosptr[0] will be set to the cosine value.
665 *
666 * Supports 1,2,3,4 components
667 */
668_RS_RUNTIME float __attribute__((overloadable)) sincos(float v, float *cosptr);
669FN_FUNC_FN_PFN(sincos);
670
671/**
672 * Return the hyperbolic sine of a value specified in radians.
673 *
674 * Supports 1,2,3,4 components
675 */
676extern float __attribute__((overloadable)) sinh(float);
677FN_FUNC_FN(sinh)
678
679/**
680 * Return the sin(v * PI).
681 *
682 * Supports 1,2,3,4 components
683 */
684_RS_RUNTIME float __attribute__((overloadable)) sinpi(float v);
685FN_FUNC_FN(sinpi)
686
687/**
688 * Return the tangent of a value.
689 *
690 * Supports 1,2,3,4 components
691 * @param v The incoming value in radians
692 */
693extern float __attribute__((overloadable)) tan(float v);
694FN_FUNC_FN(tan)
695
696/**
697 * Return the hyperbolic tangent of a value.
698 *
699 * Supports 1,2,3,4 components
700 * @param v The incoming value in radians
701 */
702extern float __attribute__((overloadable)) tanh(float);
703FN_FUNC_FN(tanh)
704
705/**
706 * Return tan(v * PI)
707 *
708 * Supports 1,2,3,4 components
709 */
710_RS_RUNTIME float __attribute__((overloadable)) tanpi(float v);
711FN_FUNC_FN(tanpi)
712
713/**
714 * Compute the gamma function of a value.
715 *
716 * Supports 1,2,3,4 components
717 */
718extern float __attribute__((overloadable)) tgamma(float);
719FN_FUNC_FN(tgamma)
720
721/**
722 * Round to integral using truncation.
723 *
724 * Supports 1,2,3,4 components
725 */
726extern float __attribute__((overloadable)) trunc(float);
727FN_FUNC_FN(trunc)
728
729
730#define XN_FUNC_YN(typeout, fnc, typein)                                \
731extern typeout __attribute__((overloadable)) fnc(typein);               \
732_RS_RUNTIME typeout##2 __attribute__((overloadable)) fnc(typein##2 v);  \
733_RS_RUNTIME typeout##3 __attribute__((overloadable)) fnc(typein##3 v);  \
734_RS_RUNTIME typeout##4 __attribute__((overloadable)) fnc(typein##4 v);
735
736#define UIN_FUNC_IN(fnc)          \
737XN_FUNC_YN(uchar, fnc, char)      \
738XN_FUNC_YN(ushort, fnc, short)    \
739XN_FUNC_YN(uint, fnc, int)
740
741#define IN_FUNC_IN(fnc)           \
742XN_FUNC_YN(uchar, fnc, uchar)     \
743XN_FUNC_YN(char, fnc, char)       \
744XN_FUNC_YN(ushort, fnc, ushort)   \
745XN_FUNC_YN(short, fnc, short)     \
746XN_FUNC_YN(uint, fnc, uint)       \
747XN_FUNC_YN(int, fnc, int)
748
749
750#define XN_FUNC_XN_XN_BODY(type, fnc, body)         \
751_RS_RUNTIME type __attribute__((overloadable))      \
752        fnc(type v1, type v2);                      \
753_RS_RUNTIME type##2 __attribute__((overloadable))   \
754        fnc(type##2 v1, type##2 v2);                \
755_RS_RUNTIME type##3 __attribute__((overloadable))   \
756        fnc(type##3 v1, type##3 v2);                \
757_RS_RUNTIME type##4 __attribute__((overloadable))   \
758        fnc(type##4 v1, type##4 v2);
759
760#define IN_FUNC_IN_IN_BODY(fnc, body)   \
761XN_FUNC_XN_XN_BODY(uchar, fnc, body)    \
762XN_FUNC_XN_XN_BODY(char, fnc, body)     \
763XN_FUNC_XN_XN_BODY(ushort, fnc, body)   \
764XN_FUNC_XN_XN_BODY(short, fnc, body)    \
765XN_FUNC_XN_XN_BODY(uint, fnc, body)     \
766XN_FUNC_XN_XN_BODY(int, fnc, body)      \
767XN_FUNC_XN_XN_BODY(float, fnc, body)
768
769/**
770 * Return the absolute value of a value.
771 *
772 * Supports 1,2,3,4 components of char, short, int.
773 */
774UIN_FUNC_IN(abs)
775
776/**
777 * Return the number of leading 0-bits in a value.
778 *
779 * Supports 1,2,3,4 components of uchar, char, ushort, short, uint, int.
780 */
781IN_FUNC_IN(clz)
782
783/**
784 * Return the minimum of two values.
785 *
786 * Supports 1,2,3,4 components of uchar, char, ushort, short, uint, int, float.
787 */
788IN_FUNC_IN_IN_BODY(min, (v1 < v2 ? v1 : v2))
789FN_FUNC_FN_F(min)
790
791/**
792 * Return the maximum of two values.
793 *
794 * Supports 1,2,3,4 components of uchar, char, ushort, short, uint, int, float.
795 */
796IN_FUNC_IN_IN_BODY(max, (v1 > v2 ? v1 : v2))
797FN_FUNC_FN_F(max)
798
799/**
800 *  Clamp a value to a specified high and low bound.
801 *
802 * @param amount value to be clamped.  Supports 1,2,3,4 components
803 * @param low Lower bound, must be scalar or matching vector.
804 * @param high High bound, must match type of low
805 */
806_RS_RUNTIME float __attribute__((overloadable)) clamp(float amount, float low, float high);
807FN_FUNC_FN_FN_FN(clamp)
808FN_FUNC_FN_F_F(clamp)
809
810/**
811 * Convert from radians to degrees.
812 *
813 * Supports 1,2,3,4 components
814 */
815_RS_RUNTIME float __attribute__((overloadable)) degrees(float radians);
816FN_FUNC_FN(degrees)
817
818/**
819 * return start + ((stop - start) * amount);
820 *
821 * Supports 1,2,3,4 components
822 */
823_RS_RUNTIME float __attribute__((overloadable)) mix(float start, float stop, float amount);
824FN_FUNC_FN_FN_FN(mix)
825FN_FUNC_FN_FN_F(mix)
826
827/**
828 * Convert from degrees to radians.
829 *
830 * Supports 1,2,3,4 components
831 */
832_RS_RUNTIME float __attribute__((overloadable)) radians(float degrees);
833FN_FUNC_FN(radians)
834
835/**
836 * if (v < edge)
837 *     return 0.f;
838 * else
839 *     return 1.f;
840 *
841 * Supports 1,2,3,4 components
842 */
843_RS_RUNTIME float __attribute__((overloadable)) step(float edge, float v);
844FN_FUNC_FN_FN(step)
845FN_FUNC_FN_F(step)
846
847// not implemented
848extern float __attribute__((overloadable)) smoothstep(float, float, float);
849extern float2 __attribute__((overloadable)) smoothstep(float2, float2, float2);
850extern float3 __attribute__((overloadable)) smoothstep(float3, float3, float3);
851extern float4 __attribute__((overloadable)) smoothstep(float4, float4, float4);
852extern float2 __attribute__((overloadable)) smoothstep(float, float, float2);
853extern float3 __attribute__((overloadable)) smoothstep(float, float, float3);
854extern float4 __attribute__((overloadable)) smoothstep(float, float, float4);
855
856/**
857 * Return the sign of a value.
858 *
859 * if (v < 0) return -1.f;
860 * else if (v > 0) return 1.f;
861 * else return 0.f;
862 *
863 * Supports 1,2,3,4 components
864 */
865_RS_RUNTIME float __attribute__((overloadable)) sign(float v);
866FN_FUNC_FN(sign)
867
868/**
869 * Compute the cross product of two vectors.
870 *
871 * Supports 3,4 components
872 */
873_RS_RUNTIME float3 __attribute__((overloadable)) cross(float3 lhs, float3 rhs);
874_RS_RUNTIME float4 __attribute__((overloadable)) cross(float4 lhs, float4 rhs);
875
876/**
877 * Compute the dot product of two vectors.
878 *
879 * Supports 1,2,3,4 components
880 */
881_RS_RUNTIME float __attribute__((overloadable)) dot(float lhs, float rhs);
882F_FUNC_FN_FN(dot)
883
884/**
885 * Compute the length of a vector.
886 *
887 * Supports 1,2,3,4 components
888 */
889_RS_RUNTIME float __attribute__((overloadable)) length(float v);
890F_FUNC_FN(length)
891
892/**
893 * Compute the distance between two points.
894 *
895 * Supports 1,2,3,4 components
896 */
897_RS_RUNTIME float __attribute__((overloadable)) distance(float lhs, float rhs);
898F_FUNC_FN_FN(distance)
899
900/**
901 * Normalize a vector.
902 *
903 * Supports 1,2,3,4 components
904 */
905_RS_RUNTIME float __attribute__((overloadable)) normalize(float v);
906FN_FUNC_FN(normalize)
907
908
909// New approx API functions
910#if (defined(RS_VERSION) && (RS_VERSION >= 17))
911
912/**
913 * Return the approximate reciprocal of a value.
914 *
915 * Supports 1,2,3,4 components
916 */
917_RS_RUNTIME float __attribute__((overloadable)) half_recip(float);
918FN_FUNC_FN(half_recip)
919
920/**
921 * Return the approximate square root of a value.
922 *
923 * Supports 1,2,3,4 components
924 */
925_RS_RUNTIME float __attribute__((overloadable)) half_sqrt(float);
926FN_FUNC_FN(half_sqrt)
927
928/**
929 * Return the approximate value of (1 / sqrt(value)).
930 *
931 * Supports 1,2,3,4 components
932 */
933_RS_RUNTIME float __attribute__((overloadable)) half_rsqrt(float v);
934FN_FUNC_FN(half_rsqrt)
935
936/**
937 * Compute the approximate length of a vector.
938 *
939 * Supports 1,2,3,4 components
940 */
941_RS_RUNTIME float __attribute__((overloadable)) fast_length(float v);
942F_FUNC_FN(fast_length)
943
944/**
945 * Compute the approximate distance between two points.
946 *
947 * Supports 1,2,3,4 components
948 */
949_RS_RUNTIME float __attribute__((overloadable)) fast_distance(float lhs, float rhs);
950F_FUNC_FN_FN(fast_distance)
951
952/**
953 * Approximately normalize a vector.
954 *
955 * Supports 1,2,3,4 components
956 */
957_RS_RUNTIME float __attribute__((overloadable)) fast_normalize(float v);
958F_FUNC_FN(fast_normalize)
959
960#endif  // (defined(RS_VERSION) && (RS_VERSION >= 17))
961
962
963#undef CVT_FUNC
964#undef CVT_FUNC_2
965#undef FN_FUNC_FN
966#undef F_FUNC_FN
967#undef IN_FUNC_FN
968#undef FN_FUNC_FN_FN
969#undef F_FUNC_FN_FN
970#undef FN_FUNC_FN_F
971#undef FN_FUNC_FN_IN
972#undef FN_FUNC_FN_I
973#undef FN_FUNC_FN_PFN
974#undef FN_FUNC_FN_PIN
975#undef FN_FUNC_FN_FN_FN
976#undef FN_FUNC_FN_FN_F
977#undef FN_FUNC_FN_F_F
978#undef FN_FUNC_FN_FN_PIN
979#undef XN_FUNC_YN
980#undef UIN_FUNC_IN
981#undef IN_FUNC_IN
982#undef XN_FUNC_XN_XN_BODY
983#undef IN_FUNC_IN_IN_BODY
984
985#endif
986