Move implementations of quaternion functions into driver/runtime.

Move the implementation of all quaternion functions from rs_math.rsh
driver/runtime. The old implementation remains in place for older
versions, and the move occurs only for an UNRELEASED API level.
Added a new file driver/runtime/rs_quaternion.c to contain the
implementations.

Change-Id: I70ffcb29e64b4287e37e349488a59a9f485847e4
Signed-off-by: Verena Beckham <verena@codeplay.com>
diff --git a/api/rs_quaternion.spec b/api/rs_quaternion.spec
index ced31c8..d9d2b94 100644
--- a/api/rs_quaternion.spec
+++ b/api/rs_quaternion.spec
@@ -21,6 +21,7 @@
 end:
 
 function: rsQuaternionAdd
+version: 9 23
 ret: void
 arg: rs_quaternion* q, "Destination quaternion to add to."
 arg: const rs_quaternion* rhs, "Quaternion to add."
@@ -36,6 +37,7 @@
 end:
 
 function: rsQuaternionConjugate
+version: 9 23
 ret: void
 arg: rs_quaternion* q, "Quaternion to modify."
 summary: Conjugate a quaternion
@@ -49,6 +51,7 @@
 end:
 
 function: rsQuaternionDot
+version: 9 23
 ret: float
 arg: const rs_quaternion* q0, "First quaternion."
 arg: const rs_quaternion* q1, "Second quaternion."
@@ -61,6 +64,7 @@
 end:
 
 function: rsQuaternionGetMatrixUnit
+version: 9 23
 ret: void
 arg: rs_matrix4x4* m, "Resulting matrix."
 arg: const rs_quaternion* q, "Normalized quaternion."
@@ -93,6 +97,7 @@
 end:
 
 function: rsQuaternionLoadRotateUnit
+version: 9 23
 ret: void
 arg: rs_quaternion* q, "Destination quaternion."
 arg: float rot, "Angle to rotate by, in radians."
@@ -115,6 +120,7 @@
 end:
 
 function: rsQuaternionSet
+version: 9 23
 ret: void
 arg: rs_quaternion* q, "Destination quaternion."
 arg: float w, "W component."
@@ -133,6 +139,7 @@
 end:
 
 function: rsQuaternionSet
+version: 9 23
 ret: void
 arg: rs_quaternion* q
 arg: const rs_quaternion* rhs, "Source quaternion."
@@ -148,6 +155,7 @@
 # for the compilation to work.
 
 function: rsQuaternionLoadRotate
+version: 9 23
 ret: void
 arg: rs_quaternion* q, "Destination quaternion."
 arg: float rot, "Angle to rotate by."
@@ -171,6 +179,7 @@
 end:
 
 function: rsQuaternionNormalize
+version: 9 23
 ret: void
 arg: rs_quaternion* q, "Quaternion to normalize."
 summary:  Normalize a quaternion
@@ -189,6 +198,7 @@
 end:
 
 function: rsQuaternionMultiply
+version: 9 23
 ret: void
 arg: rs_quaternion* q, "Destination quaternion."
 arg: float scalar, "Scalar to multiply the quaternion by."
@@ -205,6 +215,7 @@
 end:
 
 function: rsQuaternionMultiply
+version: 9 23
 ret: void
 arg: rs_quaternion* q
 arg: const rs_quaternion* rhs, "Quaternion to multiply the destination quaternion by."
@@ -221,6 +232,7 @@
 end:
 
 function: rsQuaternionSlerp
+version: 9 23
 ret: void
 arg: rs_quaternion* q, "Result quaternion from the interpolation."
 arg: const rs_quaternion* q0, "First input quaternion."
@@ -270,3 +282,112 @@
                      tempq0.y*scale + tempq1.y*invScale, tempq0.z*scale + tempq1.z*invScale);
 test: none
 end:
+
+# New versions. Same signatures but don't contain a body.
+function: rsQuaternionAdd
+version: UNRELEASED
+ret: void
+arg: rs_quaternion* q
+arg: const rs_quaternion* rhs
+test: none
+end:
+
+function: rsQuaternionConjugate
+version: UNRELEASED
+ret: void
+arg: rs_quaternion* q
+test: none
+end:
+
+function: rsQuaternionDot
+version: UNRELEASED
+ret: float
+arg: const rs_quaternion* q0
+arg: const rs_quaternion* q1
+test: none
+end:
+
+function: rsQuaternionGetMatrixUnit
+version: UNRELEASED
+ret: void
+arg: rs_matrix4x4* m
+arg: const rs_quaternion* q
+test: none
+end:
+
+function: rsQuaternionLoadRotateUnit
+version: UNRELEASED
+ret: void
+arg: rs_quaternion* q
+arg: float rot
+arg: float x
+arg: float y
+arg: float z
+test: none
+end:
+
+function: rsQuaternionSet
+version: UNRELEASED
+ret: void
+arg: rs_quaternion* q
+arg: float w
+arg: float x
+arg: float y
+arg: float z
+test: none
+end:
+
+function: rsQuaternionSet
+version: UNRELEASED
+ret: void
+arg: rs_quaternion* q
+arg: const rs_quaternion* rhs
+test: none
+end:
+
+# NOTE: The following inline definitions depend on each other.  The order must be preserved
+# for the compilation to work.
+
+function: rsQuaternionLoadRotate
+version: UNRELEASED
+ret: void
+arg: rs_quaternion* q
+arg: float rot
+arg: float x
+arg: float y
+arg: float z
+test: none
+end:
+
+function: rsQuaternionNormalize
+version: UNRELEASED
+ret: void
+arg: rs_quaternion* q
+test: none
+end:
+
+function: rsQuaternionMultiply
+version: UNRELEASED
+ret: void
+arg: rs_quaternion* q
+arg: float scalar
+test: none
+end:
+
+function: rsQuaternionMultiply
+version: UNRELEASED
+ret: void
+arg: rs_quaternion* q
+arg: const rs_quaternion* rhs
+test: none
+end:
+
+function: rsQuaternionSlerp
+version: UNRELEASED
+ret: void
+arg: rs_quaternion* q
+arg: const rs_quaternion* q0
+arg: const rs_quaternion* q1
+arg: float t
+test: none
+end:
diff --git a/driver/runtime/Android.mk b/driver/runtime/Android.mk
index 1b9f6d1..8685ecc 100755
--- a/driver/runtime/Android.mk
+++ b/driver/runtime/Android.mk
@@ -28,7 +28,8 @@
     rs_program.c \
     rs_sample.c \
     rs_sampler.c \
-    rs_convert.c
+    rs_convert.c \
+    rs_quaternion.c
 
 clcore_cflags := -Iframeworks/rs/cpu_ref -DRS_DECLARE_EXPIRED_APIS
 
diff --git a/driver/runtime/rs_quaternion.c b/driver/runtime/rs_quaternion.c
new file mode 100644
index 0000000..8fbfd20
--- /dev/null
+++ b/driver/runtime/rs_quaternion.c
@@ -0,0 +1,169 @@
+#include "rs_core.rsh"
+
+/* Implementation of Core Runtime */
+
+
+/////////////////////////////////////////////////////
+// Quaternion ops
+/////////////////////////////////////////////////////
+
+#if (defined(RS_VERSION) && (RS_VERSION >= UNRELEASED))
+extern void __attribute__((overloadable))
+    rsQuaternionAdd(rs_quaternion* q, const rs_quaternion* rhs) {
+    q->w += rhs->w;
+    q->x += rhs->x;
+    q->y += rhs->y;
+    q->z += rhs->z;
+}
+
+extern void __attribute__((overloadable))
+    rsQuaternionConjugate(rs_quaternion* q) {
+    q->x = -q->x;
+    q->y = -q->y;
+    q->z = -q->z;
+}
+
+extern float __attribute__((overloadable))
+    rsQuaternionDot(const rs_quaternion* q0, const rs_quaternion* q1) {
+    return q0->w*q1->w + q0->x*q1->x + q0->y*q1->y + q0->z*q1->z;
+}
+
+extern void __attribute__((overloadable))
+    rsQuaternionGetMatrixUnit(rs_matrix4x4* m, const rs_quaternion* q) {
+    float xx = q->x * q->x;
+    float xy = q->x * q->y;
+    float xz = q->x * q->z;
+    float xw = q->x * q->w;
+    float yy = q->y * q->y;
+    float yz = q->y * q->z;
+    float yw = q->y * q->w;
+    float zz = q->z * q->z;
+    float zw = q->z * q->w;
+
+    m->m[0]  = 1.0f - 2.0f * ( yy + zz );
+    m->m[4]  =        2.0f * ( xy - zw );
+    m->m[8]  =        2.0f * ( xz + yw );
+    m->m[1]  =        2.0f * ( xy + zw );
+    m->m[5]  = 1.0f - 2.0f * ( xx + zz );
+    m->m[9]  =        2.0f * ( yz - xw );
+    m->m[2]  =        2.0f * ( xz - yw );
+    m->m[6]  =        2.0f * ( yz + xw );
+    m->m[10] = 1.0f - 2.0f * ( xx + yy );
+    m->m[3]  = m->m[7] = m->m[11] = m->m[12] = m->m[13] = m->m[14] = 0.0f;
+    m->m[15] = 1.0f;
+}
+
+extern void __attribute__((overloadable))
+    rsQuaternionLoadRotateUnit(rs_quaternion* q, float rot, float x, float y, float z) {
+    rot *= (float)(M_PI / 180.0f) * 0.5f;
+    float c = cos(rot);
+    float s = sin(rot);
+
+    q->w = c;
+    q->x = x * s;
+    q->y = y * s;
+    q->z = z * s;
+}
+
+extern void __attribute__((overloadable))
+    rsQuaternionSet(rs_quaternion* q, float w, float x, float y, float z) {
+    q->w = w;
+    q->x = x;
+    q->y = y;
+    q->z = z;
+}
+
+extern void __attribute__((overloadable))
+    rsQuaternionSet(rs_quaternion* q, const rs_quaternion* rhs) {
+    q->w = rhs->w;
+    q->x = rhs->x;
+    q->y = rhs->y;
+    q->z = rhs->z;
+}
+
+extern void __attribute__((overloadable))
+    rsQuaternionLoadRotate(rs_quaternion* q, float rot, float x, float y, float z) {
+    const float len = x*x + y*y + z*z;
+    if (len != 1) {
+        const float recipLen = 1.f / sqrt(len);
+        x *= recipLen;
+        y *= recipLen;
+        z *= recipLen;
+    }
+    rsQuaternionLoadRotateUnit(q, rot, x, y, z);
+}
+
+extern void __attribute__((overloadable))
+    rsQuaternionNormalize(rs_quaternion* q) {
+    const float len = rsQuaternionDot(q, q);
+    if (len != 1) {
+        const float recipLen = 1.f / sqrt(len);
+        q->w *= recipLen;
+        q->x *= recipLen;
+        q->y *= recipLen;
+        q->z *= recipLen;
+    }
+}
+
+extern void __attribute__((overloadable))
+    rsQuaternionMultiply(rs_quaternion* q, float scalar) {
+    q->w *= scalar;
+    q->x *= scalar;
+    q->y *= scalar;
+    q->z *= scalar;
+}
+
+extern void __attribute__((overloadable))
+    rsQuaternionMultiply(rs_quaternion* q, const rs_quaternion* rhs) {
+    rs_quaternion qtmp;
+    rsQuaternionSet(&qtmp, q);
+
+    q->w = qtmp.w*rhs->w - qtmp.x*rhs->x - qtmp.y*rhs->y - qtmp.z*rhs->z;
+    q->x = qtmp.w*rhs->x + qtmp.x*rhs->w + qtmp.y*rhs->z - qtmp.z*rhs->y;
+    q->y = qtmp.w*rhs->y + qtmp.y*rhs->w + qtmp.z*rhs->x - qtmp.x*rhs->z;
+    q->z = qtmp.w*rhs->z + qtmp.z*rhs->w + qtmp.x*rhs->y - qtmp.y*rhs->x;
+    rsQuaternionNormalize(q);
+}
+
+extern void __attribute__((overloadable))
+    rsQuaternionSlerp(rs_quaternion* q, const rs_quaternion* q0, const rs_quaternion* q1, float t) {
+    if (t <= 0.0f) {
+        rsQuaternionSet(q, q0);
+        return;
+    }
+    if (t >= 1.0f) {
+        rsQuaternionSet(q, q1);
+        return;
+    }
+
+    rs_quaternion tempq0, tempq1;
+    rsQuaternionSet(&tempq0, q0);
+    rsQuaternionSet(&tempq1, q1);
+
+    float angle = rsQuaternionDot(q0, q1);
+    if (angle < 0) {
+        rsQuaternionMultiply(&tempq0, -1.0f);
+        angle *= -1.0f;
+    }
+
+    float scale, invScale;
+    if (angle + 1.0f > 0.05f) {
+        if (1.0f - angle >= 0.05f) {
+            float theta = acos(angle);
+            float invSinTheta = 1.0f / sin(theta);
+            scale = sin(theta * (1.0f - t)) * invSinTheta;
+            invScale = sin(theta * t) * invSinTheta;
+        } else {
+            scale = 1.0f - t;
+            invScale = t;
+        }
+    } else {
+        rsQuaternionSet(&tempq1, tempq0.z, -tempq0.y, tempq0.x, -tempq0.w);
+        scale = sin(M_PI * (0.5f - t));
+        invScale = sin(M_PI * t);
+    }
+
+    rsQuaternionSet(q, tempq0.w*scale + tempq1.w*invScale, tempq0.x*scale + tempq1.x*invScale,
+                        tempq0.y*scale + tempq1.y*invScale, tempq0.z*scale + tempq1.z*invScale);
+}
+#endif // (defined(RS_VERSION) && (RS_VERSION >= UNRELEASED))
diff --git a/scriptc/rs_quaternion.rsh b/scriptc/rs_quaternion.rsh
index c7c32e2..50b14b3 100644
--- a/scriptc/rs_quaternion.rsh
+++ b/scriptc/rs_quaternion.rsh
@@ -34,6 +34,7 @@
  *   q: Destination quaternion to add to.
  *   rhs: Quaternion to add.
  */
+#if !defined(RS_VERSION) || (RS_VERSION <= 23)
 static inline void __attribute__((overloadable))
     rsQuaternionAdd(rs_quaternion* q, const rs_quaternion* rhs) {
     q->w += rhs->w;
@@ -41,6 +42,7 @@
     q->y += rhs->y;
     q->z += rhs->z;
 }
+#endif
 
 /*
  * rsQuaternionConjugate: Conjugate a quaternion
@@ -50,12 +52,14 @@
  * Parameters:
  *   q: Quaternion to modify.
  */
+#if !defined(RS_VERSION) || (RS_VERSION <= 23)
 static inline void __attribute__((overloadable))
     rsQuaternionConjugate(rs_quaternion* q) {
     q->x = -q->x;
     q->y = -q->y;
     q->z = -q->z;
 }
+#endif
 
 /*
  * rsQuaternionDot: Dot product of two quaternions
@@ -66,10 +70,12 @@
  *   q0: First quaternion.
  *   q1: Second quaternion.
  */
+#if !defined(RS_VERSION) || (RS_VERSION <= 23)
 static inline float __attribute__((overloadable))
     rsQuaternionDot(const rs_quaternion* q0, const rs_quaternion* q1) {
     return q0->w*q1->w + q0->x*q1->x + q0->y*q1->y + q0->z*q1->z;
 }
+#endif
 
 /*
  * rsQuaternionGetMatrixUnit: Get a rotation matrix from a quaternion
@@ -80,6 +86,7 @@
  *   m: Resulting matrix.
  *   q: Normalized quaternion.
  */
+#if !defined(RS_VERSION) || (RS_VERSION <= 23)
 static inline void __attribute__((overloadable))
     rsQuaternionGetMatrixUnit(rs_matrix4x4* m, const rs_quaternion* q) {
     float xx = q->x * q->x;
@@ -104,6 +111,7 @@
     m->m[3]  = m->m[7] = m->m[11] = m->m[12] = m->m[13] = m->m[14] = 0.0f;
     m->m[15] = 1.0f;
 }
+#endif
 
 /*
  * rsQuaternionLoadRotateUnit: Quaternion that represents a rotation about an arbitrary unit vector
@@ -117,6 +125,7 @@
  *   y: Y component of the vector.
  *   z: Z component of the vector.
  */
+#if !defined(RS_VERSION) || (RS_VERSION <= 23)
 static inline void __attribute__((overloadable))
     rsQuaternionLoadRotateUnit(rs_quaternion* q, float rot, float x, float y, float z) {
     rot *= (float)(M_PI / 180.0f) * 0.5f;
@@ -128,6 +137,7 @@
     q->y = y * s;
     q->z = z * s;
 }
+#endif
 
 /*
  * rsQuaternionSet: Create a quaternion
@@ -142,6 +152,7 @@
  *   z: Z component.
  *   rhs: Source quaternion.
  */
+#if !defined(RS_VERSION) || (RS_VERSION <= 23)
 static inline void __attribute__((overloadable))
     rsQuaternionSet(rs_quaternion* q, float w, float x, float y, float z) {
     q->w = w;
@@ -149,7 +160,9 @@
     q->y = y;
     q->z = z;
 }
+#endif
 
+#if !defined(RS_VERSION) || (RS_VERSION <= 23)
 static inline void __attribute__((overloadable))
     rsQuaternionSet(rs_quaternion* q, const rs_quaternion* rhs) {
     q->w = rhs->w;
@@ -157,6 +170,7 @@
     q->y = rhs->y;
     q->z = rhs->z;
 }
+#endif
 
 /*
  * rsQuaternionLoadRotate: Create a rotation quaternion
@@ -171,6 +185,7 @@
  *   y: Y component of a vector.
  *   z: Z component of a vector.
  */
+#if !defined(RS_VERSION) || (RS_VERSION <= 23)
 static inline void __attribute__((overloadable))
     rsQuaternionLoadRotate(rs_quaternion* q, float rot, float x, float y, float z) {
     const float len = x*x + y*y + z*z;
@@ -182,6 +197,7 @@
     }
     rsQuaternionLoadRotateUnit(q, rot, x, y, z);
 }
+#endif
 
 /*
  * rsQuaternionNormalize: Normalize a quaternion
@@ -191,6 +207,7 @@
  * Parameters:
  *   q: Quaternion to normalize.
  */
+#if !defined(RS_VERSION) || (RS_VERSION <= 23)
 static inline void __attribute__((overloadable))
     rsQuaternionNormalize(rs_quaternion* q) {
     const float len = rsQuaternionDot(q, q);
@@ -202,6 +219,7 @@
         q->z *= recipLen;
     }
 }
+#endif
 
 /*
  * rsQuaternionMultiply: Multiply a quaternion by a scalar or another quaternion
@@ -214,6 +232,7 @@
  *   scalar: Scalar to multiply the quaternion by.
  *   rhs: Quaternion to multiply the destination quaternion by.
  */
+#if !defined(RS_VERSION) || (RS_VERSION <= 23)
 static inline void __attribute__((overloadable))
     rsQuaternionMultiply(rs_quaternion* q, float scalar) {
     q->w *= scalar;
@@ -221,7 +240,9 @@
     q->y *= scalar;
     q->z *= scalar;
 }
+#endif
 
+#if !defined(RS_VERSION) || (RS_VERSION <= 23)
 static inline void __attribute__((overloadable))
     rsQuaternionMultiply(rs_quaternion* q, const rs_quaternion* rhs) {
     rs_quaternion qtmp;
@@ -233,6 +254,7 @@
     q->z = qtmp.w*rhs->z + qtmp.z*rhs->w + qtmp.x*rhs->y - qtmp.y*rhs->x;
     rsQuaternionNormalize(q);
 }
+#endif
 
 /*
  * rsQuaternionSlerp: Spherical linear interpolation between two quaternions
@@ -245,6 +267,7 @@
  *   q1: Second input quaternion.
  *   t: How much to interpolate by.
  */
+#if !defined(RS_VERSION) || (RS_VERSION <= 23)
 static inline void __attribute__((overloadable))
     rsQuaternionSlerp(rs_quaternion* q, const rs_quaternion* q0, const rs_quaternion* q1, float t) {
     if (t <= 0.0f) {
@@ -286,5 +309,66 @@
     rsQuaternionSet(q, tempq0.w*scale + tempq1.w*invScale, tempq0.x*scale + tempq1.x*invScale,
                         tempq0.y*scale + tempq1.y*invScale, tempq0.z*scale + tempq1.z*invScale);
 }
+#endif
+
+#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
+extern void __attribute__((overloadable))
+    rsQuaternionAdd(rs_quaternion* q, const rs_quaternion* rhs);
+#endif
+
+#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
+extern void __attribute__((overloadable))
+    rsQuaternionConjugate(rs_quaternion* q);
+#endif
+
+#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
+extern float __attribute__((overloadable))
+    rsQuaternionDot(const rs_quaternion* q0, const rs_quaternion* q1);
+#endif
+
+#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
+extern void __attribute__((overloadable))
+    rsQuaternionGetMatrixUnit(rs_matrix4x4* m, const rs_quaternion* q);
+#endif
+
+#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
+extern void __attribute__((overloadable))
+    rsQuaternionLoadRotateUnit(rs_quaternion* q, float rot, float x, float y, float z);
+#endif
+
+#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
+extern void __attribute__((overloadable))
+    rsQuaternionSet(rs_quaternion* q, float w, float x, float y, float z);
+#endif
+
+#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
+extern void __attribute__((overloadable))
+    rsQuaternionSet(rs_quaternion* q, const rs_quaternion* rhs);
+#endif
+
+#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
+extern void __attribute__((overloadable))
+    rsQuaternionLoadRotate(rs_quaternion* q, float rot, float x, float y, float z);
+#endif
+
+#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
+extern void __attribute__((overloadable))
+    rsQuaternionNormalize(rs_quaternion* q);
+#endif
+
+#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
+extern void __attribute__((overloadable))
+    rsQuaternionMultiply(rs_quaternion* q, float scalar);
+#endif
+
+#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
+extern void __attribute__((overloadable))
+    rsQuaternionMultiply(rs_quaternion* q, const rs_quaternion* rhs);
+#endif
+
+#if (defined(RS_VERSION) && (RS_VERSION >= 4294967295) && (defined(RS_DECLARE_EXPIRED_APIS) || RS_VERSION <= 4294967295))
+extern void __attribute__((overloadable))
+    rsQuaternionSlerp(rs_quaternion* q, const rs_quaternion* q0, const rs_quaternion* q1, float t);
+#endif
 
 #endif // RENDERSCRIPT_RS_QUATERNION_RSH