Show Menu
Cheatography

RenderScript Math Functions Cheat Sheet (DRAFT) by

Overview of RenderScript intrinsic math functions

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Intrinsic math functions

Render­Script provides a lot of intrinsic math functions, which are optimized using NEON instru­ctions (on ARMv7 devices). These functions should be used instead of writing pure calcul­ations when possible.

For example,
a * b + c
should be translated into
fma(a, b, c)
.

Note: functions' arguments are assumed to be of type that matches function's return one. Otherwise, different type is specified.
Following functions have been taken directly from Android source code.

Float functions (part 1)

Definition
Descri­ption
acos(v)
Inverse cosine
acosh(v)
Inverse hyperbolic cosine
acospi(v)
Inverse cosine divided by pi
asin(v)
Inverse sine
asinh(v)
Inverse hyperbolic sine
asinpi(v)
Inverse sine divided by pi
atan(v)
Inverse tangent
atan2(num, den)
Inverse tangent of a ratio
atan2p­i(num, den)
Inverse tangent of a ratio, divided by pi
atanh(v)
Inverse hyperbolic tangent
atanpi(v)
Inverse tangent divided by pi
cbrt(v)
Cube root
ceil(v)
Smallest integer not less than a value
clamp(­value, min, max)
Restrain a value to a range
copysi­gn(­mag­nitude, sign)
Copies the sign of a number to another.
copysi­gn(­4.0f, -2.7f)
returns
-4.0f
cos(v)
Cosine
cosh(v)
Hypebolic cosine
cospi(v)
Cosine of a number multiplied by pi
degrees(v)
Converts radians into degrees
erf(v)
Mathem­atical error function
erfc(v)
Mathem­atical comple­mentary error function
exp(v)
e raised to a number
exp10(v)
10 raised to a number
exp2(v)
2 raised to a number
expm1(v)
e raised to a number minus one:
(e ^ v) - 1
fabs(v)
Absolute value of a float
fdim(a, b)
Positive difference between two values. If
a > b
, returns
(a - b)
otherwise returns
0f
floor(v)
Smallest integer not greater than a value/
(multi­pli­cand1 * multip­lic­and2) + offset
fma(mul1, mul2, offset)
Multiply and add
fmax(a, b)
Maximum of two floats
fmin(a, b)
Minimum of two floats
fmod(num, den)
Modulo
fract(v, float* floor)
Positive fractional part.
fract(­2.3f, &val)
returns
0.3f
and sets val to
2.f
frexp(v, int* exponent)
Binary mantissa and exponent
half_r­ecip(v)
Reciprocal computed to 16 bit precision
half_r­sqrt(v)
Reciprocal of a square root computed to 16 bit precision
half_s­qrt(v)
Square root computed to 16 bit precision
 

Predefined constants

Type
Name
Value
float
M_PI
3.1415­92... (pi)
float
M_PI_2
M_PI / 2
float
M_PI_4
M_PI / 4
float
M_1_PI
1 / M_PI
float
M_2_PI
2 / M_PI
float
M_2_SQRTPI
2 / sqrt(M_PI)
float
M_SQRT2
sqrt(2)
float
M_E
2.7182­81... (e)
float
M_LN10
log_e(10)
float
M_LN2
log_e(2)
float
M_LOG10E
log_10­(M_E)
float
M_LOG2E
log_2(M_E)
float
M_SQRT1_2
1 / sqrt(2)

Float functions (part 2)

Definition
Descri­ption
hypot(a, b)
Hypotenuse
ldexp(­man­tissa, int exponent)
Creates a floating point from mantissa and exponent.
mantissa * 2 ^ exponent
lgamma(v)
Natural logarithm of the gamma function
lgamma(v, int* sign_o­f_g­amma)
Natural logarithm of the gamma function. If
sign_o­f_gamma
is not null,
*sign_­of_­gamma
will be set to
-1.f
if the gamma of
v
is negative, otherwise to
1.f
log(v)
Natural logarithm
log10(v)
Base 10 logarithm
log1p(v)
Natural logarithm of a value plus 1
log2(v)
Base 2 logarithm
logb(v)
Base two exponent.
logb(8.5f)
returns
3.f
mad(mul1, mul2, offset)
Multiply and add
max(a, b)
Maximum
min(a, b)
Minimum
modf(v, float* integr­al_­part)
Integral and fractional compon­ents.
modf(-­3.72f)
will return
0.72f
and
integr­al_part
will be set to
-3.f
nan(uint v)
Returns NaN
nextaf­ter(v, target)
Next repres­entable floating point number from v towards target
pow(base, exp)
Base raised to an exponent
pown(base, int expt)
Base raised to an integer exponent
powr(base, exp)
Positive base raised to an exponent
radians(v)
Converts degrees into radians
remain­der­(num, den)
Remainder of a division
remquo­(num, int* quotient)
Remainder and quotient of a division
rint(v)
Round to even
rootn(v, int n)
Nth root
round(v)
Round away from zero
rsqrt(v)
Reciprocal of a square root
sign(v)
Sign of a value
sin(v)
Sine
sincos(v, float* cos)
Sine and cosine
sinh(v)
Hyperbolic sine
sinpi(v)
Sine of a number multiplied by pi
sqrt(v)
Square root
step(edge, v)
Returns
0.f
if
v < edge
,
1.f
otherwise
tan(v)
Tangent
tanh(v)
Hyperbolic tangent
tanpi(v)
Tangent of a number multiplied by pi
tgamma(v)
Gamma function
trunc(v)
Truncates a floating point
 

Integer functions (return int)

Definition
Descri­ption
abs(v)
Absolute value of an integer
clamp(­value, min, max)
Restrain a value to a range (min API 19)
clz(value)
Number of leading 0 bits
ilogb(­float v)
Base two exponent
max(a, b)
Maximum value of two arguments
min(a, b)
Minimum value of two arguments
rsRand­(max)
Pseudo­-random number
rsRand­(min, max)
Pseudo­-random number

Approx­imate float functions (API >= 21)

Following functions have stricter limits than precise ones. Please refer to specs to see them.
Definition
Descri­ption
native­_lo­g2(v)
Approx­imate base 2 logarithm (API 18)
native­_po­wr(­base, exp)
Approx­imate positive base raised to an exponent (API 18)
native­_ac­os(v)
Approx­imate inverse cosine
native­_ac­osh(v)
Approx­imate inverse hyperbolic cosine
native­_ac­ospi(v)
Approx­imate inverse cosine divided by pi
native­_as­in(v)
Approx­imate inverse sine
native­_as­inh(v)
Approx­imate inverse hyperbolic sine
native­_as­inpi(v)
Approx­imate inverse sine divided by pi
native­_at­an(v)
Approx­imate inverse tangent
native­_at­an2­(num, den)
Approx­imate inverse tangent of a ratio
native­_at­an2­pi(num, den)
Approx­imate inverse tangent of a ratio, divided by pi
native­_at­anh(v)
Approx­imate inverse hyperbolic tangent
native­_at­anpi(v)
Approx­imate inverse tangent divided by pi
native­_cb­rt(v)
Approx­imate cube root
native­_cos(v)
Approx­imate cosine
native­_co­sh(v)
Approx­imate hypebolic cosine
native­_co­spi(v)
Approx­imate cosine of a number multiplied by pi
native­_di­vid­e(left, right)
Approx­imate division
native­_exp(v)
Approx­imate e raised to a number
native­_ex­p10(v)
Approx­imate 10 raised to a number
native­_ex­p2(v)
Approx­imate 2 raised to a number
native­_ex­pm1(v)
Approx­imate e raised to a number minus one
native­_hy­pot(a, b)
Approx­imate hypotenuse
native­_log(v)
Approx­imate natural logarithm
native­_lo­g10(v)
Approx­imate base 10 logarithm
native­_lo­g1p(v)
Approx­imate natural logarithm of a value plus 1
native­_re­cip(v)
Approx­imate reciprocal
native­_ro­otn(v, int n)
Approx­imate nth root
native­_rs­qrt(v)
Approx­imate reciprocal of a square root
native­_sin(v)
Approx­imate sine
native­_si­ncos(v, float* cos);
Approx­imate sine and cosine
native­_si­nh(v)
Approx­imate hyperbolic sine
native­_si­npi(v)
Approx­imate sine of a number multiplied by pi
native­_sq­rt(v)
Approx­imate square root
native­_tan(v)
Approx­imate tangent
native­_ta­nh(v)
Approx­imate hyperbolic tangent
native­_ta­npi(v)
Approx­imate tangent of a number multiplied by pi