Perform transcendental and trigonometric functions on vectors of any length.
Framework
- Accelerate
Overview
The v
header file declares a set of trigonometric and transcendental functions in terms of C arrays (double *
or float *
), which can be of any length. Internally, the functions are vectorized for the current architecture.
The functions declared in v
have the customary mathematical names, but with the prefix "vv." Each mathematical function is available in two variants: one for single-precision floating-point data and one for double-precision data. The single-precision forms have the suffix "f," while the double-precision forms have no suffix. For example, vvcosf
is the single-precision cosine function, while vvcos
is the double-precision variant.
All of the vForce.h functions follow a common format:
The return type is
void
.The first parameter points to an array to hold the results. The only exceptions are
vvsincosf()
andvvsincos()
, which have two result arrays which the first two parameters point to.One or more parameters point to operand arrays that are the same length as the result array.
The last parameter is the array length.
Using vForce
vForce provides a high-performance alternative to for
loops and map(_:) when applying operations on arrays of floating-point values.
For example, given an arbitrarily sized array, x
, that contains single-precision values, the following code uses map(_:)
to create a second array, y
, that contains the square root of each array element.
var x = [Float]()
for _ in 0...10_000_000 {
x.append(Float(drand48() * 1_000_000))
}
let y = x.map{
return sqrt($0)
}
The equivalent functionality implemented in vForce runs significantly faster:
var n = Int32(x.count)
var y = [Float](repeating: 0, count: x.count)
vvsqrtf(&y, x, &n)