Merge commit 'b10a68c3' into manualmerge

Conflicts:
	cpu_ref/rsCpuIntrinsicColorMatrix.cpp

Change-Id: Ibc2f1514f8858d99f08380f698bc9ae533c69212
diff --git a/Android.mk b/Android.mk
index 4c02056..bdde8a1 100644
--- a/Android.mk
+++ b/Android.mk
@@ -40,6 +40,7 @@
 LOCAL_SHARED_LIBRARIES += libbcc libbcinfo libLLVM libui libgui libsync
 
 LOCAL_C_INCLUDES += frameworks/compile/libbcc/include
+LOCAL_C_INCLUDES += frameworks/rs/cpu_ref/linkloader/include
 
 LOCAL_CFLAGS += $(rs_base_CFLAGS)
 
@@ -117,12 +118,14 @@
 	rsAnimation.cpp \
 	rsComponent.cpp \
 	rsContext.cpp \
+	rsCppUtils.cpp \
 	rsDevice.cpp \
 	rsElement.cpp \
 	rsFBOCache.cpp \
 	rsFifoSocket.cpp \
 	rsFileA3D.cpp \
 	rsFont.cpp \
+	rsGrallocConsumer.cpp \
 	rsObjectBase.cpp \
 	rsMatrix2x2.cpp \
 	rsMatrix3x3.cpp \
@@ -149,8 +152,7 @@
 
 LOCAL_SHARED_LIBRARIES += liblog libcutils libutils libEGL libGLESv1_CM libGLESv2 libbcc
 LOCAL_SHARED_LIBRARIES += libui libbcinfo libLLVM libgui libsync libdl
-
-LOCAL_STATIC_LIBRARIES := libft2
+LOCAL_SHARED_LIBRARIES += libft2 libpng libz
 
 LOCAL_C_INCLUDES += external/freetype/include
 LOCAL_C_INCLUDES += frameworks/compile/libbcc/include
@@ -253,17 +255,17 @@
 #-----------------------------------------------------------------------------
 
 rsloader_SRC_FILES := \
-  driver/linkloader/android/librsloader.cpp \
-  driver/linkloader/lib/ELFHeader.cpp \
-  driver/linkloader/lib/ELFSymbol.cpp \
-  driver/linkloader/lib/ELFSectionHeader.cpp \
-  driver/linkloader/lib/ELFTypes.cpp \
-  driver/linkloader/lib/GOT.cpp \
-  driver/linkloader/lib/MemChunk.cpp \
-  driver/linkloader/lib/StubLayout.cpp \
-  driver/linkloader/utils/helper.cpp \
-  driver/linkloader/utils/raw_ostream.cpp \
-  driver/linkloader/utils/rsl_assert.cpp
+  cpu_ref/linkloader/android/librsloader.cpp \
+  cpu_ref/linkloader/lib/ELFHeader.cpp \
+  cpu_ref/linkloader/lib/ELFSymbol.cpp \
+  cpu_ref/linkloader/lib/ELFSectionHeader.cpp \
+  cpu_ref/linkloader/lib/ELFTypes.cpp \
+  cpu_ref/linkloader/lib/GOT.cpp \
+  cpu_ref/linkloader/lib/MemChunk.cpp \
+  cpu_ref/linkloader/lib/StubLayout.cpp \
+  cpu_ref/linkloader/utils/helper.cpp \
+  cpu_ref/linkloader/utils/raw_ostream.cpp \
+  cpu_ref/linkloader/utils/rsl_assert.cpp
 
 include $(CLEAR_VARS)
 
@@ -278,8 +280,8 @@
 LOCAL_CFLAGS += $(rs_base_CFLAGS)
 
 LOCAL_C_INCLUDES := \
-  $(LOCAL_PATH)/driver/linkloader \
-  $(LOCAL_PATH)/driver/linkloader/include \
+  $(LOCAL_PATH)/cpu_ref/linkloader \
+  $(LOCAL_PATH)/cpu_ref/linkloader/include \
   $(LOCAL_C_INCLUDES)
 
 include $(LLVM_ROOT_PATH)/llvm-device-build.mk
@@ -299,7 +301,7 @@
 LOCAL_SRC_FILES := $(rsloader_SRC_FILES)
 
 ifdef USE_MINGW
-LOCAL_SRC_FILES += driver/linkloader/lib/mmanWindows.cpp
+LOCAL_SRC_FILES += cpu_ref/linkloader/lib/mmanWindows.cpp
 endif
 
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
@@ -308,149 +310,11 @@
 LOCAL_CFLAGS += -D__HOST__
 
 LOCAL_C_INCLUDES := \
-  $(LOCAL_PATH)/driver/linkloader \
-  $(LOCAL_PATH)/driver/linkloader/include \
+  $(LOCAL_PATH)/cpu_ref/linkloader \
+  $(LOCAL_PATH)/cpu_ref/linkloader/include \
   $(LOCAL_C_INCLUDES)
 
 include $(LLVM_ROOT_PATH)/llvm-host-build.mk
 include $(BUILD_HOST_STATIC_LIBRARY)
 
-
-#=============================================================================
-# librsloader-test (Device)
-#-----------------------------------------------------------------------------
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := test-librsloader
-
-LOCAL_MODULE_TAGS := tests
-
-LOCAL_SHARED_LIBRARIES := \
-  libstlport
-
-LOCAL_STATIC_LIBRARIES := \
-  librsloader \
-  libcutils \
-  liblog \
-  libLLVMSupport
-
-LOCAL_SRC_FILES := \
-  driver/linkloader/android/test-librsloader.c
-
-LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
-
-LOCAL_CFLAGS += $(rs_base_CFLAGS)
-
-LOCAL_C_INCLUDES := \
-  $(LOCAL_PATH)/driver/linkloader \
-  $(LOCAL_PATH)/driver/linkloader/include
-
-include $(LLVM_ROOT_PATH)/llvm-device-build.mk
-include $(BUILD_EXECUTABLE)
-
-
-#=============================================================================
-# librsloader-test (Host)
-#-----------------------------------------------------------------------------
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := test-librsloader
-
-LOCAL_MODULE_TAGS := tests
-
-LOCAL_LDLIBS := \
-  -lpthread \
-  -ldl
-
-LOCAL_STATIC_LIBRARIES := \
-  librsloader \
-  libcutils \
-  liblog \
-  libLLVMSupport
-
-LOCAL_SRC_FILES := \
-  driver/linkloader/android/test-librsloader.c
-
-LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
-
-LOCAL_CFLAGS += $(rs_base_CFLAGS)
-
-LOCAL_C_INCLUDES := \
-  $(LOCAL_PATH)/driver/linkloader \
-  $(LOCAL_PATH)/driver/linkloader/include
-
-include $(LLVM_ROOT_PATH)/llvm-host-build.mk
-include $(BUILD_HOST_EXECUTABLE)
-
-
-#=============================================================================
-# rsloader
-#-----------------------------------------------------------------------------
-
-ifdef BUILD_RSLOADER_TOOL
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := rsloader
-
-LOCAL_MODULE_TAGS := tests
-
-LOCAL_SHARED_LIBRARIES := \
-  libstlport
-
-LOCAL_STATIC_LIBRARIES := \
-  libLLVMSupport
-
-LOCAL_SRC_FILES := \
-  driver/linkloader/lib/ELFHeader.cpp \
-  driver/linkloader/lib/ELFSymbol.cpp \
-  driver/linkloader/lib/ELFSectionHeader.cpp \
-  driver/linkloader/lib/ELFTypes.cpp \
-  driver/linkloader/lib/StubLayout.cpp \
-  driver/linkloader/utils/raw_ostream.cpp \
-  driver/linkloader/utils/rsl_assert.cpp \
-  driver/linkloader/utils/helper.cpp \
-  driver/linkloader/main.cpp
-
-LOCAL_C_INCLUDES := \
-  $(LOCAL_PATH)/driver/linkloader \
-  $(LOCAL_PATH)/driver/linkloader/include \
-  $(LOCAL_C_INCLUDES)
-
-include $(LLVM_ROOT_PATH)/llvm-device-build.mk
-include $(BUILD_EXECUTABLE)
-endif
-
-
-#=============================================================================
-# stub-layout-unit-test
-#-----------------------------------------------------------------------------
-
-ifdef BUILD_STUB_LAYOUT_TEST
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := stub-layout-unit-test
-
-LOCAL_MODULE_TAGS := tests
-
-LOCAL_SHARED_LIBRARIES := \
-  libstlport
-
-LOCAL_SRC_FILES := \
-  driver/linkloader/lib/StubLayout.cpp \
-  driver/linkloader/utils/raw_ostream.cpp \
-  driver/linkloader/utils/helper.cpp \
-  driver/linkloader/tests/stub-test.cpp
-
-LOCAL_C_INCLUDES := \
-  $(LOCAL_PATH)/driver/linkloader \
-  $(LOCAL_PATH)/driver/linkloader/include \
-  $(LOCAL_C_INCLUDES)
-
-include $(LLVM_ROOT_PATH)/llvm-device-build.mk
-include $(BUILD_EXECUTABLE)
-endif
-
-
 include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/cpp/Allocation.cpp b/cpp/Allocation.cpp
index 7755f74..f05b005 100644
--- a/cpp/Allocation.cpp
+++ b/cpp/Allocation.cpp
@@ -15,7 +15,6 @@
  */
 
 #include "RenderScript.h"
-#include <rs.h>
 
 using namespace android;
 using namespace RSC;
@@ -64,11 +63,14 @@
     mType = t;
     mUsage = usage;
 
-    if (t.get() != NULL) {
+    if (t != NULL) {
         updateCacheInfo(t);
     }
+
 }
 
+
+
 void Allocation::validateIsInt32() {
     RsDataType dt = mType->getElement()->getDataType();
     if ((dt == RS_TYPE_SIGNED_32) || (dt == RS_TYPE_UNSIGNED_32)) {
@@ -121,7 +123,7 @@
 void Allocation::updateFromNative() {
     BaseObj::updateFromNative();
 
-    const void *typeID = rsaAllocationGetType(mRS->getContext(), getID());
+    const void *typeID = RS::dispatch->AllocationGetType(mRS->getContext(), getID());
     if(typeID != NULL) {
         sp<const Type> old = mType;
         sp<Type> t = new Type((void *)typeID, mRS);
@@ -141,7 +143,7 @@
     default:
         ALOGE("Source must be exactly one usage type.");
     }
-    rsAllocationSyncAll(mRS->getContext(), getIDSafe(), srcLocation);
+    RS::dispatch->AllocationSyncAll(mRS->getContext(), getIDSafe(), srcLocation);
 }
 
 void Allocation::ioSendOutput() {
@@ -149,7 +151,7 @@
     if ((mUsage & RS_ALLOCATION_USAGE_IO_OUTPUT) == 0) {
         ALOGE("Can only send buffer if IO_OUTPUT usage specified.");
     }
-    rsAllocationIoSend(mRS->getContext(), getID());
+    RS::dispatch->AllocationIoSend(mRS->getContext(), getID());
 #endif
 }
 
@@ -158,12 +160,12 @@
     if ((mUsage & RS_ALLOCATION_USAGE_IO_INPUT) == 0) {
         ALOGE("Can only send buffer if IO_OUTPUT usage specified.");
     }
-    rsAllocationIoReceive(mRS->getContext(), getID());
+    RS::dispatch->AllocationIoReceive(mRS->getContext(), getID());
 #endif
 }
 
 void Allocation::generateMipmaps() {
-    rsAllocationGenerateMipmaps(mRS->getContext(), getID());
+    RS::dispatch->AllocationGenerateMipmaps(mRS->getContext(), getID());
 }
 
 void Allocation::copy1DRangeFrom(uint32_t off, size_t count, const void *data) {
@@ -177,8 +179,8 @@
         return;
     }
 
-    rsAllocation1DData(mRS->getContext(), getIDSafe(), off, mSelectedLOD, count, data,
-                       count * mType->getElement()->getSizeBytes());
+    RS::dispatch->Allocation1DData(mRS->getContext(), getIDSafe(), off, mSelectedLOD, count, data,
+                                   count * mType->getElement()->getSizeBytes());
 }
 
 void Allocation::copy1DRangeTo(uint32_t off, size_t count, void *data) {
@@ -191,17 +193,17 @@
         return;
     }
 
-    rsAllocation1DRead(mRS->getContext(), getIDSafe(), off, mSelectedLOD, count, data,
-                       count * mType->getElement()->getSizeBytes());
+    RS::dispatch->Allocation1DRead(mRS->getContext(), getIDSafe(), off, mSelectedLOD, count, data,
+                                   count * mType->getElement()->getSizeBytes());
 }
 
 void Allocation::copy1DRangeFrom(uint32_t off, size_t count, sp<const Allocation> data,
                                  uint32_t dataOff) {
 
-    rsAllocationCopy2DRange(mRS->getContext(), getIDSafe(), off, 0,
-                            mSelectedLOD, mSelectedFace,
-                            count, 1, data->getIDSafe(), dataOff, 0,
-                            data->mSelectedLOD, data->mSelectedFace);
+    RS::dispatch->AllocationCopy2DRange(mRS->getContext(), getIDSafe(), off, 0,
+                                        mSelectedLOD, mSelectedFace,
+                                        count, 1, data->getIDSafe(), dataOff, 0,
+                                        data->mSelectedLOD, data->mSelectedFace);
 }
 
 void Allocation::copy1DFrom(const void* data) {
@@ -226,31 +228,31 @@
 void Allocation::copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
                                  const void *data) {
     validate2DRange(xoff, yoff, w, h);
-    rsAllocation2DData(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
-                       w, h, data, w * h * mType->getElement()->getSizeBytes(), w * mType->getElement()->getSizeBytes());
+    RS::dispatch->Allocation2DData(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
+                                   w, h, data, w * h * mType->getElement()->getSizeBytes(), w * mType->getElement()->getSizeBytes());
 }
 
 void Allocation::copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
                                  sp<const Allocation> data, uint32_t dataXoff, uint32_t dataYoff) {
     validate2DRange(xoff, yoff, w, h);
-    rsAllocationCopy2DRange(mRS->getContext(), getIDSafe(), xoff, yoff,
-                            mSelectedLOD, mSelectedFace,
-                            w, h, data->getIDSafe(), dataXoff, dataYoff,
-                            data->mSelectedLOD, data->mSelectedFace);
+    RS::dispatch->AllocationCopy2DRange(mRS->getContext(), getIDSafe(), xoff, yoff,
+                                        mSelectedLOD, mSelectedFace,
+                                        w, h, data->getIDSafe(), dataXoff, dataYoff,
+                                        data->mSelectedLOD, data->mSelectedFace);
 }
 
 void Allocation::copy2DRangeTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
                                void* data) {
     validate2DRange(xoff, yoff, w, h);
-    rsAllocation2DRead(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
-                       w, h, data, w * h * mType->getElement()->getSizeBytes(), w * mType->getElement()->getSizeBytes());
+    RS::dispatch->Allocation2DRead(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
+                                   w, h, data, w * h * mType->getElement()->getSizeBytes(), w * mType->getElement()->getSizeBytes());
 }
 
 void Allocation::copy2DStridedFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
                                    const void *data, size_t stride) {
     validate2DRange(xoff, yoff, w, h);
-    rsAllocation2DData(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
-                       w, h, data, w * h * mType->getElement()->getSizeBytes(), stride);
+    RS::dispatch->Allocation2DData(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
+                                   w, h, data, w * h * mType->getElement()->getSizeBytes(), stride);
 }
 
 void Allocation::copy2DStridedFrom(const void* data, size_t stride) {
@@ -260,52 +262,17 @@
 void Allocation::copy2DStridedTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
                                  void *data, size_t stride) {
     validate2DRange(xoff, yoff, w, h);
-    rsAllocation2DRead(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
-                       w, h, data, w * h * mType->getElement()->getSizeBytes(), stride);
+    RS::dispatch->Allocation2DRead(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
+                                   w, h, data, w * h * mType->getElement()->getSizeBytes(), stride);
 }
 
 void Allocation::copy2DStridedTo(void* data, size_t stride) {
     copy2DStridedTo(0, 0, mCurrentDimX, mCurrentDimY, data, stride);
 }
 
-
-/*
-void resize(int dimX) {
-    if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
-        throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
-    }
-    mRS.nAllocationResize1D(getID(), dimX);
-    mRS.finish();  // Necessary because resize is fifoed and update is async.
-
-    int typeID = mRS.nAllocationGetType(getID());
-    mType = new Type(typeID, mRS);
-    mType.updateFromNative();
-    updateCacheInfo(mType);
-}
-
-void resize(int dimX, int dimY) {
-    if ((mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
-        throw new RSInvalidStateException(
-            "Resize only support for 2D allocations at this time.");
-    }
-    if (mType.getY() == 0) {
-        throw new RSInvalidStateException(
-            "Resize only support for 2D allocations at this time.");
-    }
-    mRS.nAllocationResize2D(getID(), dimX, dimY);
-    mRS.finish();  // Necessary because resize is fifoed and update is async.
-
-    int typeID = mRS.nAllocationGetType(getID());
-    mType = new Type(typeID, mRS);
-    mType.updateFromNative();
-    updateCacheInfo(mType);
-}
-*/
-
-
-android::sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
-                                                RsAllocationMipmapControl mips, uint32_t usage) {
-    void *id = rsAllocationCreateTyped(rs->getContext(), type->getID(), mips, usage, 0);
+sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
+                                    RsAllocationMipmapControl mips, uint32_t usage) {
+    void *id = RS::dispatch->AllocationCreateTyped(rs->getContext(), type->getID(), mips, usage, 0);
     if (id == 0) {
         ALOGE("Allocation creation failed.");
         return NULL;
@@ -313,24 +280,24 @@
     return new Allocation(id, rs, type, usage);
 }
 
-android::sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
-                                                RsAllocationMipmapControl mips, uint32_t usage,
-                                                void *pointer) {
-    void *id = rsAllocationCreateTyped(rs->getContext(), type->getID(), mips, usage,
-                                       (uintptr_t)pointer);
+sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
+                                    RsAllocationMipmapControl mips, uint32_t usage,
+                                    void *pointer) {
+    void *id = RS::dispatch->AllocationCreateTyped(rs->getContext(), type->getID(), mips, usage,
+                                                   (uintptr_t)pointer);
     if (id == 0) {
         ALOGE("Allocation creation failed.");
     }
     return new Allocation(id, rs, type, usage);
 }
 
-android::sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
-                                                uint32_t usage) {
+sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
+                                    uint32_t usage) {
     return createTyped(rs, type, RS_ALLOCATION_MIPMAP_NONE, usage);
 }
 
-android::sp<Allocation> Allocation::createSized(sp<RS> rs, sp<const Element> e,
-                                                size_t count, uint32_t usage) {
+sp<Allocation> Allocation::createSized(sp<RS> rs, sp<const Element> e,
+                                    size_t count, uint32_t usage) {
     Type::Builder b(rs, e);
     b.setX(count);
     sp<const Type> t = b.create();
@@ -338,8 +305,8 @@
     return createTyped(rs, t, usage);
 }
 
-android::sp<Allocation> Allocation::createSized2D(sp<RS> rs, sp<const Element> e,
-                                                  size_t x, size_t y, uint32_t usage) {
+sp<Allocation> Allocation::createSized2D(sp<RS> rs, sp<const Element> e,
+                                      size_t x, size_t y, uint32_t usage) {
     Type::Builder b(rs, e);
     b.setX(x);
     b.setY(y);
diff --git a/cpp/Android.mk b/cpp/Android.mk
index 145f487..d266d14 100644
--- a/cpp/Android.mk
+++ b/cpp/Android.mk
@@ -1,3 +1,14 @@
+rs_cpp_SRC_FILES := \
+	RenderScript.cpp \
+	BaseObj.cpp \
+	Element.cpp \
+	Type.cpp \
+	Allocation.cpp \
+	Script.cpp \
+	ScriptC.cpp \
+	ScriptIntrinsics.cpp \
+	Sampler.cpp
+
 LOCAL_PATH:= $(call my-dir)
 include $(CLEAR_VARS)
 
@@ -11,37 +22,50 @@
 endif
 local_cflags_for_rs_cpp += -DRS_VERSION=$(RS_VERSION)
 
+LOCAL_SRC_FILES := $(rs_cpp_SRC_FILES)
+
 LOCAL_CFLAGS += $(local_cflags_for_rs_cpp)
 
-LOCAL_SRC_FILES:= \
-	RenderScript.cpp \
-	BaseObj.cpp \
-	Element.cpp \
-	Type.cpp \
-	Allocation.cpp \
-	Script.cpp \
-	ScriptC.cpp \
-	ScriptIntrinsics.cpp
-
 LOCAL_SHARED_LIBRARIES := \
-	libRS \
 	libz \
 	libcutils \
 	libutils \
-	liblog
+	liblog \
+	libdl \
+	libstlport
 
 LOCAL_MODULE:= libRScpp
 
 LOCAL_MODULE_TAGS := optional
 
-intermediates := $(call intermediates-dir-for,STATIC_LIBRARIES,libRS,TARGET,)
-librs_generated_headers := \
-    $(intermediates)/rsgApiStructs.h \
-    $(intermediates)/rsgApiFuncDecl.h
-LOCAL_GENERATED_SOURCES := $(librs_generated_headers)
-
 LOCAL_C_INCLUDES += frameworks/rs
+LOCAL_C_INCLUDES += external/stlport/stlport bionic/ bionic/libstdc++/include
 LOCAL_C_INCLUDES += $(intermediates)
 
-
 include $(BUILD_SHARED_LIBRARY)
+
+
+include $(CLEAR_VARS)
+
+LOCAL_CFLAGS += $(local_cflags_for_rs_cpp)
+
+LOCAL_SRC_FILES := $(rs_cpp_SRC_FILES)
+
+LOCAL_STATIC_LIBRARIES := \
+	libz \
+	libcutils \
+	libutils \
+	liblog \
+	libstlport_static
+
+LOCAL_SHARED_LIBRARIES := libdl
+
+LOCAL_MODULE:= libRScpp_static
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_C_INCLUDES += frameworks/rs
+LOCAL_C_INCLUDES += external/stlport/stlport bionic/ bionic/libstdc++/include
+LOCAL_C_INCLUDES += $(intermediates)
+
+include $(BUILD_STATIC_LIBRARY)
diff --git a/cpp/BaseObj.cpp b/cpp/BaseObj.cpp
index 828bd87..efcb83a 100644
--- a/cpp/BaseObj.cpp
+++ b/cpp/BaseObj.cpp
@@ -15,7 +15,6 @@
  */
 
 #include "RenderScript.h"
-#include <rs.h>
 
 using namespace android;
 using namespace RSC;
@@ -28,7 +27,7 @@
 }
 
 void * BaseObj::getObjID(sp<const BaseObj> o) {
-    return o.get() == NULL ? NULL : o->getID();
+    return o == NULL ? NULL : o->getID();
 }
 
 
@@ -44,20 +43,20 @@
 }
 
 BaseObj::~BaseObj() {
-    rsObjDestroy(mRS->getContext(), mID);
+    RS::dispatch->ObjDestroy(mRS->getContext(), mID);
     mRS = NULL;
     mID = NULL;
 }
 
 void BaseObj::updateFromNative() {
     const char *name = NULL;
-    rsaGetName(mRS->getContext(), mID, &name);
+    RS::dispatch->GetName(mRS->getContext(), mID, &name);
     mName = name;
 }
 
-bool BaseObj::equals(const BaseObj *obj) {
+bool BaseObj::equals(sp<const BaseObj> obj) {
     // Early-out check to see if both BaseObjs are actually the same
-    if (this == obj)
+    if (this == obj.get())
         return true;
     return mID == obj->mID;
 }
diff --git a/cpp/Element.cpp b/cpp/Element.cpp
index 51a96cd..d4b2c66 100644
--- a/cpp/Element.cpp
+++ b/cpp/Element.cpp
@@ -18,12 +18,11 @@
 #include <string.h>
 
 #include "RenderScript.h"
-#include <rs.h>
 
 using namespace android;
 using namespace RSC;
 
-sp<const Element> Element::getSubElement(uint32_t index) {
+android::RSC::sp<const Element> Element::getSubElement(uint32_t index) {
     if (!mVisibleElementMap.size()) {
         mRS->throwError("Element contains no sub-elements");
     }
@@ -64,9 +63,13 @@
 }
 
 
-#define CREATE_USER(N, T) sp<const Element> Element::N(sp<RS> rs) { \
-    return createUser(rs, RS_TYPE_##T); \
-}
+#define CREATE_USER(N, T) android::RSC::sp<const Element> Element::N(android::RSC::sp<RS> rs) { \
+    if (rs->mElements.N == NULL) {                                  \
+        rs->mElements.N = (createUser(rs, RS_TYPE_##T)).get();      \
+    }                                                               \
+    return rs->mElements.N;                                         \
+    }
+
 CREATE_USER(BOOLEAN, BOOLEAN);
 CREATE_USER(U8, UNSIGNED_8);
 CREATE_USER(I8, SIGNED_8);
@@ -92,7 +95,7 @@
 CREATE_USER(MATRIX_3X3, MATRIX_3X3);
 CREATE_USER(MATRIX_2X2, MATRIX_2X2);
 
-#define CREATE_PIXEL(N, T, K) sp<const Element> Element::N(sp<RS> rs) { \
+#define CREATE_PIXEL(N, T, K) android::RSC::sp<const Element> Element::N(android::RSC::sp<RS> rs) { \
     return createPixel(rs, RS_TYPE_##T, RS_KIND_##K); \
 }
 CREATE_PIXEL(A_8, UNSIGNED_8, PIXEL_A);
@@ -101,13 +104,13 @@
 CREATE_PIXEL(RGBA_4444, UNSIGNED_4_4_4_4, PIXEL_RGBA);
 CREATE_PIXEL(RGBA_8888, UNSIGNED_8, PIXEL_RGBA);
 
-#define CREATE_VECTOR(N, T) sp<const Element> Element::N##_2(sp<RS> rs) { \
+#define CREATE_VECTOR(N, T) android::RSC::sp<const Element> Element::N##_2(android::RSC::sp<RS> rs) { \
     return createVector(rs, RS_TYPE_##T, 2); \
 } \
-sp<const Element> Element::N##_3(sp<RS> rs) { \
+android::RSC::sp<const Element> Element::N##_3(android::RSC::sp<RS> rs) { \
     return createVector(rs, RS_TYPE_##T, 3); \
 } \
-sp<const Element> Element::N##_4(sp<RS> rs) { \
+android::RSC::sp<const Element> Element::N##_4(android::RSC::sp<RS> rs) { \
     return createVector(rs, RS_TYPE_##T, 4); \
 }
 CREATE_VECTOR(U8, UNSIGNED_8);
@@ -140,15 +143,15 @@
     // Make a map that points us at non-padding elements
     for (size_t ct = 0; ct < fieldCount; ct ++) {
         if (mElementNames[ct].string()[0] != '#') {
-            mVisibleElementMap.push((uint32_t)ct);
+            mVisibleElementMap.push_back((uint32_t)ct);
         }
     }
 }
 
-Element::Element(void *id, sp<RS> rs,
-                 android::Vector<sp<Element> > &elements,
-                 android::Vector<android::String8> &elementNames,
-                 android::Vector<uint32_t> &arraySizes) : BaseObj(id, rs) {
+Element::Element(void *id, android::RSC::sp<RS> rs,
+                 std::vector<android::RSC::sp<Element> > &elements,
+                 std::vector<android::String8> &elementNames,
+                 std::vector<uint32_t> &arraySizes) : BaseObj(id, rs) {
     mSizeBytes = 0;
     mVectorSize = 1;
     mElements = elements;
@@ -159,7 +162,7 @@
     mKind = RS_KIND_USER;
 
     for (size_t ct = 0; ct < mElements.size(); ct++ ) {
-        mOffsetInBytes.push(mSizeBytes);
+        mOffsetInBytes.push_back(mSizeBytes);
         mSizeBytes += mElements[ct]->mSizeBytes * mArraySizes[ct];
     }
     updateVisibleSubElements();
@@ -219,7 +222,7 @@
     return 0;
 }
 
-Element::Element(void *id, sp<RS> rs,
+Element::Element(void *id, android::RSC::sp<RS> rs,
                  RsDataType dt, RsDataKind dk, bool norm, uint32_t size) :
     BaseObj(id, rs)
 {
@@ -249,20 +252,20 @@
     updateVisibleSubElements();
 }
 
-sp<const Element> Element::createUser(sp<RS> rs, RsDataType dt) {
-    void * id = rsElementCreate(rs->getContext(), dt, RS_KIND_USER, false, 1);
+android::RSC::sp<const Element> Element::createUser(android::RSC::sp<RS> rs, RsDataType dt) {
+    void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, 1);
     return new Element(id, rs, dt, RS_KIND_USER, false, 1);
 }
 
-sp<const Element> Element::createVector(sp<RS> rs, RsDataType dt, uint32_t size) {
+android::RSC::sp<const Element> Element::createVector(android::RSC::sp<RS> rs, RsDataType dt, uint32_t size) {
     if (size < 2 || size > 4) {
         rs->throwError("Vector size out of range 2-4.");
     }
-    void *id = rsElementCreate(rs->getContext(), dt, RS_KIND_USER, false, size);
+    void *id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, size);
     return new Element(id, rs, dt, RS_KIND_USER, false, size);
 }
 
-sp<const Element> Element::createPixel(sp<RS> rs, RsDataType dt, RsDataKind dk) {
+android::RSC::sp<const Element> Element::createPixel(android::RSC::sp<RS> rs, RsDataType dt, RsDataKind dk) {
     if (!(dk == RS_KIND_PIXEL_L ||
           dk == RS_KIND_PIXEL_A ||
           dk == RS_KIND_PIXEL_LA ||
@@ -309,11 +312,11 @@
         break;
     }
 
-    void * id = rsElementCreate(rs->getContext(), dt, dk, true, size);
+    void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, dk, true, size);
     return new Element(id, rs, dt, dk, true, size);
 }
 
-bool Element::isCompatible(sp<const Element>e) {
+bool Element::isCompatible(android::RSC::sp<const Element>e) {
     // Try strict BaseObj equality to start with.
     if (this == e.get()) {
         return true;
@@ -329,12 +332,12 @@
             (mVectorSize == e->mVectorSize));
 }
 
-Element::Builder::Builder(sp<RS> rs) {
+Element::Builder::Builder(android::RSC::sp<RS> rs) {
     mRS = rs;
     mSkipPadding = false;
 }
 
-void Element::Builder::add(sp</*const*/ Element>e, android::String8 &name, uint32_t arraySize) {
+void Element::Builder::add(android::RSC::sp</*const*/ Element>e, android::String8 &name, uint32_t arraySize) {
     // Skip padding fields after a vector 3 type.
     if (mSkipPadding) {
         const char *s1 = "#padding_";
@@ -354,12 +357,12 @@
         mSkipPadding = false;
     }
 
-    mElements.add(e);
-    mElementNames.add(name);
-    mArraySizes.add(arraySize);
+    mElements.push_back(e);
+    mElementNames.push_back(name);
+    mArraySizes.push_back(arraySize);
 }
 
-sp<const Element> Element::Builder::create() {
+android::RSC::sp<const Element> Element::Builder::create() {
     size_t fieldCount = mElements.size();
     const char ** nameArray = (const char **)calloc(fieldCount, sizeof(char *));
     const Element ** elementArray = (const Element **)calloc(fieldCount, sizeof(Element *));
@@ -371,10 +374,10 @@
         sizeArray[ct] = mElementNames[ct].length();
     }
 
-    void *id = rsElementCreate2(mRS->getContext(),
+    void *id = RS::dispatch->ElementCreate2(mRS->getContext(),
                                 (RsElement *)elementArray, fieldCount,
                                 nameArray, fieldCount * sizeof(size_t),  sizeArray,
-                                (const uint32_t *)mArraySizes.array(), fieldCount);
+                                (const uint32_t *)&mArraySizes[0], fieldCount);
 
 
     free(nameArray);
diff --git a/cpp/RenderScript.cpp b/cpp/RenderScript.cpp
index 134d34b..18e7117 100644
--- a/cpp/RenderScript.cpp
+++ b/cpp/RenderScript.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012 The Android Open Source Project
+ * Copyright (C) 2013 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -19,13 +19,25 @@
 #include <pthread.h>
 
 #include "RenderScript.h"
-#include "rs.h"
+#include "rsCppStructs.h"
+
+#include <dlfcn.h>
+
+#if !defined(RS_SERVER) && defined(HAVE_ANDROID_OS)
+#include <cutils/properties.h>
+#endif
+
+#define LOG_NDEBUG 0
+#define LOG_TAG "rsC++"
 
 using namespace android;
 using namespace RSC;
 
 bool RS::gInitialized = false;
+bool RS::usingNative = false;
 pthread_mutex_t RS::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
+dispatchTable* RS::dispatch = NULL;
+static int gInitError = 0;
 
 RS::RS() {
     mDev = NULL;
@@ -33,36 +45,435 @@
     mErrorFunc = NULL;
     mMessageFunc = NULL;
     mMessageRun = false;
+    mInit = false;
 
     memset(&mElements, 0, sizeof(mElements));
+    memset(&mSamplers, 0, sizeof(mSamplers));
 }
 
 RS::~RS() {
-    mMessageRun = false;
+    if (mInit == true) {
+        mMessageRun = false;
 
-    rsContextDeinitToClient(mContext);
+        RS::dispatch->ContextDeinitToClient(mContext);
 
-    void *res = NULL;
-    int status = pthread_join(mMessageThreadId, &res);
+        void *res = NULL;
+        int status = pthread_join(mMessageThreadId, &res);
 
-    rsContextDestroy(mContext);
-    mContext = NULL;
-    rsDeviceDestroy(mDev);
-    mDev = NULL;
+        RS::dispatch->ContextDestroy(mContext);
+        mContext = NULL;
+        RS::dispatch->DeviceDestroy(mDev);
+        mDev = NULL;
+    }
 }
 
 bool RS::init(bool forceCpu, bool synchronous) {
     return RS::init(RS_VERSION, forceCpu, synchronous);
 }
 
+static bool loadSymbols(void* handle) {
+
+    RS::dispatch->AllocationGetType = (AllocationGetTypeFnPtr)dlsym(handle, "rsaAllocationGetType");
+    if (RS::dispatch->AllocationGetType == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->AllocationGetType");
+        return false;
+    }
+    RS::dispatch->TypeGetNativeData = (TypeGetNativeDataFnPtr)dlsym(handle, "rsaTypeGetNativeData");
+    if (RS::dispatch->TypeGetNativeData == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->TypeGetNativeData");
+        return false;
+    }
+    RS::dispatch->ElementGetNativeData = (ElementGetNativeDataFnPtr)dlsym(handle, "rsaElementGetNativeData");
+    if (RS::dispatch->ElementGetNativeData == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ElementGetNativeData");
+        return false;
+    }
+    RS::dispatch->ElementGetSubElements = (ElementGetSubElementsFnPtr)dlsym(handle, "rsaElementGetSubElements");
+    if (RS::dispatch->ElementGetSubElements == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ElementGetSubElements");
+        return false;
+    }
+    RS::dispatch->DeviceCreate = (DeviceCreateFnPtr)dlsym(handle, "rsDeviceCreate");
+    if (RS::dispatch->DeviceCreate == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->DeviceCreate");
+        return false;
+    }
+    RS::dispatch->DeviceDestroy = (DeviceDestroyFnPtr)dlsym(handle, "rsDeviceDestroy");
+    if (RS::dispatch->DeviceDestroy == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->DeviceDestroy");
+        return false;
+    }
+    RS::dispatch->DeviceSetConfig = (DeviceSetConfigFnPtr)dlsym(handle, "rsDeviceSetConfig");
+    if (RS::dispatch->DeviceSetConfig == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->DeviceSetConfig");
+        return false;
+    }
+    RS::dispatch->ContextCreate = (ContextCreateFnPtr)dlsym(handle, "rsContextCreate");;
+    if (RS::dispatch->ContextCreate == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ContextCreate");
+        return false;
+    }
+    RS::dispatch->GetName = (GetNameFnPtr)dlsym(handle, "rsaGetName");;
+    if (RS::dispatch->GetName == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->GetName");
+        return false;
+    }
+    RS::dispatch->ContextDestroy = (ContextDestroyFnPtr)dlsym(handle, "rsContextDestroy");
+    if (RS::dispatch->ContextDestroy == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ContextDestroy");
+        return false;
+    }
+    RS::dispatch->ContextGetMessage = (ContextGetMessageFnPtr)dlsym(handle, "rsContextGetMessage");
+    if (RS::dispatch->ContextGetMessage == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ContextGetMessage");
+        return false;
+    }
+    RS::dispatch->ContextPeekMessage = (ContextPeekMessageFnPtr)dlsym(handle, "rsContextPeekMessage");
+    if (RS::dispatch->ContextPeekMessage == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ContextPeekMessage");
+        return false;
+    }
+    RS::dispatch->ContextSendMessage = (ContextSendMessageFnPtr)dlsym(handle, "rsContextSendMessage");
+    if (RS::dispatch->ContextSendMessage == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ContextSendMessage");
+        return false;
+    }
+    RS::dispatch->ContextInitToClient = (ContextInitToClientFnPtr)dlsym(handle, "rsContextInitToClient");
+    if (RS::dispatch->ContextInitToClient == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ContextInitToClient");
+        return false;
+    }
+    RS::dispatch->ContextDeinitToClient = (ContextDeinitToClientFnPtr)dlsym(handle, "rsContextDeinitToClient");
+    if (RS::dispatch->ContextDeinitToClient == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ContextDeinitToClient");
+        return false;
+    }
+    RS::dispatch->TypeCreate = (TypeCreateFnPtr)dlsym(handle, "rsTypeCreate");
+    if (RS::dispatch->TypeCreate == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->TypeCreate");
+        return false;
+    }
+    RS::dispatch->AllocationCreateTyped = (AllocationCreateTypedFnPtr)dlsym(handle, "rsAllocationCreateTyped");
+    if (RS::dispatch->AllocationCreateTyped == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->AllocationCreateTyped");
+        return false;
+    }
+    RS::dispatch->AllocationCreateFromBitmap = (AllocationCreateFromBitmapFnPtr)dlsym(handle, "rsAllocationCreateFromBitmap");
+    if (RS::dispatch->AllocationCreateFromBitmap == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->AllocationCreateFromBitmap");
+        return false;
+    }
+    RS::dispatch->AllocationCubeCreateFromBitmap = (AllocationCubeCreateFromBitmapFnPtr)dlsym(handle, "rsAllocationCubeCreateFromBitmap");
+    if (RS::dispatch->AllocationCubeCreateFromBitmap == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->AllocationCubeCreateFromBitmap");
+        return false;
+    }
+    RS::dispatch->AllocationGetSurface = (AllocationGetSurfaceFnPtr)dlsym(handle, "rsAllocationGetSurface");
+    if (RS::dispatch->AllocationGetSurface == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->AllocationGetSurface");
+        return false;
+    }
+    RS::dispatch->AllocationSetSurface = (AllocationSetSurfaceFnPtr)dlsym(handle, "rsAllocationSetSurface");
+    if (RS::dispatch->AllocationSetSurface == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->AllocationSetSurface");
+        return false;
+    }
+    RS::dispatch->ContextFinish = (ContextFinishFnPtr)dlsym(handle, "rsContextFinish");
+    if (RS::dispatch->ContextFinish == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ContextFinish");
+        return false;
+    }
+    RS::dispatch->ContextDump = (ContextDumpFnPtr)dlsym(handle, "rsContextDump");
+    if (RS::dispatch->ContextDump == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ContextDump");
+        return false;
+    }
+    RS::dispatch->ContextSetPriority = (ContextSetPriorityFnPtr)dlsym(handle, "rsContextSetPriority");
+    if (RS::dispatch->ContextSetPriority == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ContextSetPriority");
+        return false;
+    }
+    RS::dispatch->AssignName = (AssignNameFnPtr)dlsym(handle, "rsAssignName");
+    if (RS::dispatch->AssignName == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->AssignName");
+        return false;
+    }
+    RS::dispatch->ObjDestroy = (ObjDestroyFnPtr)dlsym(handle, "rsObjDestroy");
+    if (RS::dispatch->ObjDestroy == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ObjDestroy");
+        return false;
+    }
+    RS::dispatch->ElementCreate = (ElementCreateFnPtr)dlsym(handle, "rsElementCreate");
+    if (RS::dispatch->ElementCreate == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ElementCreate");
+        return false;
+    }
+    RS::dispatch->ElementCreate2 = (ElementCreate2FnPtr)dlsym(handle, "rsElementCreate2");
+    if (RS::dispatch->ElementCreate2 == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ElementCreate2");
+        return false;
+    }
+    RS::dispatch->AllocationCopyToBitmap = (AllocationCopyToBitmapFnPtr)dlsym(handle, "rsAllocationCopyToBitmap");
+    if (RS::dispatch->AllocationCopyToBitmap == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->AllocationCopyToBitmap");
+        return false;
+    }
+    RS::dispatch->Allocation1DData = (Allocation1DDataFnPtr)dlsym(handle, "rsAllocation1DData");
+    if (RS::dispatch->Allocation1DData == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->Allocation1DData");
+        return false;
+    }
+    RS::dispatch->Allocation1DElementData = (Allocation1DElementDataFnPtr)dlsym(handle, "rsAllocation1DElementData");
+    if (RS::dispatch->Allocation1DElementData == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->Allocation1DElementData");
+        return false;
+    }
+    RS::dispatch->Allocation2DData = (Allocation2DDataFnPtr)dlsym(handle, "rsAllocation2DData");
+    if (RS::dispatch->Allocation2DData == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->Allocation2DData");
+        return false;
+    }
+    RS::dispatch->Allocation3DData = (Allocation3DDataFnPtr)dlsym(handle, "rsAllocation3DData");
+    if (RS::dispatch->Allocation3DData == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->Allocation3DData");
+        return false;
+    }
+    RS::dispatch->AllocationGenerateMipmaps = (AllocationGenerateMipmapsFnPtr)dlsym(handle, "rsAllocationGenerateMipmaps");
+    if (RS::dispatch->AllocationGenerateMipmaps == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->AllocationGenerateMipmaps");
+        return false;
+    }
+    RS::dispatch->AllocationRead = (AllocationReadFnPtr)dlsym(handle, "rsAllocationRead");
+    if (RS::dispatch->AllocationRead == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->AllocationRead");
+        return false;
+    }
+    RS::dispatch->Allocation1DRead = (Allocation1DReadFnPtr)dlsym(handle, "rsAllocation1DRead");
+    if (RS::dispatch->Allocation1DRead == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->Allocation1DRead");
+        return false;
+    }
+    RS::dispatch->Allocation2DRead = (Allocation2DReadFnPtr)dlsym(handle, "rsAllocation2DRead");
+    if (RS::dispatch->Allocation2DRead == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->Allocation2DRead");
+        return false;
+    }
+    RS::dispatch->AllocationSyncAll = (AllocationSyncAllFnPtr)dlsym(handle, "rsAllocationSyncAll");
+    if (RS::dispatch->AllocationSyncAll == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->AllocationSyncAll");
+        return false;
+    }
+    RS::dispatch->AllocationResize1D = (AllocationResize1DFnPtr)dlsym(handle, "rsAllocationResize1D");
+    if (RS::dispatch->AllocationResize1D == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->AllocationResize1D");
+        return false;
+    }
+    RS::dispatch->AllocationCopy2DRange = (AllocationCopy2DRangeFnPtr)dlsym(handle, "rsAllocationCopy2DRange");
+    if (RS::dispatch->AllocationCopy2DRange == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->AllocationCopy2DRange");
+        return false;
+    }
+    RS::dispatch->AllocationCopy3DRange = (AllocationCopy3DRangeFnPtr)dlsym(handle, "rsAllocationCopy3DRange");
+    if (RS::dispatch->AllocationCopy3DRange == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->AllocationCopy3DRange");
+        return false;
+    }
+    RS::dispatch->SamplerCreate = (SamplerCreateFnPtr)dlsym(handle, "rsSamplerCreate");
+    if (RS::dispatch->SamplerCreate == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->SamplerCreate");
+        return false;
+    }
+    RS::dispatch->ScriptBindAllocation = (ScriptBindAllocationFnPtr)dlsym(handle, "rsScriptBindAllocation");
+    if (RS::dispatch->ScriptBindAllocation == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptBindAllocation");
+        return false;
+    }
+    RS::dispatch->ScriptSetTimeZone = (ScriptSetTimeZoneFnPtr)dlsym(handle, "rsScriptSetTimeZone");
+    if (RS::dispatch->ScriptSetTimeZone == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptSetTimeZone");
+        return false;
+    }
+    RS::dispatch->ScriptInvoke = (ScriptInvokeFnPtr)dlsym(handle, "rsScriptInvoke");
+    if (RS::dispatch->ScriptInvoke == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptInvoke");
+        return false;
+    }
+    RS::dispatch->ScriptInvokeV = (ScriptInvokeVFnPtr)dlsym(handle, "rsScriptInvokeV");
+    if (RS::dispatch->ScriptInvokeV == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptInvokeV");
+        return false;
+    }
+    RS::dispatch->ScriptForEach = (ScriptForEachFnPtr)dlsym(handle, "rsScriptForEach");
+    if (RS::dispatch->ScriptForEach == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptForEach");
+        return false;
+    }
+    RS::dispatch->ScriptSetVarI = (ScriptSetVarIFnPtr)dlsym(handle, "rsScriptSetVarI");
+    if (RS::dispatch->ScriptSetVarI == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarI");
+        return false;
+    }
+    RS::dispatch->ScriptSetVarObj = (ScriptSetVarObjFnPtr)dlsym(handle, "rsScriptSetVarObj");
+    if (RS::dispatch->ScriptSetVarObj == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarObj");
+        return false;
+    }
+    RS::dispatch->ScriptSetVarJ = (ScriptSetVarJFnPtr)dlsym(handle, "rsScriptSetVarJ");
+    if (RS::dispatch->ScriptSetVarJ == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarJ");
+        return false;
+    }
+    RS::dispatch->ScriptSetVarF = (ScriptSetVarFFnPtr)dlsym(handle, "rsScriptSetVarF");
+    if (RS::dispatch->ScriptSetVarF == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarF");
+        return false;
+    }
+    RS::dispatch->ScriptSetVarD = (ScriptSetVarDFnPtr)dlsym(handle, "rsScriptSetVarD");
+    if (RS::dispatch->ScriptSetVarD == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarD");
+        return false;
+    }
+    RS::dispatch->ScriptSetVarV = (ScriptSetVarVFnPtr)dlsym(handle, "rsScriptSetVarV");
+    if (RS::dispatch->ScriptSetVarV == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarV");
+        return false;
+    }
+    RS::dispatch->ScriptGetVarV = (ScriptGetVarVFnPtr)dlsym(handle, "rsScriptGetVarV");
+    if (RS::dispatch->ScriptGetVarV == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptGetVarV");
+        return false;
+    }
+    RS::dispatch->ScriptSetVarVE = (ScriptSetVarVEFnPtr)dlsym(handle, "rsScriptSetVarVE");
+    if (RS::dispatch->ScriptSetVarVE == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarVE");
+        return false;
+    }
+    RS::dispatch->ScriptCCreate = (ScriptCCreateFnPtr)dlsym(handle, "rsScriptCCreate");
+    if (RS::dispatch->ScriptCCreate == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptCCreate");
+        return false;
+    }
+    RS::dispatch->ScriptIntrinsicCreate = (ScriptIntrinsicCreateFnPtr)dlsym(handle, "rsScriptIntrinsicCreate");
+    if (RS::dispatch->ScriptIntrinsicCreate == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptIntrinsicCreate");
+        return false;
+    }
+    RS::dispatch->ScriptKernelIDCreate = (ScriptKernelIDCreateFnPtr)dlsym(handle, "rsScriptKernelIDCreate");
+    if (RS::dispatch->ScriptKernelIDCreate == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptKernelIDCreate");
+        return false;
+    }
+    RS::dispatch->ScriptFieldIDCreate = (ScriptFieldIDCreateFnPtr)dlsym(handle, "rsScriptFieldIDCreate");
+    if (RS::dispatch->ScriptFieldIDCreate == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptFieldIDCreate");
+        return false;
+    }
+    RS::dispatch->ScriptGroupCreate = (ScriptGroupCreateFnPtr)dlsym(handle, "rsScriptGroupCreate");
+    if (RS::dispatch->ScriptGroupCreate == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptGroupCreate");
+        return false;
+    }
+    RS::dispatch->ScriptGroupSetOutput = (ScriptGroupSetOutputFnPtr)dlsym(handle, "rsScriptGroupSetOutput");
+    if (RS::dispatch->ScriptGroupSetOutput == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptGroupSetOutput");
+        return false;
+    }
+    RS::dispatch->ScriptGroupSetInput = (ScriptGroupSetInputFnPtr)dlsym(handle, "rsScriptGroupSetInput");
+    if (RS::dispatch->ScriptGroupSetInput == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptGroupSetInput");
+        return false;
+    }
+    RS::dispatch->ScriptGroupExecute = (ScriptGroupExecuteFnPtr)dlsym(handle, "rsScriptGroupExecute");
+    if (RS::dispatch->ScriptGroupExecute == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->ScriptGroupExecute");
+        return false;
+    }
+    RS::dispatch->AllocationIoSend = (AllocationIoSendFnPtr)dlsym(handle, "rsAllocationIoSend");
+    if (RS::dispatch->AllocationIoSend == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->AllocationIoSend");
+        return false;
+    }
+    RS::dispatch->AllocationIoReceive = (AllocationIoReceiveFnPtr)dlsym(handle, "rsAllocationIoReceive");
+    if (RS::dispatch->AllocationIoReceive == NULL) {
+        ALOGE("Couldn't initialize RS::dispatch->AllocationIoReceive");
+        return false;
+    }
+
+    return true;
+}
+
+static bool loadSO(const char* filename) {
+    void* handle = dlopen(filename, RTLD_LAZY | RTLD_LOCAL);
+    if (handle == NULL) {
+        ALOGE("couldn't dlopen %s, %s", filename, dlerror());
+        return false;
+    }
+
+    if (loadSymbols(handle) == false) {
+        ALOGE("%s init failed!", filename);
+        return false;
+    }
+    ALOGE("Successfully loaded %s", filename);
+    return true;
+}
+
+static uint32_t getProp(const char *str) {
+#if !defined(RS_SERVER) && defined(HAVE_ANDROID_OS)
+    char buf[256];
+    property_get(str, buf, "0");
+    return atoi(buf);
+#else
+    return 0;
+#endif
+}
+
+bool RS::initDispatch(int targetApi) {
+    pthread_mutex_lock(&gInitMutex);
+    if (gInitError) {
+        goto error;
+    } else if (gInitialized) {
+        pthread_mutex_unlock(&gInitMutex);
+        return true;
+    }
+
+    RS::dispatch = new dispatchTable;
+
+    // attempt to load libRS, load libRSSupport on failure
+    // if property is set, proceed directly to libRSSupport
+    if (getProp("debug.rs.forcecompat") == 0) {
+        usingNative = loadSO("libRS.so");
+    }
+    if (usingNative == false) {
+        if (loadSO("libRSSupport.so") == false) {
+            ALOGE("Failed to load libRS.so and libRSSupport.so");
+            goto error;
+        }
+    }
+
+    gInitialized = true;
+
+    pthread_mutex_unlock(&gInitMutex);
+    return true;
+
+ error:
+    gInitError = 1;
+    pthread_mutex_unlock(&gInitMutex);
+    return false;
+}
+
 bool RS::init(int targetApi, bool forceCpu, bool synchronous) {
-    mDev = rsDeviceCreate();
+    if (initDispatch(targetApi) == false) {
+        ALOGE("Couldn't initialize dispatch table");
+        return false;
+    }
+
+    mDev = RS::dispatch->DeviceCreate();
     if (mDev == 0) {
         ALOGE("Device creation failed");
         return false;
     }
 
-    mContext = rsContextCreate(mDev, 0, targetApi, RS_CONTEXT_TYPE_NORMAL, forceCpu, synchronous);
+    mContext = RS::dispatch->ContextCreate(mDev, 0, targetApi, RS_CONTEXT_TYPE_NORMAL, forceCpu, synchronous);
     if (mContext == 0) {
         ALOGE("Context creation failed");
         return false;
@@ -80,6 +491,8 @@
         usleep(1000);
     }
 
+    mInit = true;
+
     return true;
 }
 
@@ -95,16 +508,16 @@
     size_t rbuf_size = 256;
     void * rbuf = malloc(rbuf_size);
 
-    rsContextInitToClient(rs->mContext);
+    RS::dispatch->ContextInitToClient(rs->mContext);
     rs->mMessageRun = true;
 
     while (rs->mMessageRun) {
         size_t receiveLen = 0;
         uint32_t usrID = 0;
         uint32_t subID = 0;
-        RsMessageToClientType r = rsContextPeekMessage(rs->mContext,
-                                                       &receiveLen, sizeof(receiveLen),
-                                                       &usrID, sizeof(usrID));
+        RsMessageToClientType r = RS::dispatch->ContextPeekMessage(rs->mContext,
+                                                                   &receiveLen, sizeof(receiveLen),
+                                                                   &usrID, sizeof(usrID));
 
         if (receiveLen >= rbuf_size) {
             rbuf_size = receiveLen + 32;
@@ -114,7 +527,7 @@
             ALOGE("RS::message handler realloc error %zu", rbuf_size);
             // No clean way to recover now?
         }
-        rsContextGetMessage(rs->mContext, rbuf, rbuf_size, &receiveLen, sizeof(receiveLen),
+        RS::dispatch->ContextGetMessage(rs->mContext, rbuf, rbuf_size, &receiveLen, sizeof(receiveLen),
                             &subID, sizeof(subID));
 
         switch(r) {
@@ -163,5 +576,5 @@
 }
 
 void RS::finish() {
-    rsContextFinish(mContext);
+    RS::dispatch->ContextFinish(mContext);
 }
diff --git a/cpp/RenderScript.h b/cpp/RenderScript.h
index 9a7a565..8ed8d0e 100644
--- a/cpp/RenderScript.h
+++ b/cpp/RenderScript.h
@@ -17,7 +17,6 @@
 #ifndef ANDROID_RENDERSCRIPT_H
 #define ANDROID_RENDERSCRIPT_H
 
-#include "rsDefines.h"
 #include "rsCppStructs.h"
 
 #ifdef RS_SERVER
diff --git a/cpp/Sampler.cpp b/cpp/Sampler.cpp
new file mode 100644
index 0000000..a1f4669
--- /dev/null
+++ b/cpp/Sampler.cpp
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2008-2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "RenderScript.h"
+
+using namespace android;
+using namespace RSC;
+
+Sampler::Sampler(sp<RS> rs, void* id):
+    BaseObj(id, rs)
+{
+    RsSamplerValue mMin = RS_SAMPLER_INVALID;
+    RsSamplerValue mMag = RS_SAMPLER_INVALID;
+    RsSamplerValue mWrapS = RS_SAMPLER_INVALID;;
+    RsSamplerValue mWrapT = RS_SAMPLER_INVALID;;
+    float mAniso = 0.f;
+}
+
+RsSamplerValue Sampler::getMinification() {
+    return mMin;
+}
+
+RsSamplerValue Sampler::getMagnification() {
+    return mMag;
+}
+
+RsSamplerValue Sampler::getWrapS() {
+    return mWrapS;
+}
+
+RsSamplerValue Sampler::getWrapT() {
+    return mWrapT;
+}
+
+float Sampler::getAnisotropy() {
+    return mAniso;
+}
+
+sp<Sampler> Sampler::create(sp<RS> rs, RsSamplerValue min, RsSamplerValue mag, RsSamplerValue wrapS, RsSamplerValue wrapT, float anisotropy) {
+    // we aren't supporting wrapR in C++ API atm, so always pass wrap for that
+    void* id = RS::dispatch->SamplerCreate(rs.get(), min, mag, wrapS, wrapT, RS_SAMPLER_WRAP, anisotropy);
+    return new Sampler(rs, id);
+}
+
+#define CREATE_SAMPLER(N, MIN, MAG, WRAPS, WRAPT) sp<const Sampler> Sampler::N(sp<RS> rs) { \
+        if (rs->mSamplers.N == NULL) {                                  \
+            rs->mSamplers.N = (create(rs, MIN, MAG, WRAPS, WRAPT, 0.f)); \
+        }                                                               \
+        return rs->mSamplers.N;                                         \
+    }
+
+CREATE_SAMPLER(CLAMP_NEAREST, RS_SAMPLER_CLAMP, RS_SAMPLER_CLAMP, RS_SAMPLER_NEAREST, RS_SAMPLER_NEAREST);
+CREATE_SAMPLER(CLAMP_LINEAR, RS_SAMPLER_CLAMP, RS_SAMPLER_CLAMP, RS_SAMPLER_LINEAR, RS_SAMPLER_LINEAR);
+CREATE_SAMPLER(CLAMP_LINEAR_MIP_LINEAR, RS_SAMPLER_CLAMP, RS_SAMPLER_CLAMP, RS_SAMPLER_LINEAR_MIP_LINEAR, RS_SAMPLER_LINEAR_MIP_LINEAR);
+CREATE_SAMPLER(WRAP_NEAREST, RS_SAMPLER_WRAP, RS_SAMPLER_WRAP, RS_SAMPLER_NEAREST, RS_SAMPLER_NEAREST);
+CREATE_SAMPLER(WRAP_LINEAR, RS_SAMPLER_WRAP, RS_SAMPLER_WRAP, RS_SAMPLER_LINEAR, RS_SAMPLER_LINEAR);
+CREATE_SAMPLER(WRAP_LINEAR_MIP_LINEAR, RS_SAMPLER_WRAP, RS_SAMPLER_WRAP, RS_SAMPLER_LINEAR_MIP_LINEAR, RS_SAMPLER_LINEAR_MIP_LINEAR);
+CREATE_SAMPLER(MIRRORED_REPEAT_NEAREST, RS_SAMPLER_MIRRORED_REPEAT, RS_SAMPLER_MIRRORED_REPEAT, RS_SAMPLER_NEAREST, RS_SAMPLER_NEAREST);
+CREATE_SAMPLER(MIRRORED_REPEAT_LINEAR, RS_SAMPLER_MIRRORED_REPEAT, RS_SAMPLER_MIRRORED_REPEAT, RS_SAMPLER_LINEAR, RS_SAMPLER_LINEAR);
+CREATE_SAMPLER(MIRRORED_REPEAT_LINEAR_MIP_LINEAR, RS_SAMPLER_MIRRORED_REPEAT, RS_SAMPLER_MIRRORED_REPEAT, RS_SAMPLER_LINEAR_MIP_LINEAR, RS_SAMPLER_LINEAR_MIP_LINEAR);
diff --git a/cpp/Script.cpp b/cpp/Script.cpp
index 54a571a..9bc55ff 100644
--- a/cpp/Script.cpp
+++ b/cpp/Script.cpp
@@ -17,13 +17,12 @@
 #include <malloc.h>
 
 #include "RenderScript.h"
-#include <rs.h>
 
 using namespace android;
 using namespace RSC;
 
 void Script::invoke(uint32_t slot, const void *v, size_t len) const {
-    rsScriptInvokeV(mRS->getContext(), getID(), slot, v, len);
+    RS::dispatch->ScriptInvokeV(mRS->getContext(), getID(), slot, v, len);
 }
 
 void Script::forEach(uint32_t slot, sp<const Allocation> ain, sp<const Allocation> aout,
@@ -33,7 +32,7 @@
     }
     void *in_id = BaseObj::getObjID(ain);
     void *out_id = BaseObj::getObjID(aout);
-    rsScriptForEach(mRS->getContext(), getID(), slot, in_id, out_id, usr, usrLen, NULL, 0);
+    RS::dispatch->ScriptForEach(mRS->getContext(), getID(), slot, in_id, out_id, usr, usrLen, NULL, 0);
 }
 
 
@@ -42,16 +41,16 @@
 
 
 void Script::bindAllocation(sp<Allocation> va, uint32_t slot) const {
-    rsScriptBindAllocation(mRS->getContext(), getID(), BaseObj::getObjID(va), slot);
+    RS::dispatch->ScriptBindAllocation(mRS->getContext(), getID(), BaseObj::getObjID(va), slot);
 }
 
 
 void Script::setVar(uint32_t index, sp<const BaseObj> o) const {
-    rsScriptSetVarObj(mRS->getContext(), getID(), index, (o == NULL) ? 0 : o->getID());
+    RS::dispatch->ScriptSetVarObj(mRS->getContext(), getID(), index, (o == NULL) ? 0 : o->getID());
 }
 
 void Script::setVar(uint32_t index, const void *v, size_t len) const {
-    rsScriptSetVarV(mRS->getContext(), getID(), index, v, len);
+    RS::dispatch->ScriptSetVarV(mRS->getContext(), getID(), index, v, len);
 }
 
 void Script::FieldBase::init(sp<RS> rs, uint32_t dimx, uint32_t usages) {
diff --git a/cpp/ScriptC.cpp b/cpp/ScriptC.cpp
index f66e0ec..0d653bd 100644
--- a/cpp/ScriptC.cpp
+++ b/cpp/ScriptC.cpp
@@ -14,10 +14,7 @@
  * limitations under the License.
  */
 
-#include <malloc.h>
-
 #include "RenderScript.h"
-#include <rs.h>
 
 using namespace android;
 using namespace RSC;
@@ -27,7 +24,7 @@
                  const char *cachedName, size_t cachedNameLength,
                  const char *cacheDir, size_t cacheDirLength)
 : Script(NULL, rs) {
-    mID = rsScriptCCreate(rs->getContext(), cachedName, cachedNameLength,
-                          cacheDir, cacheDirLength, (const char *)codeTxt, codeLength);
+    mID = RS::dispatch->ScriptCCreate(rs->getContext(), cachedName, cachedNameLength,
+                                      cacheDir, cacheDirLength, (const char *)codeTxt, codeLength);
 }
 
diff --git a/cpp/ScriptIntrinsics.cpp b/cpp/ScriptIntrinsics.cpp
index 44e8760..76328c4 100644
--- a/cpp/ScriptIntrinsics.cpp
+++ b/cpp/ScriptIntrinsics.cpp
@@ -17,20 +17,28 @@
 #include <malloc.h>
 
 #include "RenderScript.h"
-#include <rs.h>
-#include "rsDefines.h"
 
 using namespace android;
 using namespace RSC;
 
 ScriptIntrinsic::ScriptIntrinsic(sp<RS> rs, int id, sp<const Element> e)
     : Script(NULL, rs) {
-    mID = rsScriptIntrinsicCreate(rs->getContext(), id, e->getID());
+    mID = RS::dispatch->ScriptIntrinsicCreate(rs->getContext(), id, e->getID());
+}
+
+ScriptIntrinsic3DLUT::ScriptIntrinsic3DLUT(sp<RS> rs, sp<const Element> e)
+    : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_3DLUT, e) {
+
+}
+void ScriptIntrinsic3DLUT::forEach(sp<Allocation> ain, sp<Allocation> aout) {
+    Script::forEach(0, ain, aout, NULL, 0);
+}
+void ScriptIntrinsic3DLUT::setLUT(sp<Allocation> lut) {
+    Script::setVar(0, lut);
 }
 
 ScriptIntrinsicBlend::ScriptIntrinsicBlend(sp<RS> rs, sp<const Element> e)
     : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_BLEND, e) {
-
 }
 
 void ScriptIntrinsicBlend::blendClear(sp<Allocation> in, sp<Allocation> out) {
@@ -95,6 +103,9 @@
     Script::forEach(35, in, out, NULL, 0);
 }
 
+
+
+
 ScriptIntrinsicBlur::ScriptIntrinsicBlur(sp<RS> rs, sp<const Element> e)
     : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_BLUR, e) {
 
@@ -108,3 +119,90 @@
 void ScriptIntrinsicBlur::setRadius(float radius) {
     Script::setVar(0, &radius, sizeof(float));
 }
+
+
+
+ScriptIntrinsicColorMatrix::ScriptIntrinsicColorMatrix(sp<RS> rs, sp<const Element> e)
+    : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_COLOR_MATRIX, e) {
+
+}
+
+void ScriptIntrinsicColorMatrix::forEach(sp<Allocation> in, sp<Allocation> out) {
+    Script::forEach(0, in, out, NULL, 0);
+}
+
+
+void ScriptIntrinsicColorMatrix::setColorMatrix3(float* m) {
+    Script::setVar(0, (void*)m, sizeof(float) * 9);
+}
+
+
+void ScriptIntrinsicColorMatrix::setColorMatrix4(float* m) {
+    Script::setVar(0, (void*)m, sizeof(float) * 16);
+}
+
+
+void ScriptIntrinsicColorMatrix::setGreyscale() {
+    float matrix[] = {0.299f, 0.587f,  0.114f, 0.299f, 0.587f, 0.114f, 0.299f, 0.587f, 0.114f};
+    setColorMatrix3(matrix);
+}
+
+
+void ScriptIntrinsicColorMatrix::setRGBtoYUV() {
+    float matrix[] = {0.299f,0.587f,0.114f,-0.14713f,-0.28886f,0.436f,0.615f,-0.51499f,-0.10001f};
+    setColorMatrix3(matrix);
+}
+
+
+void ScriptIntrinsicColorMatrix::setYUVtoRGB() {
+    float matrix[] = {1.f,0.f,1.13983f,1.f,-0.39465f,-0.5806f,1.f,2.03211f,0.f};
+    setColorMatrix3(matrix);
+}
+
+ScriptIntrinsicConvolve3x3::ScriptIntrinsicConvolve3x3(sp<RS> rs, sp<const Element> e)
+    : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_CONVOLVE_3x3, e) {
+
+}
+
+void ScriptIntrinsicConvolve3x3::setInput(sp<Allocation> in) {
+    Script::setVar(1, in);
+}
+
+void ScriptIntrinsicConvolve3x3::forEach(sp<Allocation> out) {
+    Script::forEach(0, NULL, out, NULL, 0);
+}
+
+void ScriptIntrinsicConvolve3x3::setCoefficients(float* v) {
+    Script::setVar(0, (void*)v, sizeof(float) * 9);
+}
+
+ScriptIntrinsicConvolve5x5::ScriptIntrinsicConvolve5x5(sp<RS> rs, sp<const Element> e)
+    : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_CONVOLVE_5x5, e) {
+
+}
+
+void ScriptIntrinsicConvolve5x5::setInput(sp<Allocation> in) {
+    Script::setVar(1, in);
+}
+
+void ScriptIntrinsicConvolve5x5::forEach(sp<Allocation> out) {
+    Script::forEach(0, NULL, out, NULL, 0);
+}
+
+void ScriptIntrinsicConvolve5x5::setCoefficients(float* v) {
+    Script::setVar(0, (void*)v, sizeof(float) * 25);
+}
+
+/*ScriptIntrinsicLUT::ScriptIntrinsicLUT(sp<RS> rs, sp<const Element> e)
+    : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_LUT, e) {
+
+}
+
+void ScriptIntrinsicLUT::forEach(sp<Allocation> ain, sp<Allocation> aout) {
+
+}
+
+void ScriptIntrinsicLUT::setLUT(sp<Allocation> lut) {
+
+}*/
+
diff --git a/cpp/Type.cpp b/cpp/Type.cpp
index 312020a..192dc68 100644
--- a/cpp/Type.cpp
+++ b/cpp/Type.cpp
@@ -17,7 +17,6 @@
 #include <malloc.h>
 #include <string.h>
 
-#include <rs.h>
 #include "RenderScript.h"
 
 using namespace android;
@@ -93,7 +92,7 @@
 }
 
 sp<const Type> Type::create(sp<RS> rs, sp<const Element> e, uint32_t dimX, uint32_t dimY, uint32_t dimZ) {
-    void * id = rsTypeCreate(rs->getContext(), e->getID(), dimX, dimY, dimZ, false, false, 0);
+    void * id = RS::dispatch->TypeCreate(rs->getContext(), e->getID(), dimX, dimY, dimZ, false, false, 0);
     Type *t = new Type(id, rs);
 
     t->mElement = e;
@@ -160,8 +159,8 @@
         }
     }
 
-    void * id = rsTypeCreate(mRS->getContext(), mElement->getID(), mDimX, mDimY, mDimZ,
-            mDimMipmaps, mDimFaces, 0);
+    void * id = RS::dispatch->TypeCreate(mRS->getContext(), mElement->getID(), mDimX, mDimY, mDimZ,
+                                         mDimMipmaps, mDimFaces, 0);
     Type *t = new Type(id, mRS);
     t->mElement = mElement;
     t->mDimX = mDimX;
diff --git a/cpp/rsCppStructs.h b/cpp/rsCppStructs.h
index be011aa..b85499c 100644
--- a/cpp/rsCppStructs.h
+++ b/cpp/rsCppStructs.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012 The Android Open Source Project
+ * Copyright (C) 2013 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,12 +17,12 @@
 #ifndef ANDROID_RSCPPSTRUCTS_H
 #define ANDROID_RSCPPSTRUCTS_H
 
+#include "rsDefines.h"
 #include "rsCppUtils.h"
-#ifndef RS_SERVER
-#include "utils/RefBase.h"
-#else
-#include "RefBase.h"
-#endif
+#include "util/RefBase.h"
+#include "rsDispatch.h"
+
+#include <vector>
 
 // Every row in an RS allocation is guaranteed to be aligned by this amount
 // Every row in a user-backed allocation must be aligned by this amount
@@ -41,8 +41,9 @@
 class Allocation;
 class Script;
 class ScriptC;
+class Sampler;
 
-class RS : public android::LightRefBase<RS> {
+ class RS : public android::RSC::LightRefBase<RS> {
 
  public:
     RS();
@@ -62,7 +63,12 @@
 
     void finish();
 
+    static dispatchTable* dispatch;
+
  private:
+    static bool usingNative;
+    static bool initDispatch(int targetApi);
+
     bool init(int targetApi, bool forceCpu, bool synchronous);
     static void * threadProc(void *);
 
@@ -78,86 +84,106 @@
 
     ErrorHandlerFunc_t mErrorFunc;
     MessageHandlerFunc_t mMessageFunc;
+    bool mInit;
 
     struct {
-        Element *U8;
-        Element *I8;
-        Element *U16;
-        Element *I16;
-        Element *U32;
-        Element *I32;
-        Element *U64;
-        Element *I64;
-        Element *F32;
-        Element *F64;
-        Element *BOOLEAN;
+        sp<const Element> U8;
+        sp<const Element> I8;
+        sp<const Element> U16;
+        sp<const Element> I16;
+        sp<const Element> U32;
+        sp<const Element> I32;
+        sp<const Element> U64;
+        sp<const Element> I64;
+        sp<const Element> F32;
+        sp<const Element> F64;
+        sp<const Element> BOOLEAN;
 
-        Element *ELEMENT;
-        Element *TYPE;
-        Element *ALLOCATION;
-        Element *SAMPLER;
-        Element *SCRIPT;
-        Element *MESH;
-        Element *PROGRAM_FRAGMENT;
-        Element *PROGRAM_VERTEX;
-        Element *PROGRAM_RASTER;
-        Element *PROGRAM_STORE;
+        sp<const Element> ELEMENT;
+        sp<const Element> TYPE;
+        sp<const Element> ALLOCATION;
+        sp<const Element> SAMPLER;
+        sp<const Element> SCRIPT;
+        sp<const Element> MESH;
+        sp<const Element> PROGRAM_FRAGMENT;
+        sp<const Element> PROGRAM_VERTEX;
+        sp<const Element> PROGRAM_RASTER;
+        sp<const Element> PROGRAM_STORE;
 
-        Element *A_8;
-        Element *RGB_565;
-        Element *RGB_888;
-        Element *RGBA_5551;
-        Element *RGBA_4444;
-        Element *RGBA_8888;
+        sp<const Element> A_8;
+        sp<const Element> RGB_565;
+        sp<const Element> RGB_888;
+        sp<const Element> RGBA_5551;
+        sp<const Element> RGBA_4444;
+        sp<const Element> RGBA_8888;
 
-        Element *FLOAT_2;
-        Element *FLOAT_3;
-        Element *FLOAT_4;
+        sp<const Element> FLOAT_2;
+        sp<const Element> FLOAT_3;
+        sp<const Element> FLOAT_4;
 
-        Element *DOUBLE_2;
-        Element *DOUBLE_3;
-        Element *DOUBLE_4;
+        sp<const Element> DOUBLE_2;
+        sp<const Element> DOUBLE_3;
+        sp<const Element> DOUBLE_4;
 
-        Element *UCHAR_2;
-        Element *UCHAR_3;
-        Element *UCHAR_4;
+        sp<const Element> UCHAR_2;
+        sp<const Element> UCHAR_3;
+        sp<const Element> UCHAR_4;
 
-        Element *CHAR_2;
-        Element *CHAR_3;
-        Element *CHAR_4;
+        sp<const Element> CHAR_2;
+        sp<const Element> CHAR_3;
+        sp<const Element> CHAR_4;
 
-        Element *USHORT_2;
-        Element *USHORT_3;
-        Element *USHORT_4;
+        sp<const Element> USHORT_2;
+        sp<const Element> USHORT_3;
+        sp<const Element> USHORT_4;
 
-        Element *SHORT_2;
-        Element *SHORT_3;
-        Element *SHORT_4;
+        sp<const Element> SHORT_2;
+        sp<const Element> SHORT_3;
+        sp<const Element> SHORT_4;
 
-        Element *UINT_2;
-        Element *UINT_3;
-        Element *UINT_4;
+        sp<const Element> UINT_2;
+        sp<const Element> UINT_3;
+        sp<const Element> UINT_4;
 
-        Element *INT_2;
-        Element *INT_3;
-        Element *INT_4;
+        sp<const Element> INT_2;
+        sp<const Element> INT_3;
+        sp<const Element> INT_4;
 
-        Element *ULONG_2;
-        Element *ULONG_3;
-        Element *ULONG_4;
+        sp<const Element> ULONG_2;
+        sp<const Element> ULONG_3;
+        sp<const Element> ULONG_4;
 
-        Element *LONG_2;
-        Element *LONG_3;
-        Element *LONG_4;
+        sp<const Element> LONG_2;
+        sp<const Element> LONG_3;
+        sp<const Element> LONG_4;
 
-        Element *MATRIX_4X4;
-        Element *MATRIX_3X3;
-        Element *MATRIX_2X2;
+        sp<const Element> MATRIX_4X4;
+        sp<const Element> MATRIX_3X3;
+        sp<const Element> MATRIX_2X2;
     } mElements;
 
+    struct {
+        sp<const Sampler> CLAMP_NEAREST;
+        sp<const Sampler> CLAMP_LINEAR;
+        sp<const Sampler> CLAMP_LINEAR_MIP_LINEAR;
+        sp<const Sampler> WRAP_NEAREST;
+        sp<const Sampler> WRAP_LINEAR;
+        sp<const Sampler> WRAP_LINEAR_MIP_LINEAR;
+        sp<const Sampler> MIRRORED_REPEAT_NEAREST;
+        sp<const Sampler> MIRRORED_REPEAT_LINEAR;
+        sp<const Sampler> MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
+    } mSamplers;
+    friend class Sampler;
+    friend class Element;
 };
 
-class BaseObj : public android::LightRefBase<BaseObj> {
+class BaseObj : public android::RSC::LightRefBase<BaseObj> {
+public:
+    void * getID() const;
+    virtual ~BaseObj();
+    virtual void updateFromNative();
+    virtual bool equals(sp<const BaseObj> obj);
+
 protected:
     void *mID;
     sp<RS> mRS;
@@ -168,20 +194,14 @@
 
     static void * getObjID(sp<const BaseObj> o);
 
-public:
-
-    void * getID() const;
-    virtual ~BaseObj();
-    virtual void updateFromNative();
-    virtual bool equals(const BaseObj *obj);
 };
 
 
 class Allocation : public BaseObj {
 protected:
-    android::sp<const Type> mType;
+    sp<const Type> mType;
     uint32_t mUsage;
-    android::sp<Allocation> mAdaptedAllocation;
+    sp<Allocation> mAdaptedAllocation;
 
     bool mConstrainedLOD;
     bool mConstrainedFace;
@@ -215,7 +235,7 @@
     void validate2DRange(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h);
 
 public:
-    android::sp<const Type> getType() {
+    sp<const Type> getType() {
         return mType;
     }
 
@@ -355,43 +375,44 @@
     static sp<const Element> MATRIX_3X3(sp<RS> rs);
     static sp<const Element> MATRIX_2X2(sp<RS> rs);
 
-    Element(void *id, sp<RS> rs,
-            android::Vector<sp<Element> > &elements,
-            android::Vector<android::String8> &elementNames,
-            android::Vector<uint32_t> &arraySizes);
-    Element(void *id, sp<RS> rs, RsDataType dt, RsDataKind dk, bool norm, uint32_t size);
-    Element(sp<RS> rs);
-    virtual ~Element();
-
     void updateFromNative();
     static sp<const Element> createUser(sp<RS> rs, RsDataType dt);
     static sp<const Element> createVector(sp<RS> rs, RsDataType dt, uint32_t size);
     static sp<const Element> createPixel(sp<RS> rs, RsDataType dt, RsDataKind dk);
     bool isCompatible(sp<const Element>e);
 
+    Element(void *id, sp<RS> rs,
+            std::vector<sp<Element> > &elements,
+            std::vector<android::String8> &elementNames,
+            std::vector<uint32_t> &arraySizes);
+    Element(void *id, sp<RS> rs, RsDataType dt, RsDataKind dk, bool norm, uint32_t size);
+    Element(sp<RS> rs);
+    virtual ~Element();
+
+ protected:
     class Builder {
     private:
         sp<RS> mRS;
-        android::Vector<sp<Element> > mElements;
-        android::Vector<android::String8> mElementNames;
-        android::Vector<uint32_t> mArraySizes;
+        std::vector<sp<Element> > mElements;
+        std::vector<android::String8> mElementNames;
+        std::vector<uint32_t> mArraySizes;
         bool mSkipPadding;
 
     public:
         Builder(sp<RS> rs);
         ~Builder();
-        void add(sp<Element>, android::String8 &name, uint32_t arraySize = 1);
+        void add(sp<Element> e, android::String8 &name, uint32_t arraySize = 1);
         sp<const Element> create();
     };
 
 private:
     void updateVisibleSubElements();
 
-    android::Vector<sp</*const*/ Element> > mElements;
-    android::Vector<android::String8> mElementNames;
-    android::Vector<uint32_t> mArraySizes;
-    android::Vector<uint32_t> mVisibleElementMap;
-    android::Vector<uint32_t> mOffsetInBytes;
+    std::vector<sp<Element> > mElements;
+    std::vector<android::String8> mElementNames;
+    std::vector<uint32_t> mArraySizes;
+    std::vector<uint32_t> mVisibleElementMap;
+    std::vector<uint32_t> mOffsetInBytes;
 
     RsDataType mType;
     RsDataKind mKind;
@@ -408,10 +429,9 @@
 
 public:
     FieldPacker(size_t len)
-        : mPos(0),
-          mLen(len) {
-        mData = new unsigned char[len];
-    }
+        : mPos(0), mLen(len) {
+            mData = new unsigned char[len];
+        }
 
     virtual ~FieldPacker() {
         delete [] mData;
@@ -458,7 +478,7 @@
     }
 
     template <typename T>
-    void add(T t) {
+        void add(T t) {
         align(sizeof(t));
         if (mPos + sizeof(t) <= mLen) {
             memcpy(&mData[mPos], &t, sizeof(t));
@@ -467,26 +487,26 @@
     }
 
     /*
-    void add(rs_matrix4x4 m) {
-        for (size_t i = 0; i < 16; i++) {
-            add(m.m[i]);
-        }
-    }
+      void add(rs_matrix4x4 m) {
+      for (size_t i = 0; i < 16; i++) {
+      add(m.m[i]);
+      }
+      }
 
-    void add(rs_matrix3x3 m) {
-        for (size_t i = 0; i < 9; i++) {
-            add(m.m[i]);
-        }
-    }
+      void add(rs_matrix3x3 m) {
+      for (size_t i = 0; i < 9; i++) {
+      add(m.m[i]);
+      }
+      }
 
-    void add(rs_matrix2x2 m) {
-        for (size_t i = 0; i < 4; i++) {
-            add(m.m[i]);
-        }
-    }
+      void add(rs_matrix2x2 m) {
+      for (size_t i = 0; i < 4; i++) {
+      add(m.m[i]);
+      }
+      }
     */
 
-    void add(BaseObj* obj) {
+    void add(sp<BaseObj> obj) {
         if (obj != NULL) {
             add((uint32_t) (uintptr_t) obj->getID());
         } else {
@@ -495,6 +515,7 @@
     }
 };
 
+
 class Type : public BaseObj {
 protected:
     friend class Allocation;
@@ -641,9 +662,16 @@
     ScriptIntrinsic(sp<RS> rs, int id, sp<const Element> e);
 };
 
+class ScriptIntrinsic3DLUT : public ScriptIntrinsic {
+ public:
+    ScriptIntrinsic3DLUT(sp<RS> rs, sp<const Element> e);
+    void forEach(sp<Allocation> ain, sp<Allocation> aout);
+    void setLUT(sp<Allocation> lut);
+};
+
 class ScriptIntrinsicBlend : public ScriptIntrinsic {
  public:
-    ScriptIntrinsicBlend(sp<RS> rs, sp <const Element> e);
+    ScriptIntrinsicBlend(sp<RS> rs, sp<const Element> e);
     void blendClear(sp<Allocation> in, sp<Allocation> out);
     void blendSrc(sp<Allocation> in, sp<Allocation> out);
     void blendDst(sp<Allocation> in, sp<Allocation> out);
@@ -663,11 +691,81 @@
 
 class ScriptIntrinsicBlur : public ScriptIntrinsic {
  public:
-    ScriptIntrinsicBlur(sp<RS> rs, sp <const Element> e);
+    ScriptIntrinsicBlur(sp<RS> rs, sp<const Element> e);
     void blur(sp<Allocation> in, sp<Allocation> out);
     void setRadius(float radius);
 };
 
+class ScriptIntrinsicColorMatrix : public ScriptIntrinsic {
+ public:
+    ScriptIntrinsicColorMatrix(sp<RS> rs, sp<const Element> e);
+    void forEach(sp<Allocation> in, sp<Allocation> out);
+    void setColorMatrix3(float* m);
+    void setColorMatrix4(float* m);
+    void setGreyscale();
+    void setRGBtoYUV();
+    void setYUVtoRGB();
+};
+
+class ScriptIntrinsicConvolve3x3 : public ScriptIntrinsic {
+ public:
+    ScriptIntrinsicConvolve3x3(sp<RS> rs, sp<const Element> e);
+    void setInput(sp<Allocation> in);
+    void forEach(sp<Allocation> out);
+    void setCoefficients(float* v);
+};
+
+class ScriptIntrinsicConvolve5x5 : public ScriptIntrinsic {
+ public:
+    ScriptIntrinsicConvolve5x5(sp<RS> rs, sp<const Element> e);
+    void setInput(sp<Allocation> in);
+    void forEach(sp<Allocation> out);
+    void setCoefficients(float* v);
+};
+
+/*class ScriptIntrinsicLUT : public ScriptIntrinsic {
+ public:
+    ScriptIntrinsicLUT(sp<RS> rs, sp<const Element> e);
+    void forEach(sp<Allocation> ain, sp<Allocation> aout);
+    void setLUT(sp<Allocation> lut);
+    };*/
+/*
+class ScriptIntrinsicYuvToRGB : public ScriptIntrinsic {
+
+};
+*/
+
+ class Sampler : public BaseObj {
+ private:
+    Sampler(sp<RS> rs, void* id);
+    RsSamplerValue mMin;
+    RsSamplerValue mMag;
+    RsSamplerValue mWrapS;
+    RsSamplerValue mWrapT;
+    RsSamplerValue mWrapR;
+    float mAniso;
+
+ public:
+    static sp<Sampler> create(sp<RS> rs, RsSamplerValue min, RsSamplerValue mag, RsSamplerValue wrapS, RsSamplerValue wrapT, float anisotropy);
+
+    RsSamplerValue getMinification();
+    RsSamplerValue getMagnification();
+    RsSamplerValue getWrapS();
+    RsSamplerValue getWrapT();
+    float getAnisotropy();
+
+    sp<const Sampler> CLAMP_NEAREST(sp<RS> rs);
+    sp<const Sampler> CLAMP_LINEAR(sp<RS> rs);
+    sp<const Sampler> CLAMP_LINEAR_MIP_LINEAR(sp<RS> rs);
+    sp<const Sampler> WRAP_NEAREST(sp<RS> rs);
+    sp<const Sampler> WRAP_LINEAR(sp<RS> rs);
+    sp<const Sampler> WRAP_LINEAR_MIP_LINEAR(sp<RS> rs);
+    sp<const Sampler> MIRRORED_REPEAT_NEAREST(sp<RS> rs);
+    sp<const Sampler> MIRRORED_REPEAT_LINEAR(sp<RS> rs);
+    sp<const Sampler> MIRRORED_REPEAT_LINEAR_MIP_LINEAR(sp<RS> rs);
+
+};
+
 }
 
 }
diff --git a/cpp/rsDispatch.h b/cpp/rsDispatch.h
new file mode 100644
index 0000000..139ef52
--- /dev/null
+++ b/cpp/rsDispatch.h
@@ -0,0 +1,161 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_RSDISPATCH_H
+#define ANDROID_RSDISPATCH_H
+
+#include "rsDefines.h"
+
+typedef const void* (*AllocationGetTypeFnPtr)(RsContext con, RsAllocation va);
+typedef void (*TypeGetNativeDataFnPtr)(RsContext, RsType, uint32_t *typeData, uint32_t typeDataSize);
+typedef void (*ElementGetNativeDataFnPtr)(RsContext, RsElement, uint32_t *elemData, uint32_t elemDataSize);
+typedef void (*ElementGetSubElementsFnPtr)(RsContext, RsElement, uint32_t *ids, const char **names, uint32_t *arraySizes, uint32_t dataSize);
+typedef RsDevice (*DeviceCreateFnPtr) ();
+typedef void (*DeviceDestroyFnPtr) (RsDevice dev);
+typedef void (*DeviceSetConfigFnPtr) (RsDevice dev, RsDeviceParam p, int32_t value);
+typedef RsContext (*ContextCreateFnPtr)(RsDevice vdev, uint32_t version, uint32_t sdkVersion, RsContextType ct, bool forceCpu, bool synchronous);
+typedef void (*GetNameFnPtr)(RsContext, void * obj, const char **name);
+
+typedef void (*ContextDestroyFnPtr) (RsContext);
+typedef RsMessageToClientType (*ContextGetMessageFnPtr) (RsContext, void*, size_t, size_t*, size_t, uint32_t*, size_t);
+typedef RsMessageToClientType (*ContextPeekMessageFnPtr) (RsContext, size_t*, size_t, uint32_t*, size_t);
+typedef void (*ContextSendMessageFnPtr) (RsContext, uint32_t, const uint8_t*, size_t);
+typedef void (*ContextInitToClientFnPtr) (RsContext);
+typedef void (*ContextDeinitToClientFnPtr) (RsContext);
+typedef RsType (*TypeCreateFnPtr) (RsContext, RsElement, uint32_t, uint32_t, uint32_t, bool, bool, uint32_t);
+typedef RsAllocation (*AllocationCreateTypedFnPtr) (RsContext, RsType, RsAllocationMipmapControl, uint32_t, uintptr_t);
+typedef RsAllocation (*AllocationCreateFromBitmapFnPtr) (RsContext, RsType, RsAllocationMipmapControl, const void*, size_t, uint32_t);
+typedef RsAllocation (*AllocationCubeCreateFromBitmapFnPtr) (RsContext, RsType, RsAllocationMipmapControl, const void*, size_t, uint32_t);
+typedef RsNativeWindow (*AllocationGetSurfaceFnPtr) (RsContext, RsAllocation);
+typedef void (*AllocationSetSurfaceFnPtr) (RsContext, RsAllocation, RsNativeWindow);
+typedef void (*ContextFinishFnPtr) (RsContext);
+typedef void (*ContextDumpFnPtr) (RsContext, int32_t);
+typedef void (*ContextSetPriorityFnPtr) (RsContext, int32_t);
+typedef void (*AssignNameFnPtr) (RsContext, RsObjectBase, const char*, size_t);
+typedef void (*ObjDestroyFnPtr) (RsContext, RsAsyncVoidPtr);
+typedef RsElement (*ElementCreateFnPtr) (RsContext, RsDataType, RsDataKind, bool, uint32_t);
+typedef RsElement (*ElementCreate2FnPtr) (RsContext, const RsElement*, size_t, const char**, size_t, const size_t*, const uint32_t*, size_t);
+typedef void (*AllocationCopyToBitmapFnPtr) (RsContext, RsAllocation, void*, size_t);
+typedef void (*Allocation1DDataFnPtr) (RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, const void*, size_t);
+typedef void (*Allocation1DElementDataFnPtr) (RsContext, RsAllocation, uint32_t, uint32_t, const void*, size_t, size_t);
+typedef void (*Allocation2DDataFnPtr) (RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, RsAllocationCubemapFace, uint32_t, uint32_t, const void*, size_t, size_t);
+typedef void (*Allocation3DDataFnPtr) (RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, const void*, size_t, size_t);
+typedef void (*AllocationGenerateMipmapsFnPtr) (RsContext, RsAllocation);
+typedef void (*AllocationReadFnPtr) (RsContext, RsAllocation, void*, size_t);
+typedef void (*Allocation1DReadFnPtr) (RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, void*, size_t);
+typedef void (*Allocation2DReadFnPtr) (RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, RsAllocationCubemapFace, uint32_t, uint32_t, void*, size_t, size_t);
+typedef void (*AllocationSyncAllFnPtr) (RsContext, RsAllocation, RsAllocationUsageType);
+typedef void (*AllocationResize1DFnPtr) (RsContext, RsAllocation, uint32_t);
+typedef void (*AllocationCopy2DRangeFnPtr) (RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, RsAllocation, uint32_t, uint32_t, uint32_t, uint32_t);
+typedef void (*AllocationCopy3DRangeFnPtr) (RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, RsAllocation, uint32_t, uint32_t, uint32_t, uint32_t);
+typedef RsSampler (*SamplerCreateFnPtr) (RsContext, RsSamplerValue, RsSamplerValue, RsSamplerValue, RsSamplerValue, RsSamplerValue, float);
+typedef void (*ScriptBindAllocationFnPtr) (RsContext, RsScript, RsAllocation, uint32_t);
+typedef void (*ScriptSetTimeZoneFnPtr) (RsContext, RsScript, const char*, size_t);
+typedef void (*ScriptInvokeFnPtr) (RsContext, RsScript, uint32_t);
+typedef void (*ScriptInvokeVFnPtr) (RsContext, RsScript, uint32_t, const void*, size_t);
+typedef void (*ScriptForEachFnPtr) (RsContext, RsScript, uint32_t, RsAllocation, RsAllocation, const void*, size_t, const RsScriptCall*, size_t);
+typedef void (*ScriptSetVarIFnPtr) (RsContext, RsScript, uint32_t, int);
+typedef void (*ScriptSetVarObjFnPtr) (RsContext, RsScript, uint32_t, RsObjectBase);
+typedef void (*ScriptSetVarJFnPtr) (RsContext, RsScript, uint32_t, int64_t);
+typedef void (*ScriptSetVarFFnPtr) (RsContext, RsScript, uint32_t, float);
+typedef void (*ScriptSetVarDFnPtr) (RsContext, RsScript, uint32_t, double);
+typedef void (*ScriptSetVarVFnPtr) (RsContext, RsScript, uint32_t, const void*, size_t);
+typedef void (*ScriptGetVarVFnPtr) (RsContext, RsScript, uint32_t, void*, size_t);
+typedef void (*ScriptSetVarVEFnPtr) (RsContext, RsScript, uint32_t, const void*, size_t, RsElement, const size_t*, size_t);
+typedef RsScript (*ScriptCCreateFnPtr) (RsContext, const char*, size_t, const char*, size_t, const char*, size_t);
+typedef RsScript (*ScriptIntrinsicCreateFnPtr) (RsContext, uint32_t id, RsElement);
+typedef RsScriptKernelID (*ScriptKernelIDCreateFnPtr) (RsContext, RsScript, int, int);
+typedef RsScriptFieldID (*ScriptFieldIDCreateFnPtr) (RsContext, RsScript, int);
+typedef RsScriptGroup (*ScriptGroupCreateFnPtr) (RsContext, RsScriptKernelID*, size_t, RsScriptKernelID*, size_t, RsScriptKernelID*, size_t, RsScriptFieldID*, size_t, const RsType*, size_t);
+typedef void (*ScriptGroupSetOutputFnPtr) (RsContext, RsScriptGroup, RsScriptKernelID, RsAllocation);
+typedef void (*ScriptGroupSetInputFnPtr) (RsContext, RsScriptGroup, RsScriptKernelID, RsAllocation);
+typedef void (*ScriptGroupExecuteFnPtr) (RsContext, RsScriptGroup);
+typedef void (*AllocationIoSendFnPtr) (RsContext, RsAllocation);
+typedef void (*AllocationIoReceiveFnPtr) (RsContext, RsAllocation);
+
+typedef struct {
+    // inserted by hand from rs.h
+    AllocationGetTypeFnPtr AllocationGetType;
+    TypeGetNativeDataFnPtr TypeGetNativeData;
+    ElementGetNativeDataFnPtr ElementGetNativeData;
+    ElementGetSubElementsFnPtr ElementGetSubElements;
+
+    DeviceCreateFnPtr DeviceCreate;
+    DeviceDestroyFnPtr DeviceDestroy;
+    DeviceSetConfigFnPtr DeviceSetConfig;
+    ContextCreateFnPtr ContextCreate;
+    GetNameFnPtr GetName;
+
+    // generated from rs.spec
+    ContextDestroyFnPtr ContextDestroy;
+    ContextGetMessageFnPtr ContextGetMessage;
+    ContextPeekMessageFnPtr ContextPeekMessage;
+    ContextSendMessageFnPtr ContextSendMessage;
+    ContextInitToClientFnPtr ContextInitToClient;
+    ContextDeinitToClientFnPtr ContextDeinitToClient;
+    TypeCreateFnPtr TypeCreate;
+    AllocationCreateTypedFnPtr AllocationCreateTyped;
+    AllocationCreateFromBitmapFnPtr AllocationCreateFromBitmap;
+    AllocationCubeCreateFromBitmapFnPtr AllocationCubeCreateFromBitmap;
+    AllocationGetSurfaceFnPtr AllocationGetSurface;
+    AllocationSetSurfaceFnPtr AllocationSetSurface;
+    ContextFinishFnPtr ContextFinish;
+    ContextDumpFnPtr ContextDump;
+    ContextSetPriorityFnPtr ContextSetPriority;
+    AssignNameFnPtr AssignName;
+    ObjDestroyFnPtr ObjDestroy;
+    ElementCreateFnPtr ElementCreate;
+    ElementCreate2FnPtr ElementCreate2;
+    AllocationCopyToBitmapFnPtr AllocationCopyToBitmap;
+    Allocation1DDataFnPtr Allocation1DData;
+    Allocation1DElementDataFnPtr Allocation1DElementData;
+    Allocation2DDataFnPtr Allocation2DData;
+    Allocation3DDataFnPtr Allocation3DData;
+    AllocationGenerateMipmapsFnPtr AllocationGenerateMipmaps;
+    AllocationReadFnPtr AllocationRead;
+    Allocation1DReadFnPtr Allocation1DRead;
+    Allocation2DReadFnPtr Allocation2DRead;
+    AllocationSyncAllFnPtr AllocationSyncAll;
+    AllocationResize1DFnPtr AllocationResize1D;
+    AllocationCopy2DRangeFnPtr AllocationCopy2DRange;
+    AllocationCopy3DRangeFnPtr AllocationCopy3DRange;
+    SamplerCreateFnPtr SamplerCreate;
+    ScriptBindAllocationFnPtr ScriptBindAllocation;
+    ScriptSetTimeZoneFnPtr ScriptSetTimeZone;
+    ScriptInvokeFnPtr ScriptInvoke;
+    ScriptInvokeVFnPtr ScriptInvokeV;
+    ScriptForEachFnPtr ScriptForEach;
+    ScriptSetVarIFnPtr ScriptSetVarI;
+    ScriptSetVarObjFnPtr ScriptSetVarObj;
+    ScriptSetVarJFnPtr ScriptSetVarJ;
+    ScriptSetVarFFnPtr ScriptSetVarF;
+    ScriptSetVarDFnPtr ScriptSetVarD;
+    ScriptSetVarVFnPtr ScriptSetVarV;
+    ScriptGetVarVFnPtr ScriptGetVarV;
+    ScriptSetVarVEFnPtr ScriptSetVarVE;
+    ScriptCCreateFnPtr ScriptCCreate;
+    ScriptIntrinsicCreateFnPtr ScriptIntrinsicCreate;
+    ScriptKernelIDCreateFnPtr ScriptKernelIDCreate;
+    ScriptFieldIDCreateFnPtr ScriptFieldIDCreate;
+    ScriptGroupCreateFnPtr ScriptGroupCreate;
+    ScriptGroupSetOutputFnPtr ScriptGroupSetOutput;
+    ScriptGroupSetInputFnPtr ScriptGroupSetInput;
+    ScriptGroupExecuteFnPtr ScriptGroupExecute;
+    AllocationIoSendFnPtr AllocationIoSend;
+    AllocationIoReceiveFnPtr AllocationIoReceive;
+} dispatchTable;
+
+#endif
diff --git a/cpp/util/RefBase.h b/cpp/util/RefBase.h
new file mode 100644
index 0000000..eab26ab
--- /dev/null
+++ b/cpp/util/RefBase.h
@@ -0,0 +1,529 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef RS_REF_BASE_H
+#define RS_REF_BASE_H
+
+
+#include <stdint.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "StrongPointer.h"
+#include "TypeHelpers.h"
+
+// ---------------------------------------------------------------------------
+namespace android{
+namespace RSC {
+
+class TextOutput;
+TextOutput& printWeakPointer(TextOutput& to, const void* val);
+
+// ---------------------------------------------------------------------------
+
+#define COMPARE_WEAK(_op_)                                      \
+inline bool operator _op_ (const sp<T>& o) const {              \
+    return m_ptr _op_ o.m_ptr;                                  \
+}                                                               \
+inline bool operator _op_ (const T* o) const {                  \
+    return m_ptr _op_ o;                                        \
+}                                                               \
+template<typename U>                                            \
+inline bool operator _op_ (const sp<U>& o) const {              \
+    return m_ptr _op_ o.m_ptr;                                  \
+}                                                               \
+template<typename U>                                            \
+inline bool operator _op_ (const U* o) const {                  \
+    return m_ptr _op_ o;                                        \
+}
+
+// ---------------------------------------------------------------------------
+class ReferenceMover;
+class ReferenceConverterBase {
+public:
+    virtual size_t getReferenceTypeSize() const = 0;
+    virtual void* getReferenceBase(void const*) const = 0;
+    inline virtual ~ReferenceConverterBase() { }
+};
+
+// ---------------------------------------------------------------------------
+
+class RefBase
+{
+public:
+            void            incStrong(const void* id) const;
+            void            decStrong(const void* id) const;
+
+            void            forceIncStrong(const void* id) const;
+
+            //! DEBUGGING ONLY: Get current strong ref count.
+            int32_t         getStrongCount() const;
+
+    class weakref_type
+    {
+    public:
+        RefBase*            refBase() const;
+
+        void                incWeak(const void* id);
+        void                decWeak(const void* id);
+
+        // acquires a strong reference if there is already one.
+        bool                attemptIncStrong(const void* id);
+
+        // acquires a weak reference if there is already one.
+        // This is not always safe. see ProcessState.cpp and BpBinder.cpp
+        // for proper use.
+        bool                attemptIncWeak(const void* id);
+
+        //! DEBUGGING ONLY: Get current weak ref count.
+        int32_t             getWeakCount() const;
+
+        //! DEBUGGING ONLY: Print references held on object.
+        void                printRefs() const;
+
+        //! DEBUGGING ONLY: Enable tracking for this object.
+        // enable -- enable/disable tracking
+        // retain -- when tracking is enable, if true, then we save a stack trace
+        //           for each reference and dereference; when retain == false, we
+        //           match up references and dereferences and keep only the
+        //           outstanding ones.
+
+        void                trackMe(bool enable, bool retain);
+    };
+
+            weakref_type*   createWeak(const void* id) const;
+
+            weakref_type*   getWeakRefs() const;
+
+            //! DEBUGGING ONLY: Print references held on object.
+    inline  void            printRefs() const { getWeakRefs()->printRefs(); }
+
+            //! DEBUGGING ONLY: Enable tracking of object.
+    inline  void            trackMe(bool enable, bool retain)
+    {
+        getWeakRefs()->trackMe(enable, retain);
+    }
+
+    typedef RefBase basetype;
+
+protected:
+                            RefBase();
+    virtual                 ~RefBase();
+
+    //! Flags for extendObjectLifetime()
+    enum {
+        OBJECT_LIFETIME_STRONG  = 0x0000,
+        OBJECT_LIFETIME_WEAK    = 0x0001,
+        OBJECT_LIFETIME_MASK    = 0x0001
+    };
+
+            void            extendObjectLifetime(int32_t mode);
+
+    //! Flags for onIncStrongAttempted()
+    enum {
+        FIRST_INC_STRONG = 0x0001
+    };
+
+    virtual void            onFirstRef();
+    virtual void            onLastStrongRef(const void* id);
+    virtual bool            onIncStrongAttempted(uint32_t flags, const void* id);
+    virtual void            onLastWeakRef(const void* id);
+
+private:
+    friend class ReferenceMover;
+    static void moveReferences(void* d, void const* s, size_t n,
+            const ReferenceConverterBase& caster);
+
+private:
+    friend class weakref_type;
+    class weakref_impl;
+
+                            RefBase(const RefBase& o);
+            RefBase&        operator=(const RefBase& o);
+
+        weakref_impl* const mRefs;
+};
+
+// ---------------------------------------------------------------------------
+
+template <class T>
+class LightRefBase
+{
+public:
+    inline LightRefBase() : mCount(0) { }
+    inline void incStrong(__attribute__((unused)) const void* id) const {
+        __sync_fetch_and_add(&mCount, 1);
+    }
+    inline void decStrong(__attribute__((unused)) const void* id) const {
+        if (__sync_fetch_and_sub(&mCount, 1) == 1) {
+            delete static_cast<const T*>(this);
+        }
+    }
+    //! DEBUGGING ONLY: Get current strong ref count.
+    inline int32_t getStrongCount() const {
+        return mCount;
+    }
+
+    typedef LightRefBase<T> basetype;
+
+protected:
+    inline ~LightRefBase() { }
+
+private:
+    friend class ReferenceMover;
+    inline static void moveReferences(void* d, void const* s, size_t n,
+            const ReferenceConverterBase& caster) { }
+
+private:
+    mutable volatile int32_t mCount;
+};
+
+// ---------------------------------------------------------------------------
+
+template <typename T>
+class wp
+{
+public:
+    typedef typename RefBase::weakref_type weakref_type;
+
+    inline wp() : m_ptr(0) { }
+
+    wp(T* other);
+    wp(const wp<T>& other);
+    wp(const sp<T>& other);
+    template<typename U> wp(U* other);
+    template<typename U> wp(const sp<U>& other);
+    template<typename U> wp(const wp<U>& other);
+
+    ~wp();
+
+    // Assignment
+
+    wp& operator = (T* other);
+    wp& operator = (const wp<T>& other);
+    wp& operator = (const sp<T>& other);
+
+    template<typename U> wp& operator = (U* other);
+    template<typename U> wp& operator = (const wp<U>& other);
+    template<typename U> wp& operator = (const sp<U>& other);
+
+    void set_object_and_refs(T* other, weakref_type* refs);
+
+    // promotion to sp
+
+    sp<T> promote() const;
+
+    // Reset
+
+    void clear();
+
+    // Accessors
+
+    inline  weakref_type* get_refs() const { return m_refs; }
+
+    inline  T* unsafe_get() const { return m_ptr; }
+
+    // Operators
+
+    COMPARE_WEAK(==)
+    COMPARE_WEAK(!=)
+    COMPARE_WEAK(>)
+    COMPARE_WEAK(<)
+    COMPARE_WEAK(<=)
+    COMPARE_WEAK(>=)
+
+    inline bool operator == (const wp<T>& o) const {
+        return (m_ptr == o.m_ptr) && (m_refs == o.m_refs);
+    }
+    template<typename U>
+    inline bool operator == (const wp<U>& o) const {
+        return m_ptr == o.m_ptr;
+    }
+
+    inline bool operator > (const wp<T>& o) const {
+        return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
+    }
+    template<typename U>
+    inline bool operator > (const wp<U>& o) const {
+        return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
+    }
+
+    inline bool operator < (const wp<T>& o) const {
+        return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
+    }
+    template<typename U>
+    inline bool operator < (const wp<U>& o) const {
+        return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
+    }
+                         inline bool operator != (const wp<T>& o) const { return m_refs != o.m_refs; }
+    template<typename U> inline bool operator != (const wp<U>& o) const { return !operator == (o); }
+                         inline bool operator <= (const wp<T>& o) const { return !operator > (o); }
+    template<typename U> inline bool operator <= (const wp<U>& o) const { return !operator > (o); }
+                         inline bool operator >= (const wp<T>& o) const { return !operator < (o); }
+    template<typename U> inline bool operator >= (const wp<U>& o) const { return !operator < (o); }
+
+private:
+    template<typename Y> friend class sp;
+    template<typename Y> friend class wp;
+
+    T*              m_ptr;
+    weakref_type*   m_refs;
+};
+
+template <typename T>
+TextOutput& operator<<(TextOutput& to, const wp<T>& val);
+
+#undef COMPARE_WEAK
+
+// ---------------------------------------------------------------------------
+// No user serviceable parts below here.
+
+template<typename T>
+wp<T>::wp(T* other)
+    : m_ptr(other)
+{
+    if (other) m_refs = other->createWeak(this);
+}
+
+template<typename T>
+wp<T>::wp(const wp<T>& other)
+    : m_ptr(other.m_ptr), m_refs(other.m_refs)
+{
+    if (m_ptr) m_refs->incWeak(this);
+}
+
+template<typename T>
+wp<T>::wp(const sp<T>& other)
+    : m_ptr(other.m_ptr)
+{
+    if (m_ptr) {
+        m_refs = m_ptr->createWeak(this);
+    }
+}
+
+template<typename T> template<typename U>
+wp<T>::wp(U* other)
+    : m_ptr(other)
+{
+    if (other) m_refs = other->createWeak(this);
+}
+
+template<typename T> template<typename U>
+wp<T>::wp(const wp<U>& other)
+    : m_ptr(other.m_ptr)
+{
+    if (m_ptr) {
+        m_refs = other.m_refs;
+        m_refs->incWeak(this);
+    }
+}
+
+template<typename T> template<typename U>
+wp<T>::wp(const sp<U>& other)
+    : m_ptr(other.m_ptr)
+{
+    if (m_ptr) {
+        m_refs = m_ptr->createWeak(this);
+    }
+}
+
+template<typename T>
+wp<T>::~wp()
+{
+    if (m_ptr) m_refs->decWeak(this);
+}
+
+template<typename T>
+wp<T>& wp<T>::operator = (T* other)
+{
+    weakref_type* newRefs =
+        other ? other->createWeak(this) : 0;
+    if (m_ptr) m_refs->decWeak(this);
+    m_ptr = other;
+    m_refs = newRefs;
+    return *this;
+}
+
+template<typename T>
+wp<T>& wp<T>::operator = (const wp<T>& other)
+{
+    weakref_type* otherRefs(other.m_refs);
+    T* otherPtr(other.m_ptr);
+    if (otherPtr) otherRefs->incWeak(this);
+    if (m_ptr) m_refs->decWeak(this);
+    m_ptr = otherPtr;
+    m_refs = otherRefs;
+    return *this;
+}
+
+template<typename T>
+wp<T>& wp<T>::operator = (const sp<T>& other)
+{
+    weakref_type* newRefs =
+        other != NULL ? other->createWeak(this) : 0;
+    T* otherPtr(other.m_ptr);
+    if (m_ptr) m_refs->decWeak(this);
+    m_ptr = otherPtr;
+    m_refs = newRefs;
+    return *this;
+}
+
+template<typename T> template<typename U>
+wp<T>& wp<T>::operator = (U* other)
+{
+    weakref_type* newRefs =
+        other ? other->createWeak(this) : 0;
+    if (m_ptr) m_refs->decWeak(this);
+    m_ptr = other;
+    m_refs = newRefs;
+    return *this;
+}
+
+template<typename T> template<typename U>
+wp<T>& wp<T>::operator = (const wp<U>& other)
+{
+    weakref_type* otherRefs(other.m_refs);
+    U* otherPtr(other.m_ptr);
+    if (otherPtr) otherRefs->incWeak(this);
+    if (m_ptr) m_refs->decWeak(this);
+    m_ptr = otherPtr;
+    m_refs = otherRefs;
+    return *this;
+}
+
+template<typename T> template<typename U>
+wp<T>& wp<T>::operator = (const sp<U>& other)
+{
+    weakref_type* newRefs =
+        other != NULL ? other->createWeak(this) : 0;
+    U* otherPtr(other.m_ptr);
+    if (m_ptr) m_refs->decWeak(this);
+    m_ptr = otherPtr;
+    m_refs = newRefs;
+    return *this;
+}
+
+template<typename T>
+void wp<T>::set_object_and_refs(T* other, weakref_type* refs)
+{
+    if (other) refs->incWeak(this);
+    if (m_ptr) m_refs->decWeak(this);
+    m_ptr = other;
+    m_refs = refs;
+}
+
+template<typename T>
+sp<T> wp<T>::promote() const
+{
+    sp<T> result;
+    if (m_ptr && m_refs->attemptIncStrong(&result)) {
+        result.set_pointer(m_ptr);
+    }
+    return result;
+}
+
+template<typename T>
+void wp<T>::clear()
+{
+    if (m_ptr) {
+        m_refs->decWeak(this);
+        m_ptr = 0;
+    }
+}
+
+template <typename T>
+inline TextOutput& operator<<(TextOutput& to, const wp<T>& val)
+{
+    return printWeakPointer(to, val.unsafe_get());
+}
+
+// ---------------------------------------------------------------------------
+
+// this class just serves as a namespace so TYPE::moveReferences can stay
+// private.
+
+class ReferenceMover {
+    // StrongReferenceCast and WeakReferenceCast do the impedance matching
+    // between the generic (void*) implementation in Refbase and the strongly typed
+    // template specializations below.
+
+    template <typename TYPE>
+    struct StrongReferenceCast : public ReferenceConverterBase {
+        virtual size_t getReferenceTypeSize() const { return sizeof( sp<TYPE> ); }
+        virtual void* getReferenceBase(void const* p) const {
+            sp<TYPE> const* sptr(reinterpret_cast<sp<TYPE> const*>(p));
+            return static_cast<typename TYPE::basetype *>(sptr->get());
+        }
+    };
+
+    template <typename TYPE>
+    struct WeakReferenceCast : public ReferenceConverterBase {
+        virtual size_t getReferenceTypeSize() const { return sizeof( wp<TYPE> ); }
+        virtual void* getReferenceBase(void const* p) const {
+            wp<TYPE> const* sptr(reinterpret_cast<wp<TYPE> const*>(p));
+            return static_cast<typename TYPE::basetype *>(sptr->unsafe_get());
+        }
+    };
+
+public:
+    template<typename TYPE> static inline
+    void move_references(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
+        memmove(d, s, n*sizeof(sp<TYPE>));
+        StrongReferenceCast<TYPE> caster;
+        TYPE::moveReferences(d, s, n, caster);
+    }
+    template<typename TYPE> static inline
+    void move_references(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
+        memmove(d, s, n*sizeof(wp<TYPE>));
+        WeakReferenceCast<TYPE> caster;
+        TYPE::moveReferences(d, s, n, caster);
+    }
+};
+
+// specialization for moving sp<> and wp<> types.
+// these are used by the [Sorted|Keyed]Vector<> implementations
+// sp<> and wp<> need to be handled specially, because they do not
+// have trivial copy operation in the general case (see RefBase.cpp
+// when DEBUG ops are enabled), but can be implemented very
+// efficiently in most cases.
+
+template<typename TYPE> inline
+void move_forward_type(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
+    ReferenceMover::move_references(d, s, n);
+}
+
+template<typename TYPE> inline
+void move_backward_type(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
+    ReferenceMover::move_references(d, s, n);
+}
+
+template<typename TYPE> inline
+void move_forward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
+    ReferenceMover::move_references(d, s, n);
+}
+
+template<typename TYPE> inline
+void move_backward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
+    ReferenceMover::move_references(d, s, n);
+}
+
+
+}; // namespace RSC
+}; // namespace android
+// ---------------------------------------------------------------------------
+
+#endif // RS_REF_BASE_H
diff --git a/cpp/util/StrongPointer.h b/cpp/util/StrongPointer.h
new file mode 100644
index 0000000..0f68615
--- /dev/null
+++ b/cpp/util/StrongPointer.h
@@ -0,0 +1,222 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef RS_STRONG_POINTER_H
+#define RS_STRONG_POINTER_H
+
+//#include <cutils/atomic.h>
+
+#include <stdint.h>
+#include <sys/types.h>
+#include <stdlib.h>
+
+// ---------------------------------------------------------------------------
+namespace android {
+namespace RSC {
+
+class TextOutput;
+TextOutput& printStrongPointer(TextOutput& to, const void* val);
+
+template<typename T> class wp;
+
+// ---------------------------------------------------------------------------
+
+#define COMPARE(_op_)                                           \
+inline bool operator _op_ (const sp<T>& o) const {              \
+    return m_ptr _op_ o.m_ptr;                                  \
+}                                                               \
+inline bool operator _op_ (const T* o) const {                  \
+    return m_ptr _op_ o;                                        \
+}                                                               \
+template<typename U>                                            \
+inline bool operator _op_ (const sp<U>& o) const {              \
+    return m_ptr _op_ o.m_ptr;                                  \
+}                                                               \
+template<typename U>                                            \
+inline bool operator _op_ (const U* o) const {                  \
+    return m_ptr _op_ o;                                        \
+}                                                               \
+inline bool operator _op_ (const wp<T>& o) const {              \
+    return m_ptr _op_ o.m_ptr;                                  \
+}                                                               \
+template<typename U>                                            \
+inline bool operator _op_ (const wp<U>& o) const {              \
+    return m_ptr _op_ o.m_ptr;                                  \
+}
+
+// ---------------------------------------------------------------------------
+
+template <typename T>
+class sp
+{
+public:
+    inline sp() : m_ptr(0) { }
+
+    sp(T* other);
+    sp(const sp<T>& other);
+    template<typename U> sp(U* other);
+    template<typename U> sp(const sp<U>& other);
+
+    ~sp();
+
+    // Assignment
+
+    sp& operator = (T* other);
+    sp& operator = (const sp<T>& other);
+
+    template<typename U> sp& operator = (const sp<U>& other);
+    template<typename U> sp& operator = (U* other);
+
+    //! Special optimization for use by ProcessState (and nobody else).
+    void force_set(T* other);
+
+    // Reset
+
+    void clear();
+
+    // Accessors
+
+    inline  T&      operator* () const  { return *m_ptr; }
+    inline  T*      operator-> () const { return m_ptr;  }
+    inline  T*      get() const         { return m_ptr; }
+
+    // Operators
+
+    COMPARE(==)
+    COMPARE(!=)
+    COMPARE(>)
+    COMPARE(<)
+    COMPARE(<=)
+    COMPARE(>=)
+
+private:
+    template<typename Y> friend class sp;
+    template<typename Y> friend class wp;
+    void set_pointer(T* ptr);
+    T* m_ptr;
+};
+
+#undef COMPARE
+
+template <typename T>
+TextOutput& operator<<(TextOutput& to, const sp<T>& val);
+
+// ---------------------------------------------------------------------------
+// No user serviceable parts below here.
+
+template<typename T>
+sp<T>::sp(T* other)
+: m_ptr(other)
+  {
+    if (other) other->incStrong(this);
+  }
+
+template<typename T>
+sp<T>::sp(const sp<T>& other)
+: m_ptr(other.m_ptr)
+  {
+    if (m_ptr) m_ptr->incStrong(this);
+  }
+
+template<typename T> template<typename U>
+sp<T>::sp(U* other) : m_ptr(other)
+{
+    if (other) ((T*)other)->incStrong(this);
+}
+
+template<typename T> template<typename U>
+sp<T>::sp(const sp<U>& other)
+: m_ptr(other.m_ptr)
+  {
+    if (m_ptr) m_ptr->incStrong(this);
+  }
+
+template<typename T>
+sp<T>::~sp()
+{
+    if (m_ptr) m_ptr->decStrong(this);
+}
+
+template<typename T>
+sp<T>& sp<T>::operator = (const sp<T>& other) {
+    T* otherPtr(other.m_ptr);
+    if (otherPtr) otherPtr->incStrong(this);
+    if (m_ptr) m_ptr->decStrong(this);
+    m_ptr = otherPtr;
+    return *this;
+}
+
+template<typename T>
+sp<T>& sp<T>::operator = (T* other)
+{
+    if (other) other->incStrong(this);
+    if (m_ptr) m_ptr->decStrong(this);
+    m_ptr = other;
+    return *this;
+}
+
+template<typename T> template<typename U>
+sp<T>& sp<T>::operator = (const sp<U>& other)
+{
+    T* otherPtr(other.m_ptr);
+    if (otherPtr) otherPtr->incStrong(this);
+    if (m_ptr) m_ptr->decStrong(this);
+    m_ptr = otherPtr;
+    return *this;
+}
+
+template<typename T> template<typename U>
+sp<T>& sp<T>::operator = (U* other)
+{
+    if (other) ((T*)other)->incStrong(this);
+    if (m_ptr) m_ptr->decStrong(this);
+    m_ptr = other;
+    return *this;
+}
+
+template<typename T>
+void sp<T>::force_set(T* other)
+{
+    other->forceIncStrong(this);
+    m_ptr = other;
+}
+
+template<typename T>
+void sp<T>::clear()
+{
+    if (m_ptr) {
+        m_ptr->decStrong(this);
+        m_ptr = 0;
+    }
+}
+
+template<typename T>
+void sp<T>::set_pointer(T* ptr) {
+    m_ptr = ptr;
+}
+
+template <typename T>
+inline TextOutput& operator<<(TextOutput& to, const sp<T>& val)
+{
+    return printStrongPointer(to, val.get());
+}
+
+}; // namespace RSC
+}; // namespace android
+
+// ---------------------------------------------------------------------------
+
+#endif // RS_STRONG_POINTER_H
diff --git a/cpp/util/TypeHelpers.h b/cpp/util/TypeHelpers.h
new file mode 100644
index 0000000..33a5201
--- /dev/null
+++ b/cpp/util/TypeHelpers.h
@@ -0,0 +1,302 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef RS_TYPE_HELPERS_H
+#define RS_TYPE_HELPERS_H
+
+#include <new>
+#include <stdint.h>
+#include <string.h>
+#include <sys/types.h>
+
+// ---------------------------------------------------------------------------
+namespace android {
+namespace RSC {
+
+/*
+ * Types traits
+ */
+
+template <typename T> struct trait_trivial_ctor { enum { value = false }; };
+template <typename T> struct trait_trivial_dtor { enum { value = false }; };
+template <typename T> struct trait_trivial_copy { enum { value = false }; };
+template <typename T> struct trait_trivial_move { enum { value = false }; };
+template <typename T> struct trait_pointer      { enum { value = false }; };
+template <typename T> struct trait_pointer<T*>  { enum { value = true }; };
+
+template <typename TYPE>
+struct traits {
+    enum {
+        // whether this type is a pointer
+        is_pointer          = trait_pointer<TYPE>::value,
+        // whether this type's constructor is a no-op
+        has_trivial_ctor    = is_pointer || trait_trivial_ctor<TYPE>::value,
+        // whether this type's destructor is a no-op
+        has_trivial_dtor    = is_pointer || trait_trivial_dtor<TYPE>::value,
+        // whether this type type can be copy-constructed with memcpy
+        has_trivial_copy    = is_pointer || trait_trivial_copy<TYPE>::value,
+        // whether this type can be moved with memmove
+        has_trivial_move    = is_pointer || trait_trivial_move<TYPE>::value
+    };
+};
+
+template <typename T, typename U>
+struct aggregate_traits {
+    enum {
+        is_pointer          = false,
+        has_trivial_ctor    =
+            traits<T>::has_trivial_ctor && traits<U>::has_trivial_ctor,
+        has_trivial_dtor    =
+            traits<T>::has_trivial_dtor && traits<U>::has_trivial_dtor,
+        has_trivial_copy    =
+            traits<T>::has_trivial_copy && traits<U>::has_trivial_copy,
+        has_trivial_move    =
+            traits<T>::has_trivial_move && traits<U>::has_trivial_move
+    };
+};
+
+#define RS_TRIVIAL_CTOR_TRAIT( T ) \
+    template<> struct trait_trivial_ctor< T >   { enum { value = true }; };
+
+#define RS_TRIVIAL_DTOR_TRAIT( T ) \
+    template<> struct trait_trivial_dtor< T >   { enum { value = true }; };
+
+#define RS_TRIVIAL_COPY_TRAIT( T ) \
+    template<> struct trait_trivial_copy< T >   { enum { value = true }; };
+
+#define RS_TRIVIAL_MOVE_TRAIT( T ) \
+    template<> struct trait_trivial_move< T >   { enum { value = true }; };
+
+#define RS_BASIC_TYPES_TRAITS( T ) \
+    RS_TRIVIAL_CTOR_TRAIT( T ) \
+    RS_TRIVIAL_DTOR_TRAIT( T ) \
+    RS_TRIVIAL_COPY_TRAIT( T ) \
+    RS_TRIVIAL_MOVE_TRAIT( T )
+
+// ---------------------------------------------------------------------------
+
+/*
+ * basic types traits
+ */
+
+RS_BASIC_TYPES_TRAITS( void )
+RS_BASIC_TYPES_TRAITS( bool )
+RS_BASIC_TYPES_TRAITS( char )
+RS_BASIC_TYPES_TRAITS( unsigned char )
+RS_BASIC_TYPES_TRAITS( short )
+RS_BASIC_TYPES_TRAITS( unsigned short )
+RS_BASIC_TYPES_TRAITS( int )
+RS_BASIC_TYPES_TRAITS( unsigned int )
+RS_BASIC_TYPES_TRAITS( long )
+RS_BASIC_TYPES_TRAITS( unsigned long )
+RS_BASIC_TYPES_TRAITS( long long )
+RS_BASIC_TYPES_TRAITS( unsigned long long )
+RS_BASIC_TYPES_TRAITS( float )
+RS_BASIC_TYPES_TRAITS( double )
+
+// ---------------------------------------------------------------------------
+
+
+/*
+ * compare and order types
+ */
+
+template<typename TYPE> inline
+int strictly_order_type(const TYPE& lhs, const TYPE& rhs) {
+    return (lhs < rhs) ? 1 : 0;
+}
+
+template<typename TYPE> inline
+int compare_type(const TYPE& lhs, const TYPE& rhs) {
+    return strictly_order_type(rhs, lhs) - strictly_order_type(lhs, rhs);
+}
+
+/*
+ * create, destroy, copy and move types...
+ */
+
+template<typename TYPE> inline
+void construct_type(TYPE* p, size_t n) {
+    if (!traits<TYPE>::has_trivial_ctor) {
+        while (n--) {
+            new(p++) TYPE;
+        }
+    }
+}
+
+template<typename TYPE> inline
+void destroy_type(TYPE* p, size_t n) {
+    if (!traits<TYPE>::has_trivial_dtor) {
+        while (n--) {
+            p->~TYPE();
+            p++;
+        }
+    }
+}
+
+template<typename TYPE> inline
+void copy_type(TYPE* d, const TYPE* s, size_t n) {
+    if (!traits<TYPE>::has_trivial_copy) {
+        while (n--) {
+            new(d) TYPE(*s);
+            d++, s++;
+        }
+    } else {
+        memcpy(d,s,n*sizeof(TYPE));
+    }
+}
+
+template<typename TYPE> inline
+void splat_type(TYPE* where, const TYPE* what, size_t n) {
+    if (!traits<TYPE>::has_trivial_copy) {
+        while (n--) {
+            new(where) TYPE(*what);
+            where++;
+        }
+    } else {
+        while (n--) {
+            *where++ = *what;
+        }
+    }
+}
+
+template<typename TYPE> inline
+void move_forward_type(TYPE* d, const TYPE* s, size_t n = 1) {
+    if ((traits<TYPE>::has_trivial_dtor && traits<TYPE>::has_trivial_copy)
+            || traits<TYPE>::has_trivial_move)
+    {
+        memmove(d,s,n*sizeof(TYPE));
+    } else {
+        d += n;
+        s += n;
+        while (n--) {
+            --d, --s;
+            if (!traits<TYPE>::has_trivial_copy) {
+                new(d) TYPE(*s);
+            } else {
+                *d = *s;
+            }
+            if (!traits<TYPE>::has_trivial_dtor) {
+                s->~TYPE();
+            }
+        }
+    }
+}
+
+template<typename TYPE> inline
+void move_backward_type(TYPE* d, const TYPE* s, size_t n = 1) {
+    if ((traits<TYPE>::has_trivial_dtor && traits<TYPE>::has_trivial_copy)
+            || traits<TYPE>::has_trivial_move)
+    {
+        memmove(d,s,n*sizeof(TYPE));
+    } else {
+        while (n--) {
+            if (!traits<TYPE>::has_trivial_copy) {
+                new(d) TYPE(*s);
+            } else {
+                *d = *s;
+            }
+            if (!traits<TYPE>::has_trivial_dtor) {
+                s->~TYPE();
+            }
+            d++, s++;
+        }
+    }
+}
+
+// ---------------------------------------------------------------------------
+
+/*
+ * a key/value pair
+ */
+
+template <typename KEY, typename VALUE>
+struct key_value_pair_t {
+    typedef KEY key_t;
+    typedef VALUE value_t;
+
+    KEY     key;
+    VALUE   value;
+    key_value_pair_t() { }
+    key_value_pair_t(const key_value_pair_t& o) : key(o.key), value(o.value) { }
+    key_value_pair_t(const KEY& k, const VALUE& v) : key(k), value(v)  { }
+    key_value_pair_t(const KEY& k) : key(k) { }
+    inline bool operator < (const key_value_pair_t& o) const {
+        return strictly_order_type(key, o.key);
+    }
+    inline const KEY& getKey() const {
+        return key;
+    }
+    inline const VALUE& getValue() const {
+        return value;
+    }
+};
+
+template <typename K, typename V>
+struct trait_trivial_ctor< key_value_pair_t<K, V> >
+{ enum { value = aggregate_traits<K,V>::has_trivial_ctor }; };
+template <typename K, typename V>
+struct trait_trivial_dtor< key_value_pair_t<K, V> >
+{ enum { value = aggregate_traits<K,V>::has_trivial_dtor }; };
+template <typename K, typename V>
+struct trait_trivial_copy< key_value_pair_t<K, V> >
+{ enum { value = aggregate_traits<K,V>::has_trivial_copy }; };
+template <typename K, typename V>
+struct trait_trivial_move< key_value_pair_t<K, V> >
+{ enum { value = aggregate_traits<K,V>::has_trivial_move }; };
+
+// ---------------------------------------------------------------------------
+
+/*
+ * Hash codes.
+ */
+typedef uint32_t hash_t;
+
+template <typename TKey>
+hash_t hash_type(const TKey& key);
+
+/* Built-in hash code specializations.
+ * Assumes pointers are 32bit. */
+#define RS_INT32_HASH(T) \
+        template <> inline hash_t hash_type(const T& value) { return hash_t(value); }
+#define RS_INT64_HASH(T) \
+        template <> inline hash_t hash_type(const T& value) { \
+                return hash_t((value >> 32) ^ value); }
+#define RS_REINTERPRET_HASH(T, R) \
+        template <> inline hash_t hash_type(const T& value) { \
+                return hash_type(*reinterpret_cast<const R*>(&value)); }
+
+RS_INT32_HASH(bool)
+RS_INT32_HASH(int8_t)
+RS_INT32_HASH(uint8_t)
+RS_INT32_HASH(int16_t)
+RS_INT32_HASH(uint16_t)
+RS_INT32_HASH(int32_t)
+RS_INT32_HASH(uint32_t)
+RS_INT64_HASH(int64_t)
+RS_INT64_HASH(uint64_t)
+RS_REINTERPRET_HASH(float, uint32_t)
+RS_REINTERPRET_HASH(double, uint64_t)
+
+template <typename T> inline hash_t hash_type(T* const & value) {
+    return hash_type(uintptr_t(value));
+}
+
+}; // namespace RSC
+}; // namespace android
+// ---------------------------------------------------------------------------
+
+#endif // RS_TYPE_HELPERS_H
diff --git a/cpu_ref/Android.mk b/cpu_ref/Android.mk
index 67e0786..b3de4f4 100644
--- a/cpu_ref/Android.mk
+++ b/cpu_ref/Android.mk
@@ -27,19 +27,25 @@
 	rsCpuIntrinsicColorMatrix.cpp \
 	rsCpuIntrinsicConvolve3x3.cpp \
 	rsCpuIntrinsicConvolve5x5.cpp \
+	rsCpuIntrinsicHistogram.cpp \
 	rsCpuIntrinsicLUT.cpp \
 	rsCpuIntrinsicYuvToRGB.cpp
 
 ifeq ($(ARCH_ARM_HAVE_NEON),true)
     LOCAL_CFLAGS += -DARCH_ARM_HAVE_NEON
     LOCAL_SRC_FILES+= \
-        rsCpuIntrinsics_neon.S
+        rsCpuIntrinsics_neon.S \
+        rsCpuIntrinsics_neon_ColorMatrix.S
 endif
 
 ifeq ($(ARCH_ARM_HAVE_VFP),true)
     LOCAL_CFLAGS += -DARCH_ARM_HAVE_VFP
 endif
 
+ifeq ($(ARCH_X86_HAVE_SSE2), true)
+    LOCAL_CFLAGS += -DARCH_X86_HAVE_SSE2
+endif
+
 LOCAL_SHARED_LIBRARIES += libRS libcutils libutils liblog libsync
 LOCAL_SHARED_LIBRARIES += libbcc libbcinfo
 
diff --git a/driver/linkloader/android/librsloader.cpp b/cpu_ref/linkloader/android/librsloader.cpp
similarity index 100%
rename from driver/linkloader/android/librsloader.cpp
rename to cpu_ref/linkloader/android/librsloader.cpp
diff --git a/driver/linkloader/android/librsloader.h b/cpu_ref/linkloader/android/librsloader.h
similarity index 100%
rename from driver/linkloader/android/librsloader.h
rename to cpu_ref/linkloader/android/librsloader.h
diff --git a/driver/linkloader/android/test-librsloader.c b/cpu_ref/linkloader/android/test-librsloader.c
similarity index 100%
rename from driver/linkloader/android/test-librsloader.c
rename to cpu_ref/linkloader/android/test-librsloader.c
diff --git a/driver/linkloader/include/ELF.h b/cpu_ref/linkloader/include/ELF.h
similarity index 100%
rename from driver/linkloader/include/ELF.h
rename to cpu_ref/linkloader/include/ELF.h
diff --git a/driver/linkloader/include/ELFHeader.h b/cpu_ref/linkloader/include/ELFHeader.h
similarity index 100%
rename from driver/linkloader/include/ELFHeader.h
rename to cpu_ref/linkloader/include/ELFHeader.h
diff --git a/driver/linkloader/include/ELFObject.h b/cpu_ref/linkloader/include/ELFObject.h
similarity index 100%
rename from driver/linkloader/include/ELFObject.h
rename to cpu_ref/linkloader/include/ELFObject.h
diff --git a/driver/linkloader/include/ELFReloc.h b/cpu_ref/linkloader/include/ELFReloc.h
similarity index 100%
rename from driver/linkloader/include/ELFReloc.h
rename to cpu_ref/linkloader/include/ELFReloc.h
diff --git a/driver/linkloader/include/ELFSection.h b/cpu_ref/linkloader/include/ELFSection.h
similarity index 100%
rename from driver/linkloader/include/ELFSection.h
rename to cpu_ref/linkloader/include/ELFSection.h
diff --git a/driver/linkloader/include/ELFSectionBits.h b/cpu_ref/linkloader/include/ELFSectionBits.h
similarity index 100%
rename from driver/linkloader/include/ELFSectionBits.h
rename to cpu_ref/linkloader/include/ELFSectionBits.h
diff --git a/driver/linkloader/include/ELFSectionHeader.h b/cpu_ref/linkloader/include/ELFSectionHeader.h
similarity index 100%
rename from driver/linkloader/include/ELFSectionHeader.h
rename to cpu_ref/linkloader/include/ELFSectionHeader.h
diff --git a/driver/linkloader/include/ELFSectionHeaderTable.h b/cpu_ref/linkloader/include/ELFSectionHeaderTable.h
similarity index 100%
rename from driver/linkloader/include/ELFSectionHeaderTable.h
rename to cpu_ref/linkloader/include/ELFSectionHeaderTable.h
diff --git a/driver/linkloader/include/ELFSectionNoBits.h b/cpu_ref/linkloader/include/ELFSectionNoBits.h
similarity index 100%
rename from driver/linkloader/include/ELFSectionNoBits.h
rename to cpu_ref/linkloader/include/ELFSectionNoBits.h
diff --git a/driver/linkloader/include/ELFSectionProgBits.h b/cpu_ref/linkloader/include/ELFSectionProgBits.h
similarity index 100%
rename from driver/linkloader/include/ELFSectionProgBits.h
rename to cpu_ref/linkloader/include/ELFSectionProgBits.h
diff --git a/driver/linkloader/include/ELFSectionRelTable.h b/cpu_ref/linkloader/include/ELFSectionRelTable.h
similarity index 100%
rename from driver/linkloader/include/ELFSectionRelTable.h
rename to cpu_ref/linkloader/include/ELFSectionRelTable.h
diff --git a/driver/linkloader/include/ELFSectionStrTab.h b/cpu_ref/linkloader/include/ELFSectionStrTab.h
similarity index 100%
rename from driver/linkloader/include/ELFSectionStrTab.h
rename to cpu_ref/linkloader/include/ELFSectionStrTab.h
diff --git a/driver/linkloader/include/ELFSectionSymTab.h b/cpu_ref/linkloader/include/ELFSectionSymTab.h
similarity index 100%
rename from driver/linkloader/include/ELFSectionSymTab.h
rename to cpu_ref/linkloader/include/ELFSectionSymTab.h
diff --git a/driver/linkloader/include/ELFSymbol.h b/cpu_ref/linkloader/include/ELFSymbol.h
similarity index 100%
rename from driver/linkloader/include/ELFSymbol.h
rename to cpu_ref/linkloader/include/ELFSymbol.h
diff --git a/driver/linkloader/include/ELFTypes.h b/cpu_ref/linkloader/include/ELFTypes.h
similarity index 100%
rename from driver/linkloader/include/ELFTypes.h
rename to cpu_ref/linkloader/include/ELFTypes.h
diff --git a/driver/linkloader/include/GOT.h b/cpu_ref/linkloader/include/GOT.h
similarity index 100%
rename from driver/linkloader/include/GOT.h
rename to cpu_ref/linkloader/include/GOT.h
diff --git a/driver/linkloader/include/MemChunk.h b/cpu_ref/linkloader/include/MemChunk.h
similarity index 73%
rename from driver/linkloader/include/MemChunk.h
rename to cpu_ref/linkloader/include/MemChunk.h
index fb853e9..c4d78c9 100644
--- a/driver/linkloader/include/MemChunk.h
+++ b/cpu_ref/linkloader/include/MemChunk.h
@@ -18,12 +18,22 @@
 #define MEM_CHUNK_H
 
 #include <stddef.h>
+#include <stdint.h>
 #include <stdlib.h>
 
+typedef void *(*AllocFunc) (size_t, uint32_t);
+typedef void (*FreeFunc) (void *);
+
 class MemChunk {
 private:
   unsigned char *buf;
   size_t buf_size;
+  bool bVendorBuf;
+
+  static AllocFunc VendorAlloc;
+  static FreeFunc VendorFree;
+
+  bool invalidBuf() const;
 
 public:
   MemChunk();
@@ -56,6 +66,12 @@
     return buf_size;
   }
 
+  // The allocation function must return page-aligned memory or we will be
+  // unable to mprotect the region appropriately.
+  static void registerAllocFreeCallbacks(AllocFunc a, FreeFunc f) {
+    VendorAlloc = a;
+    VendorFree = f;
+  }
 };
 
 #endif // MEM_CHUNK_H
diff --git a/driver/linkloader/include/StubLayout.h b/cpu_ref/linkloader/include/StubLayout.h
similarity index 100%
rename from driver/linkloader/include/StubLayout.h
rename to cpu_ref/linkloader/include/StubLayout.h
diff --git a/driver/linkloader/include/impl/ELFHeader.hxx b/cpu_ref/linkloader/include/impl/ELFHeader.hxx
similarity index 100%
rename from driver/linkloader/include/impl/ELFHeader.hxx
rename to cpu_ref/linkloader/include/impl/ELFHeader.hxx
diff --git a/driver/linkloader/include/impl/ELFObject.hxx b/cpu_ref/linkloader/include/impl/ELFObject.hxx
similarity index 100%
rename from driver/linkloader/include/impl/ELFObject.hxx
rename to cpu_ref/linkloader/include/impl/ELFObject.hxx
diff --git a/driver/linkloader/include/impl/ELFReloc.hxx b/cpu_ref/linkloader/include/impl/ELFReloc.hxx
similarity index 100%
rename from driver/linkloader/include/impl/ELFReloc.hxx
rename to cpu_ref/linkloader/include/impl/ELFReloc.hxx
diff --git a/driver/linkloader/include/impl/ELFSection.hxx b/cpu_ref/linkloader/include/impl/ELFSection.hxx
similarity index 100%
rename from driver/linkloader/include/impl/ELFSection.hxx
rename to cpu_ref/linkloader/include/impl/ELFSection.hxx
diff --git a/driver/linkloader/include/impl/ELFSectionBits.hxx b/cpu_ref/linkloader/include/impl/ELFSectionBits.hxx
similarity index 100%
rename from driver/linkloader/include/impl/ELFSectionBits.hxx
rename to cpu_ref/linkloader/include/impl/ELFSectionBits.hxx
diff --git a/driver/linkloader/include/impl/ELFSectionHeader.hxx b/cpu_ref/linkloader/include/impl/ELFSectionHeader.hxx
similarity index 100%
rename from driver/linkloader/include/impl/ELFSectionHeader.hxx
rename to cpu_ref/linkloader/include/impl/ELFSectionHeader.hxx
diff --git a/driver/linkloader/include/impl/ELFSectionHeaderTable.hxx b/cpu_ref/linkloader/include/impl/ELFSectionHeaderTable.hxx
similarity index 100%
rename from driver/linkloader/include/impl/ELFSectionHeaderTable.hxx
rename to cpu_ref/linkloader/include/impl/ELFSectionHeaderTable.hxx
diff --git a/driver/linkloader/include/impl/ELFSectionNoBits.hxx b/cpu_ref/linkloader/include/impl/ELFSectionNoBits.hxx
similarity index 100%
rename from driver/linkloader/include/impl/ELFSectionNoBits.hxx
rename to cpu_ref/linkloader/include/impl/ELFSectionNoBits.hxx
diff --git a/driver/linkloader/include/impl/ELFSectionProgBits.hxx b/cpu_ref/linkloader/include/impl/ELFSectionProgBits.hxx
similarity index 100%
rename from driver/linkloader/include/impl/ELFSectionProgBits.hxx
rename to cpu_ref/linkloader/include/impl/ELFSectionProgBits.hxx
diff --git a/driver/linkloader/include/impl/ELFSectionRelTable.hxx b/cpu_ref/linkloader/include/impl/ELFSectionRelTable.hxx
similarity index 100%
rename from driver/linkloader/include/impl/ELFSectionRelTable.hxx
rename to cpu_ref/linkloader/include/impl/ELFSectionRelTable.hxx
diff --git a/driver/linkloader/include/impl/ELFSectionStrTab.hxx b/cpu_ref/linkloader/include/impl/ELFSectionStrTab.hxx
similarity index 100%
rename from driver/linkloader/include/impl/ELFSectionStrTab.hxx
rename to cpu_ref/linkloader/include/impl/ELFSectionStrTab.hxx
diff --git a/driver/linkloader/include/impl/ELFSectionSymTab.hxx b/cpu_ref/linkloader/include/impl/ELFSectionSymTab.hxx
similarity index 100%
rename from driver/linkloader/include/impl/ELFSectionSymTab.hxx
rename to cpu_ref/linkloader/include/impl/ELFSectionSymTab.hxx
diff --git a/driver/linkloader/include/impl/ELFSymbol.hxx b/cpu_ref/linkloader/include/impl/ELFSymbol.hxx
similarity index 100%
rename from driver/linkloader/include/impl/ELFSymbol.hxx
rename to cpu_ref/linkloader/include/impl/ELFSymbol.hxx
diff --git a/driver/linkloader/include/mmanWindows.h b/cpu_ref/linkloader/include/mmanWindows.h
similarity index 100%
rename from driver/linkloader/include/mmanWindows.h
rename to cpu_ref/linkloader/include/mmanWindows.h
diff --git a/driver/linkloader/lib/ELFHeader.cpp b/cpu_ref/linkloader/lib/ELFHeader.cpp
similarity index 100%
rename from driver/linkloader/lib/ELFHeader.cpp
rename to cpu_ref/linkloader/lib/ELFHeader.cpp
diff --git a/driver/linkloader/lib/ELFSectionHeader.cpp b/cpu_ref/linkloader/lib/ELFSectionHeader.cpp
similarity index 100%
rename from driver/linkloader/lib/ELFSectionHeader.cpp
rename to cpu_ref/linkloader/lib/ELFSectionHeader.cpp
diff --git a/driver/linkloader/lib/ELFSymbol.cpp b/cpu_ref/linkloader/lib/ELFSymbol.cpp
similarity index 100%
rename from driver/linkloader/lib/ELFSymbol.cpp
rename to cpu_ref/linkloader/lib/ELFSymbol.cpp
diff --git a/driver/linkloader/lib/ELFTypes.cpp b/cpu_ref/linkloader/lib/ELFTypes.cpp
similarity index 100%
rename from driver/linkloader/lib/ELFTypes.cpp
rename to cpu_ref/linkloader/lib/ELFTypes.cpp
diff --git a/driver/linkloader/lib/GOT.cpp b/cpu_ref/linkloader/lib/GOT.cpp
similarity index 100%
rename from driver/linkloader/lib/GOT.cpp
rename to cpu_ref/linkloader/lib/GOT.cpp
diff --git a/driver/linkloader/lib/MemChunk.cpp b/cpu_ref/linkloader/lib/MemChunk.cpp
similarity index 66%
rename from driver/linkloader/lib/MemChunk.cpp
rename to cpu_ref/linkloader/lib/MemChunk.cpp
index 6a04916..5d6c102 100644
--- a/driver/linkloader/lib/MemChunk.cpp
+++ b/cpu_ref/linkloader/lib/MemChunk.cpp
@@ -41,32 +41,49 @@
 static uintptr_t StartAddr = 0x7e000000UL;
 #endif
 
-MemChunk::MemChunk() : buf((unsigned char *)MAP_FAILED), buf_size(0) {
+AllocFunc MemChunk::VendorAlloc = NULL;
+FreeFunc MemChunk::VendorFree = NULL;
+
+MemChunk::MemChunk() : buf(NULL), buf_size(0), bVendorBuf(true) {
 }
 
 MemChunk::~MemChunk() {
-  if (buf != MAP_FAILED) {
+  if (!invalidBuf() && bVendorBuf && VendorFree) {
+    (*VendorFree)(buf);
+    return;
+  }
+  if (!invalidBuf()) {
     munmap(buf, buf_size);
   }
 }
 
+bool MemChunk::invalidBuf() const {
+  return (buf == 0 || buf == (unsigned char *)MAP_FAILED);
+}
+
 bool MemChunk::allocate(size_t size) {
   if (size == 0) {
     return true;
   }
+  if (VendorAlloc) {
+    buf = (unsigned char*)(*VendorAlloc)(size, 0);
+  }
+  if (invalidBuf()) {
+    bVendorBuf = false;
 #if USE_FIXED_ADDR_MEM_CHUNK
-  buf = (unsigned char *)mmap((void *)StartAddr, size,
-                              PROT_READ | PROT_WRITE,
-                              MAP_PRIVATE | MAP_ANON | MAP_32BIT,
-                              -1, 0);
+    buf = (unsigned char *)mmap((void *)StartAddr, size,
+                                PROT_READ | PROT_WRITE,
+                                MAP_PRIVATE | MAP_ANON | MAP_32BIT,
+                                -1, 0);
 #else
-  buf = (unsigned char *)mmap(0, size,
-                              PROT_READ | PROT_WRITE,
-                              MAP_PRIVATE | MAP_ANON | MAP_32BIT,
-                              -1, 0);
+    buf = (unsigned char *)mmap(0, size,
+                                PROT_READ | PROT_WRITE,
+                                MAP_PRIVATE | MAP_ANON | MAP_32BIT,
+                                -1, 0);
 #endif
+  }
 
-  if (buf == MAP_FAILED) {
+  if (invalidBuf()) {
     return false;
   }
 
@@ -79,7 +96,7 @@
 }
 
 void MemChunk::print() const {
-  if (buf != MAP_FAILED) {
+  if (!invalidBuf()) {
     dump_hex(buf, buf_size, 0, buf_size);
   }
 }
diff --git a/driver/linkloader/lib/StubLayout.cpp b/cpu_ref/linkloader/lib/StubLayout.cpp
similarity index 100%
rename from driver/linkloader/lib/StubLayout.cpp
rename to cpu_ref/linkloader/lib/StubLayout.cpp
diff --git a/driver/linkloader/lib/mmanWindows.cpp b/cpu_ref/linkloader/lib/mmanWindows.cpp
similarity index 100%
rename from driver/linkloader/lib/mmanWindows.cpp
rename to cpu_ref/linkloader/lib/mmanWindows.cpp
diff --git a/driver/linkloader/main.cpp b/cpu_ref/linkloader/main.cpp
similarity index 100%
rename from driver/linkloader/main.cpp
rename to cpu_ref/linkloader/main.cpp
diff --git a/driver/linkloader/utils/flush_cpu_cache.h b/cpu_ref/linkloader/utils/flush_cpu_cache.h
similarity index 100%
rename from driver/linkloader/utils/flush_cpu_cache.h
rename to cpu_ref/linkloader/utils/flush_cpu_cache.h
diff --git a/driver/linkloader/utils/helper.cpp b/cpu_ref/linkloader/utils/helper.cpp
similarity index 100%
rename from driver/linkloader/utils/helper.cpp
rename to cpu_ref/linkloader/utils/helper.cpp
diff --git a/driver/linkloader/utils/helper.h b/cpu_ref/linkloader/utils/helper.h
similarity index 100%
rename from driver/linkloader/utils/helper.h
rename to cpu_ref/linkloader/utils/helper.h
diff --git a/driver/linkloader/utils/raw_ostream.cpp b/cpu_ref/linkloader/utils/raw_ostream.cpp
similarity index 100%
rename from driver/linkloader/utils/raw_ostream.cpp
rename to cpu_ref/linkloader/utils/raw_ostream.cpp
diff --git a/driver/linkloader/utils/raw_ostream.h b/cpu_ref/linkloader/utils/raw_ostream.h
similarity index 100%
rename from driver/linkloader/utils/raw_ostream.h
rename to cpu_ref/linkloader/utils/raw_ostream.h
diff --git a/driver/linkloader/utils/rsl_assert.cpp b/cpu_ref/linkloader/utils/rsl_assert.cpp
similarity index 100%
rename from driver/linkloader/utils/rsl_assert.cpp
rename to cpu_ref/linkloader/utils/rsl_assert.cpp
diff --git a/driver/linkloader/utils/rsl_assert.h b/cpu_ref/linkloader/utils/rsl_assert.h
similarity index 100%
rename from driver/linkloader/utils/rsl_assert.h
rename to cpu_ref/linkloader/utils/rsl_assert.h
diff --git a/driver/linkloader/utils/serialize.h b/cpu_ref/linkloader/utils/serialize.h
similarity index 100%
rename from driver/linkloader/utils/serialize.h
rename to cpu_ref/linkloader/utils/serialize.h
diff --git a/driver/linkloader/utils/traits.h b/cpu_ref/linkloader/utils/traits.h
similarity index 100%
rename from driver/linkloader/utils/traits.h
rename to cpu_ref/linkloader/utils/traits.h
diff --git a/cpu_ref/rsCpuCore.cpp b/cpu_ref/rsCpuCore.cpp
index 79b908c..7475ccb 100644
--- a/cpu_ref/rsCpuCore.cpp
+++ b/cpu_ref/rsCpuCore.cpp
@@ -115,6 +115,7 @@
 #ifndef RS_COMPATIBILITY_LIB
     mLinkRuntimeCallback = NULL;
     mSelectRTCallback = NULL;
+    mSetupCompilerCallback = NULL;
 #endif
 }
 
@@ -519,6 +520,8 @@
                                                 const Script *s, const Element *e);
 extern RsdCpuScriptImpl * rsdIntrinsic_Blend(RsdCpuReferenceImpl *ctx,
                                              const Script *s, const Element *e);
+extern RsdCpuScriptImpl * rsdIntrinsic_Histogram(RsdCpuReferenceImpl *ctx,
+                                                 const Script *s, const Element *e);
 
 RsdCpuReference::CpuScript * RsdCpuReferenceImpl::createIntrinsic(const Script *s,
                                     RsScriptIntrinsicID iid, Element *e) {
@@ -549,6 +552,9 @@
     case RS_SCRIPT_INTRINSIC_ID_BLEND:
         i = rsdIntrinsic_Blend(this, s, e);
         break;
+    case RS_SCRIPT_INTRINSIC_ID_HISTOGRAM:
+        i = rsdIntrinsic_Histogram(this, s, e);
+        break;
 
     default:
         rsAssert(0);
diff --git a/cpu_ref/rsCpuCore.h b/cpu_ref/rsCpuCore.h
index 0f30a48..22d0d28 100644
--- a/cpu_ref/rsCpuCore.h
+++ b/cpu_ref/rsCpuCore.h
@@ -126,6 +126,14 @@
     RSSelectRTCallback getSelectRTCallback() {
         return mSelectRTCallback;
     }
+
+    virtual void setSetupCompilerCallback(
+            RSSetupCompilerCallback pSetupCompilerCallback) {
+        mSetupCompilerCallback = pSetupCompilerCallback;
+    }
+    virtual RSSetupCompilerCallback getSetupCompilerCallback() const {
+        return mSetupCompilerCallback;
+    }
 #endif
     virtual bool getInForEach() { return mInForEach; }
 
@@ -157,6 +165,7 @@
 #ifndef RS_COMPATIBILITY_LIB
     bcc::RSLinkRuntimeCallback mLinkRuntimeCallback;
     RSSelectRTCallback mSelectRTCallback;
+    RSSetupCompilerCallback mSetupCompilerCallback;
 #endif
 };
 
diff --git a/cpu_ref/rsCpuIntrinsic.cpp b/cpu_ref/rsCpuIntrinsic.cpp
index 450ee30..cda40f0 100644
--- a/cpu_ref/rsCpuIntrinsic.cpp
+++ b/cpu_ref/rsCpuIntrinsic.cpp
@@ -73,6 +73,16 @@
 }
 
 
+void RsdCpuScriptIntrinsic::preLaunch(uint32_t slot, const Allocation * ain,
+                                      Allocation * aout, const void * usr,
+                                      uint32_t usrLen, const RsScriptCall *sc) {
+}
+
+void RsdCpuScriptIntrinsic::postLaunch(uint32_t slot, const Allocation * ain,
+                                       Allocation * aout, const void * usr,
+                                       uint32_t usrLen, const RsScriptCall *sc) {
+}
+
 void RsdCpuScriptIntrinsic::invokeForEach(uint32_t slot,
                                           const Allocation * ain,
                                           Allocation * aout,
@@ -81,6 +91,8 @@
                                           const RsScriptCall *sc) {
 
     MTLaunchStruct mtls;
+    preLaunch(slot, ain, aout, usr, usrLen, sc);
+
     forEachMtlsSetup(ain, aout, usr, usrLen, sc, &mtls);
     mtls.script = this;
     mtls.fep.slot = slot;
@@ -91,6 +103,8 @@
     RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
     mCtx->launchThreads(ain, aout, sc, &mtls);
     mCtx->setTLS(oldTLS);
+
+    postLaunch(slot, ain, aout, usr, usrLen, sc);
 }
 
 void RsdCpuScriptIntrinsic::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) {
diff --git a/cpu_ref/rsCpuIntrinsic.h b/cpu_ref/rsCpuIntrinsic.h
index 35ffc69..917b235 100644
--- a/cpu_ref/rsCpuIntrinsic.h
+++ b/cpu_ref/rsCpuIntrinsic.h
@@ -40,6 +40,13 @@
     virtual void invokeInit();
     virtual void invokeFreeChildren();
 
+    virtual void preLaunch(uint32_t slot, const Allocation * ain,
+                           Allocation * aout, const void * usr,
+                           uint32_t usrLen, const RsScriptCall *sc);
+    virtual void postLaunch(uint32_t slot, const Allocation * ain,
+                            Allocation * aout, const void * usr,
+                            uint32_t usrLen, const RsScriptCall *sc);
+
     virtual void setGlobalVar(uint32_t slot, const void *data, size_t dataLength);
     virtual void setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
                                   const Element *e, const size_t *dims, size_t dimLength);
diff --git a/cpu_ref/rsCpuIntrinsicColorMatrix.cpp b/cpu_ref/rsCpuIntrinsicColorMatrix.cpp
index c6e38c0..3ad3063 100644
--- a/cpu_ref/rsCpuIntrinsicColorMatrix.cpp
+++ b/cpu_ref/rsCpuIntrinsicColorMatrix.cpp
@@ -14,9 +14,19 @@
  * limitations under the License.
  */
 
+#include <sys/mman.h>
+#include <unistd.h>
 
 #include "rsCpuIntrinsic.h"
 #include "rsCpuIntrinsicInlines.h"
+#include "linkloader/include/MemChunk.h"
+
+#include <sys/mman.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+//#include <utils/StopWatch.h>
+
 
 using namespace android;
 using namespace android::renderscript;
@@ -34,49 +44,425 @@
     virtual ~RsdCpuScriptIntrinsicColorMatrix();
     RsdCpuScriptIntrinsicColorMatrix(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e);
 
+    virtual void preLaunch(uint32_t slot, const Allocation * ain, Allocation * aout,
+                           const void * usr, uint32_t usrLen, const RsScriptCall *sc);
+    virtual void postLaunch(uint32_t slot, const Allocation * ain, Allocation * aout,
+                            const void * usr, uint32_t usrLen, const RsScriptCall *sc);
+
 protected:
     float fp[16];
+    float fpa[4];
     short ip[16];
+    int ipa[4];
 
-    static void kernel4x4(const RsForEachStubParamStruct *p,
-                          uint32_t xstart, uint32_t xend,
-                          uint32_t instep, uint32_t outstep);
-    static void kernel3x3(const RsForEachStubParamStruct *p,
-                          uint32_t xstart, uint32_t xend,
-                          uint32_t instep, uint32_t outstep);
-    static void kernelDot(const RsForEachStubParamStruct *p,
-                          uint32_t xstart, uint32_t xend,
-                          uint32_t instep, uint32_t outstep);
+    static void kernel(const RsForEachStubParamStruct *p,
+                       uint32_t xstart, uint32_t xend,
+                       uint32_t instep, uint32_t outstep);
+
+    int64_t mLastKey;
+    unsigned char *mBuf;
+    size_t mBufSize;
+
+    int64_t computeKey(const Element *ein, const Element *eout);
+
+    bool build(int64_t key);
+
+    void (*mOptKernel)(void *dst, const void *src, const short *coef, uint32_t count);
+
 };
 
 }
 }
 
 
-void RsdCpuScriptIntrinsicColorMatrix::setGlobalVar(uint32_t slot, const void *data,
-                                                    size_t dataLength) {
-    rsAssert(slot == 0);
-    memcpy (fp, data, dataLength);
-    for(int ct=0; ct < 16; ct++) {
-        ip[ct] = (short)(fp[ct] * 255.f + 0.5f);
+#define CM_IN_VEC_SIZE_MASK         0x00000003
+#define CM_OUT_VEC_SIZE_MASK        0x0000000c
+#define CM_IN_TYPE_SIZE_MASK        0x000000F0
+#define CM_OUT_TYPE_SIZE_MASK       0x00000F00
+#define CM_DOT_MASK                 0x00001000
+#define CM_ADD_MASK                 0x00002000
+#define CM_COPY_ALPHA               0x00004000
+#define CM_MATRIX_MASK              0xFFFF0000
+
+
+int64_t RsdCpuScriptIntrinsicColorMatrix::computeKey(
+        const Element *ein, const Element *eout) {
+
+    int64_t key = 0;
+
+    // Compute a unique code key for this operation
+
+    // Add to the key the input and output types
+    key |= (ein->getVectorSize() - 1);
+    key |= (eout->getVectorSize() - 1) << 2;
+
+    bool hasFloat = false;
+    if (ein->getType() == RS_TYPE_FLOAT_32) {
+        hasFloat = true;
+        key |= 1 << 4;
+    }
+    if (eout->getType() == RS_TYPE_FLOAT_32) {
+        hasFloat = true;
+        key |= 1 << 8;
     }
 
-    mRootPtr = &kernel4x4;
-    if ((ip[3] == 0) && (ip[7] == 0) && (ip[11] == 0) &&
-        (ip[12] == 0) && (ip[13] == 0) && (ip[14] == 0) && (ip[15] == 255)) {
-        mRootPtr = &kernel3x3;
-
-        if ((ip[0] == ip[1]) && (ip[0] == ip[2]) &&
-            (ip[4] == ip[5]) && (ip[4] == ip[6]) &&
-            (ip[8] == ip[9]) && (ip[8] == ip[10])) {
-            mRootPtr = &kernelDot;
+    // Mask in the bits indicating which coefficients in the
+    // color matrix are needed.
+    if (hasFloat) {
+        for (uint32_t i=0; i < 16; i++) {
+            if (fabs(fp[i]) != 0.f) {
+                key |= (uint32_t)(1 << (i + 16));
+            }
+        }
+    } else {
+        for (uint32_t i=0; i < 16; i++) {
+            if (ip[i] != 0) {
+                key |= (uint32_t)(1 << (i + 16));
+            }
         }
     }
+
+    // Look for a dot product where the r,g,b colums are the same
+    if ((ip[0] == ip[1]) && (ip[0] == ip[2]) &&
+        (ip[4] == ip[5]) && (ip[4] == ip[6]) &&
+        (ip[8] == ip[9]) && (ip[8] == ip[10]) &&
+        (ip[12] == ip[13]) && (ip[12] == ip[14])) {
+
+        key |= CM_DOT_MASK;
+    }
+
+    // Is alpha a simple copy
+    if (!(key & 0x08880000) && (ip[15] == 256)) {
+        key |= CM_COPY_ALPHA;
+    }
+
+    //ALOGE("build key %08x, %08x", (int32_t)(key >> 32), (int32_t)key);
+    return key;
 }
 
-extern "C" void rsdIntrinsicColorMatrix4x4_K(void *dst, const void *src, const short *coef, uint32_t count);
-extern "C" void rsdIntrinsicColorMatrix3x3_K(void *dst, const void *src, const short *coef, uint32_t count);
-extern "C" void rsdIntrinsicColorMatrixDot_K(void *dst, const void *src, const short *coef, uint32_t count);
+#if defined(ARCH_ARM_HAVE_NEON)
+
+#define DEF_SYM(x)                                  \
+    extern "C" uint32_t _N_ColorMatrix_##x;      \
+    extern "C" uint32_t _N_ColorMatrix_##x##_end;  \
+    extern "C" uint32_t _N_ColorMatrix_##x##_len;
+
+DEF_SYM(prefix)
+DEF_SYM(postfix1)
+DEF_SYM(postfix2)
+DEF_SYM(load_u8_4)
+DEF_SYM(load_u8_2)
+DEF_SYM(load_u8_1)
+DEF_SYM(store_u8_4)
+DEF_SYM(store_u8_2)
+DEF_SYM(store_u8_1)
+DEF_SYM(unpack_u8_4)
+DEF_SYM(unpack_u8_3)
+DEF_SYM(unpack_u8_2)
+DEF_SYM(unpack_u8_1)
+DEF_SYM(pack_u8_4)
+DEF_SYM(pack_u8_3)
+DEF_SYM(pack_u8_2)
+DEF_SYM(pack_u8_1)
+DEF_SYM(dot)
+DEF_SYM(add_0_u8)
+DEF_SYM(add_1_u8)
+DEF_SYM(add_2_u8)
+DEF_SYM(add_3_u8)
+
+#define ADD_CHUNK(x) \
+    memcpy(buf, &_N_ColorMatrix_##x, _N_ColorMatrix_##x##_len); \
+    buf += _N_ColorMatrix_##x##_len
+
+
+static uint8_t * addBranch(uint8_t *buf, const uint8_t *target, uint32_t condition) {
+    size_t off = (target - buf - 8) >> 2;
+    rsAssert(((off & 0xff000000) == 0) ||
+           ((off & 0xff000000) == 0xff000000));
+
+    uint32_t op = (condition << 28);
+    op |= 0xa << 24;  // branch
+    op |= 0xffffff & off;
+    ((uint32_t *)buf)[0] = op;
+    return buf + 4;
+}
+
+static uint32_t encodeSIMDRegs(uint32_t vd, uint32_t vn, uint32_t vm, bool q, bool sz) {
+    rsAssert(vd < 32);
+    rsAssert(vm < 32);
+    rsAssert(vn < 32);
+
+    uint32_t op = ((vd & 0xf) << 12) | (((vd & 0x10) >> 4) << 22);
+    op |= (vm & 0xf) | (((vm & 0x10) >> 4) << 5);
+    op |= ((vn & 0xf) << 16) | (((vn & 0x10) >> 4) << 7);
+    if (q) op |= 1 << 6;
+    if (sz) op |= 1 << 8;
+    return op;
+}
+
+static uint8_t * addVMLAL_S16(uint8_t *buf, uint32_t dest_q, uint32_t src_d1, uint32_t src_d2, uint32_t src_d2_s) {
+    //vmlal.s16 Q#1, D#1, D#2[#]
+    uint32_t op = 0xf2900240 | encodeSIMDRegs(dest_q << 1, src_d1, src_d2 | (src_d2_s << 3), false, false);
+    ((uint32_t *)buf)[0] = op;
+    return buf + 4;
+}
+
+static uint8_t * addVMULL_S16(uint8_t *buf, uint32_t dest_q, uint32_t src_d1, uint32_t src_d2, uint32_t src_d2_s) {
+    //vmull.s16 Q#1, D#1, D#2[#]
+    uint32_t op = 0xf2900A40 | encodeSIMDRegs(dest_q << 1, src_d1, src_d2 | (src_d2_s << 3), false, false);
+    ((uint32_t *)buf)[0] = op;
+    return buf + 4;
+}
+#endif
+
+
+bool RsdCpuScriptIntrinsicColorMatrix::build(int64_t key) {
+#if defined(ARCH_ARM_HAVE_NEON)
+    mBufSize = 4096;
+    //StopWatch build_time("rs cm: build time");
+    mBuf = (uint8_t *)mmap(0, mBufSize, PROT_READ | PROT_WRITE,
+                                  MAP_PRIVATE | MAP_ANON, -1, 0);
+    if (!mBuf) {
+        return false;
+    }
+
+    uint8_t *buf = mBuf;
+
+    // Add the function prefix
+    // Store the address for the loop return
+    ADD_CHUNK(prefix);
+    uint8_t *buf2 = buf;
+
+    // Load the incoming r,g,b,a as needed
+    switch(key & CM_IN_VEC_SIZE_MASK) {
+    case 3:
+        ADD_CHUNK(load_u8_4);
+        if (key & CM_COPY_ALPHA) {
+            ADD_CHUNK(unpack_u8_3);
+        } else {
+            ADD_CHUNK(unpack_u8_4);
+        }
+        break;
+    case 2:
+        ADD_CHUNK(load_u8_4);
+        ADD_CHUNK(unpack_u8_3);
+        break;
+    case 1:
+        ADD_CHUNK(load_u8_2);
+        ADD_CHUNK(unpack_u8_2);
+        break;
+    case 0:
+        ADD_CHUNK(load_u8_1);
+        ADD_CHUNK(unpack_u8_1);
+        break;
+    }
+
+    // Add multiply and accumulate
+    // use MULL to init the output register,
+    // use MLAL from there
+    bool linit[4] = {false, false, false, false};
+    if (key & (1 << 16)) {
+        buf = addVMULL_S16(buf, 8, 24, 4, 0);
+        linit[0] = true;
+    }
+    if (!(key & CM_DOT_MASK)) {
+        if (key & (1 << 17)) {
+            buf = addVMULL_S16(buf, 9, 24, 4, 1);
+            linit[1] = true;
+        }
+        if (key & (1 << 18)) {
+            buf = addVMULL_S16(buf, 10, 24, 4, 2);
+            linit[2] = true;
+        }
+    }
+    if (key & (1 << 19)) {
+        buf = addVMULL_S16(buf, 11, 24, 4, 3);
+        linit[3] = true;
+    }
+
+    if (key & (1 << 20)) {
+        if (linit[0]) {
+            buf = addVMLAL_S16(buf, 8, 26, 5, 0);
+        } else {
+            buf = addVMULL_S16(buf, 8, 26, 5, 0);
+            linit[0] = true;
+        }
+    }
+    if (!(key & CM_DOT_MASK)) {
+        if (key & (1 << 21)) {
+            if (linit[1]) {
+                buf = addVMLAL_S16(buf, 9, 26, 5, 1);
+            } else {
+                buf = addVMULL_S16(buf, 9, 26, 5, 1);
+                linit[1] = true;
+            }
+        }
+        if (key & (1 << 22)) {
+            if (linit[2]) {
+                buf = addVMLAL_S16(buf, 10, 26, 5, 2);
+            } else {
+                buf = addVMULL_S16(buf, 10, 26, 5, 2);
+                linit[2] = true;
+            }
+        }
+    }
+    if (key & (1 << 23)) {
+        if (linit[3]) {
+            buf = addVMLAL_S16(buf, 11, 26, 5, 3);
+        } else {
+            buf = addVMULL_S16(buf, 11, 26, 5, 3);
+            linit[3] = true;
+        }
+    }
+
+    if (key & (1 << 24)) {
+        if (linit[0]) {
+            buf = addVMLAL_S16(buf, 8, 28, 6, 0);
+        } else {
+            buf = addVMULL_S16(buf, 8, 28, 6, 0);
+            linit[0] = true;
+        }
+    }
+    if (!(key & CM_DOT_MASK)) {
+        if (key & (1 << 25)) {
+            if (linit[1]) {
+                buf = addVMLAL_S16(buf, 9, 28, 6, 1);
+            } else {
+                buf = addVMULL_S16(buf, 9, 28, 6, 1);
+                linit[1] = true;
+            }
+        }
+        if (key & (1 << 26)) {
+            if (linit[2]) {
+                buf = addVMLAL_S16(buf, 10, 28, 6, 2);
+            } else {
+                buf = addVMULL_S16(buf, 10, 28, 6, 2);
+                linit[2] = true;
+            }
+        }
+    }
+    if (key & (1 << 27)) {
+        if (linit[3]) {
+            buf = addVMLAL_S16(buf, 11, 28, 6, 3);
+        } else {
+            buf = addVMULL_S16(buf, 11, 28, 6, 3);
+            linit[3] = true;
+        }
+    }
+
+    if (key & (1 << 28)) {
+        if (linit[0]) {
+            buf = addVMLAL_S16(buf, 8, 30, 7, 0);
+        } else {
+            buf = addVMULL_S16(buf, 8, 30, 7, 0);
+            linit[0] = true;
+        }
+    }
+    if (!(key & CM_DOT_MASK)) {
+        if (key & (1 << 29)) {
+            if (linit[1]) {
+                buf = addVMLAL_S16(buf, 9, 30, 7, 1);
+            } else {
+                buf = addVMULL_S16(buf, 9, 30, 7, 1);
+                linit[1] = true;
+            }
+        }
+        if (key & (1 << 30)) {
+            if (linit[2]) {
+                buf = addVMLAL_S16(buf, 10, 30, 7, 2);
+            } else {
+                buf = addVMULL_S16(buf, 10, 30, 7, 2);
+                linit[2] = true;
+            }
+        }
+    }
+    if (!(key & CM_COPY_ALPHA)) {
+        if (key & (1 << 31)) {
+            if (linit[3]) {
+                buf = addVMLAL_S16(buf, 11, 30, 7, 3);
+            } else {
+                buf = addVMULL_S16(buf, 11, 30, 7, 3);
+                linit[3] = true;
+            }
+        }
+    }
+
+    // If we have a dot product, perform the special pack.
+    if (key & CM_DOT_MASK) {
+        ADD_CHUNK(pack_u8_1);
+        ADD_CHUNK(dot);
+    } else {
+        switch(key & CM_IN_VEC_SIZE_MASK) {
+        case 3:
+            ADD_CHUNK(pack_u8_4);
+            break;
+        case 2:
+            ADD_CHUNK(pack_u8_3);
+            break;
+        case 1:
+            ADD_CHUNK(pack_u8_2);
+            break;
+        case 0:
+            ADD_CHUNK(pack_u8_1);
+            break;
+        }
+    }
+
+    // Write out result
+    switch(key & CM_IN_VEC_SIZE_MASK) {
+    case 3:
+    case 2:
+        ADD_CHUNK(store_u8_4);
+        break;
+    case 1:
+        ADD_CHUNK(store_u8_2);
+        break;
+    case 0:
+        ADD_CHUNK(store_u8_1);
+        break;
+    }
+
+    // Loop, branch, and cleanup
+    ADD_CHUNK(postfix1);
+    buf = addBranch(buf, buf2, 0x01);
+    ADD_CHUNK(postfix2);
+
+    int ret = mprotect(mBuf, mBufSize, PROT_READ | PROT_EXEC);
+    if (ret == -1) {
+        ALOGE("mprotect error %i", ret);
+        return false;
+    }
+
+    cacheflush((long)mBuf, (long)mBuf + mBufSize, 0);
+    return true;
+#else
+    return false;
+#endif
+}
+
+void RsdCpuScriptIntrinsicColorMatrix::setGlobalVar(uint32_t slot, const void *data,
+                                                    size_t dataLength) {
+    switch(slot) {
+    case 0:
+        memcpy (fp, data, dataLength);
+        for(int ct=0; ct < 16; ct++) {
+            ip[ct] = (short)(fp[ct] * 256.f + 0.5f);
+        }
+        break;
+    case 1:
+        memcpy (fpa, data, dataLength);
+        for(int ct=0; ct < 4; ct++) {
+            ipa[ct] = (int)(fpa[ct] * 256.f + 0.5f);
+        }
+        break;
+    default:
+        rsAssert(0);
+        break;
+    }
+
+    mRootPtr = &kernel;
+}
+
 
 static void One(const RsForEachStubParamStruct *p, uchar4 *out,
                 const uchar4 *py, const float* coeff) {
@@ -108,9 +494,9 @@
     *out = convert_uchar4(sum);
 }
 
-void RsdCpuScriptIntrinsicColorMatrix::kernel4x4(const RsForEachStubParamStruct *p,
-                                                 uint32_t xstart, uint32_t xend,
-                                                 uint32_t instep, uint32_t outstep) {
+void RsdCpuScriptIntrinsicColorMatrix::kernel(const RsForEachStubParamStruct *p,
+                                              uint32_t xstart, uint32_t xend,
+                                              uint32_t instep, uint32_t outstep) {
     RsdCpuScriptIntrinsicColorMatrix *cp = (RsdCpuScriptIntrinsicColorMatrix *)p->usr;
     uchar4 *out = (uchar4 *)p->out;
     uchar4 *in = (uchar4 *)p->in;
@@ -118,17 +504,13 @@
     uint32_t x2 = xend;
 
     if(x2 > x1) {
-#if defined(ARCH_ARM_HAVE_VFP)
-        if (gArchUseSIMD) {
-            int32_t len = (x2 - x1) >> 2;
-            if(len > 0) {
-                rsdIntrinsicColorMatrix4x4_K(out, in, cp->ip, len);
-                x1 += len << 2;
-                out += len << 2;
-                in += len << 2;
-            }
+        int32_t len = (x2 - x1) >> 2;
+        if((cp->mOptKernel != NULL) && (len > 0)) {
+            cp->mOptKernel(out, in, cp->ip, len);
+            x1 += len << 2;
+            out += len << 2;
+            in += len << 2;
         }
-#endif
 
         while(x1 != x2) {
             One(p, out++, in++, cp->fp);
@@ -137,83 +519,56 @@
     }
 }
 
-void RsdCpuScriptIntrinsicColorMatrix::kernel3x3(const RsForEachStubParamStruct *p,
-                                                 uint32_t xstart, uint32_t xend,
-                                                 uint32_t instep, uint32_t outstep) {
-    RsdCpuScriptIntrinsicColorMatrix *cp = (RsdCpuScriptIntrinsicColorMatrix *)p->usr;
-    uchar4 *out = (uchar4 *)p->out;
-    uchar4 *in = (uchar4 *)p->in;
-    uint32_t x1 = xstart;
-    uint32_t x2 = xend;
+void RsdCpuScriptIntrinsicColorMatrix::preLaunch(
+        uint32_t slot, const Allocation * ain, Allocation * aout,
+        const void * usr, uint32_t usrLen, const RsScriptCall *sc) {
 
-    if(x2 > x1) {
-#if defined(ARCH_ARM_HAVE_VFP)
-        if (gArchUseSIMD) {
-            int32_t len = (x2 - x1) >> 2;
-            if(len > 0) {
-                rsdIntrinsicColorMatrix3x3_K(out, in, cp->ip, len);
-                x1 += len << 2;
-                out += len << 2;
-                in += len << 2;
-            }
-        }
-#endif
-
-        while(x1 != x2) {
-            One(p, out++, in++, cp->fp);
-            x1++;
+    int64_t key = computeKey(ain->mHal.state.type->getElement(),
+                             aout->mHal.state.type->getElement());
+    if ((mOptKernel == NULL) || (mLastKey != key)) {
+        if (mBuf) munmap(mBuf, mBufSize);
+        mBuf = NULL;
+        mOptKernel = NULL;
+        if (build(key)) {
+            mOptKernel = (void (*)(void *, const void *, const short *, uint32_t)) mBuf;
+            mLastKey = key;
         }
     }
 }
 
-void RsdCpuScriptIntrinsicColorMatrix::kernelDot(const RsForEachStubParamStruct *p,
-                                                 uint32_t xstart, uint32_t xend,
-                                                 uint32_t instep, uint32_t outstep) {
-    RsdCpuScriptIntrinsicColorMatrix *cp = (RsdCpuScriptIntrinsicColorMatrix *)p->usr;
-    uchar4 *out = (uchar4 *)p->out;
-    uchar4 *in = (uchar4 *)p->in;
-    uint32_t x1 = xstart;
-    uint32_t x2 = xend;
+void RsdCpuScriptIntrinsicColorMatrix::postLaunch(
+        uint32_t slot, const Allocation * ain, Allocation * aout,
+        const void * usr, uint32_t usrLen, const RsScriptCall *sc) {
 
-    if(x2 > x1) {
-#if defined(ARCH_ARM_HAVE_VFP)
-        if (gArchUseSIMD) {
-            int32_t len = (x2 - x1) >> 2;
-            if(len > 0) {
-                rsdIntrinsicColorMatrixDot_K(out, in, cp->ip, len);
-                x1 += len << 2;
-                out += len << 2;
-                in += len << 2;
-            }
-        }
-#endif
-
-        while(x1 != x2) {
-            One(p, out++, in++, cp->fp);
-            x1++;
-        }
-    }
 }
 
-
 RsdCpuScriptIntrinsicColorMatrix::RsdCpuScriptIntrinsicColorMatrix(
             RsdCpuReferenceImpl *ctx, const Script *s, const Element *e)
             : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_COLOR_MATRIX) {
 
+    mLastKey = 0;
+    mBuf = NULL;
+    mBufSize = 0;
+    mOptKernel = NULL;
     const static float defaultMatrix[] = {
         1.f, 0.f, 0.f, 0.f,
         0.f, 1.f, 0.f, 0.f,
         0.f, 0.f, 1.f, 0.f,
         0.f, 0.f, 0.f, 1.f
     };
+    const static float defaultAdd[] = {0.f, 0.f, 0.f, 0.f};
     setGlobalVar(0, defaultMatrix, sizeof(defaultMatrix));
+    setGlobalVar(1, defaultAdd, sizeof(defaultAdd));
 }
 
 RsdCpuScriptIntrinsicColorMatrix::~RsdCpuScriptIntrinsicColorMatrix() {
+    if (mBuf) munmap(mBuf, mBufSize);
+    mBuf = NULL;
+    mOptKernel = NULL;
 }
 
 void RsdCpuScriptIntrinsicColorMatrix::populateScript(Script *s) {
-    s->mHal.info.exportedVariableCount = 1;
+    s->mHal.info.exportedVariableCount = 2;
 }
 
 RsdCpuScriptImpl * rsdIntrinsic_ColorMatrix(RsdCpuReferenceImpl *ctx,
diff --git a/cpu_ref/rsCpuIntrinsicConvolve3x3.cpp b/cpu_ref/rsCpuIntrinsicConvolve3x3.cpp
index 82ae1a4..5862428 100644
--- a/cpu_ref/rsCpuIntrinsicConvolve3x3.cpp
+++ b/cpu_ref/rsCpuIntrinsicConvolve3x3.cpp
@@ -42,9 +42,24 @@
     ObjectBaseRef<const Allocation> mAlloc;
     ObjectBaseRef<const Element> mElement;
 
-    static void kernel(const RsForEachStubParamStruct *p,
-                       uint32_t xstart, uint32_t xend,
-                       uint32_t instep, uint32_t outstep);
+    static void kernelU1(const RsForEachStubParamStruct *p,
+                         uint32_t xstart, uint32_t xend,
+                         uint32_t instep, uint32_t outstep);
+    static void kernelU2(const RsForEachStubParamStruct *p,
+                         uint32_t xstart, uint32_t xend,
+                         uint32_t instep, uint32_t outstep);
+    static void kernelU4(const RsForEachStubParamStruct *p,
+                         uint32_t xstart, uint32_t xend,
+                         uint32_t instep, uint32_t outstep);
+    static void kernelF1(const RsForEachStubParamStruct *p,
+                         uint32_t xstart, uint32_t xend,
+                         uint32_t instep, uint32_t outstep);
+    static void kernelF2(const RsForEachStubParamStruct *p,
+                         uint32_t xstart, uint32_t xend,
+                         uint32_t instep, uint32_t outstep);
+    static void kernelF4(const RsForEachStubParamStruct *p,
+                         uint32_t xstart, uint32_t xend,
+                         uint32_t instep, uint32_t outstep);
 };
 
 }
@@ -61,7 +76,11 @@
     rsAssert(slot == 0);
     memcpy (&mFp, data, dataLength);
     for(int ct=0; ct < 9; ct++) {
-        mIp[ct] = (short)(mFp[ct] * 255.f + 0.5f);
+        if (mFp[ct] >= 0) {
+            mIp[ct] = (short)(mFp[ct] * 256.f + 0.5f);
+        } else {
+            mIp[ct] = (short)(mFp[ct] * 256.f - 0.5f);
+        }
     }
 }
 
@@ -69,9 +88,9 @@
                                           const void *y2, const short *coef, uint32_t count);
 
 
-static void ConvolveOne(const RsForEachStubParamStruct *p, uint32_t x, uchar4 *out,
-                        const uchar4 *py0, const uchar4 *py1, const uchar4 *py2,
-                        const float* coeff) {
+static void ConvolveOneU4(const RsForEachStubParamStruct *p, uint32_t x, uchar4 *out,
+                          const uchar4 *py0, const uchar4 *py1, const uchar4 *py2,
+                          const float* coeff) {
 
     uint32_t x1 = rsMax((int32_t)x-1, 0);
     uint32_t x2 = rsMin((int32_t)x+1, (int32_t)p->dimX-1);
@@ -91,9 +110,82 @@
     *out = o;
 }
 
-void RsdCpuScriptIntrinsicConvolve3x3::kernel(const RsForEachStubParamStruct *p,
-                                              uint32_t xstart, uint32_t xend,
-                                              uint32_t instep, uint32_t outstep) {
+static void ConvolveOneU2(const RsForEachStubParamStruct *p, uint32_t x, uchar2 *out,
+                          const uchar2 *py0, const uchar2 *py1, const uchar2 *py2,
+                          const float* coeff) {
+
+    uint32_t x1 = rsMax((int32_t)x-1, 0);
+    uint32_t x2 = rsMin((int32_t)x+1, (int32_t)p->dimX-1);
+
+    float2 px = convert_float2(py0[x1]) * coeff[0] +
+                convert_float2(py0[x]) * coeff[1] +
+                convert_float2(py0[x2]) * coeff[2] +
+                convert_float2(py1[x1]) * coeff[3] +
+                convert_float2(py1[x]) * coeff[4] +
+                convert_float2(py1[x2]) * coeff[5] +
+                convert_float2(py2[x1]) * coeff[6] +
+                convert_float2(py2[x]) * coeff[7] +
+                convert_float2(py2[x2]) * coeff[8];
+
+    px = clamp(px, 0.f, 255.f);
+    *out = convert_uchar2(px);
+}
+
+static void ConvolveOneU1(const RsForEachStubParamStruct *p, uint32_t x, uchar *out,
+                          const uchar *py0, const uchar *py1, const uchar *py2,
+                          const float* coeff) {
+
+    uint32_t x1 = rsMax((int32_t)x-1, 0);
+    uint32_t x2 = rsMin((int32_t)x+1, (int32_t)p->dimX-1);
+
+    float px = ((float)py0[x1]) * coeff[0] +
+               ((float)py0[x]) * coeff[1] +
+               ((float)py0[x2]) * coeff[2] +
+               ((float)py1[x1]) * coeff[3] +
+               ((float)py1[x]) * coeff[4] +
+               ((float)py1[x2]) * coeff[5] +
+               ((float)py2[x1]) * coeff[6] +
+               ((float)py2[x]) * coeff[7] +
+               ((float)py2[x2]) * coeff[8];
+    *out = clamp(px, 0.f, 255.f);
+}
+
+static void ConvolveOneF4(const RsForEachStubParamStruct *p, uint32_t x, float4 *out,
+                          const float4 *py0, const float4 *py1, const float4 *py2,
+                          const float* coeff) {
+
+    uint32_t x1 = rsMax((int32_t)x-1, 0);
+    uint32_t x2 = rsMin((int32_t)x+1, (int32_t)p->dimX-1);
+    *out = (py0[x1] * coeff[0]) + (py0[x] * coeff[1]) + (py0[x2] * coeff[2]) +
+           (py1[x1] * coeff[3]) + (py1[x] * coeff[4]) + (py1[x2] * coeff[5]) +
+           (py2[x1] * coeff[6]) + (py2[x] * coeff[7]) + (py2[x2] * coeff[8]);
+}
+
+static void ConvolveOneF2(const RsForEachStubParamStruct *p, uint32_t x, float2 *out,
+                          const float2 *py0, const float2 *py1, const float2 *py2,
+                          const float* coeff) {
+
+    uint32_t x1 = rsMax((int32_t)x-1, 0);
+    uint32_t x2 = rsMin((int32_t)x+1, (int32_t)p->dimX-1);
+    *out = (py0[x1] * coeff[0]) + (py0[x] * coeff[1]) + (py0[x2] * coeff[2]) +
+           (py1[x1] * coeff[3]) + (py1[x] * coeff[4]) + (py1[x2] * coeff[5]) +
+           (py2[x1] * coeff[6]) + (py2[x] * coeff[7]) + (py2[x2] * coeff[8]);
+}
+
+static void ConvolveOneF1(const RsForEachStubParamStruct *p, uint32_t x, float *out,
+                          const float *py0, const float *py1, const float *py2,
+                          const float* coeff) {
+
+    uint32_t x1 = rsMax((int32_t)x-1, 0);
+    uint32_t x2 = rsMin((int32_t)x+1, (int32_t)p->dimX-1);
+    *out = (py0[x1] * coeff[0]) + (py0[x] * coeff[1]) + (py0[x2] * coeff[2]) +
+           (py1[x1] * coeff[3]) + (py1[x] * coeff[4]) + (py1[x2] * coeff[5]) +
+           (py2[x1] * coeff[6]) + (py2[x] * coeff[7]) + (py2[x2] * coeff[8]);
+}
+
+void RsdCpuScriptIntrinsicConvolve3x3::kernelU4(const RsForEachStubParamStruct *p,
+                                                uint32_t xstart, uint32_t xend,
+                                                uint32_t instep, uint32_t outstep) {
     RsdCpuScriptIntrinsicConvolve3x3 *cp = (RsdCpuScriptIntrinsicConvolve3x3 *)p->usr;
 
     if (!cp->mAlloc.get()) {
@@ -113,7 +205,7 @@
     uint32_t x1 = xstart;
     uint32_t x2 = xend;
     if(x1 == 0) {
-        ConvolveOne(p, 0, out, py0, py1, py2, cp->mFp);
+        ConvolveOneU4(p, 0, out, py0, py1, py2, cp->mFp);
         x1 ++;
         out++;
     }
@@ -131,7 +223,231 @@
 #endif
 
         while(x1 != x2) {
-            ConvolveOne(p, x1, out, py0, py1, py2, cp->mFp);
+            ConvolveOneU4(p, x1, out, py0, py1, py2, cp->mFp);
+            out++;
+            x1++;
+        }
+    }
+}
+
+void RsdCpuScriptIntrinsicConvolve3x3::kernelU2(const RsForEachStubParamStruct *p,
+                                                uint32_t xstart, uint32_t xend,
+                                                uint32_t instep, uint32_t outstep) {
+    RsdCpuScriptIntrinsicConvolve3x3 *cp = (RsdCpuScriptIntrinsicConvolve3x3 *)p->usr;
+
+    if (!cp->mAlloc.get()) {
+        ALOGE("Convolve3x3 executed without input, skipping");
+        return;
+    }
+    const uchar *pin = (const uchar *)cp->mAlloc->mHal.drvState.lod[0].mallocPtr;
+    const size_t stride = cp->mAlloc->mHal.drvState.lod[0].stride;
+
+    uint32_t y1 = rsMin((int32_t)p->y + 1, (int32_t)(p->dimY-1));
+    uint32_t y2 = rsMax((int32_t)p->y - 1, 0);
+    const uchar2 *py0 = (const uchar2 *)(pin + stride * y2);
+    const uchar2 *py1 = (const uchar2 *)(pin + stride * p->y);
+    const uchar2 *py2 = (const uchar2 *)(pin + stride * y1);
+
+    uchar2 *out = (uchar2 *)p->out;
+    uint32_t x1 = xstart;
+    uint32_t x2 = xend;
+    if(x1 == 0) {
+        ConvolveOneU2(p, 0, out, py0, py1, py2, cp->mFp);
+        x1 ++;
+        out++;
+    }
+
+    if(x2 > x1) {
+#if 0//defined(ARCH_ARM_HAVE_NEON)
+        int32_t len = (x2 - x1 - 1) >> 1;
+        if(len > 0) {
+            rsdIntrinsicConvolve3x3_K(out, &py0[x1-1], &py1[x1-1], &py2[x1-1], cp->mIp, len);
+            x1 += len << 1;
+            out += len << 1;
+        }
+#endif
+
+        while(x1 != x2) {
+            ConvolveOneU2(p, x1, out, py0, py1, py2, cp->mFp);
+            out++;
+            x1++;
+        }
+    }
+}
+
+void RsdCpuScriptIntrinsicConvolve3x3::kernelU1(const RsForEachStubParamStruct *p,
+                                                uint32_t xstart, uint32_t xend,
+                                                uint32_t instep, uint32_t outstep) {
+    RsdCpuScriptIntrinsicConvolve3x3 *cp = (RsdCpuScriptIntrinsicConvolve3x3 *)p->usr;
+
+    if (!cp->mAlloc.get()) {
+        ALOGE("Convolve3x3 executed without input, skipping");
+        return;
+    }
+    const uchar *pin = (const uchar *)cp->mAlloc->mHal.drvState.lod[0].mallocPtr;
+    const size_t stride = cp->mAlloc->mHal.drvState.lod[0].stride;
+
+    uint32_t y1 = rsMin((int32_t)p->y + 1, (int32_t)(p->dimY-1));
+    uint32_t y2 = rsMax((int32_t)p->y - 1, 0);
+    const uchar *py0 = (const uchar *)(pin + stride * y2);
+    const uchar *py1 = (const uchar *)(pin + stride * p->y);
+    const uchar *py2 = (const uchar *)(pin + stride * y1);
+
+    uchar *out = (uchar *)p->out;
+    uint32_t x1 = xstart;
+    uint32_t x2 = xend;
+    if(x1 == 0) {
+        ConvolveOneU1(p, 0, out, py0, py1, py2, cp->mFp);
+        x1 ++;
+        out++;
+    }
+
+    if(x2 > x1) {
+#if 0//defined(ARCH_ARM_HAVE_NEON)
+        int32_t len = (x2 - x1 - 1) >> 1;
+        if(len > 0) {
+            rsdIntrinsicConvolve3x3_K(out, &py0[x1-1], &py1[x1-1], &py2[x1-1], cp->mIp, len);
+            x1 += len << 1;
+            out += len << 1;
+        }
+#endif
+
+        while(x1 != x2) {
+            ConvolveOneU1(p, x1, out, py0, py1, py2, cp->mFp);
+            out++;
+            x1++;
+        }
+    }
+}
+
+void RsdCpuScriptIntrinsicConvolve3x3::kernelF4(const RsForEachStubParamStruct *p,
+                                                uint32_t xstart, uint32_t xend,
+                                                uint32_t instep, uint32_t outstep) {
+    RsdCpuScriptIntrinsicConvolve3x3 *cp = (RsdCpuScriptIntrinsicConvolve3x3 *)p->usr;
+
+    if (!cp->mAlloc.get()) {
+        ALOGE("Convolve3x3 executed without input, skipping");
+        return;
+    }
+    const uchar *pin = (const uchar *)cp->mAlloc->mHal.drvState.lod[0].mallocPtr;
+    const size_t stride = cp->mAlloc->mHal.drvState.lod[0].stride;
+
+    uint32_t y1 = rsMin((int32_t)p->y + 1, (int32_t)(p->dimY-1));
+    uint32_t y2 = rsMax((int32_t)p->y - 1, 0);
+    const float4 *py0 = (const float4 *)(pin + stride * y2);
+    const float4 *py1 = (const float4 *)(pin + stride * p->y);
+    const float4 *py2 = (const float4 *)(pin + stride * y1);
+
+    float4 *out = (float4 *)p->out;
+    uint32_t x1 = xstart;
+    uint32_t x2 = xend;
+    if(x1 == 0) {
+        ConvolveOneF4(p, 0, out, py0, py1, py2, cp->mFp);
+        x1 ++;
+        out++;
+    }
+
+    if(x2 > x1) {
+#if 0//defined(ARCH_ARM_HAVE_NEON)
+        int32_t len = (x2 - x1 - 1) >> 1;
+        if(len > 0) {
+            rsdIntrinsicConvolve3x3_K(out, &py0[x1-1], &py1[x1-1], &py2[x1-1], cp->mIp, len);
+            x1 += len << 1;
+            out += len << 1;
+        }
+#endif
+
+        while(x1 != x2) {
+            ConvolveOneF4(p, x1, out, py0, py1, py2, cp->mFp);
+            out++;
+            x1++;
+        }
+    }
+}
+
+void RsdCpuScriptIntrinsicConvolve3x3::kernelF2(const RsForEachStubParamStruct *p,
+                                                uint32_t xstart, uint32_t xend,
+                                                uint32_t instep, uint32_t outstep) {
+    RsdCpuScriptIntrinsicConvolve3x3 *cp = (RsdCpuScriptIntrinsicConvolve3x3 *)p->usr;
+
+    if (!cp->mAlloc.get()) {
+        ALOGE("Convolve3x3 executed without input, skipping");
+        return;
+    }
+    const uchar *pin = (const uchar *)cp->mAlloc->mHal.drvState.lod[0].mallocPtr;
+    const size_t stride = cp->mAlloc->mHal.drvState.lod[0].stride;
+
+    uint32_t y1 = rsMin((int32_t)p->y + 1, (int32_t)(p->dimY-1));
+    uint32_t y2 = rsMax((int32_t)p->y - 1, 0);
+    const float2 *py0 = (const float2 *)(pin + stride * y2);
+    const float2 *py1 = (const float2 *)(pin + stride * p->y);
+    const float2 *py2 = (const float2 *)(pin + stride * y1);
+
+    float2 *out = (float2 *)p->out;
+    uint32_t x1 = xstart;
+    uint32_t x2 = xend;
+    if(x1 == 0) {
+        ConvolveOneF2(p, 0, out, py0, py1, py2, cp->mFp);
+        x1 ++;
+        out++;
+    }
+
+    if(x2 > x1) {
+#if 0//defined(ARCH_ARM_HAVE_NEON)
+        int32_t len = (x2 - x1 - 1) >> 1;
+        if(len > 0) {
+            rsdIntrinsicConvolve3x3_K(out, &py0[x1-1], &py1[x1-1], &py2[x1-1], cp->mIp, len);
+            x1 += len << 1;
+            out += len << 1;
+        }
+#endif
+
+        while(x1 != x2) {
+            ConvolveOneF2(p, x1, out, py0, py1, py2, cp->mFp);
+            out++;
+            x1++;
+        }
+    }
+}
+void RsdCpuScriptIntrinsicConvolve3x3::kernelF1(const RsForEachStubParamStruct *p,
+                                                uint32_t xstart, uint32_t xend,
+                                                uint32_t instep, uint32_t outstep) {
+    RsdCpuScriptIntrinsicConvolve3x3 *cp = (RsdCpuScriptIntrinsicConvolve3x3 *)p->usr;
+
+    if (!cp->mAlloc.get()) {
+        ALOGE("Convolve3x3 executed without input, skipping");
+        return;
+    }
+    const uchar *pin = (const uchar *)cp->mAlloc->mHal.drvState.lod[0].mallocPtr;
+    const size_t stride = cp->mAlloc->mHal.drvState.lod[0].stride;
+
+    uint32_t y1 = rsMin((int32_t)p->y + 1, (int32_t)(p->dimY-1));
+    uint32_t y2 = rsMax((int32_t)p->y - 1, 0);
+    const float *py0 = (const float *)(pin + stride * y2);
+    const float *py1 = (const float *)(pin + stride * p->y);
+    const float *py2 = (const float *)(pin + stride * y1);
+
+    float *out = (float *)p->out;
+    uint32_t x1 = xstart;
+    uint32_t x2 = xend;
+    if(x1 == 0) {
+        ConvolveOneF1(p, 0, out, py0, py1, py2, cp->mFp);
+        x1 ++;
+        out++;
+    }
+
+    if(x2 > x1) {
+#if 0//defined(ARCH_ARM_HAVE_NEON)
+        int32_t len = (x2 - x1 - 1) >> 1;
+        if(len > 0) {
+            rsdIntrinsicConvolve3x3_K(out, &py0[x1-1], &py1[x1-1], &py2[x1-1], cp->mIp, len);
+            x1 += len << 1;
+            out += len << 1;
+        }
+#endif
+
+        while(x1 != x2) {
+            ConvolveOneF1(p, x1, out, py0, py1, py2, cp->mFp);
             out++;
             x1++;
         }
@@ -142,10 +458,36 @@
             RsdCpuReferenceImpl *ctx, const Script *s, const Element *e)
             : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_CONVOLVE_3x3) {
 
-    mRootPtr = &kernel;
+    if (e->getType() == RS_TYPE_FLOAT_32) {
+        switch(e->getVectorSize()) {
+        case 1:
+            mRootPtr = &kernelF1;
+            break;
+        case 2:
+            mRootPtr = &kernelF2;
+            break;
+        case 3:
+        case 4:
+            mRootPtr = &kernelF4;
+            break;
+        }
+    } else {
+        switch(e->getVectorSize()) {
+        case 1:
+            mRootPtr = &kernelU1;
+            break;
+        case 2:
+            mRootPtr = &kernelU2;
+            break;
+        case 3:
+        case 4:
+            mRootPtr = &kernelU4;
+            break;
+        }
+    }
     for(int ct=0; ct < 9; ct++) {
         mFp[ct] = 1.f / 9.f;
-        mIp[ct] = (short)(mFp[ct] * 255.f + 0.5f);
+        mIp[ct] = (short)(mFp[ct] * 256.f + 0.5f);
     }
 }
 
diff --git a/cpu_ref/rsCpuIntrinsicConvolve5x5.cpp b/cpu_ref/rsCpuIntrinsicConvolve5x5.cpp
index b4932bd..11dda59 100644
--- a/cpu_ref/rsCpuIntrinsicConvolve5x5.cpp
+++ b/cpu_ref/rsCpuIntrinsicConvolve5x5.cpp
@@ -37,14 +37,29 @@
     RsdCpuScriptIntrinsicConvolve5x5(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e);
 
 protected:
-    float fp[28];
-    short ip[28];
+    float mFp[28];
+    short mIp[28];
     ObjectBaseRef<Allocation> alloc;
 
 
-    static void kernel(const RsForEachStubParamStruct *p,
-                       uint32_t xstart, uint32_t xend,
-                       uint32_t instep, uint32_t outstep);
+    static void kernelU1(const RsForEachStubParamStruct *p,
+                         uint32_t xstart, uint32_t xend,
+                         uint32_t instep, uint32_t outstep);
+    static void kernelU2(const RsForEachStubParamStruct *p,
+                         uint32_t xstart, uint32_t xend,
+                         uint32_t instep, uint32_t outstep);
+    static void kernelU4(const RsForEachStubParamStruct *p,
+                         uint32_t xstart, uint32_t xend,
+                         uint32_t instep, uint32_t outstep);
+    static void kernelF1(const RsForEachStubParamStruct *p,
+                         uint32_t xstart, uint32_t xend,
+                         uint32_t instep, uint32_t outstep);
+    static void kernelF2(const RsForEachStubParamStruct *p,
+                         uint32_t xstart, uint32_t xend,
+                         uint32_t instep, uint32_t outstep);
+    static void kernelF4(const RsForEachStubParamStruct *p,
+                         uint32_t xstart, uint32_t xend,
+                         uint32_t instep, uint32_t outstep);
 
 
 };
@@ -60,16 +75,20 @@
 void RsdCpuScriptIntrinsicConvolve5x5::setGlobalVar(uint32_t slot,
                                                     const void *data, size_t dataLength) {
     rsAssert(slot == 0);
-    memcpy (&fp, data, dataLength);
+    memcpy (&mFp, data, dataLength);
     for(int ct=0; ct < 25; ct++) {
-        ip[ct] = (short)(fp[ct] * 255.f);
+        if (mFp[ct] >= 0) {
+            mIp[ct] = (short)(mFp[ct] * 256.f + 0.5f);
+        } else {
+            mIp[ct] = (short)(mFp[ct] * 256.f - 0.5f);
+        }
     }
 }
 
 
-static void One(const RsForEachStubParamStruct *p, uint32_t x, uchar4 *out,
-                const uchar4 *py0, const uchar4 *py1, const uchar4 *py2, const uchar4 *py3, const uchar4 *py4,
-                const float* coeff) {
+static void OneU4(const RsForEachStubParamStruct *p, uint32_t x, uchar4 *out,
+                  const uchar4 *py0, const uchar4 *py1, const uchar4 *py2, const uchar4 *py3, const uchar4 *py4,
+                  const float* coeff) {
 
     uint32_t x0 = rsMax((int32_t)x-2, 0);
     uint32_t x1 = rsMax((int32_t)x-1, 0);
@@ -106,23 +125,230 @@
                 convert_float4(py4[x2]) * coeff[22] +
                 convert_float4(py4[x3]) * coeff[23] +
                 convert_float4(py4[x4]) * coeff[24];
-
     px = clamp(px, 0.f, 255.f);
-    uchar4 o = {(uchar)px.x, (uchar)px.y, (uchar)px.z, (uchar)px.w};
-    //if ((out[0].r != o.r) || (out[0].y != o.y) || (out[0].z != o.z) || (out[0].w != o.w)) {
-        //ALOGE("x %i  %i,%i,%i,%i  %i,%i,%i,%i", x, o.x, o.y, o.z, o.w, out[0].x, out[0].y, out[0].z, out[0].w);
-    //}
-    //o.w = 0xff;
-    out->xyzw = o.xyzw;
+    *out = convert_uchar4(px);
 }
 
+static void OneU2(const RsForEachStubParamStruct *p, uint32_t x, uchar2 *out,
+                  const uchar2 *py0, const uchar2 *py1, const uchar2 *py2, const uchar2 *py3, const uchar2 *py4,
+                  const float* coeff) {
+
+    uint32_t x0 = rsMax((int32_t)x-2, 0);
+    uint32_t x1 = rsMax((int32_t)x-1, 0);
+    uint32_t x2 = x;
+    uint32_t x3 = rsMin((int32_t)x+1, (int32_t)(p->dimX-1));
+    uint32_t x4 = rsMin((int32_t)x+2, (int32_t)(p->dimX-1));
+
+    float2 px = convert_float2(py0[x0]) * coeff[0] +
+                convert_float2(py0[x1]) * coeff[1] +
+                convert_float2(py0[x2]) * coeff[2] +
+                convert_float2(py0[x3]) * coeff[3] +
+                convert_float2(py0[x4]) * coeff[4] +
+
+                convert_float2(py1[x0]) * coeff[5] +
+                convert_float2(py1[x1]) * coeff[6] +
+                convert_float2(py1[x2]) * coeff[7] +
+                convert_float2(py1[x3]) * coeff[8] +
+                convert_float2(py1[x4]) * coeff[9] +
+
+                convert_float2(py2[x0]) * coeff[10] +
+                convert_float2(py2[x1]) * coeff[11] +
+                convert_float2(py2[x2]) * coeff[12] +
+                convert_float2(py2[x3]) * coeff[13] +
+                convert_float2(py2[x4]) * coeff[14] +
+
+                convert_float2(py3[x0]) * coeff[15] +
+                convert_float2(py3[x1]) * coeff[16] +
+                convert_float2(py3[x2]) * coeff[17] +
+                convert_float2(py3[x3]) * coeff[18] +
+                convert_float2(py3[x4]) * coeff[19] +
+
+                convert_float2(py4[x0]) * coeff[20] +
+                convert_float2(py4[x1]) * coeff[21] +
+                convert_float2(py4[x2]) * coeff[22] +
+                convert_float2(py4[x3]) * coeff[23] +
+                convert_float2(py4[x4]) * coeff[24];
+    px = clamp(px, 0.f, 255.f);
+    *out = convert_uchar2(px);
+}
+
+static void OneU1(const RsForEachStubParamStruct *p, uint32_t x, uchar *out,
+                  const uchar *py0, const uchar *py1, const uchar *py2, const uchar *py3, const uchar *py4,
+                  const float* coeff) {
+
+    uint32_t x0 = rsMax((int32_t)x-2, 0);
+    uint32_t x1 = rsMax((int32_t)x-1, 0);
+    uint32_t x2 = x;
+    uint32_t x3 = rsMin((int32_t)x+1, (int32_t)(p->dimX-1));
+    uint32_t x4 = rsMin((int32_t)x+2, (int32_t)(p->dimX-1));
+
+    float px = (float)(py0[x0]) * coeff[0] +
+               (float)(py0[x1]) * coeff[1] +
+               (float)(py0[x2]) * coeff[2] +
+               (float)(py0[x3]) * coeff[3] +
+               (float)(py0[x4]) * coeff[4] +
+
+               (float)(py1[x0]) * coeff[5] +
+               (float)(py1[x1]) * coeff[6] +
+               (float)(py1[x2]) * coeff[7] +
+               (float)(py1[x3]) * coeff[8] +
+               (float)(py1[x4]) * coeff[9] +
+
+               (float)(py2[x0]) * coeff[10] +
+               (float)(py2[x1]) * coeff[11] +
+               (float)(py2[x2]) * coeff[12] +
+               (float)(py2[x3]) * coeff[13] +
+               (float)(py2[x4]) * coeff[14] +
+
+               (float)(py3[x0]) * coeff[15] +
+               (float)(py3[x1]) * coeff[16] +
+               (float)(py3[x2]) * coeff[17] +
+               (float)(py3[x3]) * coeff[18] +
+               (float)(py3[x4]) * coeff[19] +
+
+               (float)(py4[x0]) * coeff[20] +
+               (float)(py4[x1]) * coeff[21] +
+               (float)(py4[x2]) * coeff[22] +
+               (float)(py4[x3]) * coeff[23] +
+               (float)(py4[x4]) * coeff[24];
+    px = clamp(px, 0.f, 255.f);
+    *out = px;
+}
+
+static void OneF4(const RsForEachStubParamStruct *p, uint32_t x, float4 *out,
+                  const float4 *py0, const float4 *py1, const float4 *py2, const float4 *py3, const float4 *py4,
+                  const float* coeff) {
+
+    uint32_t x0 = rsMax((int32_t)x-2, 0);
+    uint32_t x1 = rsMax((int32_t)x-1, 0);
+    uint32_t x2 = x;
+    uint32_t x3 = rsMin((int32_t)x+1, (int32_t)(p->dimX-1));
+    uint32_t x4 = rsMin((int32_t)x+2, (int32_t)(p->dimX-1));
+
+    float4 px = py0[x0] * coeff[0] +
+                py0[x1] * coeff[1] +
+                py0[x2] * coeff[2] +
+                py0[x3] * coeff[3] +
+                py0[x4] * coeff[4] +
+
+                py1[x0] * coeff[5] +
+                py1[x1] * coeff[6] +
+                py1[x2] * coeff[7] +
+                py1[x3] * coeff[8] +
+                py1[x4] * coeff[9] +
+
+                py2[x0] * coeff[10] +
+                py2[x1] * coeff[11] +
+                py2[x2] * coeff[12] +
+                py2[x3] * coeff[13] +
+                py2[x4] * coeff[14] +
+
+                py3[x0] * coeff[15] +
+                py3[x1] * coeff[16] +
+                py3[x2] * coeff[17] +
+                py3[x3] * coeff[18] +
+                py3[x4] * coeff[19] +
+
+                py4[x0] * coeff[20] +
+                py4[x1] * coeff[21] +
+                py4[x2] * coeff[22] +
+                py4[x3] * coeff[23] +
+                py4[x4] * coeff[24];
+    *out = px;
+}
+
+static void OneF2(const RsForEachStubParamStruct *p, uint32_t x, float2 *out,
+                  const float2 *py0, const float2 *py1, const float2 *py2, const float2 *py3, const float2 *py4,
+                  const float* coeff) {
+
+    uint32_t x0 = rsMax((int32_t)x-2, 0);
+    uint32_t x1 = rsMax((int32_t)x-1, 0);
+    uint32_t x2 = x;
+    uint32_t x3 = rsMin((int32_t)x+1, (int32_t)(p->dimX-1));
+    uint32_t x4 = rsMin((int32_t)x+2, (int32_t)(p->dimX-1));
+
+    float2 px = py0[x0] * coeff[0] +
+                py0[x1] * coeff[1] +
+                py0[x2] * coeff[2] +
+                py0[x3] * coeff[3] +
+                py0[x4] * coeff[4] +
+
+                py1[x0] * coeff[5] +
+                py1[x1] * coeff[6] +
+                py1[x2] * coeff[7] +
+                py1[x3] * coeff[8] +
+                py1[x4] * coeff[9] +
+
+                py2[x0] * coeff[10] +
+                py2[x1] * coeff[11] +
+                py2[x2] * coeff[12] +
+                py2[x3] * coeff[13] +
+                py2[x4] * coeff[14] +
+
+                py3[x0] * coeff[15] +
+                py3[x1] * coeff[16] +
+                py3[x2] * coeff[17] +
+                py3[x3] * coeff[18] +
+                py3[x4] * coeff[19] +
+
+                py4[x0] * coeff[20] +
+                py4[x1] * coeff[21] +
+                py4[x2] * coeff[22] +
+                py4[x3] * coeff[23] +
+                py4[x4] * coeff[24];
+    *out = px;
+}
+
+static void OneF1(const RsForEachStubParamStruct *p, uint32_t x, float *out,
+                  const float *py0, const float *py1, const float *py2, const float *py3, const float *py4,
+                  const float* coeff) {
+
+    uint32_t x0 = rsMax((int32_t)x-2, 0);
+    uint32_t x1 = rsMax((int32_t)x-1, 0);
+    uint32_t x2 = x;
+    uint32_t x3 = rsMin((int32_t)x+1, (int32_t)(p->dimX-1));
+    uint32_t x4 = rsMin((int32_t)x+2, (int32_t)(p->dimX-1));
+
+    float px = py0[x0] * coeff[0] +
+               py0[x1] * coeff[1] +
+               py0[x2] * coeff[2] +
+               py0[x3] * coeff[3] +
+               py0[x4] * coeff[4] +
+
+               py1[x0] * coeff[5] +
+               py1[x1] * coeff[6] +
+               py1[x2] * coeff[7] +
+               py1[x3] * coeff[8] +
+               py1[x4] * coeff[9] +
+
+               py2[x0] * coeff[10] +
+               py2[x1] * coeff[11] +
+               py2[x2] * coeff[12] +
+               py2[x3] * coeff[13] +
+               py2[x4] * coeff[14] +
+
+               py3[x0] * coeff[15] +
+               py3[x1] * coeff[16] +
+               py3[x2] * coeff[17] +
+               py3[x3] * coeff[18] +
+               py3[x4] * coeff[19] +
+
+               py4[x0] * coeff[20] +
+               py4[x1] * coeff[21] +
+               py4[x2] * coeff[22] +
+               py4[x3] * coeff[23] +
+               py4[x4] * coeff[24];
+    *out = px;
+}
+
+
 extern "C" void rsdIntrinsicConvolve5x5_K(void *dst, const void *y0, const void *y1,
                                           const void *y2, const void *y3, const void *y4,
                                           const short *coef, uint32_t count);
 
-void RsdCpuScriptIntrinsicConvolve5x5::kernel(const RsForEachStubParamStruct *p,
-                                              uint32_t xstart, uint32_t xend,
-                                              uint32_t instep, uint32_t outstep) {
+void RsdCpuScriptIntrinsicConvolve5x5::kernelU4(const RsForEachStubParamStruct *p,
+                                                uint32_t xstart, uint32_t xend,
+                                                uint32_t instep, uint32_t outstep) {
     RsdCpuScriptIntrinsicConvolve5x5 *cp = (RsdCpuScriptIntrinsicConvolve5x5 *)p->usr;
     if (!cp->alloc.get()) {
         ALOGE("Convolve5x5 executed without input, skipping");
@@ -148,7 +374,7 @@
     uint32_t x2 = xend;
 
     while((x1 < x2) && (x1 < 2)) {
-        One(p, x1, out, py0, py1, py2, py3, py4, cp->fp);
+        OneU4(p, x1, out, py0, py1, py2, py3, py4, cp->mFp);
         out++;
         x1++;
     }
@@ -156,6 +382,55 @@
 #if defined(ARCH_ARM_HAVE_VFP)
     if(gArchUseSIMD && ((x1 + 3) < x2)) {
         uint32_t len = (x2 - x1 - 3) >> 1;
+        rsdIntrinsicConvolve5x5_K(out, py0, py1, py2, py3, py4, cp->mIp, len);
+        out += len << 1;
+        x1 += len << 1;
+    }
+#endif
+
+    while(x1 < x2) {
+        OneU4(p, x1, out, py0, py1, py2, py3, py4, cp->mFp);
+        out++;
+        x1++;
+    }
+}
+
+void RsdCpuScriptIntrinsicConvolve5x5::kernelU2(const RsForEachStubParamStruct *p,
+                                                uint32_t xstart, uint32_t xend,
+                                                uint32_t instep, uint32_t outstep) {
+    RsdCpuScriptIntrinsicConvolve5x5 *cp = (RsdCpuScriptIntrinsicConvolve5x5 *)p->usr;
+    if (!cp->alloc.get()) {
+        ALOGE("Convolve5x5 executed without input, skipping");
+        return;
+    }
+    const uchar *pin = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr;
+    const size_t stride = cp->alloc->mHal.drvState.lod[0].stride;
+
+    uint32_t y0 = rsMax((int32_t)p->y-2, 0);
+    uint32_t y1 = rsMax((int32_t)p->y-1, 0);
+    uint32_t y2 = p->y;
+    uint32_t y3 = rsMin((int32_t)p->y+1, (int32_t)(p->dimY-1));
+    uint32_t y4 = rsMin((int32_t)p->y+2, (int32_t)(p->dimY-1));
+
+    const uchar2 *py0 = (const uchar2 *)(pin + stride * y0);
+    const uchar2 *py1 = (const uchar2 *)(pin + stride * y1);
+    const uchar2 *py2 = (const uchar2 *)(pin + stride * y2);
+    const uchar2 *py3 = (const uchar2 *)(pin + stride * y3);
+    const uchar2 *py4 = (const uchar2 *)(pin + stride * y4);
+
+    uchar2 *out = (uchar2 *)p->out;
+    uint32_t x1 = xstart;
+    uint32_t x2 = xend;
+
+    while((x1 < x2) && (x1 < 2)) {
+        OneU2(p, x1, out, py0, py1, py2, py3, py4, cp->mFp);
+        out++;
+        x1++;
+    }
+
+#if 0//defined(ARCH_ARM_HAVE_NEON)
+    if((x1 + 3) < x2) {
+        uint32_t len = (x2 - x1 - 3) >> 1;
         rsdIntrinsicConvolve5x5_K(out, py0, py1, py2, py3, py4, cp->ip, len);
         out += len << 1;
         x1 += len << 1;
@@ -163,21 +438,242 @@
 #endif
 
     while(x1 < x2) {
-        One(p, x1, out, py0, py1, py2, py3, py4, cp->fp);
+        OneU2(p, x1, out, py0, py1, py2, py3, py4, cp->mFp);
         out++;
         x1++;
     }
 }
 
+void RsdCpuScriptIntrinsicConvolve5x5::kernelU1(const RsForEachStubParamStruct *p,
+                                                uint32_t xstart, uint32_t xend,
+                                                uint32_t instep, uint32_t outstep) {
+    RsdCpuScriptIntrinsicConvolve5x5 *cp = (RsdCpuScriptIntrinsicConvolve5x5 *)p->usr;
+    if (!cp->alloc.get()) {
+        ALOGE("Convolve5x5 executed without input, skipping");
+        return;
+    }
+    const uchar *pin = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr;
+    const size_t stride = cp->alloc->mHal.drvState.lod[0].stride;
+
+    uint32_t y0 = rsMax((int32_t)p->y-2, 0);
+    uint32_t y1 = rsMax((int32_t)p->y-1, 0);
+    uint32_t y2 = p->y;
+    uint32_t y3 = rsMin((int32_t)p->y+1, (int32_t)(p->dimY-1));
+    uint32_t y4 = rsMin((int32_t)p->y+2, (int32_t)(p->dimY-1));
+
+    const uchar *py0 = (const uchar *)(pin + stride * y0);
+    const uchar *py1 = (const uchar *)(pin + stride * y1);
+    const uchar *py2 = (const uchar *)(pin + stride * y2);
+    const uchar *py3 = (const uchar *)(pin + stride * y3);
+    const uchar *py4 = (const uchar *)(pin + stride * y4);
+
+    uchar *out = (uchar *)p->out;
+    uint32_t x1 = xstart;
+    uint32_t x2 = xend;
+
+    while((x1 < x2) && (x1 < 2)) {
+        OneU1(p, x1, out, py0, py1, py2, py3, py4, cp->mFp);
+        out++;
+        x1++;
+    }
+
+#if 0//defined(ARCH_ARM_HAVE_NEON)
+    if((x1 + 3) < x2) {
+        uint32_t len = (x2 - x1 - 3) >> 1;
+        rsdIntrinsicConvolve5x5_K(out, py0, py1, py2, py3, py4, cp->ip, len);
+        out += len << 1;
+        x1 += len << 1;
+    }
+#endif
+
+    while(x1 < x2) {
+        OneU1(p, x1, out, py0, py1, py2, py3, py4, cp->mFp);
+        out++;
+        x1++;
+    }
+}
+
+void RsdCpuScriptIntrinsicConvolve5x5::kernelF4(const RsForEachStubParamStruct *p,
+                                                uint32_t xstart, uint32_t xend,
+                                                uint32_t instep, uint32_t outstep) {
+    RsdCpuScriptIntrinsicConvolve5x5 *cp = (RsdCpuScriptIntrinsicConvolve5x5 *)p->usr;
+    if (!cp->alloc.get()) {
+        ALOGE("Convolve5x5 executed without input, skipping");
+        return;
+    }
+    const uchar *pin = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr;
+    const size_t stride = cp->alloc->mHal.drvState.lod[0].stride;
+
+    uint32_t y0 = rsMax((int32_t)p->y-2, 0);
+    uint32_t y1 = rsMax((int32_t)p->y-1, 0);
+    uint32_t y2 = p->y;
+    uint32_t y3 = rsMin((int32_t)p->y+1, (int32_t)(p->dimY-1));
+    uint32_t y4 = rsMin((int32_t)p->y+2, (int32_t)(p->dimY-1));
+
+    const float4 *py0 = (const float4 *)(pin + stride * y0);
+    const float4 *py1 = (const float4 *)(pin + stride * y1);
+    const float4 *py2 = (const float4 *)(pin + stride * y2);
+    const float4 *py3 = (const float4 *)(pin + stride * y3);
+    const float4 *py4 = (const float4 *)(pin + stride * y4);
+
+    float4 *out = (float4 *)p->out;
+    uint32_t x1 = xstart;
+    uint32_t x2 = xend;
+
+    while((x1 < x2) && (x1 < 2)) {
+        OneF4(p, x1, out, py0, py1, py2, py3, py4, cp->mFp);
+        out++;
+        x1++;
+    }
+
+#if 0//defined(ARCH_ARM_HAVE_NEON)
+    if((x1 + 3) < x2) {
+        uint32_t len = (x2 - x1 - 3) >> 1;
+        rsdIntrinsicConvolve5x5_K(out, py0, py1, py2, py3, py4, cp->ip, len);
+        out += len << 1;
+        x1 += len << 1;
+    }
+#endif
+
+    while(x1 < x2) {
+        OneF4(p, x1, out, py0, py1, py2, py3, py4, cp->mFp);
+        out++;
+        x1++;
+    }
+}
+
+void RsdCpuScriptIntrinsicConvolve5x5::kernelF2(const RsForEachStubParamStruct *p,
+                                                uint32_t xstart, uint32_t xend,
+                                                uint32_t instep, uint32_t outstep) {
+    RsdCpuScriptIntrinsicConvolve5x5 *cp = (RsdCpuScriptIntrinsicConvolve5x5 *)p->usr;
+    if (!cp->alloc.get()) {
+        ALOGE("Convolve5x5 executed without input, skipping");
+        return;
+    }
+    const uchar *pin = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr;
+    const size_t stride = cp->alloc->mHal.drvState.lod[0].stride;
+
+    uint32_t y0 = rsMax((int32_t)p->y-2, 0);
+    uint32_t y1 = rsMax((int32_t)p->y-1, 0);
+    uint32_t y2 = p->y;
+    uint32_t y3 = rsMin((int32_t)p->y+1, (int32_t)(p->dimY-1));
+    uint32_t y4 = rsMin((int32_t)p->y+2, (int32_t)(p->dimY-1));
+
+    const float2 *py0 = (const float2 *)(pin + stride * y0);
+    const float2 *py1 = (const float2 *)(pin + stride * y1);
+    const float2 *py2 = (const float2 *)(pin + stride * y2);
+    const float2 *py3 = (const float2 *)(pin + stride * y3);
+    const float2 *py4 = (const float2 *)(pin + stride * y4);
+
+    float2 *out = (float2 *)p->out;
+    uint32_t x1 = xstart;
+    uint32_t x2 = xend;
+
+    while((x1 < x2) && (x1 < 2)) {
+        OneF2(p, x1, out, py0, py1, py2, py3, py4, cp->mFp);
+        out++;
+        x1++;
+    }
+
+#if 0//defined(ARCH_ARM_HAVE_NEON)
+    if((x1 + 3) < x2) {
+        uint32_t len = (x2 - x1 - 3) >> 1;
+        rsdIntrinsicConvolve5x5_K(out, py0, py1, py2, py3, py4, cp->ip, len);
+        out += len << 1;
+        x1 += len << 1;
+    }
+#endif
+
+    while(x1 < x2) {
+        OneF2(p, x1, out, py0, py1, py2, py3, py4, cp->mFp);
+        out++;
+        x1++;
+    }
+}
+
+void RsdCpuScriptIntrinsicConvolve5x5::kernelF1(const RsForEachStubParamStruct *p,
+                                                uint32_t xstart, uint32_t xend,
+                                                uint32_t instep, uint32_t outstep) {
+    RsdCpuScriptIntrinsicConvolve5x5 *cp = (RsdCpuScriptIntrinsicConvolve5x5 *)p->usr;
+    if (!cp->alloc.get()) {
+        ALOGE("Convolve5x5 executed without input, skipping");
+        return;
+    }
+    const uchar *pin = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr;
+    const size_t stride = cp->alloc->mHal.drvState.lod[0].stride;
+
+    uint32_t y0 = rsMax((int32_t)p->y-2, 0);
+    uint32_t y1 = rsMax((int32_t)p->y-1, 0);
+    uint32_t y2 = p->y;
+    uint32_t y3 = rsMin((int32_t)p->y+1, (int32_t)(p->dimY-1));
+    uint32_t y4 = rsMin((int32_t)p->y+2, (int32_t)(p->dimY-1));
+
+    const float *py0 = (const float *)(pin + stride * y0);
+    const float *py1 = (const float *)(pin + stride * y1);
+    const float *py2 = (const float *)(pin + stride * y2);
+    const float *py3 = (const float *)(pin + stride * y3);
+    const float *py4 = (const float *)(pin + stride * y4);
+
+    float *out = (float *)p->out;
+    uint32_t x1 = xstart;
+    uint32_t x2 = xend;
+
+    while((x1 < x2) && (x1 < 2)) {
+        OneF1(p, x1, out, py0, py1, py2, py3, py4, cp->mFp);
+        out++;
+        x1++;
+    }
+
+#if 0//defined(ARCH_ARM_HAVE_NEON)
+    if((x1 + 3) < x2) {
+        uint32_t len = (x2 - x1 - 3) >> 1;
+        rsdIntrinsicConvolve5x5_K(out, py0, py1, py2, py3, py4, cp->ip, len);
+        out += len << 1;
+        x1 += len << 1;
+    }
+#endif
+
+    while(x1 < x2) {
+        OneF1(p, x1, out, py0, py1, py2, py3, py4, cp->mFp);
+        out++;
+        x1++;
+    }
+}
 
 RsdCpuScriptIntrinsicConvolve5x5::RsdCpuScriptIntrinsicConvolve5x5(
             RsdCpuReferenceImpl *ctx, const Script *s, const Element *e)
             : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_CONVOLVE_5x5) {
 
-    mRootPtr = &kernel;
+    if (e->getType() == RS_TYPE_FLOAT_32) {
+        switch(e->getVectorSize()) {
+        case 1:
+            mRootPtr = &kernelF1;
+            break;
+        case 2:
+            mRootPtr = &kernelF2;
+            break;
+        case 3:
+        case 4:
+            mRootPtr = &kernelF4;
+            break;
+        }
+    } else {
+        switch(e->getVectorSize()) {
+        case 1:
+            mRootPtr = &kernelU1;
+            break;
+        case 2:
+            mRootPtr = &kernelU2;
+            break;
+        case 3:
+        case 4:
+            mRootPtr = &kernelU4;
+            break;
+        }
+    }
     for(int ct=0; ct < 25; ct++) {
-        fp[ct] = 1.f / 25.f;
-        ip[ct] = (short)(fp[ct] * 255.f);
+        mFp[ct] = 1.f / 25.f;
+        mIp[ct] = (short)(mFp[ct] * 256.f);
     }
 }
 
diff --git a/cpu_ref/rsCpuIntrinsicHistogram.cpp b/cpu_ref/rsCpuIntrinsicHistogram.cpp
new file mode 100644
index 0000000..e2ac102
--- /dev/null
+++ b/cpu_ref/rsCpuIntrinsicHistogram.cpp
@@ -0,0 +1,245 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "rsCpuIntrinsic.h"
+#include "rsCpuIntrinsicInlines.h"
+
+using namespace android;
+using namespace android::renderscript;
+
+namespace android {
+namespace renderscript {
+
+
+class RsdCpuScriptIntrinsicHistogram : public RsdCpuScriptIntrinsic {
+public:
+    virtual void populateScript(Script *);
+    virtual void invokeFreeChildren();
+
+    virtual void setGlobalVar(uint32_t slot, const void *data, size_t dataLength);
+    virtual void setGlobalObj(uint32_t slot, ObjectBase *data);
+
+    virtual ~RsdCpuScriptIntrinsicHistogram();
+    RsdCpuScriptIntrinsicHistogram(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e);
+
+protected:
+    void preLaunch(uint32_t slot, const Allocation * ain,
+                   Allocation * aout, const void * usr,
+                   uint32_t usrLen, const RsScriptCall *sc);
+    void postLaunch(uint32_t slot, const Allocation * ain,
+                    Allocation * aout, const void * usr,
+                    uint32_t usrLen, const RsScriptCall *sc);
+
+
+    float mDot[4];
+    int mDotI[4];
+    int *mSums;
+    ObjectBaseRef<Allocation> mAllocOut;
+
+    static void kernelP1U4(const RsForEachStubParamStruct *p,
+                          uint32_t xstart, uint32_t xend,
+                          uint32_t instep, uint32_t outstep);
+    static void kernelP1U3(const RsForEachStubParamStruct *p,
+                          uint32_t xstart, uint32_t xend,
+                          uint32_t instep, uint32_t outstep);
+    static void kernelP1U2(const RsForEachStubParamStruct *p,
+                          uint32_t xstart, uint32_t xend,
+                          uint32_t instep, uint32_t outstep);
+    static void kernelP1L(const RsForEachStubParamStruct *p,
+                          uint32_t xstart, uint32_t xend,
+                          uint32_t instep, uint32_t outstep);
+    static void kernelP1U1(const RsForEachStubParamStruct *p,
+                          uint32_t xstart, uint32_t xend,
+                          uint32_t instep, uint32_t outstep);
+
+};
+
+}
+}
+
+void RsdCpuScriptIntrinsicHistogram::setGlobalObj(uint32_t slot, ObjectBase *data) {
+    rsAssert(slot == 1);
+    mAllocOut.set(static_cast<Allocation *>(data));
+}
+
+void RsdCpuScriptIntrinsicHistogram::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
+    rsAssert(slot == 0);
+    rsAssert(dataLength == 16);
+    memcpy(mDot, data, 16);
+    mDotI[0] = (int)((mDot[0] * 256.f) + 0.5f);
+    mDotI[1] = (int)((mDot[1] * 256.f) + 0.5f);
+    mDotI[2] = (int)((mDot[2] * 256.f) + 0.5f);
+    mDotI[3] = (int)((mDot[3] * 256.f) + 0.5f);
+}
+
+
+
+void RsdCpuScriptIntrinsicHistogram::preLaunch(uint32_t slot, const Allocation * ain,
+                                      Allocation * aout, const void * usr,
+                                      uint32_t usrLen, const RsScriptCall *sc) {
+
+    const uint32_t threads = mCtx->getThreadCount();
+    const uint32_t vSize = mAllocOut->getType()->getElement()->getVectorSize();
+
+    switch (slot) {
+    case 0:
+        switch(mAllocOut->getType()->getElement()->getVectorSize()) {
+        case 1:
+            mRootPtr = &kernelP1U1;
+            break;
+        case 2:
+            mRootPtr = &kernelP1U2;
+            break;
+        case 3:
+            mRootPtr = &kernelP1U3;
+            break;
+        case 4:
+            mRootPtr = &kernelP1U4;
+            break;
+        }
+        break;
+    case 1:
+        mRootPtr = &kernelP1L;
+        break;
+    }
+    memset(mSums, 0, 256 * 4 * threads * vSize);
+}
+
+void RsdCpuScriptIntrinsicHistogram::postLaunch(uint32_t slot, const Allocation * ain,
+                                       Allocation * aout, const void * usr,
+                                       uint32_t usrLen, const RsScriptCall *sc) {
+
+    unsigned int *o = (unsigned int *)mAllocOut->mHal.drvState.lod[0].mallocPtr;
+    uint32_t threads = mCtx->getThreadCount();
+    uint32_t vSize = mAllocOut->getType()->getElement()->getVectorSize();
+
+    for (uint32_t ct=0; ct < (256 * vSize); ct++) {
+        o[ct] = mSums[ct];
+        for (uint32_t t=1; t < threads; t++) {
+            o[ct] += mSums[ct + 256 * vSize];
+        }
+    }
+}
+
+void RsdCpuScriptIntrinsicHistogram::kernelP1U4(const RsForEachStubParamStruct *p,
+                                                uint32_t xstart, uint32_t xend,
+                                                uint32_t instep, uint32_t outstep) {
+
+    RsdCpuScriptIntrinsicHistogram *cp = (RsdCpuScriptIntrinsicHistogram *)p->usr;
+    uchar *in = (uchar *)p->in;
+    int * sums = &cp->mSums[256 * 4 * p->lid];
+
+    for (uint32_t x = xstart; x < xend; x++) {
+        sums[(in[0] << 2)    ] ++;
+        sums[(in[1] << 2) + 1] ++;
+        sums[(in[2] << 2) + 2] ++;
+        sums[(in[3] << 2) + 3] ++;
+        in += 4;
+    }
+}
+
+void RsdCpuScriptIntrinsicHistogram::kernelP1U3(const RsForEachStubParamStruct *p,
+                                                uint32_t xstart, uint32_t xend,
+                                                uint32_t instep, uint32_t outstep) {
+
+    RsdCpuScriptIntrinsicHistogram *cp = (RsdCpuScriptIntrinsicHistogram *)p->usr;
+    uchar *in = (uchar *)p->in;
+    int * sums = &cp->mSums[256 * 4 * p->lid];
+
+    for (uint32_t x = xstart; x < xend; x++) {
+        sums[(in[0] << 2)    ] ++;
+        sums[(in[1] << 2) + 1] ++;
+        sums[(in[2] << 2) + 2] ++;
+        in += 4;
+    }
+}
+
+void RsdCpuScriptIntrinsicHistogram::kernelP1U2(const RsForEachStubParamStruct *p,
+                                                uint32_t xstart, uint32_t xend,
+                                                uint32_t instep, uint32_t outstep) {
+
+    RsdCpuScriptIntrinsicHistogram *cp = (RsdCpuScriptIntrinsicHistogram *)p->usr;
+    uchar *in = (uchar *)p->in;
+    int * sums = &cp->mSums[256 * 2 * p->lid];
+
+    for (uint32_t x = xstart; x < xend; x++) {
+        sums[(in[0] << 2)    ] ++;
+        sums[(in[1] << 2) + 1] ++;
+        in += 2;
+    }
+}
+
+void RsdCpuScriptIntrinsicHistogram::kernelP1L(const RsForEachStubParamStruct *p,
+                                               uint32_t xstart, uint32_t xend,
+                                               uint32_t instep, uint32_t outstep) {
+
+    RsdCpuScriptIntrinsicHistogram *cp = (RsdCpuScriptIntrinsicHistogram *)p->usr;
+    uchar *in = (uchar *)p->in;
+    int * sums = &cp->mSums[256 * p->lid];
+
+    for (uint32_t x = xstart; x < xend; x++) {
+        int t = (cp->mDotI[0] * in[0]) +
+                (cp->mDotI[1] * in[1]) +
+                (cp->mDotI[2] * in[2]) +
+                (cp->mDotI[3] * in[3]);
+        sums[t >> 8] ++;
+        in += 4;
+    }
+}
+
+void RsdCpuScriptIntrinsicHistogram::kernelP1U1(const RsForEachStubParamStruct *p,
+                                                uint32_t xstart, uint32_t xend,
+                                                uint32_t instep, uint32_t outstep) {
+
+}
+
+
+RsdCpuScriptIntrinsicHistogram::RsdCpuScriptIntrinsicHistogram(RsdCpuReferenceImpl *ctx,
+                                                     const Script *s, const Element *e)
+            : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_BLUR) {
+
+    mRootPtr = NULL;
+    mSums = new int[256 * 4 * mCtx->getThreadCount()];
+    mDot[0] = 0.299f;
+    mDot[1] = 0.587f;
+    mDot[2] = 0.114f;
+    mDot[3] = 0;
+    mDotI[0] = (int)((mDot[0] * 256.f) + 0.5f);
+    mDotI[1] = (int)((mDot[1] * 256.f) + 0.5f);
+    mDotI[2] = (int)((mDot[2] * 256.f) + 0.5f);
+    mDotI[3] = (int)((mDot[3] * 256.f) + 0.5f);
+}
+
+RsdCpuScriptIntrinsicHistogram::~RsdCpuScriptIntrinsicHistogram() {
+    if (mSums) {
+        delete []mSums;
+    }
+}
+
+void RsdCpuScriptIntrinsicHistogram::populateScript(Script *s) {
+    s->mHal.info.exportedVariableCount = 2;
+}
+
+void RsdCpuScriptIntrinsicHistogram::invokeFreeChildren() {
+}
+
+
+RsdCpuScriptImpl * rsdIntrinsic_Histogram(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e) {
+
+    return new RsdCpuScriptIntrinsicHistogram(ctx, s, e);
+}
+
+
diff --git a/cpu_ref/rsCpuIntrinsicInlines.h b/cpu_ref/rsCpuIntrinsicInlines.h
index d6644ca..fb5003a 100644
--- a/cpu_ref/rsCpuIntrinsicInlines.h
+++ b/cpu_ref/rsCpuIntrinsicInlines.h
@@ -52,55 +52,36 @@
 
 };
 
-static inline int4 convert_int4(uchar4 i) {
-    int4 f4 = {i.x, i.y, i.z, i.w};
-    return f4;
-}
-
-static inline uint4 convert_uint4(uchar4 i) {
-    uint4 f4 = {i.x, i.y, i.z, i.w};
-    return f4;
-}
-
-static inline int4 convert_int4(float4 i) {
-    int4 f4 = {i.x, i.y, i.z, i.w};
-    return f4;
-}
-
-static inline short4 convert_short4(uchar4 i) {
-    short4 f4 = {i.x, i.y, i.z, i.w};
-    return f4;
-}
-
-static inline float4 convert_float4(uchar4 i) {
-    float4 f4 = {i.x, i.y, i.z, i.w};
-    return f4;
-}
-
-static inline float4 convert_float4(int4 i) {
-    float4 f4 = {i.x, i.y, i.z, i.w};
-    return f4;
-}
-
-static inline uchar4 convert_uchar4(short4 i) {
-    uchar4 f4 = {(uchar)i.x, (uchar)i.y, (uchar)i.z, (uchar)i.w};
-    return f4;
-}
-
-static inline uchar4 convert_uchar4(int4 i) {
-    uchar4 f4 = {(uchar)i.x, (uchar)i.y, (uchar)i.z, (uchar)i.w};
-    return f4;
-}
-
-static inline uchar4 convert_uchar4(uint4 i) {
-    uchar4 f4 = {(uchar)i.x, (uchar)i.y, (uchar)i.z, (uchar)i.w};
-    return f4;
-}
-
-static inline uchar4 convert_uchar4(float4 i) {
-    uchar4 f4 = {(uchar)i.x, (uchar)i.y, (uchar)i.z, (uchar)i.w};
-    return f4;
-}
+#define CVT_FUNC_2(typeout, typein)                             \
+static inline typeout##2 __attribute__((const, overloadable))   \
+    convert_##typeout##2(typein##2 i) {                         \
+        typeout##2 f = {(typeout)i.x, (typeout)i.y};            \
+        return f;                                               \
+    }                                                           \
+static inline typeout##3 __attribute__((const, overloadable))   \
+    convert_##typeout##3(typein##3 i) {                         \
+        typeout##3 f = {(typeout)i.x, (typeout)i.y, (typeout)i.z}; \
+        return f;                                               \
+    }                                                           \
+static inline typeout##4 __attribute__((const, overloadable))   \
+    convert_##typeout##4(typein##4 i) {                         \
+        typeout##4 f = {(typeout)i.x, (typeout)i.y, (typeout)i.z, (typeout)i.w}; \
+        return f;                                               \
+    }
+#define CVT_FUNC(type)  CVT_FUNC_2(type, uchar)     \
+                        CVT_FUNC_2(type, char)      \
+                        CVT_FUNC_2(type, ushort)    \
+                        CVT_FUNC_2(type, short)     \
+                        CVT_FUNC_2(type, uint)      \
+                        CVT_FUNC_2(type, int)       \
+                        CVT_FUNC_2(type, float)
+CVT_FUNC(char)
+CVT_FUNC(uchar)
+CVT_FUNC(short)
+CVT_FUNC(ushort)
+CVT_FUNC(int)
+CVT_FUNC(uint)
+CVT_FUNC(float)
 
 
 static inline int4 clamp(int4 amount, int low, int high) {
@@ -121,4 +102,25 @@
     return r;
 }
 
+static inline int2 clamp(int2 amount, int low, int high) {
+    int2 r;
+    r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);
+    r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);
+    return r;
+}
+
+static inline float2 clamp(float2 amount, float low, float high) {
+    float2 r;
+    r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);
+    r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);
+    return r;
+}
+
+static inline int clamp(int amount, int low, int high) {
+    return amount < low ? low : (amount > high ? high : amount);
+}
+
+static inline float clamp(float amount, float low, float high) {
+    return amount < low ? low : (amount > high ? high : amount);
+}
 
diff --git a/cpu_ref/rsCpuIntrinsics_neon.S b/cpu_ref/rsCpuIntrinsics_neon.S
index c8dc9bf..52fd565 100644
--- a/cpu_ref/rsCpuIntrinsics_neon.S
+++ b/cpu_ref/rsCpuIntrinsics_neon.S
@@ -104,173 +104,6 @@
         bx              lr
 END(rsdIntrinsicConvolve3x3_K)
 
-/*
-        r0 = dst
-        r1 = src
-        r2 = matrix
-        r3 = length
-*/
-ENTRY(rsdIntrinsicColorMatrix4x4_K)
-        stmfd           sp!, {r4, lr}
-        vpush           {q4-q7}
-
-        vld1.16 {q2}, [r2]!
-        vld1.16 {q3}, [r2]!
-
-1:
-        vld4.8 {d0[0],d1[0],d2[0],d3[0]}, [r1]!
-        vld4.8 {d0[1],d1[1],d2[1],d3[1]}, [r1]!
-        vld4.8 {d0[2],d1[2],d2[2],d3[2]}, [r1]!
-        vld4.8 {d0[3],d1[3],d2[3],d3[3]}, [r1]!
-
-        vmovl.u8 q12, d0  /* R */
-        vmovl.u8 q13, d1  /* G */
-        vmovl.u8 q14, d2  /* B */
-        vmovl.u8 q15, d3  /* A */
-
-        vmull.s16 q8,  d24, d4[0]
-        vmull.s16 q9,  d24, d4[1]
-        vmull.s16 q10, d24, d4[2]
-        vmull.s16 q11, d24, d4[3]
-
-        vmlal.s16 q8,  d26, d5[0]
-        vmlal.s16 q9,  d26, d5[1]
-        vmlal.s16 q10, d26, d5[2]
-        vmlal.s16 q11, d26, d5[3]
-
-        vmlal.s16 q8,  d28, d6[0]
-        vmlal.s16 q9,  d28, d6[1]
-        vmlal.s16 q10, d28, d6[2]
-        vmlal.s16 q11, d28, d6[3]
-
-        vmlal.s16 q8,  d30, d7[0]
-        vmlal.s16 q9,  d30, d7[1]
-        vmlal.s16 q10, d30, d7[2]
-        vmlal.s16 q11, d30, d7[3]
-
-        vshrn.i32 d24, q8, #8
-        vshrn.i32 d26, q9, #8
-        vshrn.i32 d28, q10, #8
-        vshrn.i32 d30, q11, #8
-
-        vqmovun.s16 d0, q12
-        vqmovun.s16 d1, q13
-        vqmovun.s16 d2, q14
-        vqmovun.s16 d3, q15
-
-        vst4.8 {d0[0],d1[0],d2[0],d3[0]}, [r0]!
-        vst4.8 {d0[1],d1[1],d2[1],d3[1]}, [r0]!
-        vst4.8 {d0[2],d1[2],d2[2],d3[2]}, [r0]!
-        vst4.8 {d0[3],d1[3],d2[3],d3[3]}, [r0]!
-
-        subs r3, r3, #1
-        bne 1b
-
-        vpop            {q4-q7}
-        ldmfd           sp!, {r4, lr}
-        bx              lr
-END(rsdIntrinsicColorMatrix4x4_K)
-
-/*
-        r0 = dst
-        r1 = src
-        r2 = matrix
-        r3 = length
-*/
-ENTRY(rsdIntrinsicColorMatrix3x3_K)
-        stmfd           sp!, {r4, lr}
-        vpush           {q4-q7}
-
-        vld1.16 {q2}, [r2]!
-        vld1.16 {q3}, [r2]!
-
-1:
-        vld4.8 {d0[0],d1[0],d2[0],d3[0]}, [r1]!
-        vld4.8 {d0[1],d1[1],d2[1],d3[1]}, [r1]!
-        vld4.8 {d0[2],d1[2],d2[2],d3[2]}, [r1]!
-        vld4.8 {d0[3],d1[3],d2[3],d3[3]}, [r1]!
-
-        vmovl.u8 q12, d0
-        vmovl.u8 q13, d1
-        vmovl.u8 q14, d2
-
-        vmull.s16 q8,  d24, d4[0]
-        vmull.s16 q9,  d24, d4[1]
-        vmull.s16 q10, d24, d4[2]
-
-        vmlal.s16 q8,  d26, d5[0]
-        vmlal.s16 q9,  d26, d5[1]
-        vmlal.s16 q10, d26, d5[2]
-
-        vmlal.s16 q8,  d28, d6[0]
-        vmlal.s16 q9,  d28, d6[1]
-        vmlal.s16 q10, d28, d6[2]
-
-        vshrn.i32 d24, q8, #8
-        vshrn.i32 d26, q9, #8
-        vshrn.i32 d28, q10, #8
-
-        vqmovun.s16 d0, q12
-        vqmovun.s16 d1, q13
-        vqmovun.s16 d2, q14
-
-        vst4.8 {d0[0],d1[0],d2[0],d3[0]}, [r0]!
-        vst4.8 {d0[1],d1[1],d2[1],d3[1]}, [r0]!
-        vst4.8 {d0[2],d1[2],d2[2],d3[2]}, [r0]!
-        vst4.8 {d0[3],d1[3],d2[3],d3[3]}, [r0]!
-
-        subs r3, r3, #1
-        bne 1b
-
-        vpop            {q4-q7}
-        ldmfd           sp!, {r4, lr}
-        bx              lr
-END(rsdIntrinsicColorMatrix3x3_K)
-
-/*
-        r0 = dst
-        r1 = src
-        r2 = matrix
-        r3 = length
-*/
-ENTRY(rsdIntrinsicColorMatrixDot_K)
-        stmfd           sp!, {r4, lr}
-        vpush           {q4-q7}
-
-        vld1.16 {q2}, [r2]!
-        vld1.16 {q3}, [r2]!
-
-1:
-        vld4.8 {d0[0],d1[0],d2[0],d3[0]}, [r1]!
-        vld4.8 {d0[1],d1[1],d2[1],d3[1]}, [r1]!
-        vld4.8 {d0[2],d1[2],d2[2],d3[2]}, [r1]!
-        vld4.8 {d0[3],d1[3],d2[3],d3[3]}, [r1]!
-
-        vmovl.u8 q12, d0
-        vmovl.u8 q13, d1
-        vmovl.u8 q14, d2
-
-        vmull.s16 q8,  d24, d4[0]
-        vmlal.s16 q8,  d26, d5[0]
-        vmlal.s16 q8,  d28, d6[0]
-        vshrn.i32 d24, q8, #8
-        vqmovun.s16 d0, q12
-        vmov.u8 d1, d0
-        vmov.u8 d2, d0
-
-        vst4.8 {d0[0],d1[0],d2[0],d3[0]}, [r0]!
-        vst4.8 {d0[1],d1[1],d2[1],d3[1]}, [r0]!
-        vst4.8 {d0[2],d1[2],d2[2],d3[2]}, [r0]!
-        vst4.8 {d0[3],d1[3],d2[3],d3[3]}, [r0]!
-
-        subs r3, r3, #1
-        bne 1b
-
-        vpop            {q4-q7}
-        ldmfd           sp!, {r4, lr}
-        bx              lr
-END(rsdIntrinsicColorMatrixDot_K)
-
 
 /*
 static void OneVF(float4 *out, const uchar *ptrIn, int iStride,
diff --git a/cpu_ref/rsCpuIntrinsics_neon_ColorMatrix.S b/cpu_ref/rsCpuIntrinsics_neon_ColorMatrix.S
new file mode 100644
index 0000000..ce8c033
--- /dev/null
+++ b/cpu_ref/rsCpuIntrinsics_neon_ColorMatrix.S
@@ -0,0 +1,145 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <machine/cpu-features.h>
+#include <machine/asm.h>
+
+#define SNIP_START(x) \
+	.globl x; x:
+
+#define SNIP_END(x) \
+	.globl x##_end; x##_end: \
+	.globl x##_len; x##_len: \
+    .word x##_end-x
+
+SNIP_START(_N_ColorMatrix_prefix)
+    stmfd           sp!, {r4, lr}
+    vpush           {q4-q7}
+    vld1.16 {q2}, [r2]!
+    vld1.16 {q3}, [r2]!
+    vld1.16 {d8}, [r2]!
+SNIP_END(_N_ColorMatrix_prefix)
+
+SNIP_START(_N_ColorMatrix_postfix1)
+    subs r3, r3, #1
+    #bne 1b
+SNIP_END(_N_ColorMatrix_postfix1)
+
+SNIP_START(_N_ColorMatrix_postfix2)
+
+    #mov r0, #0
+    #ldr r0, [r0]
+
+    vpop            {q4-q7}
+    ldmfd           sp!, {r4, lr}
+    bx              lr
+SNIP_END(_N_ColorMatrix_postfix2)
+
+SNIP_START(_N_ColorMatrix_load_u8_4)
+    vld4.8 {d0[0],d1[0],d2[0],d3[0]}, [r1]!
+    vld4.8 {d0[1],d1[1],d2[1],d3[1]}, [r1]!
+    vld4.8 {d0[2],d1[2],d2[2],d3[2]}, [r1]!
+    vld4.8 {d0[3],d1[3],d2[3],d3[3]}, [r1]!
+SNIP_END(_N_ColorMatrix_load_u8_4)
+
+SNIP_START(_N_ColorMatrix_load_u8_2)
+    vld2.8 {d0[0],d1[0]}, [r1]!
+    vld2.8 {d0[1],d1[1]}, [r1]!
+    vld2.8 {d0[2],d1[2]}, [r1]!
+    vld2.8 {d0[3],d1[3]}, [r1]!
+SNIP_END(_N_ColorMatrix_load_u8_2)
+
+SNIP_START(_N_ColorMatrix_load_u8_1)
+    vld1.32 {d0}, [r1]!
+SNIP_END(_N_ColorMatrix_load_u8_1)
+
+SNIP_START(_N_ColorMatrix_store_u8_4)
+    vst4.8 {d0[0],d1[0],d2[0],d3[0]}, [r0]!
+    vst4.8 {d0[1],d1[1],d2[1],d3[1]}, [r0]!
+    vst4.8 {d0[2],d1[2],d2[2],d3[2]}, [r0]!
+    vst4.8 {d0[3],d1[3],d2[3],d3[3]}, [r0]!
+SNIP_END(_N_ColorMatrix_store_u8_4)
+
+SNIP_START(_N_ColorMatrix_store_u8_2)
+    vst2.8 {d0[0],d1[0]}, [r0]!
+    vst2.8 {d0[1],d1[1]}, [r0]!
+    vst2.8 {d0[2],d1[2]}, [r0]!
+    vst2.8 {d0[3],d1[3]}, [r0]!
+SNIP_END(_N_ColorMatrix_store_u8_2)
+
+SNIP_START(_N_ColorMatrix_store_u8_1)
+    vst1.32 {d0}, [r0]!
+SNIP_END(_N_ColorMatrix_store_u8_1)
+
+SNIP_START(_N_ColorMatrix_unpack_u8_4)
+    vmovl.u8 q12, d0  /* R */
+    vmovl.u8 q13, d1  /* G */
+    vmovl.u8 q14, d2  /* B */
+    vmovl.u8 q15, d3  /* A */
+SNIP_END(_N_ColorMatrix_unpack_u8_4)
+
+SNIP_START(_N_ColorMatrix_unpack_u8_3)
+    vmovl.u8 q12, d0  /* R */
+    vmovl.u8 q13, d1  /* G */
+    vmovl.u8 q14, d2  /* B */
+SNIP_END(_N_ColorMatrix_unpack_u8_3)
+
+SNIP_START(_N_ColorMatrix_unpack_u8_2)
+    vmovl.u8 q12, d0  /* R */
+    vmovl.u8 q13, d1  /* G */
+SNIP_END(_N_ColorMatrix_unpack_u8_2)
+
+SNIP_START(_N_ColorMatrix_unpack_u8_1)
+    vmovl.u8 q12, d0  /* R */
+SNIP_END(_N_ColorMatrix_unpack_u8_1)
+
+SNIP_START(_N_ColorMatrix_pack_u8_4)
+    vshrn.i32 d24, q8, #8
+    vshrn.i32 d26, q9, #8
+    vshrn.i32 d28, q10, #8
+    vshrn.i32 d30, q11, #8
+    vqmovun.s16 d0, q12
+    vqmovun.s16 d1, q13
+    vqmovun.s16 d2, q14
+    vqmovun.s16 d3, q15
+SNIP_END(_N_ColorMatrix_pack_u8_4)
+
+SNIP_START(_N_ColorMatrix_pack_u8_3)
+    vshrn.i32 d24, q8, #8
+    vshrn.i32 d26, q9, #8
+    vshrn.i32 d28, q10, #8
+    vqmovun.s16 d0, q12
+    vqmovun.s16 d1, q13
+    vqmovun.s16 d2, q14
+SNIP_END(_N_ColorMatrix_pack_u8_3)
+
+SNIP_START(_N_ColorMatrix_pack_u8_2)
+    vshrn.i32 d24, q8, #8
+    vshrn.i32 d26, q9, #8
+    vqmovun.s16 d0, q12
+    vqmovun.s16 d1, q13
+SNIP_END(_N_ColorMatrix_pack_u8_2)
+
+SNIP_START(_N_ColorMatrix_pack_u8_1)
+    vshrn.i32 d24, q8, #8
+    vqmovun.s16 d0, q12
+SNIP_END(_N_ColorMatrix_pack_u8_1)
+
+SNIP_START(_N_ColorMatrix_dot)
+    vmov.u8 d1, d0
+    vmov.u8 d2, d0
+SNIP_END(_N_ColorMatrix_dot)
+
diff --git a/cpu_ref/rsCpuScript.cpp b/cpu_ref/rsCpuScript.cpp
index 22e2f0e..0d34d96 100644
--- a/cpu_ref/rsCpuScript.cpp
+++ b/cpu_ref/rsCpuScript.cpp
@@ -14,23 +14,252 @@
  * limitations under the License.
  */
 
-
-
 #include "rsCpuCore.h"
-
 #include "rsCpuScript.h"
 
 #ifdef RS_COMPATIBILITY_LIB
+    #include <set>
+    #include <string>
     #include <dlfcn.h>
     #include <stdio.h>
+    #include <stdlib.h>
     #include <string.h>
+    #include <unistd.h>
 #else
     #include <bcc/BCCContext.h>
     #include <bcc/Renderscript/RSCompilerDriver.h>
     #include <bcc/Renderscript/RSExecutable.h>
     #include <bcc/Renderscript/RSInfo.h>
+    #include <bcinfo/MetadataExtractor.h>
+    #include <cutils/properties.h>
+
+    #include <sys/types.h>
+    #include <sys/wait.h>
+    #include <unistd.h>
 #endif
 
+namespace {
+#ifdef RS_COMPATIBILITY_LIB
+
+// Create a len length string containing random characters from [A-Za-z0-9].
+static std::string getRandomString(size_t len) {
+    char buf[len + 1];
+    for (size_t i = 0; i < len; i++) {
+        uint32_t r = arc4random() & 0xffff;
+        r %= 62;
+        if (r < 26) {
+            // lowercase
+            buf[i] = 'a' + r;
+        } else if (r < 52) {
+            // uppercase
+            buf[i] = 'A' + (r - 26);
+        } else {
+            // Use a number
+            buf[i] = '0' + (r - 52);
+        }
+    }
+    buf[len] = '\0';
+    return std::string(buf);
+}
+
+// Attempt to load the shared library from origName, but then fall back to
+// creating the symlinked shared library if necessary (to ensure instancing).
+// This function returns the dlopen()-ed handle if successful.
+static void *loadSOHelper(const char *origName, const char *cacheDir,
+                          const char *resName) {
+    // Keep track of which .so libraries have been loaded. Once a library is
+    // in the set (per-process granularity), we must instead make a symlink to
+    // the original shared object (randomly named .so file) and load that one
+    // instead. If we don't do this, we end up aliasing global data between
+    // the various Script instances (which are supposed to be completely
+    // independent).
+    static std::set<std::string> LoadedLibraries;
+
+    void *loaded = NULL;
+
+    // Skip everything if we don't even have the original library available.
+    if (access(origName, F_OK) != 0) {
+        return NULL;
+    }
+
+    // Common path is that we have not loaded this Script/library before.
+    if (LoadedLibraries.find(origName) == LoadedLibraries.end()) {
+        loaded = dlopen(origName, RTLD_NOW | RTLD_LOCAL);
+        if (loaded) {
+            LoadedLibraries.insert(origName);
+        }
+        return loaded;
+    }
+
+    // Construct an appropriately randomized filename for the symlink.
+    std::string newName(cacheDir);
+    newName.append("/com.android.renderscript.cache/librs.");
+    newName.append(resName);
+    newName.append("#");
+    newName.append(getRandomString(6));  // 62^6 potential filename variants.
+    newName.append(".so");
+
+    int r = symlink(origName, newName.c_str());
+    if (r != 0) {
+        ALOGE("Could not create symlink %s -> %s", newName.c_str(), origName);
+        return NULL;
+    }
+    loaded = dlopen(newName.c_str(), RTLD_NOW | RTLD_LOCAL);
+    r = unlink(newName.c_str());
+    if (r != 0) {
+        ALOGE("Could not unlink symlink %s", newName.c_str());
+    }
+    if (loaded) {
+        LoadedLibraries.insert(newName.c_str());
+    }
+
+    return loaded;
+}
+
+// Load the shared library referred to by cacheDir and resName. If we have
+// already loaded this library, we instead create a new symlink (in the
+// cache dir) and then load that. We then immediately destroy the symlink.
+// This is required behavior to implement script instancing for the support
+// library, since shared objects are loaded and de-duped by name only.
+static void *loadSharedLibrary(const char *cacheDir, const char *resName) {
+    void *loaded = NULL;
+    //arc4random_stir();
+#ifndef RS_SERVER
+    std::string scriptSOName(cacheDir);
+    size_t cutPos = scriptSOName.rfind("cache");
+    if (cutPos != std::string::npos) {
+        scriptSOName.erase(cutPos);
+    } else {
+        ALOGE("Found peculiar cacheDir (missing \"cache\"): %s", cacheDir);
+    }
+    scriptSOName.append("/lib/librs.");
+#else
+    std::string scriptSOName("lib");
+#endif
+    scriptSOName.append(resName);
+    scriptSOName.append(".so");
+
+    // We should check if we can load the library from the standard app
+    // location for shared libraries first.
+    loaded = loadSOHelper(scriptSOName.c_str(), cacheDir, resName);
+
+    if (loaded == NULL) {
+        ALOGE("Unable to open shared library (%s): %s",
+              scriptSOName.c_str(), dlerror());
+
+        // One final attempt to find the library in "/system/lib".
+        // We do this to allow bundled applications to use the compatibility
+        // library fallback path. Those applications don't have a private
+        // library path, so they need to install to the system directly.
+        // Note that this is really just a testing path.
+        android::String8 scriptSONameSystem("/system/lib/librs.");
+        scriptSONameSystem.append(resName);
+        scriptSONameSystem.append(".so");
+        loaded = loadSOHelper(scriptSONameSystem.c_str(), cacheDir,
+                              resName);
+        if (loaded == NULL) {
+            ALOGE("Unable to open system shared library (%s): %s",
+                  scriptSONameSystem.c_str(), dlerror());
+        }
+    }
+
+    return loaded;
+}
+
+
+#else
+static bool is_force_recompile() {
+#ifdef RS_SERVER
+  return false;
+#else
+  char buf[PROPERTY_VALUE_MAX];
+
+  // Re-compile if floating point precision has been overridden.
+  property_get("debug.rs.precision", buf, "");
+  if (buf[0] != '\0') {
+    return true;
+  }
+
+  // Re-compile if debug.rs.forcerecompile is set.
+  property_get("debug.rs.forcerecompile", buf, "0");
+  if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
+    return true;
+  } else {
+    return false;
+  }
+#endif  // RS_SERVER
+}
+
+//#define EXTERNAL_BCC_COMPILER 1
+#ifdef EXTERNAL_BCC_COMPILER
+const static char *BCC_EXE_PATH = "/system/bin/bcc";
+
+static bool compileBitcode(const char *cacheDir,
+                           const char *resName,
+                           const char *bitcode,
+                           size_t bitcodeSize,
+                           const char *core_lib) {
+    rsAssert(cacheDir && resName && bitcode && bitcodeSize && core_lib);
+
+    android::String8 bcFilename(cacheDir);
+    bcFilename.append("/");
+    bcFilename.append(resName);
+    bcFilename.append(".bc");
+    FILE *bcfile = fopen(bcFilename.string(), "w");
+    if (!bcfile) {
+        ALOGE("Could not write to %s", bcFilename.string());
+        return false;
+    }
+    size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile);
+    fclose(bcfile);
+    if (nwritten != bitcodeSize) {
+        ALOGE("Could not write %zu bytes to %s", bitcodeSize,
+              bcFilename.string());
+        return false;
+    }
+
+    pid_t pid = fork();
+    switch (pid) {
+    case -1: {  // Error occurred (we attempt no recovery)
+        ALOGE("Couldn't fork for bcc compiler execution");
+        return false;
+    }
+    case 0: {  // Child process
+        // Execute the bcc compiler.
+        execl(BCC_EXE_PATH,
+              BCC_EXE_PATH,
+              "-o", resName,
+              "-output_path", cacheDir,
+              "-bclib", core_lib,
+              bcFilename.string(),
+              (char *) NULL);
+        ALOGE("execl() failed: %s", strerror(errno));
+        abort();
+        return false;
+    }
+    default: {  // Parent process (actual driver)
+        // Wait on child process to finish compiling the source.
+        int status = 0;
+        pid_t w = waitpid(pid, &status, 0);
+        if (w == -1) {
+            ALOGE("Could not wait for bcc compiler");
+            return false;
+        }
+
+        if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
+            return true;
+        }
+
+        ALOGE("bcc compiler terminated unexpectedly");
+        return false;
+    }
+    }
+}
+#endif  // EXTERNAL_BCC_COMPILER
+
+#endif  // !defined(RS_COMPATIBILITY_LIB)
+}  // namespace
+
 namespace android {
 namespace renderscript {
 
@@ -112,7 +341,7 @@
     mCtx->lockMutex();
 
 #ifndef RS_COMPATIBILITY_LIB
-    bcc::RSExecutable *exec;
+    bcc::RSExecutable *exec = NULL;
 
     mCompilerContext = NULL;
     mCompilerDriver = NULL;
@@ -135,7 +364,43 @@
     mCompilerDriver->setRSRuntimeLookupFunction(lookupRuntimeStub);
     mCompilerDriver->setRSRuntimeLookupContext(this);
 
-    const char *core_lib = NULL;
+    // Run any compiler setup functions we have been provided with.
+    RSSetupCompilerCallback setupCompilerCallback =
+            mCtx->getSetupCompilerCallback();
+    if (setupCompilerCallback != NULL) {
+        setupCompilerCallback(mCompilerDriver);
+    }
+
+    const char *core_lib = bcc::RSInfo::LibCLCorePath;
+
+    bcinfo::MetadataExtractor ME((const char *) bitcode, bitcodeSize);
+    if (!ME.extract()) {
+        ALOGE("Could not extract metadata from bitcode");
+        return false;
+    }
+
+    enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
+    switch (prec) {
+    case bcinfo::RS_FP_Imprecise:
+    case bcinfo::RS_FP_Relaxed:
+#if defined(ARCH_ARM_HAVE_NEON)
+        // NEON-capable devices can use an accelerated math library for all
+        // reduced precision scripts.
+        core_lib = bcc::RSInfo::LibCLCoreNEONPath;
+#endif
+        break;
+    case bcinfo::RS_FP_Full:
+        break;
+    default:
+        ALOGE("Unknown precision for bitcode");
+        return false;
+    }
+
+#if defined(ARCH_X86_HAVE_SSE2)
+    // SSE2- or above capable devices will use an optimized library.
+    core_lib = bcc::RSInfo::LibCLCoreX86Path;
+#endif
+
     RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
     if (selectRTCallback != NULL) {
         core_lib = selectRTCallback((const char *)bitcode, bitcodeSize);
@@ -145,10 +410,29 @@
         // Use the libclcore_debug.bc instead of the default library.
         core_lib = bcc::RSInfo::LibCLCoreDebugPath;
         mCompilerDriver->setDebugContext(true);
+        // Skip the cache lookup
+    } else if (!is_force_recompile()) {
+        // Attempt to just load the script from cache first if we can.
+        exec = mCompilerDriver->loadScript(cacheDir, resName,
+                                           (const char *)bitcode, bitcodeSize);
     }
-    exec = mCompilerDriver->build(*mCompilerContext, cacheDir, resName,
-                                  (const char *)bitcode, bitcodeSize, core_lib,
-                                  mCtx->getLinkRuntimeCallback());
+
+    if (exec == NULL) {
+#ifdef EXTERNAL_BCC_COMPILER
+        bool built = compileBitcode(cacheDir, resName, (const char *)bitcode,
+                                    bitcodeSize, core_lib);
+#else
+        bool built = mCompilerDriver->build(*mCompilerContext, cacheDir,
+                                            resName, (const char *)bitcode,
+                                            bitcodeSize, core_lib,
+                                            mCtx->getLinkRuntimeCallback());
+#endif  // EXTERNAL_BCC_COMPILER
+        if (built) {
+            exec = mCompilerDriver->loadScript(cacheDir, resName,
+                                               (const char *)bitcode,
+                                               bitcodeSize);
+        }
+    }
 
     if (exec == NULL) {
         ALOGE("bcc: FAILS to prepare executable for '%s'", resName);
@@ -179,38 +463,7 @@
 
 #else
 
-#ifndef RS_SERVER
-    String8 scriptSOName(cacheDir);
-    scriptSOName = scriptSOName.getPathDir();
-    scriptSOName.append("/lib/librs.");
-#else
-    String8 scriptSOName("lib");
-#endif
-    scriptSOName.append(resName);
-    scriptSOName.append(".so");
-
-    //script->mHal.drv = drv;
-
-    //ALOGV("Opening up shared object: %s", scriptSOName.string());
-    mScriptSO = dlopen(scriptSOName.string(), RTLD_NOW | RTLD_LOCAL);
-    if (mScriptSO == NULL) {
-        ALOGE("Unable to open shared library (%s): %s",
-              scriptSOName.string(), dlerror());
-
-        // One final attempt to find the library in "/system/lib".
-        // We do this to allow bundled applications to use the compatibility
-        // library fallback path. Those applications don't have a private
-        // library path, so they need to install to the system directly.
-        String8 scriptSONameSystem("/system/lib/librs.");
-        scriptSONameSystem.append(resName);
-        scriptSONameSystem.append(".so");
-        mScriptSO = dlopen(scriptSONameSystem.string(), RTLD_NOW | RTLD_LOCAL);
-        if (mScriptSO == NULL) {
-            ALOGE("Unable to open system shared library (%s): %s",
-                  scriptSONameSystem.string(), dlerror());
-            goto error;
-        }
-    }
+    mScriptSO = loadSharedLibrary(cacheDir, resName);
 
     if (mScriptSO) {
         char line[MAXLINE];
@@ -403,6 +656,8 @@
         if (mScriptSO == (void*)1) {
             //rsdLookupRuntimeStub(script, "acos");
         }
+    } else {
+        goto error;
     }
 #endif
 
@@ -433,6 +688,7 @@
     // Copy info over to runtime
     script->mHal.info.exportedFunctionCount = info->getExportFuncNames().size();
     script->mHal.info.exportedVariableCount = info->getExportVarNames().size();
+    script->mHal.info.exportedForeachFuncList = info->getExportForeachFuncs().array();
     script->mHal.info.exportedPragmaCount = info->getPragmas().size();
     script->mHal.info.exportedPragmaKeyList =
         const_cast<const char**>(mExecutable->getPragmaKeys().array());
@@ -473,11 +729,11 @@
 
     // possible for this to occur if IO_OUTPUT/IO_INPUT with no bound surface
     if (ain && (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == NULL) {
-        mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
+        mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null in allocations");
         return;
     }
     if (aout && (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == NULL) {
-        mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
+        mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null out allocations");
         return;
     }
 
diff --git a/cpu_ref/rsd_cpu.h b/cpu_ref/rsd_cpu.h
index d5642aa..d81a145 100644
--- a/cpu_ref/rsd_cpu.h
+++ b/cpu_ref/rsd_cpu.h
@@ -28,12 +28,16 @@
 
 namespace bcc {
 
+class RSCompilerDriver;
 class RSScript;
-typedef llvm::Module* (*RSLinkRuntimeCallback) (bcc::RSScript *, llvm::Module *, llvm::Module *);
+typedef llvm::Module* (*RSLinkRuntimeCallback)
+        (bcc::RSScript *, llvm::Module *, llvm::Module *);
 
 }  // end namespace bcc;
 
 typedef const char* (*RSSelectRTCallback) (const char*, size_t);
+
+typedef void (*RSSetupCompilerCallback) (bcc::RSCompilerDriver *);
 #endif
 
 namespace android {
@@ -119,6 +123,11 @@
     virtual CpuScriptGroup * createScriptGroup(const ScriptGroup *sg) = 0;
     virtual bool getInForEach() = 0;
 
+#ifndef RS_COMPATIBILITY_LIB
+    virtual void setSetupCompilerCallback(
+            RSSetupCompilerCallback pSetupCompilerCallback) = 0;
+    virtual RSSetupCompilerCallback getSetupCompilerCallback() const = 0;
+#endif
 };
 
 
diff --git a/driver/Android.mk b/driver/Android.mk
new file mode 100644
index 0000000..271de29
--- /dev/null
+++ b/driver/Android.mk
@@ -0,0 +1,4 @@
+
+LOCAL_PATH:=$(call my-dir)
+
+include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/driver/linkloader/tests/images/gen-testcases.sh b/driver/linkloader/tests/images/gen-testcases.sh
deleted file mode 100755
index 26c7cdf..0000000
--- a/driver/linkloader/tests/images/gen-testcases.sh
+++ /dev/null
@@ -1,41 +0,0 @@
-#!/bin/bash -e
-
-
-# Copyright (C) 2011-2012 The Android Open Source Project
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-
-CYAN='\033[1;36m'
-RESET='\033[m'
-
-echo -e "${CYAN}Generating bitcode ...${RESET}"
-clang -emit-llvm -std=c89 -Wall -c test.c -o test.bc
-clang -emit-llvm -std=c89 -Wall -c simple-test.c -o simple-test.bc
-clang -emit-llvm -std=c89 -Wall -c rodata-test.c -o rodata-test.bc
-
-function gen_test_cases {
-  echo -e "${CYAN}Generating for $1 ...${RESET}"
-  llc -filetype=obj -relocation-model=static -mtriple $2 $3 test.bc -o test-$1.o
-  llc -filetype=obj -relocation-model=static -mtriple $2 $3 simple-test.bc -o simple-test-$1.o
-  llc -filetype=obj -relocation-model=static -mtriple $2 $3 rodata-test.bc -o rodata-test-$1.o
-}
-
-gen_test_cases arm    armv7-none-linux-gnueabi
-gen_test_cases tegra2 armv7-none-linux-gnueabi '-mcpu=cortex-a9 -mattr=+vfp3'
-gen_test_cases thumb2 thumb-none-linux-gnueabi '-march=thumb -mattr=+thumb2'
-gen_test_cases thumb2lc thumb-none-linux-gnueabi '-mattr=+thumb2,+neonfp,+vfp3 -arm-long-calls'
-gen_test_cases thumb2lc-xoom thumb-none-linux-gnueabi '-mattr=+thumb2 -arm-long-calls'
-gen_test_cases x86_32 i686-none-linux
-gen_test_cases x86_64 x86_64-none-linux
-gen_test_cases mipsel mipsel-none-linux-gnueabi
diff --git a/driver/linkloader/tests/images/rodata-test.c b/driver/linkloader/tests/images/rodata-test.c
deleted file mode 100644
index d695d01..0000000
--- a/driver/linkloader/tests/images/rodata-test.c
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Copyright 2011, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include<stdio.h>
-
-static char const *const test_str[] = {
-  "string 1",
-  "string 2",
-  "string 3",
-  "long long long long long long string"
-};
-
-static size_t test_str_count = sizeof(test_str) / sizeof(char const *const);
-
-int main(){
-  int i;
-  printf("test_str: %p\n", &test_str);
-  for (i = 0; i < test_str_count; ++i) {
-    printf("%p\n", test_str[i]);
-    printf("%s\n", test_str[i]);
-  }
-
-  return 0;
-}
diff --git a/driver/linkloader/tests/images/simple-test.c b/driver/linkloader/tests/images/simple-test.c
deleted file mode 100644
index c59201d..0000000
--- a/driver/linkloader/tests/images/simple-test.c
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * Copyright 2011, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <time.h>
-
-int main() {
-  srand(time(NULL));
-
-  unsigned int ans = rand() % 100;
-  unsigned int user = 100;
-  unsigned int left = 0;
-  unsigned int right = 99;
-
-  printf("Hello, droid!  Let's play a number guessing game!\n");
-
-  while (user != ans) {
-    printf("Please input a number [%d-%d]:\n", left, right);
-
-    if (scanf("%u", &user) != 1) {
-      break;
-    }
-
-    if (user < left || user > right) {
-      /* Out of range, ignore this answer. */
-      continue;
-    } else if (user == ans) {
-      printf("You got it!\n");
-      break;
-    } else if (user < ans) {
-      left = user;
-    } else {
-      right = user;
-    }
-  }
-
-  return 0;
-}
diff --git a/driver/linkloader/tests/images/test.c b/driver/linkloader/tests/images/test.c
deleted file mode 100644
index 01b69a1..0000000
--- a/driver/linkloader/tests/images/test.c
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Copyright 2011, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include<stdio.h>
-static void hello_function(const char *ptr){
-        printf("%s", ptr);
-}
-int my_add(int para_x, int para_y){
-        return para_x + para_y;
-}
-int global_z_i;
-double global_z_d;
-int global_big_z_i[1000];
-double global_big_z_d[1000];
-static int global_static_z_i;
-static double global_static_z_d;
-static int global_static_big_z_i[1000];
-static double global_static_big_z_d[1000];
-int global_z_i_init = 1;
-double global_z_d_init = 1.1;
-/*extern int extern_z_i;   */
-/*extern double extern_z_d;*/
-int main(){
-        static int local_static_z_i;
-        static double local_static_z_d;
-        static int local_static_z_i_init = 2;
-        static double local_static_z_d_init = 2.2;
-        local_static_z_i = local_static_z_i_init;
-        local_static_z_d = local_static_z_d_init;
-        printf("%d %f\n", local_static_z_i, local_static_z_d);
-        printf("%d %f\n", local_static_z_i_init, local_static_z_d_init);
-        hello_function("Hello world!1\n");
-        hello_function("Hello world!2\n");
-        hello_function("Hello world!3\n");
-        global_z_i = my_add(1,2);
-        global_z_d = 3.3;
-        printf("%d %f\n", global_z_i, global_z_d);
-        global_big_z_i[100] = 4;
-        global_big_z_d[100] = 4.4;
-        printf("%d %f\n", global_big_z_i[100], global_big_z_d[100]);
-        global_static_z_i = my_add(2,1);
-        global_static_z_d = 3.3;
-        printf("%d %f\n", global_static_z_i, global_static_z_d);
-        int local_z_i = global_static_z_i = global_z_i;
-        double local_z_d = global_static_z_d = global_z_d;
-        printf("%d %f\n", local_z_i, local_z_d);
-        global_static_big_z_i[500] = 5;
-        global_static_big_z_d[500] = 5.5;
-        printf("%d %f\n", global_static_big_z_i[500], global_static_big_z_d[500]);
-        global_z_i_init = 6;
-        global_z_d_init = 6.6;
-        printf("%d %f\n", global_z_i_init, global_z_d_init);
-        /*printf("%d %f\n", extern_z_i, extern_z_d);*/
-        return 0;
-}
diff --git a/driver/linkloader/tests/stubs/arm-stub.c b/driver/linkloader/tests/stubs/arm-stub.c
deleted file mode 100644
index 2138f9a..0000000
--- a/driver/linkloader/tests/stubs/arm-stub.c
+++ /dev/null
@@ -1,27 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-
-
-// Note: The first instruction stands for ldr, which loads the data from
-// memory to the specified register.  Notice that due to the pipeline design,
-// when ldr is executed, the program will be advanced by 8.  So, to get our
-// address we should substract it by 4.
-
-uint32_t stub[] = {
-  0xe51ff004ul, // ldr pc, [pc, #-4]
-  0x00000000ul  // address
-};
-
-int test() {
-  printf("hello world!\n");
-  return 5;
-}
-
-int main() {
-  int (*f)() = (int (*)())stub;
-  stub[1] = (uint32_t)(uintptr_t)test;
-
-  printf("return = %d\n", f());
-  return EXIT_SUCCESS;
-}
diff --git a/driver/linkloader/tests/stubs/mips-stub.c b/driver/linkloader/tests/stubs/mips-stub.c
deleted file mode 100644
index ff34d03..0000000
--- a/driver/linkloader/tests/stubs/mips-stub.c
+++ /dev/null
@@ -1,48 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-
-// --------------
-// Register Usage
-// --------------
-// $0       zero
-// $1       at
-// $2-$3    function return value registers
-// $4-$7    function argument registers
-// $8-$15   temporary
-// $16-$23  saved register
-// $24-$25  temporary
-// $26-$27  os kernel
-// $28      global pointer
-// $29      stack pointer
-// $30      saved register
-// $31      return addres reigster
-
-// --------------------
-// Instruction Encoding
-// --------------------
-// lui: 0011 1100 000t tttt iiii iiii iiii iiii
-// ori: 0011 01ss ssst tttt iiii iiii iiii iiii
-// jr:  0000 00ss sss0 0000 0000 0000 0000 1000
-// nop:  0000 0000 0000 0000 0000 0000 0000 0000
-
-uint32_t stub[] = {
-  0x3c190000ul,
-  0x37390000ul,
-  0x03200008ul,
-  0x00000000ul
-};
-
-int test() {
-  printf("hello world!\n");
-  return 5;
-}
-
-int main() {
-  int (*f)() = (int (*)())stub;
-  stub[0] |= (((uint32_t)(uintptr_t)test) >> 16) & 0xffff;
-  stub[1] |= (((uint32_t)(uintptr_t)test)) & 0xffff;
-
-  printf("return = %d\n", f());
-  return EXIT_SUCCESS;
-}
diff --git a/driver/linkloader/tests/stubs/stub-layout-test.cpp b/driver/linkloader/tests/stubs/stub-layout-test.cpp
deleted file mode 100644
index ba07cd2..0000000
--- a/driver/linkloader/tests/stubs/stub-layout-test.cpp
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Copyright 2011, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "StubLayout.h"
-
-void function1() {
-  printf("hello ");
-}
-
-void function2() {
-  printf("world!\n");
-}
-
-int main() {
-  StubLayout stubs;
-
-  void (*func1)() = (void (*)())stubs.allocateStub((void *)&function1);
-  void (*func2)() = (void (*)())stubs.allocateStub((void *)&function2);
-
-  if (!func1) {
-    fprintf(stderr, "ERROR: Unable to allocate stub for function1\n");
-    exit(EXIT_FAILURE);
-  }
-
-  if (!func2) {
-    fprintf(stderr, "ERROR: Unable to allocate stub for function2\n");
-    exit(EXIT_FAILURE);
-  }
-
-  function1();
-  function2();
-
-  func1();
-  func2();
-
-  return EXIT_SUCCESS;
-}
diff --git a/driver/rsdAllocation.cpp b/driver/rsdAllocation.cpp
index 2daafbe..19b080f 100644
--- a/driver/rsdAllocation.cpp
+++ b/driver/rsdAllocation.cpp
@@ -617,34 +617,6 @@
 }
 
 #ifndef RS_COMPATIBILITY_LIB
-void DrvAllocation::NewBufferListener::onFrameAvailable() {
-    intptr_t ip = (intptr_t)alloc;
-    rsc->sendMessageToClient(NULL, RS_MESSAGE_TO_CLIENT_NEW_BUFFER, ip, 0, true);
-}
-#endif
-
-void* rsdAllocationGetSurface(const Context *rsc, const Allocation *alloc) {
-#ifndef RS_COMPATIBILITY_LIB
-    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
-
-    // Configure CpuConsumer to be in asynchronous mode
-    drv->cpuConsumer = new CpuConsumer(2, false);
-    sp<IGraphicBufferProducer> bp = drv->cpuConsumer->getProducerInterface();
-    bp->incStrong(NULL);
-
-    drv->mBufferListener = new DrvAllocation::NewBufferListener();
-    drv->mBufferListener->rsc = rsc;
-    drv->mBufferListener->alloc = alloc;
-
-    drv->cpuConsumer->setFrameAvailableListener(drv->mBufferListener);
-
-    return bp.get();
-#else
-    return NULL;
-#endif
-}
-
-#ifndef RS_COMPATIBILITY_LIB
 static bool IoGetBuffer(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
     DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
 
@@ -721,24 +693,10 @@
 
         int format = 0;
         const Element *e = alloc->mHal.state.type->getElement();
-        switch(e->getType()) {
-        case RS_TYPE_UNSIGNED_8:
-            switch (e->getVectorSize()) {
-            case 1:
-                rsAssert(e->getKind() == RS_KIND_PIXEL_A);
-                format = PIXEL_FORMAT_A_8;
-                break;
-            case 4:
-                rsAssert(e->getKind() == RS_KIND_PIXEL_RGBA);
-                format = PIXEL_FORMAT_RGBA_8888;
-                break;
-            default:
-                rsAssert(0);
-            }
-            break;
-        default:
-            rsAssert(0);
-        }
+        rsAssert(e->getType() == RS_TYPE_UNSIGNED_8);
+        rsAssert(e->getVectorSize() == 4);
+        rsAssert(e->getKind() == RS_KIND_PIXEL_RGBA);
+        format = PIXEL_FORMAT_RGBA_8888;
 
         r = native_window_set_buffers_format(nw, format);
         if (r) {
@@ -793,33 +751,9 @@
 void rsdAllocationIoReceive(const Context *rsc, Allocation *alloc) {
 #ifndef RS_COMPATIBILITY_LIB
     DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
-
-    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
-        CpuConsumer::LockedBuffer lb;
-        status_t ret = drv->cpuConsumer->lockNextBuffer(&lb);
-        if (ret == OK) {
-            if (drv->lb.data != NULL) {
-                drv->cpuConsumer->unlockBuffer(drv->lb);
-            }
-            drv->lb = lb;
-            alloc->mHal.drvState.lod[0].mallocPtr = drv->lb.data;
-            alloc->mHal.drvState.lod[0].stride = drv->lb.stride *
-                    alloc->mHal.state.elementSizeBytes;
-
-            if (alloc->mHal.state.yuv) {
-                DeriveYUVLayout(alloc->mHal.state.yuv, &alloc->mHal.drvState);
-            }
-        } else if (ret == BAD_VALUE) {
-            // No new frame, don't do anything
-        } else {
-            rsc->setError(RS_ERROR_DRIVER, "Error receiving IO input buffer.");
-        }
-
-    } else {
+    if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
         drv->surfaceTexture->updateTexImage();
     }
-
-
 #endif
 }
 
@@ -1213,3 +1147,10 @@
         }
     }
 }
+
+uint32_t rsdAllocationGrallocBits(const android::renderscript::Context *rsc,
+                                  android::renderscript::Allocation *alloc)
+{
+    return 0;
+}
+
diff --git a/driver/rsdAllocation.h b/driver/rsdAllocation.h
index 35999d3..ff47f03 100644
--- a/driver/rsdAllocation.h
+++ b/driver/rsdAllocation.h
@@ -29,7 +29,6 @@
 #endif
 
 #if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
-#include "gui/CpuConsumer.h"
 #include "gui/GLConsumer.h"
 #endif
 
@@ -51,16 +50,6 @@
     uint32_t renderTargetID;
 
 #ifndef RS_COMPATIBILITY_LIB
-    class NewBufferListener : public android::ConsumerBase::FrameAvailableListener {
-    public:
-        const android::renderscript::Context *rsc;
-        const android::renderscript::Allocation *alloc;
-
-        virtual void onFrameAvailable();
-    };
-    android::sp<NewBufferListener> mBufferListener;
-
-
     GLenum glTarget;
     GLenum glType;
     GLenum glFormat;
@@ -79,11 +68,6 @@
     RsdFrameBufferObj * readBackFBO;
     ANativeWindow *wnd;
     ANativeWindowBuffer *wndBuffer;
-
-#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
-    android::sp< android::CpuConsumer > cpuConsumer;
-    android::CpuConsumer::LockedBuffer lb;
-#endif
 };
 
 #ifndef RS_COMPATIBILITY_LIB
@@ -92,6 +76,8 @@
 #endif
 
 
+uint32_t rsdAllocationGrallocBits(const android::renderscript::Context *rsc,
+                                  android::renderscript::Allocation *alloc);
 bool rsdAllocationInit(const android::renderscript::Context *rsc,
                        android::renderscript::Allocation *alloc,
                        bool forceZero);
@@ -106,8 +92,6 @@
                           RsAllocationUsageType src);
 void rsdAllocationMarkDirty(const android::renderscript::Context *rsc,
                             const android::renderscript::Allocation *alloc);
-void* rsdAllocationGetSurface(const android::renderscript::Context *rsc,
-                              const android::renderscript::Allocation *alloc);
 void rsdAllocationSetSurface(const android::renderscript::Context *rsc,
                             android::renderscript::Allocation *alloc, ANativeWindow *nw);
 void rsdAllocationIoSend(const android::renderscript::Context *rsc,
diff --git a/driver/rsdCore.cpp b/driver/rsdCore.cpp
index d69255b..2b473c4 100644
--- a/driver/rsdCore.cpp
+++ b/driver/rsdCore.cpp
@@ -20,6 +20,7 @@
 #include "rsdAllocation.h"
 #include "rsdBcc.h"
 #ifndef RS_COMPATIBILITY_LIB
+    #include "MemChunk.h"
     #include "rsdGL.h"
     #include "rsdPath.h"
     #include "rsdProgramStore.h"
@@ -88,10 +89,10 @@
     {
         rsdAllocationInit,
         rsdAllocationDestroy,
+        rsdAllocationGrallocBits,
         rsdAllocationResize,
         rsdAllocationSyncAll,
         rsdAllocationMarkDirty,
-        NATIVE_FUNC(rsdAllocationGetSurface),
         NATIVE_FUNC(rsdAllocationSetSurface),
         NATIVE_FUNC(rsdAllocationIoSend),
         NATIVE_FUNC(rsdAllocationIoReceive),
@@ -189,7 +190,7 @@
     }
     rsc->mHal.drv = dc;
 
-    dc->mCpuRef = RsdCpuReference::create((Context *)c, version_major, version_minor,
+    dc->mCpuRef = RsdCpuReference::create(rsc, version_major, version_minor,
                                           &rsdLookupRuntimeStub, &LookupScript);
     if (!dc->mCpuRef) {
         ALOGE("RsdCpuReference::create for driver hal failed.");
@@ -197,6 +198,23 @@
         return false;
     }
 
+#ifndef RS_COMPATIBILITY_LIB
+    // Set a callback for compiler setup here.
+    if (false) {
+        dc->mCpuRef->setSetupCompilerCallback(NULL);
+    }
+
+    // Set a callback for switching MemChunk's allocator here.
+    // Note that the allocation function must return page-aligned memory, so
+    // that it can be mprotected properly (i.e. code should be written and
+    // later switched to read+execute only).
+    if (false) {
+        MemChunk::registerAllocFreeCallbacks(
+                rsc->mHal.funcs.allocRuntimeMem,
+                rsc->mHal.funcs.freeRuntimeMem);
+    }
+#endif
+
     return true;
 }
 
diff --git a/driver/rsdGL.cpp b/driver/rsdGL.cpp
index 0a23a44..c421ade 100644
--- a/driver/rsdGL.cpp
+++ b/driver/rsdGL.cpp
@@ -43,7 +43,6 @@
 #include "rsdFrameBufferObj.h"
 
 #include <gui/Surface.h>
-#include <gui/DummyConsumer.h>
 
 using namespace android;
 using namespace android::renderscript;
@@ -326,18 +325,13 @@
     }
     gGLContextCount++;
 
-    // Create a BufferQueue with a fake consumer
-    sp<BufferQueue> bq = new BufferQueue();
-    bq->consumerConnect(new DummyConsumer());
-    sp<Surface> stc(new Surface(static_cast<sp<IGraphicBufferProducer> >(bq)));
-
-    dc->gl.egl.surfaceDefault = eglCreateWindowSurface(dc->gl.egl.display, dc->gl.egl.config,
-                                                       static_cast<ANativeWindow*>(stc.get()),
-                                                       NULL);
-
-    checkEglError("eglCreateWindowSurface");
+    EGLint pbuffer_attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
+    rsc->setWatchdogGL("eglCreatePbufferSurface", __LINE__, __FILE__);
+    dc->gl.egl.surfaceDefault = eglCreatePbufferSurface(dc->gl.egl.display, dc->gl.egl.config,
+            pbuffer_attribs);
+    checkEglError("eglCreatePbufferSurface");
     if (dc->gl.egl.surfaceDefault == EGL_NO_SURFACE) {
-        ALOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
+        ALOGE("eglCreatePbufferSurface returned EGL_NO_SURFACE");
         rsdGLShutdown(rsc);
         rsc->setWatchdogGL(NULL, 0, NULL);
         return false;
diff --git a/driver/rsdRuntimeStubs.cpp b/driver/rsdRuntimeStubs.cpp
index cb3a5b4..ac80ba3 100644
--- a/driver/rsdRuntimeStubs.cpp
+++ b/driver/rsdRuntimeStubs.cpp
@@ -141,13 +141,13 @@
 #ifndef RS_COMPATIBILITY_LIB
 static void SC_AllocationIoSend(Allocation *alloc) {
     Context *rsc = RsdCpuReference::getTlsContext();
-    rsdAllocationIoSend(rsc, alloc);
+    rsrAllocationIoSend(rsc, alloc);
 }
 
 
 static void SC_AllocationIoReceive(Allocation *alloc) {
     Context *rsc = RsdCpuReference::getTlsContext();
-    rsdAllocationIoReceive(rsc, alloc);
+    rsrAllocationIoReceive(rsc, alloc);
 }
 
 
diff --git a/driver/runtime/Android.mk b/driver/runtime/Android.mk
new file mode 100755
index 0000000..d2ced77
--- /dev/null
+++ b/driver/runtime/Android.mk
@@ -0,0 +1,115 @@
+#
+# Copyright (C) 2013 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+# C/LLVM-IR source files for the library
+clcore_base_files := \
+    rs_allocation.c \
+    rs_cl.c \
+    rs_core.c \
+    rs_element.c \
+    rs_mesh.c \
+    rs_matrix.c \
+    rs_program.c \
+    rs_sample.c \
+    rs_sampler.c \
+    convert.ll \
+    allocation.ll \
+    rsClamp.ll
+
+clcore_files := \
+    $(clcore_base_files) \
+    math.ll \
+    arch/generic.c \
+    arch/sqrt.c \
+    arch/dot_length.c
+
+clcore_neon_files := \
+    $(clcore_base_files) \
+    math.ll \
+    arch/neon.ll \
+    arch/sqrt.c \
+    arch/dot_length.c \
+    arch/clamp.c
+
+ifeq ($(ARCH_X86_HAVE_SSE2), true)
+    clcore_x86_files := \
+    $(clcore_base_files) \
+    arch/x86_generic.c \
+    arch/x86_clamp.ll \
+    arch/x86_math.ll
+
+    ifeq ($(ARCH_X86_HAVE_SSE3), true)
+        clcore_x86_files += arch/x86_dot_length.ll
+    else
+        # FIXME: without SSE3, it is still able to get better code through PSHUFD. But,
+        # so far, there is no such device with SSE2 only.
+        clcore_x86_files += arch/dot_length.c
+    endif
+endif
+
+ifeq "REL" "$(PLATFORM_VERSION_CODENAME)"
+  RS_VERSION := $(PLATFORM_SDK_VERSION)
+else
+  # Increment by 1 whenever this is not a final release build, since we want to
+  # be able to see the RS version number change during development.
+  # See build/core/version_defaults.mk for more information about this.
+  RS_VERSION := "(1 + $(PLATFORM_SDK_VERSION))"
+endif
+
+# Build the base version of the library
+include $(CLEAR_VARS)
+LOCAL_MODULE := libclcore.bc
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_CLASS := SHARED_LIBRARIES
+LOCAL_SRC_FILES := $(clcore_files)
+
+include $(LOCAL_PATH)/build_bc_lib.mk
+
+# Build a debug version of the library
+include $(CLEAR_VARS)
+LOCAL_MODULE := libclcore_debug.bc
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_CLASS := SHARED_LIBRARIES
+rs_debug_runtime := 1
+LOCAL_SRC_FILES := $(clcore_files)
+
+include $(LOCAL_PATH)/build_bc_lib.mk
+
+# Build an optimized version of the library if the device is SSE2- or above
+# capable.
+ifeq ($(ARCH_X86_HAVE_SSE2),true)
+include $(CLEAR_VARS)
+LOCAL_MODULE := libclcore_x86.bc
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_CLASS := SHARED_LIBRARIES
+LOCAL_SRC_FILES := $(clcore_x86_files)
+
+include $(LOCAL_PATH)/build_bc_lib.mk
+endif
+
+# Build a NEON-enabled version of the library (if possible)
+ifeq ($(ARCH_ARM_HAVE_NEON),true)
+  include $(CLEAR_VARS)
+  LOCAL_MODULE := libclcore_neon.bc
+  LOCAL_MODULE_TAGS := optional
+  LOCAL_MODULE_CLASS := SHARED_LIBRARIES
+  LOCAL_SRC_FILES := $(clcore_neon_files)
+  LOCAL_CFLAGS += -DARCH_ARM_HAVE_NEON
+
+  include $(LOCAL_PATH)/build_bc_lib.mk
+endif
diff --git a/driver/runtime/allocation.ll b/driver/runtime/allocation.ll
new file mode 100644
index 0000000..e1d6c7e
--- /dev/null
+++ b/driver/runtime/allocation.ll
@@ -0,0 +1,664 @@
+target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:64:128-a0:0:64-n32-S64"
+target triple = "armv7-none-linux-gnueabi"
+
+declare i8* @rsOffset([1 x i32] %a.coerce, i32 %sizeOf, i32 %x, i32 %y, i32 %z)
+
+; The loads and stores in this file are annotated with RenderScript-specific
+; information for the type based alias analysis, such that the TBAA analysis
+; understands that loads and stores from two allocations with different types
+; can never access the same memory element. This is different from C, where
+; a char or uchar load/store is special as it can alias with about everything.
+;
+; The TBAA tree in this file has the the node "RenderScript TBAA" as its root.
+; This means all loads/stores that share this common root can be proven to not
+; alias. However, the alias analysis still has to assume MayAlias between
+; memory accesses in this file and memory accesses annotated with the C/C++
+; TBAA metadata.
+; If we can ensure that all accesses to elements loaded from RenderScript
+; allocations are either annotated with the RenderScript TBAA information or
+; not annotated at all, but never annotated with the C/C++ metadata, we
+; can add the RenderScript TBAA tree under the C/C++ TBAA tree. This enables
+; then the TBAA to prove that an access to data from the RenderScript allocation
+; does not alias with a load/store accessing something not part of a RenderScript
+; allocation.
+
+
+!14 = metadata !{metadata !"RenderScript TBAA"}
+!15 = metadata !{metadata !"allocation", metadata !14}
+
+!21 = metadata !{metadata !"char", metadata !15}
+define void @rsSetElementAtImpl_char([1 x i32] %a.coerce, i8 signext %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 1, i32 %x, i32 %y, i32 %z) #10
+  store i8 %val, i8* %1, align 1, !tbaa !21
+  ret void
+}
+
+define signext i8 @rsGetElementAtImpl_char([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 1, i32 %x, i32 %y, i32 %z) #10
+  %2 = load i8* %1, align 1, !tbaa !21
+  ret i8 %2
+}
+
+!22 = metadata !{metadata !"char2", metadata !15}
+define void @rsSetElementAtImpl_char2([1 x i32] %a.coerce, <2 x i8> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 2, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x i8>*
+  store <2 x i8> %val, <2 x i8>* %2, align 2, !tbaa !22
+  ret void
+}
+
+define <2 x i8> @rsGetElementAtImpl_char2([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 2, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x i8>*
+  %3 = load <2 x i8>* %2, align 2, !tbaa !22
+  ret <2 x i8> %3
+}
+
+!23 = metadata !{metadata !"char3", metadata !15}
+define void @rsSetElementAtImpl_char3([1 x i32] %a.coerce, <3 x i8> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 4, i32 %x, i32 %y, i32 %z) #10
+  %2 = shufflevector <3 x i8> %val, <3 x i8> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 undef>
+  %3 = bitcast i8* %1 to <4 x i8>*
+  store <4 x i8> %2, <4 x i8>* %3, align 4, !tbaa !23
+  ret void
+}
+
+define <3 x i8> @rsGetElementAtImpl_char3([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 4, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i8>*
+  %3 = load <4 x i8>* %2, align 4, !tbaa !23
+  %4 = shufflevector <4 x i8> %3, <4 x i8> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x i8> %4
+}
+
+!24 = metadata !{metadata !"char4", metadata !15}
+define void @rsSetElementAtImpl_char4([1 x i32] %a.coerce, <4 x i8> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 4, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i8>*
+  store <4 x i8> %val, <4 x i8>* %2, align 4, !tbaa !24
+  ret void
+}
+
+define <4 x i8> @rsGetElementAtImpl_char4([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 4, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i8>*
+  %3 = load <4 x i8>* %2, align 4, !tbaa !24
+  ret <4 x i8> %3
+}
+
+!25 = metadata !{metadata !"uchar", metadata !15}
+define void @rsSetElementAtImpl_uchar([1 x i32] %a.coerce, i8 zeroext %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 1, i32 %x, i32 %y, i32 %z) #10
+  store i8 %val, i8* %1, align 1, !tbaa !25
+  ret void
+}
+
+define zeroext i8 @rsGetElementAtImpl_uchar([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 1, i32 %x, i32 %y, i32 %z) #10
+  %2 = load i8* %1, align 1, !tbaa !25
+  ret i8 %2
+}
+
+!26 = metadata !{metadata !"uchar2", metadata !15}
+define void @rsSetElementAtImpl_uchar2([1 x i32] %a.coerce, <2 x i8> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 2, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x i8>*
+  store <2 x i8> %val, <2 x i8>* %2, align 2, !tbaa !26
+  ret void
+}
+
+define <2 x i8> @rsGetElementAtImpl_uchar2([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 2, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x i8>*
+  %3 = load <2 x i8>* %2, align 2, !tbaa !26
+  ret <2 x i8> %3
+}
+
+!27 = metadata !{metadata !"uchar3", metadata !15}
+define void @rsSetElementAtImpl_uchar3([1 x i32] %a.coerce, <3 x i8> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 4, i32 %x, i32 %y, i32 %z) #10
+  %2 = shufflevector <3 x i8> %val, <3 x i8> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 undef>
+  %3 = bitcast i8* %1 to <4 x i8>*
+  store <4 x i8> %2, <4 x i8>* %3, align 4, !tbaa !27
+  ret void
+}
+
+define <3 x i8> @rsGetElementAtImpl_uchar3([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 4, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i8>*
+  %3 = load <4 x i8>* %2, align 4, !tbaa !27
+  %4 = shufflevector <4 x i8> %3, <4 x i8> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x i8> %4
+}
+
+!28 = metadata !{metadata !"uchar4", metadata !15}
+define void @rsSetElementAtImpl_uchar4([1 x i32] %a.coerce, <4 x i8> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 4, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i8>*
+  store <4 x i8> %val, <4 x i8>* %2, align 4, !tbaa !28
+  ret void
+}
+
+define <4 x i8> @rsGetElementAtImpl_uchar4([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 4, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i8>*
+  %3 = load <4 x i8>* %2, align 4, !tbaa !28
+  ret <4 x i8> %3
+}
+
+!29 = metadata !{metadata !"short", metadata !15}
+define void @rsSetElementAtImpl_short([1 x i32] %a.coerce, i16 signext %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 2, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to i16*
+  store i16 %val, i16* %2, align 2, !tbaa !29
+  ret void
+}
+
+define signext i16 @rsGetElementAtImpl_short([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 2, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to i16*
+  %3 = load i16* %2, align 2, !tbaa !29
+  ret i16 %3
+}
+
+!30 = metadata !{metadata !"short2", metadata !15}
+define void @rsSetElementAtImpl_short2([1 x i32] %a.coerce, <2 x i16> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 4, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x i16>*
+  store <2 x i16> %val, <2 x i16>* %2, align 4, !tbaa !30
+  ret void
+}
+
+define <2 x i16> @rsGetElementAtImpl_short2([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 4, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x i16>*
+  %3 = load <2 x i16>* %2, align 4, !tbaa !30
+  ret <2 x i16> %3
+}
+
+!31 = metadata !{metadata !"short3", metadata !15}
+define void @rsSetElementAtImpl_short3([1 x i32] %a.coerce, <3 x i16> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = shufflevector <3 x i16> %val, <3 x i16> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 undef>
+  %3 = bitcast i8* %1 to <4 x i16>*
+  store <4 x i16> %2, <4 x i16>* %3, align 8, !tbaa !31
+  ret void
+}
+
+define <3 x i16> @rsGetElementAtImpl_short3([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i16>*
+  %3 = load <4 x i16>* %2, align 8, !tbaa !31
+  %4 = shufflevector <4 x i16> %3, <4 x i16> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x i16> %4
+}
+
+!32 = metadata !{metadata !"short4", metadata !15}
+define void @rsSetElementAtImpl_short4([1 x i32] %a.coerce, <4 x i16> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i16>*
+  store <4 x i16> %val, <4 x i16>* %2, align 8, !tbaa !32
+  ret void
+}
+
+define <4 x i16> @rsGetElementAtImpl_short4([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i16>*
+  %3 = load <4 x i16>* %2, align 8, !tbaa !32
+  ret <4 x i16> %3
+}
+
+!33 = metadata !{metadata !"ushort", metadata !15}
+define void @rsSetElementAtImpl_ushort([1 x i32] %a.coerce, i16 zeroext %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 2, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to i16*
+  store i16 %val, i16* %2, align 2, !tbaa !33
+  ret void
+}
+
+define zeroext i16 @rsGetElementAtImpl_ushort([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 2, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to i16*
+  %3 = load i16* %2, align 2, !tbaa !33
+  ret i16 %3
+}
+
+!34 = metadata !{metadata !"ushort2", metadata !15}
+define void @rsSetElementAtImpl_ushort2([1 x i32] %a.coerce, <2 x i16> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 4, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x i16>*
+  store <2 x i16> %val, <2 x i16>* %2, align 4, !tbaa !34
+  ret void
+}
+
+define <2 x i16> @rsGetElementAtImpl_ushort2([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 4, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x i16>*
+  %3 = load <2 x i16>* %2, align 4, !tbaa !34
+  ret <2 x i16> %3
+}
+
+!35 = metadata !{metadata !"ushort3", metadata !15}
+define void @rsSetElementAtImpl_ushort3([1 x i32] %a.coerce, <3 x i16> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = shufflevector <3 x i16> %val, <3 x i16> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 undef>
+  %3 = bitcast i8* %1 to <4 x i16>*
+  store <4 x i16> %2, <4 x i16>* %3, align 8, !tbaa !35
+  ret void
+}
+
+define <3 x i16> @rsGetElementAtImpl_ushort3([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i16>*
+  %3 = load <4 x i16>* %2, align 8, !tbaa !35
+  %4 = shufflevector <4 x i16> %3, <4 x i16> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x i16> %4
+}
+
+!36 = metadata !{metadata !"ushort4", metadata !15}
+define void @rsSetElementAtImpl_ushort4([1 x i32] %a.coerce, <4 x i16> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i16>*
+  store <4 x i16> %val, <4 x i16>* %2, align 8, !tbaa !36
+  ret void
+}
+
+define <4 x i16> @rsGetElementAtImpl_ushort4([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i16>*
+  %3 = load <4 x i16>* %2, align 8, !tbaa !36
+  ret <4 x i16> %3
+}
+
+!37 = metadata !{metadata !"int", metadata !15}
+define void @rsSetElementAtImpl_int([1 x i32] %a.coerce, i32 %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 4, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to i32*
+  store i32 %val, i32* %2, align 4, !tbaa !37
+  ret void
+}
+
+define i32 @rsGetElementAtImpl_int([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 4, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to i32*
+  %3 = load i32* %2, align 4, !tbaa !37
+  ret i32 %3
+}
+
+!38 = metadata !{metadata !"int2", metadata !15}
+define void @rsSetElementAtImpl_int2([1 x i32] %a.coerce, <2 x i32> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x i32>*
+  store <2 x i32> %val, <2 x i32>* %2, align 8, !tbaa !38
+  ret void
+}
+
+define <2 x i32> @rsGetElementAtImpl_int2([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x i32>*
+  %3 = load <2 x i32>* %2, align 8, !tbaa !38
+  ret <2 x i32> %3
+}
+
+!39 = metadata !{metadata !"int3", metadata !15}
+define void @rsSetElementAtImpl_int3([1 x i32] %a.coerce, <3 x i32> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 16, i32 %x, i32 %y, i32 %z) #10
+  %2 = shufflevector <3 x i32> %val, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 undef>
+  %3 = bitcast i8* %1 to <4 x i32>*
+  store <4 x i32> %2, <4 x i32>* %3, align 16, !tbaa !39
+  ret void
+}
+
+define <3 x i32> @rsGetElementAtImpl_int3([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 16, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i32>*
+  %3 = load <4 x i32>* %2, align 8, !tbaa !39
+  %4 = shufflevector <4 x i32> %3, <4 x i32> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x i32> %4
+}
+
+!40 = metadata !{metadata !"int4", metadata !15}
+define void @rsSetElementAtImpl_int4([1 x i32] %a.coerce, <4 x i32> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 16, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i32>*
+  store <4 x i32> %val, <4 x i32>* %2, align 16, !tbaa !40
+  ret void
+}
+
+define <4 x i32> @rsGetElementAtImpl_int4([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 16, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i32>*
+  %3 = load <4 x i32>* %2, align 16, !tbaa !40
+  ret <4 x i32> %3
+}
+
+!41 = metadata !{metadata !"uint", metadata !15}
+define void @rsSetElementAtImpl_uint([1 x i32] %a.coerce, i32 %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 4, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to i32*
+  store i32 %val, i32* %2, align 4, !tbaa !41
+  ret void
+}
+
+define i32 @rsGetElementAtImpl_uint([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 4, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to i32*
+  %3 = load i32* %2, align 4, !tbaa !41
+  ret i32 %3
+}
+
+!42 = metadata !{metadata !"uint2", metadata !15}
+define void @rsSetElementAtImpl_uint2([1 x i32] %a.coerce, <2 x i32> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x i32>*
+  store <2 x i32> %val, <2 x i32>* %2, align 8, !tbaa !42
+  ret void
+}
+
+define <2 x i32> @rsGetElementAtImpl_uint2([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x i32>*
+  %3 = load <2 x i32>* %2, align 8, !tbaa !42
+  ret <2 x i32> %3
+}
+
+!43 = metadata !{metadata !"uint3", metadata !15}
+define void @rsSetElementAtImpl_uint3([1 x i32] %a.coerce, <3 x i32> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 16, i32 %x, i32 %y, i32 %z) #10
+  %2 = shufflevector <3 x i32> %val, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 undef>
+  %3 = bitcast i8* %1 to <4 x i32>*
+  store <4 x i32> %2, <4 x i32>* %3, align 16, !tbaa !43
+  ret void
+}
+
+define <3 x i32> @rsGetElementAtImpl_uint3([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 16, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i32>*
+  %3 = load <4 x i32>* %2, align 8, !tbaa !43
+  %4 = shufflevector <4 x i32> %3, <4 x i32> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x i32> %4
+}
+
+!44 = metadata !{metadata !"uint4", metadata !15}
+define void @rsSetElementAtImpl_uint4([1 x i32] %a.coerce, <4 x i32> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 16, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i32>*
+  store <4 x i32> %val, <4 x i32>* %2, align 16, !tbaa !44
+  ret void
+}
+
+define <4 x i32> @rsGetElementAtImpl_uint4([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 16, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i32>*
+  %3 = load <4 x i32>* %2, align 16, !tbaa !44
+  ret <4 x i32> %3
+}
+
+!45 = metadata !{metadata !"long", metadata !15}
+define void @rsSetElementAtImpl_long([1 x i32] %a.coerce, i64 %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to i64*
+  store i64 %val, i64* %2, align 8, !tbaa !45
+  ret void
+}
+
+define i64 @rsGetElementAtImpl_long([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to i64*
+  %3 = load i64* %2, align 8, !tbaa !45
+  ret i64 %3
+}
+
+!46 = metadata !{metadata !"long2", metadata !15}
+define void @rsSetElementAtImpl_long2([1 x i32] %a.coerce, <2 x i64> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 16, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x i64>*
+  store <2 x i64> %val, <2 x i64>* %2, align 16, !tbaa !46
+  ret void
+}
+
+define <2 x i64> @rsGetElementAtImpl_long2([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 16, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x i64>*
+  %3 = load <2 x i64>* %2, align 16, !tbaa !46
+  ret <2 x i64> %3
+}
+
+!47 = metadata !{metadata !"long3", metadata !15}
+define void @rsSetElementAtImpl_long3([1 x i32] %a.coerce, <3 x i64> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 32, i32 %x, i32 %y, i32 %z) #10
+  %2 = shufflevector <3 x i64> %val, <3 x i64> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 undef>
+  %3 = bitcast i8* %1 to <4 x i64>*
+  store <4 x i64> %2, <4 x i64>* %3, align 32, !tbaa !47
+  ret void
+}
+
+define void @rsGetElementAtImpl_long3(<3 x i64>* noalias nocapture sret %agg.result, [1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 32, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i64>*
+  %3 = load <4 x i64>* %2, align 32
+  %4 = bitcast <3 x i64>* %agg.result to <4 x i64>*
+  store <4 x i64> %3, <4 x i64>* %4, align 32, !tbaa !47
+  ret void
+}
+
+!48 = metadata !{metadata !"long4", metadata !15}
+define void @rsSetElementAtImpl_long4([1 x i32] %a.coerce, <4 x i64> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 32, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i64>*
+  store <4 x i64> %val, <4 x i64>* %2, align 32, !tbaa !48
+  ret void
+}
+
+define void @rsGetElementAtImpl_long4(<4 x i64>* noalias nocapture sret %agg.result, [1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 32, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i64>*
+  %3 = load <4 x i64>* %2, align 32, !tbaa !15
+  store <4 x i64> %3, <4 x i64>* %agg.result, align 32, !tbaa !48
+  ret void
+}
+
+!49 = metadata !{metadata !"ulong", metadata !15}
+define void @rsSetElementAtImpl_ulong([1 x i32] %a.coerce, i64 %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to i64*
+  store i64 %val, i64* %2, align 8, !tbaa !49
+  ret void
+}
+
+define i64 @rsGetElementAtImpl_ulong([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to i64*
+  %3 = load i64* %2, align 8, !tbaa !49
+  ret i64 %3
+}
+
+!50 = metadata !{metadata !"ulong2", metadata !15}
+define void @rsSetElementAtImpl_ulong2([1 x i32] %a.coerce, <2 x i64> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 16, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x i64>*
+  store <2 x i64> %val, <2 x i64>* %2, align 16, !tbaa !50
+  ret void
+}
+
+define <2 x i64> @rsGetElementAtImpl_ulong2([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 16, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x i64>*
+  %3 = load <2 x i64>* %2, align 16, !tbaa !50
+  ret <2 x i64> %3
+}
+
+!51 = metadata !{metadata !"ulong3", metadata !15}
+define void @rsSetElementAtImpl_ulong3([1 x i32] %a.coerce, <3 x i64> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 32, i32 %x, i32 %y, i32 %z) #10
+  %2 = shufflevector <3 x i64> %val, <3 x i64> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 undef>
+  %3 = bitcast i8* %1 to <4 x i64>*
+  store <4 x i64> %2, <4 x i64>* %3, align 32, !tbaa !51
+  ret void
+}
+
+define void @rsGetElementAtImpl_ulong3(<3 x i64>* noalias nocapture sret %agg.result, [1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 32, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i64>*
+  %3 = load <4 x i64>* %2, align 32
+  %4 = bitcast <3 x i64>* %agg.result to <4 x i64>*
+  store <4 x i64> %3, <4 x i64>* %4, align 32, !tbaa !51
+  ret void
+}
+
+!52 = metadata !{metadata !"ulong4", metadata !15}
+define void @rsSetElementAtImpl_ulong4([1 x i32] %a.coerce, <4 x i64> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 32, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i64>*
+  store <4 x i64> %val, <4 x i64>* %2, align 32, !tbaa !52
+  ret void
+}
+
+define void @rsGetElementAtImpl_ulong4(<4 x i64>* noalias nocapture sret %agg.result, [1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 32, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x i64>*
+  %3 = load <4 x i64>* %2, align 32, !tbaa !15
+  store <4 x i64> %3, <4 x i64>* %agg.result, align 32, !tbaa !52
+  ret void
+}
+
+!53 = metadata !{metadata !"float", metadata !15}
+define void @rsSetElementAtImpl_float([1 x i32] %a.coerce, float %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 4, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to float*
+  store float %val, float* %2, align 4, !tbaa !53
+  ret void
+}
+
+define float @rsGetElementAtImpl_float([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 4, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to float*
+  %3 = load float* %2, align 4, !tbaa !53
+  ret float %3
+}
+
+!54 = metadata !{metadata !"float2", metadata !15}
+define void @rsSetElementAtImpl_float2([1 x i32] %a.coerce, <2 x float> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x float>*
+  store <2 x float> %val, <2 x float>* %2, align 8, !tbaa !54
+  ret void
+}
+
+define <2 x float> @rsGetElementAtImpl_float2([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x float>*
+  %3 = load <2 x float>* %2, align 8, !tbaa !54
+  ret <2 x float> %3
+}
+
+!55 = metadata !{metadata !"float3", metadata !15}
+define void @rsSetElementAtImpl_float3([1 x i32] %a.coerce, <3 x float> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 16, i32 %x, i32 %y, i32 %z) #10
+  %2 = shufflevector <3 x float> %val, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 undef>
+  %3 = bitcast i8* %1 to <4 x float>*
+  store <4 x float> %2, <4 x float>* %3, align 16, !tbaa !55
+  ret void
+}
+
+define <3 x float> @rsGetElementAtImpl_float3([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 16, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x float>*
+  %3 = load <4 x float>* %2, align 8, !tbaa !55
+  %4 = shufflevector <4 x float> %3, <4 x float> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x float> %4
+}
+
+!56 = metadata !{metadata !"float4", metadata !15}
+define void @rsSetElementAtImpl_float4([1 x i32] %a.coerce, <4 x float> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 16, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x float>*
+  store <4 x float> %val, <4 x float>* %2, align 16, !tbaa !56
+  ret void
+}
+
+define <4 x float> @rsGetElementAtImpl_float4([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 16, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x float>*
+  %3 = load <4 x float>* %2, align 16, !tbaa !56
+  ret <4 x float> %3
+}
+
+!57 = metadata !{metadata !"double", metadata !15}
+define void @rsSetElementAtImpl_double([1 x i32] %a.coerce, double %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to double*
+  store double %val, double* %2, align 8, !tbaa !57
+  ret void
+}
+
+define double @rsGetElementAtImpl_double([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 8, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to double*
+  %3 = load double* %2, align 8, !tbaa !57
+  ret double %3
+}
+
+!58 = metadata !{metadata !"double2", metadata !15}
+define void @rsSetElementAtImpl_double2([1 x i32] %a.coerce, <2 x double> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 16, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x double>*
+  store <2 x double> %val, <2 x double>* %2, align 16, !tbaa !58
+  ret void
+}
+
+define <2 x double> @rsGetElementAtImpl_double2([1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #3 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 16, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <2 x double>*
+  %3 = load <2 x double>* %2, align 16, !tbaa !58
+  ret <2 x double> %3
+}
+
+!59 = metadata !{metadata !"double3", metadata !15}
+define void @rsSetElementAtImpl_double3([1 x i32] %a.coerce, <3 x double> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 32, i32 %x, i32 %y, i32 %z) #10
+  %2 = shufflevector <3 x double> %val, <3 x double> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 undef>
+  %3 = bitcast i8* %1 to <4 x double>*
+  store <4 x double> %2, <4 x double>* %3, align 32, !tbaa !59
+  ret void
+}
+
+
+define void @rsGetElementAtImpl_double3(<3 x double>* noalias nocapture sret %agg.result, [1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 32, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x double>*
+  %3 = load <4 x double>* %2, align 32
+  %4 = bitcast <3 x double>* %agg.result to <4 x double>*
+  store <4 x double> %3, <4 x double>* %4, align 32, !tbaa !59
+  ret void
+}
+
+!60 = metadata !{metadata !"double4", metadata !15}
+define void @rsSetElementAtImpl_double4([1 x i32] %a.coerce, <4 x double> %val, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 32, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x double>*
+  store <4 x double> %val, <4 x double>* %2, align 32, !tbaa !60
+  ret void
+}
+define void @rsGetElementAtImpl_double4(<4 x double>* noalias nocapture sret %agg.result, [1 x i32] %a.coerce, i32 %x, i32 %y, i32 %z) #2 {
+  %1 = tail call i8* @rsOffset([1 x i32] %a.coerce, i32 32, i32 %x, i32 %y, i32 %z) #10
+  %2 = bitcast i8* %1 to <4 x double>*
+  %3 = load <4 x double>* %2, align 32, !tbaa !15
+  store <4 x double> %3, <4 x double>* %agg.result, align 32, !tbaa !60
+  ret void
+}
+
+attributes #0 = { nounwind readonly "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #1 = { nounwind "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #2 = { nounwind "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #3 = { nounwind readonly "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #4 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #5 = { nounwind readnone "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #6 = { nounwind readnone }
+attributes #7 = { nounwind }
+attributes #8 = { alwaysinline nounwind readnone }
+attributes #9 = { nounwind readonly }
+attributes #10 = { nobuiltin }
+attributes #11 = { nobuiltin nounwind }
+attributes #12 = { nobuiltin nounwind readnone }
+
diff --git a/driver/runtime/arch/clamp.c b/driver/runtime/arch/clamp.c
new file mode 100644
index 0000000..c2c2226
--- /dev/null
+++ b/driver/runtime/arch/clamp.c
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "rs_types.rsh"
+
+#define S_CLAMP(T) \
+extern T __attribute__((overloadable)) clamp(T amount, T low, T high) {             \
+    return amount < low ? low : (amount > high ? high : amount);                    \
+}
+
+//_CLAMP(float);  implemented in .ll
+S_CLAMP(double);
+S_CLAMP(char);
+S_CLAMP(uchar);
+S_CLAMP(short);
+S_CLAMP(ushort);
+S_CLAMP(int);
+S_CLAMP(uint);
+S_CLAMP(long);
+S_CLAMP(ulong);
+
+#undef S_CLAMP
+
+
+                                                                                    \
+#define V_CLAMP(T) \
+extern T##2 __attribute__((overloadable)) clamp(T##2 amount, T##2 low, T##2 high) { \
+    T##2 r;                                                                         \
+    r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x);       \
+    r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y);       \
+    return r;                                                                       \
+}                                                                                   \
+                                                                                    \
+extern T##3 __attribute__((overloadable)) clamp(T##3 amount, T##3 low, T##3 high) { \
+    T##3 r;                                                                         \
+    r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x);       \
+    r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y);       \
+    r.z = amount.z < low.z ? low.z : (amount.z > high.z ? high.z : amount.z);       \
+    return r;                                                                       \
+}                                                                                   \
+                                                                                    \
+extern T##4 __attribute__((overloadable)) clamp(T##4 amount, T##4 low, T##4 high) { \
+    T##4 r;                                                                         \
+    r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x);       \
+    r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y);       \
+    r.z = amount.z < low.z ? low.z : (amount.z > high.z ? high.z : amount.z);       \
+    r.w = amount.w < low.w ? low.w : (amount.w > high.w ? high.w : amount.w);       \
+    return r;                                                                       \
+}                                                                                   \
+                                                                                    \
+extern T##2 __attribute__((overloadable)) clamp(T##2 amount, T low, T high) {       \
+    T##2 r;                                                                         \
+    r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);               \
+    r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);               \
+    return r;                                                                       \
+}                                                                                   \
+                                                                                    \
+extern T##3 __attribute__((overloadable)) clamp(T##3 amount, T low, T high) {       \
+    T##3 r;                                                                         \
+    r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);               \
+    r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);               \
+    r.z = amount.z < low ? low : (amount.z > high ? high : amount.z);               \
+    return r;                                                                       \
+}                                                                                   \
+                                                                                    \
+extern T##4 __attribute__((overloadable)) clamp(T##4 amount, T low, T high) {       \
+    T##4 r;                                                                         \
+    r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);               \
+    r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);               \
+    r.z = amount.z < low ? low : (amount.z > high ? high : amount.z);               \
+    r.w = amount.w < low ? low : (amount.w > high ? high : amount.w);               \
+    return r;                                                                       \
+}
+
+//V_CLAMP(float);  implemented in .ll
+V_CLAMP(double);
+V_CLAMP(char);
+V_CLAMP(uchar);
+V_CLAMP(short);
+V_CLAMP(ushort);
+#ifndef ARCH_ARM_HAVE_NEON
+    V_CLAMP(int);  //implemented in .ll
+    V_CLAMP(uint);  //implemented in .ll
+#endif
+V_CLAMP(long);
+V_CLAMP(ulong);
+
+#undef _CLAMP
+
diff --git a/driver/runtime/arch/dot_length.c b/driver/runtime/arch/dot_length.c
new file mode 100644
index 0000000..94c99b6
--- /dev/null
+++ b/driver/runtime/arch/dot_length.c
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "rs_types.rsh"
+
+extern float __attribute__((overloadable)) dot(float lhs, float rhs) {
+    return lhs * rhs;
+}
+extern float __attribute__((overloadable)) dot(float2 lhs, float2 rhs) {
+    return lhs.x*rhs.x + lhs.y*rhs.y;
+}
+extern float __attribute__((overloadable)) dot(float3 lhs, float3 rhs) {
+    return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z;
+}
+extern float __attribute__((overloadable)) dot(float4 lhs, float4 rhs) {
+    return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z + lhs.w*rhs.w;
+}
+
+extern float __attribute__((overloadable)) fabs(float);
+extern float __attribute__((overloadable)) sqrt(float);
+
+extern float __attribute__((overloadable)) length(float v) {
+    return fabs(v);
+}
+extern float __attribute__((overloadable)) length(float2 v) {
+    return sqrt(v.x*v.x + v.y*v.y);
+}
+extern float __attribute__((overloadable)) length(float3 v) {
+    return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
+}
+extern float __attribute__((overloadable)) length(float4 v) {
+    return sqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w);
+}
+
diff --git a/driver/runtime/arch/generic.c b/driver/runtime/arch/generic.c
new file mode 100644
index 0000000..da83c2a
--- /dev/null
+++ b/driver/runtime/arch/generic.c
@@ -0,0 +1,948 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#include "rs_types.rsh"
+
+extern short __attribute__((overloadable, always_inline)) rsClamp(short amount, short low, short high);
+extern uchar4 __attribute__((overloadable)) convert_uchar4(short4);
+extern uchar4 __attribute__((overloadable)) convert_uchar4(float4);
+extern float4 __attribute__((overloadable)) convert_float4(uchar4);
+extern float __attribute__((overloadable)) sqrt(float);
+
+/*
+ * CLAMP
+ */
+#define _CLAMP(T) \
+extern T __attribute__((overloadable)) clamp(T amount, T low, T high) {             \
+    return amount < low ? low : (amount > high ? high : amount);                    \
+}                                                                                   \
+                                                                                    \
+extern T##2 __attribute__((overloadable)) clamp(T##2 amount, T##2 low, T##2 high) { \
+    T##2 r;                                                                         \
+    r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x);       \
+    r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y);       \
+    return r;                                                                       \
+}                                                                                   \
+                                                                                    \
+extern T##3 __attribute__((overloadable)) clamp(T##3 amount, T##3 low, T##3 high) { \
+    T##3 r;                                                                         \
+    r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x);       \
+    r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y);       \
+    r.z = amount.z < low.z ? low.z : (amount.z > high.z ? high.z : amount.z);       \
+    return r;                                                                       \
+}                                                                                   \
+                                                                                    \
+extern T##4 __attribute__((overloadable)) clamp(T##4 amount, T##4 low, T##4 high) { \
+    T##4 r;                                                                         \
+    r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x);       \
+    r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y);       \
+    r.z = amount.z < low.z ? low.z : (amount.z > high.z ? high.z : amount.z);       \
+    r.w = amount.w < low.w ? low.w : (amount.w > high.w ? high.w : amount.w);       \
+    return r;                                                                       \
+}                                                                                   \
+                                                                                    \
+extern T##2 __attribute__((overloadable)) clamp(T##2 amount, T low, T high) {       \
+    T##2 r;                                                                         \
+    r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);               \
+    r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);               \
+    return r;                                                                       \
+}                                                                                   \
+                                                                                    \
+extern T##3 __attribute__((overloadable)) clamp(T##3 amount, T low, T high) {       \
+    T##3 r;                                                                         \
+    r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);               \
+    r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);               \
+    r.z = amount.z < low ? low : (amount.z > high ? high : amount.z);               \
+    return r;                                                                       \
+}                                                                                   \
+                                                                                    \
+extern T##4 __attribute__((overloadable)) clamp(T##4 amount, T low, T high) {       \
+    T##4 r;                                                                         \
+    r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);               \
+    r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);               \
+    r.z = amount.z < low ? low : (amount.z > high ? high : amount.z);               \
+    r.w = amount.w < low ? low : (amount.w > high ? high : amount.w);               \
+    return r;                                                                       \
+}
+
+_CLAMP(float);
+_CLAMP(double);
+_CLAMP(char);
+_CLAMP(uchar);
+_CLAMP(short);
+_CLAMP(ushort);
+_CLAMP(int);
+_CLAMP(uint);
+_CLAMP(long);
+_CLAMP(ulong);
+
+#undef _CLAMP
+
+/*
+ * FMAX
+ */
+
+extern float __attribute__((overloadable)) fmax(float v1, float v2) {
+    return v1 > v2 ? v1 : v2;
+}
+
+extern float2 __attribute__((overloadable)) fmax(float2 v1, float2 v2) {
+    float2 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern float3 __attribute__((overloadable)) fmax(float3 v1, float3 v2) {
+    float3 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern float4 __attribute__((overloadable)) fmax(float4 v1, float4 v2) {
+    float4 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    r.w = v1.w > v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern float2 __attribute__((overloadable)) fmax(float2 v1, float v2) {
+    float2 r;
+    r.x = v1.x > v2 ? v1.x : v2;
+    r.y = v1.y > v2 ? v1.y : v2;
+    return r;
+}
+
+extern float3 __attribute__((overloadable)) fmax(float3 v1, float v2) {
+    float3 r;
+    r.x = v1.x > v2 ? v1.x : v2;
+    r.y = v1.y > v2 ? v1.y : v2;
+    r.z = v1.z > v2 ? v1.z : v2;
+    return r;
+}
+
+extern float4 __attribute__((overloadable)) fmax(float4 v1, float v2) {
+    float4 r;
+    r.x = v1.x > v2 ? v1.x : v2;
+    r.y = v1.y > v2 ? v1.y : v2;
+    r.z = v1.z > v2 ? v1.z : v2;
+    r.w = v1.w > v2 ? v1.w : v2;
+    return r;
+}
+
+extern float __attribute__((overloadable)) fmin(float v1, float v2) {
+    return v1 < v2 ? v1 : v2;
+}
+
+
+/*
+ * FMIN
+ */
+extern float2 __attribute__((overloadable)) fmin(float2 v1, float2 v2) {
+    float2 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern float3 __attribute__((overloadable)) fmin(float3 v1, float3 v2) {
+    float3 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern float4 __attribute__((overloadable)) fmin(float4 v1, float4 v2) {
+    float4 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    r.w = v1.w < v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern float2 __attribute__((overloadable)) fmin(float2 v1, float v2) {
+    float2 r;
+    r.x = v1.x < v2 ? v1.x : v2;
+    r.y = v1.y < v2 ? v1.y : v2;
+    return r;
+}
+
+extern float3 __attribute__((overloadable)) fmin(float3 v1, float v2) {
+    float3 r;
+    r.x = v1.x < v2 ? v1.x : v2;
+    r.y = v1.y < v2 ? v1.y : v2;
+    r.z = v1.z < v2 ? v1.z : v2;
+    return r;
+}
+
+extern float4 __attribute__((overloadable)) fmin(float4 v1, float v2) {
+    float4 r;
+    r.x = v1.x < v2 ? v1.x : v2;
+    r.y = v1.y < v2 ? v1.y : v2;
+    r.z = v1.z < v2 ? v1.z : v2;
+    r.w = v1.w < v2 ? v1.w : v2;
+    return r;
+}
+
+
+/*
+ * MAX
+ */
+
+extern char __attribute__((overloadable)) max(char v1, char v2) {
+    return v1 > v2 ? v1 : v2;
+}
+
+extern char2 __attribute__((overloadable)) max(char2 v1, char2 v2) {
+    char2 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern char3 __attribute__((overloadable)) max(char3 v1, char3 v2) {
+    char3 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern char4 __attribute__((overloadable)) max(char4 v1, char4 v2) {
+    char4 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    r.w = v1.w > v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern short __attribute__((overloadable)) max(short v1, short v2) {
+    return v1 > v2 ? v1 : v2;
+}
+
+extern short2 __attribute__((overloadable)) max(short2 v1, short2 v2) {
+    short2 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern short3 __attribute__((overloadable)) max(short3 v1, short3 v2) {
+    short3 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern short4 __attribute__((overloadable)) max(short4 v1, short4 v2) {
+    short4 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    r.w = v1.w > v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern int __attribute__((overloadable)) max(int v1, int v2) {
+    return v1 > v2 ? v1 : v2;
+}
+
+extern int2 __attribute__((overloadable)) max(int2 v1, int2 v2) {
+    int2 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern int3 __attribute__((overloadable)) max(int3 v1, int3 v2) {
+    int3 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern int4 __attribute__((overloadable)) max(int4 v1, int4 v2) {
+    int4 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    r.w = v1.w > v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern int64_t __attribute__((overloadable)) max(int64_t v1, int64_t v2) {
+    return v1 > v2 ? v1 : v2;
+}
+
+extern long2 __attribute__((overloadable)) max(long2 v1, long2 v2) {
+    long2 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern long3 __attribute__((overloadable)) max(long3 v1, long3 v2) {
+    long3 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern long4 __attribute__((overloadable)) max(long4 v1, long4 v2) {
+    long4 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    r.w = v1.w > v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern uchar __attribute__((overloadable)) max(uchar v1, uchar v2) {
+    return v1 > v2 ? v1 : v2;
+}
+
+extern uchar2 __attribute__((overloadable)) max(uchar2 v1, uchar2 v2) {
+    uchar2 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern uchar3 __attribute__((overloadable)) max(uchar3 v1, uchar3 v2) {
+    uchar3 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern uchar4 __attribute__((overloadable)) max(uchar4 v1, uchar4 v2) {
+    uchar4 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    r.w = v1.w > v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern ushort __attribute__((overloadable)) max(ushort v1, ushort v2) {
+    return v1 > v2 ? v1 : v2;
+}
+
+extern ushort2 __attribute__((overloadable)) max(ushort2 v1, ushort2 v2) {
+    ushort2 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern ushort3 __attribute__((overloadable)) max(ushort3 v1, ushort3 v2) {
+    ushort3 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern ushort4 __attribute__((overloadable)) max(ushort4 v1, ushort4 v2) {
+    ushort4 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    r.w = v1.w > v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern uint __attribute__((overloadable)) max(uint v1, uint v2) {
+    return v1 > v2 ? v1 : v2;
+}
+
+extern uint2 __attribute__((overloadable)) max(uint2 v1, uint2 v2) {
+    uint2 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern uint3 __attribute__((overloadable)) max(uint3 v1, uint3 v2) {
+    uint3 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern uint4 __attribute__((overloadable)) max(uint4 v1, uint4 v2) {
+    uint4 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    r.w = v1.w > v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern ulong __attribute__((overloadable)) max(ulong v1, ulong v2) {
+    return v1 > v2 ? v1 : v2;
+}
+
+extern ulong2 __attribute__((overloadable)) max(ulong2 v1, ulong2 v2) {
+    ulong2 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern ulong3 __attribute__((overloadable)) max(ulong3 v1, ulong3 v2) {
+    ulong3 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern ulong4 __attribute__((overloadable)) max(ulong4 v1, ulong4 v2) {
+    ulong4 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    r.w = v1.w > v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern float __attribute__((overloadable)) max(float v1, float v2) {
+    return fmax(v1, v2);
+}
+
+extern float2 __attribute__((overloadable)) max(float2 v1, float2 v2) {
+    return fmax(v1, v2);
+}
+
+extern float2 __attribute__((overloadable)) max(float2 v1, float v2) {
+    return fmax(v1, v2);
+}
+
+extern float3 __attribute__((overloadable)) max(float3 v1, float3 v2) {
+    return fmax(v1, v2);
+}
+
+extern float3 __attribute__((overloadable)) max(float3 v1, float v2) {
+    return fmax(v1, v2);
+}
+
+extern float4 __attribute__((overloadable)) max(float4 v1, float4 v2) {
+    return fmax(v1, v2);
+}
+
+extern float4 __attribute__((overloadable)) max(float4 v1, float v2) {
+    return fmax(v1, v2);
+}
+
+
+/*
+ * MIN
+ */
+
+extern int8_t __attribute__((overloadable)) min(int8_t v1, int8_t v2) {
+    return v1 < v2 ? v1 : v2;
+}
+
+extern char2 __attribute__((overloadable)) min(char2 v1, char2 v2) {
+    char2 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern char3 __attribute__((overloadable)) min(char3 v1, char3 v2) {
+    char3 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern char4 __attribute__((overloadable)) min(char4 v1, char4 v2) {
+    char4 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    r.w = v1.w < v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern int16_t __attribute__((overloadable)) min(int16_t v1, int16_t v2) {
+    return v1 < v2 ? v1 : v2;
+}
+
+extern short2 __attribute__((overloadable)) min(short2 v1, short2 v2) {
+    short2 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern short3 __attribute__((overloadable)) min(short3 v1, short3 v2) {
+    short3 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern short4 __attribute__((overloadable)) min(short4 v1, short4 v2) {
+    short4 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    r.w = v1.w < v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern int32_t __attribute__((overloadable)) min(int32_t v1, int32_t v2) {
+    return v1 < v2 ? v1 : v2;
+}
+
+extern int2 __attribute__((overloadable)) min(int2 v1, int2 v2) {
+    int2 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern int3 __attribute__((overloadable)) min(int3 v1, int3 v2) {
+    int3 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern int4 __attribute__((overloadable)) min(int4 v1, int4 v2) {
+    int4 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    r.w = v1.w < v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern int64_t __attribute__((overloadable)) min(int64_t v1, int64_t v2) {
+    return v1 < v2 ? v1 : v2;
+}
+
+extern long2 __attribute__((overloadable)) min(long2 v1, long2 v2) {
+    long2 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern long3 __attribute__((overloadable)) min(long3 v1, long3 v2) {
+    long3 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern long4 __attribute__((overloadable)) min(long4 v1, long4 v2) {
+    long4 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    r.w = v1.w < v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern uchar __attribute__((overloadable)) min(uchar v1, uchar v2) {
+    return v1 < v2 ? v1 : v2;
+}
+
+extern uchar2 __attribute__((overloadable)) min(uchar2 v1, uchar2 v2) {
+    uchar2 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern uchar3 __attribute__((overloadable)) min(uchar3 v1, uchar3 v2) {
+    uchar3 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern uchar4 __attribute__((overloadable)) min(uchar4 v1, uchar4 v2) {
+    uchar4 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    r.w = v1.w < v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern ushort __attribute__((overloadable)) min(ushort v1, ushort v2) {
+    return v1 < v2 ? v1 : v2;
+}
+
+extern ushort2 __attribute__((overloadable)) min(ushort2 v1, ushort2 v2) {
+    ushort2 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern ushort3 __attribute__((overloadable)) min(ushort3 v1, ushort3 v2) {
+    ushort3 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern ushort4 __attribute__((overloadable)) min(ushort4 v1, ushort4 v2) {
+    ushort4 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    r.w = v1.w < v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern uint __attribute__((overloadable)) min(uint v1, uint v2) {
+    return v1 < v2 ? v1 : v2;
+}
+
+extern uint2 __attribute__((overloadable)) min(uint2 v1, uint2 v2) {
+    uint2 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern uint3 __attribute__((overloadable)) min(uint3 v1, uint3 v2) {
+    uint3 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern uint4 __attribute__((overloadable)) min(uint4 v1, uint4 v2) {
+    uint4 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    r.w = v1.w < v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern ulong __attribute__((overloadable)) min(ulong v1, ulong v2) {
+    return v1 < v2 ? v1 : v2;
+}
+
+extern ulong2 __attribute__((overloadable)) min(ulong2 v1, ulong2 v2) {
+    ulong2 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern ulong3 __attribute__((overloadable)) min(ulong3 v1, ulong3 v2) {
+    ulong3 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern ulong4 __attribute__((overloadable)) min(ulong4 v1, ulong4 v2) {
+    ulong4 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    r.w = v1.w < v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern float __attribute__((overloadable)) min(float v1, float v2) {
+    return fmin(v1, v2);
+}
+
+extern float2 __attribute__((overloadable)) min(float2 v1, float2 v2) {
+    return fmin(v1, v2);
+}
+
+extern float2 __attribute__((overloadable)) min(float2 v1, float v2) {
+    return fmin(v1, v2);
+}
+
+extern float3 __attribute__((overloadable)) min(float3 v1, float3 v2) {
+    return fmin(v1, v2);
+}
+
+extern float3 __attribute__((overloadable)) min(float3 v1, float v2) {
+    return fmin(v1, v2);
+}
+
+extern float4 __attribute__((overloadable)) min(float4 v1, float4 v2) {
+    return fmin(v1, v2);
+}
+
+extern float4 __attribute__((overloadable)) min(float4 v1, float v2) {
+    return fmin(v1, v2);
+}
+
+/*
+ * YUV
+ */
+
+extern uchar4 __attribute__((overloadable)) rsYuvToRGBA_uchar4(uchar y, uchar u, uchar v) {
+    short Y = ((short)y) - 16;
+    short U = ((short)u) - 128;
+    short V = ((short)v) - 128;
+
+    short4 p;
+    p.r = (Y * 298 + V * 409 + 128) >> 8;
+    p.g = (Y * 298 - U * 100 - V * 208 + 128) >> 8;
+    p.b = (Y * 298 + U * 516 + 128) >> 8;
+    p.a = 255;
+    p.r = rsClamp(p.r, (short)0, (short)255);
+    p.g = rsClamp(p.g, (short)0, (short)255);
+    p.b = rsClamp(p.b, (short)0, (short)255);
+
+    return convert_uchar4(p);
+}
+
+static float4 yuv_U_values = {0.f, -0.392f * 0.003921569f, +2.02 * 0.003921569f, 0.f};
+static float4 yuv_V_values = {1.603f * 0.003921569f, -0.815f * 0.003921569f, 0.f, 0.f};
+
+extern float4 __attribute__((overloadable)) rsYuvToRGBA_float4(uchar y, uchar u, uchar v) {
+    float4 color = (float)y * 0.003921569f;
+    float4 fU = ((float)u) - 128.f;
+    float4 fV = ((float)v) - 128.f;
+
+    color += fU * yuv_U_values;
+    color += fV * yuv_V_values;
+    color = clamp(color, 0.f, 1.f);
+    return color;
+}
+
+
+/*
+ * half_RECIP
+ */
+
+extern float __attribute__((overloadable)) half_recip(float v) {
+    // FIXME:  actual algorithm for generic approximate reciprocal
+    return 1.f / v;
+}
+
+extern float2 __attribute__((overloadable)) half_recip(float2 v) {
+    float2 r;
+    r.x = half_recip(r.x);
+    r.y = half_recip(r.y);
+    return r;
+}
+
+extern float3 __attribute__((overloadable)) half_recip(float3 v) {
+    float3 r;
+    r.x = half_recip(r.x);
+    r.y = half_recip(r.y);
+    r.z = half_recip(r.z);
+    return r;
+}
+
+extern float4 __attribute__((overloadable)) half_recip(float4 v) {
+    float4 r;
+    r.x = half_recip(r.x);
+    r.y = half_recip(r.y);
+    r.z = half_recip(r.z);
+    r.w = half_recip(r.w);
+    return r;
+}
+
+
+/*
+ * half_SQRT
+ */
+
+extern float __attribute__((overloadable)) half_sqrt(float v) {
+    return sqrt(v);
+}
+
+extern float2 __attribute__((overloadable)) half_sqrt(float2 v) {
+    float2 r;
+    r.x = half_sqrt(v.x);
+    r.y = half_sqrt(v.y);
+    return r;
+}
+
+extern float3 __attribute__((overloadable)) half_sqrt(float3 v) {
+    float3 r;
+    r.x = half_sqrt(v.x);
+    r.y = half_sqrt(v.y);
+    r.z = half_sqrt(v.z);
+    return r;
+}
+
+extern float4 __attribute__((overloadable)) half_sqrt(float4 v) {
+    float4 r;
+    r.x = half_sqrt(v.x);
+    r.y = half_sqrt(v.y);
+    r.z = half_sqrt(v.z);
+    r.w = half_sqrt(v.w);
+    return r;
+}
+
+
+/*
+ * half_rsqrt
+ */
+
+extern float __attribute__((overloadable)) half_rsqrt(float v) {
+    return 1.f / sqrt(v);
+}
+
+extern float2 __attribute__((overloadable)) half_rsqrt(float2 v) {
+    float2 r;
+    r.x = half_rsqrt(v.x);
+    r.y = half_rsqrt(v.y);
+    return r;
+}
+
+extern float3 __attribute__((overloadable)) half_rsqrt(float3 v) {
+    float3 r;
+    r.x = half_rsqrt(v.x);
+    r.y = half_rsqrt(v.y);
+    r.z = half_rsqrt(v.z);
+    return r;
+}
+
+extern float4 __attribute__((overloadable)) half_rsqrt(float4 v) {
+    float4 r;
+    r.x = half_rsqrt(v.x);
+    r.y = half_rsqrt(v.y);
+    r.z = half_rsqrt(v.z);
+    r.w = half_rsqrt(v.w);
+    return r;
+}
+
+/**
+ * matrix ops
+ */
+
+extern float4 __attribute__((overloadable))
+rsMatrixMultiply(const rs_matrix4x4 *m, float4 in) {
+    float4 ret;
+    ret.x = (m->m[0] * in.x) + (m->m[4] * in.y) + (m->m[8] * in.z) + (m->m[12] * in.w);
+    ret.y = (m->m[1] * in.x) + (m->m[5] * in.y) + (m->m[9] * in.z) + (m->m[13] * in.w);
+    ret.z = (m->m[2] * in.x) + (m->m[6] * in.y) + (m->m[10] * in.z) + (m->m[14] * in.w);
+    ret.w = (m->m[3] * in.x) + (m->m[7] * in.y) + (m->m[11] * in.z) + (m->m[15] * in.w);
+    return ret;
+}
+
+extern float4 __attribute__((overloadable))
+rsMatrixMultiply(const rs_matrix4x4 *m, float3 in) {
+    float4 ret;
+    ret.x = (m->m[0] * in.x) + (m->m[4] * in.y) + (m->m[8] * in.z) + m->m[12];
+    ret.y = (m->m[1] * in.x) + (m->m[5] * in.y) + (m->m[9] * in.z) + m->m[13];
+    ret.z = (m->m[2] * in.x) + (m->m[6] * in.y) + (m->m[10] * in.z) + m->m[14];
+    ret.w = (m->m[3] * in.x) + (m->m[7] * in.y) + (m->m[11] * in.z) + m->m[15];
+    return ret;
+}
+
+extern float4 __attribute__((overloadable))
+rsMatrixMultiply(const rs_matrix4x4 *m, float2 in) {
+    float4 ret;
+    ret.x = (m->m[0] * in.x) + (m->m[4] * in.y) + m->m[12];
+    ret.y = (m->m[1] * in.x) + (m->m[5] * in.y) + m->m[13];
+    ret.z = (m->m[2] * in.x) + (m->m[6] * in.y) + m->m[14];
+    ret.w = (m->m[3] * in.x) + (m->m[7] * in.y) + m->m[15];
+    return ret;
+}
+
+extern float3 __attribute__((overloadable))
+rsMatrixMultiply(const rs_matrix3x3 *m, float3 in) {
+    float3 ret;
+    ret.x = (m->m[0] * in.x) + (m->m[3] * in.y) + (m->m[6] * in.z);
+    ret.y = (m->m[1] * in.x) + (m->m[4] * in.y) + (m->m[7] * in.z);
+    ret.z = (m->m[2] * in.x) + (m->m[5] * in.y) + (m->m[8] * in.z);
+    return ret;
+}
+
+extern float3 __attribute__((overloadable))
+rsMatrixMultiply(const rs_matrix3x3 *m, float2 in) {
+    float3 ret;
+    ret.x = (m->m[0] * in.x) + (m->m[3] * in.y);
+    ret.y = (m->m[1] * in.x) + (m->m[4] * in.y);
+    ret.z = (m->m[2] * in.x) + (m->m[5] * in.y);
+    return ret;
+}
+
+/**
+ * Pixel Ops
+ */
+extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b)
+{
+    uchar4 c;
+    c.x = (uchar)clamp((r * 255.f + 0.5f), 0.f, 255.f);
+    c.y = (uchar)clamp((g * 255.f + 0.5f), 0.f, 255.f);
+    c.z = (uchar)clamp((b * 255.f + 0.5f), 0.f, 255.f);
+    c.w = 255;
+    return c;
+}
+
+extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b, float a)
+{
+    uchar4 c;
+    c.x = (uchar)clamp((r * 255.f + 0.5f), 0.f, 255.f);
+    c.y = (uchar)clamp((g * 255.f + 0.5f), 0.f, 255.f);
+    c.z = (uchar)clamp((b * 255.f + 0.5f), 0.f, 255.f);
+    c.w = (uchar)clamp((a * 255.f + 0.5f), 0.f, 255.f);
+    return c;
+}
+
+extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float3 color)
+{
+    color *= 255.f;
+    color += 0.5f;
+    color = clamp(color, 0.f, 255.f);
+    uchar4 c = {color.x, color.y, color.z, 255};
+    return c;
+}
+
+extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float4 color)
+{
+    color *= 255.f;
+    color += 0.5f;
+    color = clamp(color, 0.f, 255.f);
+    uchar4 c = {color.x, color.y, color.z, color.w};
+    return c;
+}
+
diff --git a/driver/runtime/arch/neon.ll b/driver/runtime/arch/neon.ll
new file mode 100644
index 0000000..4a1172b
--- /dev/null
+++ b/driver/runtime/arch/neon.ll
@@ -0,0 +1,1172 @@
+target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:64:128-a0:0:64-n32-S64"
+target triple = "armv7-none-linux-gnueabi"
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;               INTRINSICS               ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+declare <2 x float> @llvm.arm.neon.vmaxs.v2f32(<2 x float>, <2 x float>) nounwind readnone
+declare <4 x float> @llvm.arm.neon.vmaxs.v4f32(<4 x float>, <4 x float>) nounwind readnone
+declare <2 x i32> @llvm.arm.neon.vmaxs.v2i32(<2 x i32>, <2 x i32>) nounwind readnone
+declare <4 x i32> @llvm.arm.neon.vmaxs.v4i32(<4 x i32>, <4 x i32>) nounwind readnone
+declare <2 x i32> @llvm.arm.neon.vmaxu.v2i32(<2 x i32>, <2 x i32>) nounwind readnone
+declare <4 x i32> @llvm.arm.neon.vmaxu.v4i32(<4 x i32>, <4 x i32>) nounwind readnone
+declare <4 x i16> @llvm.arm.neon.vmaxs.v4i16(<4 x i16>, <4 x i16>) nounwind readnone
+declare <4 x i16> @llvm.arm.neon.vmaxu.v4i16(<4 x i16>, <4 x i16>) nounwind readnone
+
+declare <2 x float> @llvm.arm.neon.vmins.v2f32(<2 x float>, <2 x float>) nounwind readnone
+declare <4 x float> @llvm.arm.neon.vmins.v4f32(<4 x float>, <4 x float>) nounwind readnone
+declare <2 x i32> @llvm.arm.neon.vmins.v2i32(<2 x i32>, <2 x i32>) nounwind readnone
+declare <4 x i32> @llvm.arm.neon.vmins.v4i32(<4 x i32>, <4 x i32>) nounwind readnone
+declare <2 x i32> @llvm.arm.neon.vminu.v2i32(<2 x i32>, <2 x i32>) nounwind readnone
+declare <4 x i32> @llvm.arm.neon.vminu.v4i32(<4 x i32>, <4 x i32>) nounwind readnone
+declare <4 x i16> @llvm.arm.neon.vmins.v4i16(<4 x i16>, <4 x i16>) nounwind readnone
+declare <4 x i16> @llvm.arm.neon.vminu.v4i16(<4 x i16>, <4 x i16>) nounwind readnone
+
+declare <8 x i8>  @llvm.arm.neon.vqshiftns.v8i8(<8 x i16>, <8 x i16>) nounwind readnone
+declare <4 x i16> @llvm.arm.neon.vqshiftns.v4i16(<4 x i32>, <4 x i32>) nounwind readnone
+declare <2 x i32> @llvm.arm.neon.vqshiftns.v2i32(<2 x i64>, <2 x i64>) nounwind readnone
+
+declare <8 x i8>  @llvm.arm.neon.vqshiftnu.v8i8(<8 x i16>, <8 x i16>) nounwind readnone
+declare <4 x i16> @llvm.arm.neon.vqshiftnu.v4i16(<4 x i32>, <4 x i32>) nounwind readnone
+declare <2 x i32> @llvm.arm.neon.vqshiftnu.v2i32(<2 x i64>, <2 x i64>) nounwind readnone
+
+declare <8 x i8>  @llvm.arm.neon.vqshiftnsu.v8i8(<8 x i16>, <8 x i16>) nounwind readnone
+declare <4 x i16> @llvm.arm.neon.vqshiftnsu.v4i16(<4 x i32>, <4 x i32>) nounwind readnone
+declare <2 x i32> @llvm.arm.neon.vqshiftnsu.v2i32(<2 x i64>, <2 x i64>) nounwind readnone
+
+declare <2 x float> @llvm.arm.neon.vrecpe.v2f32(<2 x float>) nounwind readnone
+declare <4 x float> @llvm.arm.neon.vrecpe.v4f32(<4 x float>) nounwind readnone
+
+declare <2 x float> @llvm.arm.neon.vrsqrte.v2f32(<2 x float>) nounwind readnone
+declare <4 x float> @llvm.arm.neon.vrsqrte.v4f32(<4 x float>) nounwind readnone
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;                HELPERS                 ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+define internal <4 x float> @smear_4f(float %in) nounwind readnone alwaysinline {
+  %1 = insertelement <4 x float> undef, float %in, i32 0
+  %2 = insertelement <4 x float> %1, float %in, i32 1
+  %3 = insertelement <4 x float> %2, float %in, i32 2
+  %4 = insertelement <4 x float> %3, float %in, i32 3
+  ret <4 x float> %4
+}
+
+define internal <4 x i32> @smear_4i(i32 %in) nounwind readnone alwaysinline {
+  %1 = insertelement <4 x i32> undef, i32 %in, i32 0
+  %2 = insertelement <4 x i32> %1, i32 %in, i32 1
+  %3 = insertelement <4 x i32> %2, i32 %in, i32 2
+  %4 = insertelement <4 x i32> %3, i32 %in, i32 3
+  ret <4 x i32> %4
+}
+
+define internal <4 x i16> @smear_4s(i16 %in) nounwind readnone alwaysinline {
+  %1 = insertelement <4 x i16> undef, i16 %in, i32 0
+  %2 = insertelement <4 x i16> %1, i16 %in, i32 1
+  %3 = insertelement <4 x i16> %2, i16 %in, i32 2
+  %4 = insertelement <4 x i16> %3, i16 %in, i32 3
+  ret <4 x i16> %4
+}
+
+
+
+define internal <2 x float> @smear_2f(float %in) nounwind readnone alwaysinline {
+  %1 = insertelement <2 x float> undef, float %in, i32 0
+  %2 = insertelement <2 x float> %1, float %in, i32 1
+  ret <2 x float> %2
+}
+
+define internal <2 x i32> @smear_2i(i32 %in) nounwind readnone alwaysinline {
+  %1 = insertelement <2 x i32> undef, i32 %in, i32 0
+  %2 = insertelement <2 x i32> %1, i32 %in, i32 1
+  ret <2 x i32> %2
+}
+
+define internal <2 x i16> @smear_2s(i16 %in) nounwind readnone alwaysinline {
+  %1 = insertelement <2 x i16> undef, i16 %in, i32 0
+  %2 = insertelement <2 x i16> %1, i16 %in, i32 1
+  ret <2 x i16> %2
+}
+
+
+define internal <4 x i32> @smear_4i32(i32 %in) nounwind readnone alwaysinline {
+  %1 = insertelement <4 x i32> undef, i32 %in, i32 0
+  %2 = insertelement <4 x i32> %1, i32 %in, i32 1
+  %3 = insertelement <4 x i32> %2, i32 %in, i32 2
+  %4 = insertelement <4 x i32> %3, i32 %in, i32 3
+  ret <4 x i32> %4
+}
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;                 CLAMP                  ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+define <4 x float> @_Z5clampDv4_fS_S_(<4 x float> %value, <4 x float> %low, <4 x float> %high) nounwind readonly {
+  %1 = tail call <4 x float> @llvm.arm.neon.vmins.v4f32(<4 x float> %value, <4 x float> %high) nounwind readnone
+  %2 = tail call <4 x float> @llvm.arm.neon.vmaxs.v4f32(<4 x float> %1, <4 x float> %low) nounwind readnone
+  ret <4 x float> %2
+}
+
+define <4 x float> @_Z5clampDv4_fff(<4 x float> %value, float %low, float %high) nounwind readonly {
+  %_high = tail call <4 x float> @smear_4f(float %high) nounwind readnone
+  %_low = tail call <4 x float> @smear_4f(float %low) nounwind readnone
+  %out = tail call <4 x float> @_Z5clampDv4_fS_S_(<4 x float> %value, <4 x float> %_low, <4 x float> %_high) nounwind readonly
+  ret <4 x float> %out
+}
+
+define <3 x float> @_Z5clampDv3_fS_S_(<3 x float> %value, <3 x float> %low, <3 x float> %high) nounwind readonly {
+  %_value = shufflevector <3 x float> %value, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %_low = shufflevector <3 x float> %low, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %_high = shufflevector <3 x float> %high, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %a = tail call <4 x float> @llvm.arm.neon.vmins.v4f32(<4 x float> %_value, <4 x float> %_high) nounwind readnone
+  %b = tail call <4 x float> @llvm.arm.neon.vmaxs.v4f32(<4 x float> %a, <4 x float> %_low) nounwind readnone
+  %c = shufflevector <4 x float> %b, <4 x float> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x float> %c
+}
+
+define <3 x float> @_Z5clampDv3_fff(<3 x float> %value, float %low, float %high) nounwind readonly {
+  %_value = shufflevector <3 x float> %value, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %_high = tail call <4 x float> @smear_4f(float %high) nounwind readnone
+  %_low = tail call <4 x float> @smear_4f(float %low) nounwind readnone
+  %a = tail call <4 x float> @llvm.arm.neon.vmins.v4f32(<4 x float> %_value, <4 x float> %_high) nounwind readnone
+  %b = tail call <4 x float> @llvm.arm.neon.vmaxs.v4f32(<4 x float> %a, <4 x float> %_low) nounwind readnone
+  %c = shufflevector <4 x float> %b, <4 x float> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x float> %c
+}
+
+define <2 x float> @_Z5clampDv2_fS_S_(<2 x float> %value, <2 x float> %low, <2 x float> %high) nounwind readonly {
+  %1 = tail call <2 x float> @llvm.arm.neon.vmins.v2f32(<2 x float> %value, <2 x float> %high) nounwind readnone
+  %2 = tail call <2 x float> @llvm.arm.neon.vmaxs.v2f32(<2 x float> %1, <2 x float> %low) nounwind readnone
+  ret <2 x float> %2
+}
+
+define <2 x float> @_Z5clampDv2_fff(<2 x float> %value, float %low, float %high) nounwind readonly {
+  %_high = tail call <2 x float> @smear_2f(float %high) nounwind readnone
+  %_low = tail call <2 x float> @smear_2f(float %low) nounwind readnone
+  %a = tail call <2 x float> @llvm.arm.neon.vmins.v2f32(<2 x float> %value, <2 x float> %_high) nounwind readnone
+  %b = tail call <2 x float> @llvm.arm.neon.vmaxs.v2f32(<2 x float> %a, <2 x float> %_low) nounwind readnone
+  ret <2 x float> %b
+}
+
+define float @_Z5clampfff(float %value, float %low, float %high) nounwind readonly {
+  %1 = fcmp olt float %value, %high
+  %2 = select i1 %1, float %value, float %high
+  %3 = fcmp ogt float %2, %low
+  %4 = select i1 %3, float %2, float %low
+  ret float %4
+}
+
+
+
+define <4 x i32> @_Z5clampDv4_iS_S_(<4 x i32> %value, <4 x i32> %low, <4 x i32> %high) nounwind readonly {
+  %1 = tail call <4 x i32> @llvm.arm.neon.vmins.v4i32(<4 x i32> %value, <4 x i32> %high) nounwind readnone
+  %2 = tail call <4 x i32> @llvm.arm.neon.vmaxs.v4i32(<4 x i32> %1, <4 x i32> %low) nounwind readnone
+  ret <4 x i32> %2
+}
+
+define <4 x i32> @_Z5clampDv4_iii(<4 x i32> %value, i32 %low, i32 %high) nounwind readonly {
+  %_high = tail call <4 x i32> @smear_4i(i32 %high) nounwind readnone
+  %_low = tail call <4 x i32> @smear_4i(i32 %low) nounwind readnone
+  %1 = tail call <4 x i32> @llvm.arm.neon.vmins.v4i32(<4 x i32> %value, <4 x i32> %_high) nounwind readnone
+  %2 = tail call <4 x i32> @llvm.arm.neon.vmaxs.v4i32(<4 x i32> %1, <4 x i32> %_low) nounwind readnone
+  ret <4 x i32> %2
+}
+
+define <3 x i32> @_Z5clampDv3_iS_S_(<3 x i32> %value, <3 x i32> %low, <3 x i32> %high) nounwind readonly {
+  %_value = shufflevector <3 x i32> %value, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %_low = shufflevector <3 x i32> %low, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %_high = shufflevector <3 x i32> %high, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %a = tail call <4 x i32> @llvm.arm.neon.vmins.v4i32(<4 x i32> %_value, <4 x i32> %_high) nounwind readnone
+  %b = tail call <4 x i32> @llvm.arm.neon.vmaxs.v4i32(<4 x i32> %a, <4 x i32> %_low) nounwind readnone
+  %c = shufflevector <4 x i32> %b, <4 x i32> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x i32> %c
+}
+
+define <3 x i32> @_Z5clampDv3_iii(<3 x i32> %value, i32 %low, i32 %high) nounwind readonly {
+  %_value = shufflevector <3 x i32> %value, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %_high = tail call <4 x i32> @smear_4i(i32 %high) nounwind readnone
+  %_low = tail call <4 x i32> @smear_4i(i32 %low) nounwind readnone
+  %a = tail call <4 x i32> @llvm.arm.neon.vmins.v4i32(<4 x i32> %_value, <4 x i32> %_high) nounwind readnone
+  %b = tail call <4 x i32> @llvm.arm.neon.vmaxs.v4i32(<4 x i32> %a, <4 x i32> %_low) nounwind readnone
+  %c = shufflevector <4 x i32> %b, <4 x i32> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x i32> %c
+}
+
+define <2 x i32> @_Z5clampDv2_iS_S_(<2 x i32> %value, <2 x i32> %low, <2 x i32> %high) nounwind readonly {
+  %1 = tail call <2 x i32> @llvm.arm.neon.vmins.v2i32(<2 x i32> %value, <2 x i32> %high) nounwind readnone
+  %2 = tail call <2 x i32> @llvm.arm.neon.vmaxs.v2i32(<2 x i32> %1, <2 x i32> %low) nounwind readnone
+  ret <2 x i32> %2
+}
+
+define <2 x i32> @_Z5clampDv2_iii(<2 x i32> %value, i32 %low, i32 %high) nounwind readonly {
+  %_high = tail call <2 x i32> @smear_2i(i32 %high) nounwind readnone
+  %_low = tail call <2 x i32> @smear_2i(i32 %low) nounwind readnone
+  %a = tail call <2 x i32> @llvm.arm.neon.vmins.v2i32(<2 x i32> %value, <2 x i32> %_high) nounwind readnone
+  %b = tail call <2 x i32> @llvm.arm.neon.vmaxs.v2i32(<2 x i32> %a, <2 x i32> %_low) nounwind readnone
+  ret <2 x i32> %b
+}
+
+
+
+define <4 x i32> @_Z5clampDv4_jS_S_(<4 x i32> %value, <4 x i32> %low, <4 x i32> %high) nounwind readonly {
+  %1 = tail call <4 x i32> @llvm.arm.neon.vminu.v4i32(<4 x i32> %value, <4 x i32> %high) nounwind readnone
+  %2 = tail call <4 x i32> @llvm.arm.neon.vmaxu.v4i32(<4 x i32> %1, <4 x i32> %low) nounwind readnone
+  ret <4 x i32> %2
+}
+
+define <4 x i32> @_Z5clampDv4_jjj(<4 x i32> %value, i32 %low, i32 %high) nounwind readonly {
+  %_high = tail call <4 x i32> @smear_4i(i32 %high) nounwind readnone
+  %_low = tail call <4 x i32> @smear_4i(i32 %low) nounwind readnone
+  %1 = tail call <4 x i32> @llvm.arm.neon.vminu.v4i32(<4 x i32> %value, <4 x i32> %_high) nounwind readnone
+  %2 = tail call <4 x i32> @llvm.arm.neon.vmaxu.v4i32(<4 x i32> %1, <4 x i32> %_low) nounwind readnone
+  ret <4 x i32> %2
+}
+
+define <3 x i32> @_Z5clampDv3_jS_S_(<3 x i32> %value, <3 x i32> %low, <3 x i32> %high) nounwind readonly {
+  %_value = shufflevector <3 x i32> %value, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %_low = shufflevector <3 x i32> %low, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %_high = shufflevector <3 x i32> %high, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %a = tail call <4 x i32> @llvm.arm.neon.vminu.v4i32(<4 x i32> %_value, <4 x i32> %_high) nounwind readnone
+  %b = tail call <4 x i32> @llvm.arm.neon.vmaxu.v4i32(<4 x i32> %a, <4 x i32> %_low) nounwind readnone
+  %c = shufflevector <4 x i32> %b, <4 x i32> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x i32> %c
+}
+
+define <3 x i32> @_Z5clampDv3_jjj(<3 x i32> %value, i32 %low, i32 %high) nounwind readonly {
+  %_value = shufflevector <3 x i32> %value, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %_high = tail call <4 x i32> @smear_4i(i32 %high) nounwind readnone
+  %_low = tail call <4 x i32> @smear_4i(i32 %low) nounwind readnone
+  %a = tail call <4 x i32> @llvm.arm.neon.vminu.v4i32(<4 x i32> %_value, <4 x i32> %_high) nounwind readnone
+  %b = tail call <4 x i32> @llvm.arm.neon.vmaxu.v4i32(<4 x i32> %a, <4 x i32> %_low) nounwind readnone
+  %c = shufflevector <4 x i32> %b, <4 x i32> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x i32> %c
+}
+
+define <2 x i32> @_Z5clampDv2_jS_S_(<2 x i32> %value, <2 x i32> %low, <2 x i32> %high) nounwind readonly {
+  %1 = tail call <2 x i32> @llvm.arm.neon.vminu.v2i32(<2 x i32> %value, <2 x i32> %high) nounwind readnone
+  %2 = tail call <2 x i32> @llvm.arm.neon.vmaxu.v2i32(<2 x i32> %1, <2 x i32> %low) nounwind readnone
+  ret <2 x i32> %2
+}
+
+define <2 x i32> @_Z5clampDv2_jjj(<2 x i32> %value, i32 %low, i32 %high) nounwind readonly {
+  %_high = tail call <2 x i32> @smear_2i(i32 %high) nounwind readnone
+  %_low = tail call <2 x i32> @smear_2i(i32 %low) nounwind readnone
+  %a = tail call <2 x i32> @llvm.arm.neon.vminu.v2i32(<2 x i32> %value, <2 x i32> %_high) nounwind readnone
+  %b = tail call <2 x i32> @llvm.arm.neon.vmaxu.v2i32(<2 x i32> %a, <2 x i32> %_low) nounwind readnone
+  ret <2 x i32> %b
+}
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;                  FMAX                  ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+define <4 x float> @_Z4fmaxDv4_fS_(<4 x float> %v1, <4 x float> %v2) nounwind readonly {
+  %1 = tail call <4 x float> @llvm.arm.neon.vmaxs.v4f32(<4 x float> %v1, <4 x float> %v2) nounwind readnone
+  ret <4 x float> %1
+}
+
+define <4 x float> @_Z4fmaxDv4_ff(<4 x float> %v1, float %v2) nounwind readonly {
+  %1 = tail call <4 x float> @smear_4f(float %v2) nounwind readnone
+  %2 = tail call <4 x float> @llvm.arm.neon.vmaxs.v4f32(<4 x float> %v1, <4 x float> %1) nounwind readnone
+  ret <4 x float> %2
+}
+
+define <3 x float> @_Z4fmaxDv3_fS_(<3 x float> %v1, <3 x float> %v2) nounwind readonly {
+  %1 = shufflevector <3 x float> %v1, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %2 = shufflevector <3 x float> %v2, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %3 = tail call <4 x float> @llvm.arm.neon.vmaxs.v4f32(<4 x float> %1, <4 x float> %2) nounwind readnone
+  %4 = shufflevector <4 x float> %3, <4 x float> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x float> %4
+}
+
+define <3 x float> @_Z4fmaxDv3_ff(<3 x float> %v1, float %v2) nounwind readonly {
+  %1 = shufflevector <3 x float> %v1, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %2 = tail call <4 x float> @smear_4f(float %v2) nounwind readnone
+  %3 = tail call <4 x float> @llvm.arm.neon.vmaxs.v4f32(<4 x float> %1, <4 x float> %2) nounwind readnone
+  %c = shufflevector <4 x float> %3, <4 x float> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x float> %c
+}
+
+define <2 x float> @_Z4fmaxDv2_fS_(<2 x float> %v1, <2 x float> %v2) nounwind readonly {
+  %1 = tail call <2 x float> @llvm.arm.neon.vmaxs.v2f32(<2 x float> %v1, <2 x float> %v2) nounwind readnone
+  ret <2 x float> %1
+}
+
+define <2 x float> @_Z4fmaxDv2_ff(<2 x float> %v1, float %v2) nounwind readonly {
+  %1 = tail call <2 x float> @smear_2f(float %v2) nounwind readnone
+  %2 = tail call <2 x float> @llvm.arm.neon.vmaxs.v2f32(<2 x float> %v1, <2 x float> %1) nounwind readnone
+  ret <2 x float> %2
+}
+
+define float @_Z4fmaxff(float %v1, float %v2) nounwind readonly {
+  %1 = fcmp ogt float %v1, %v2
+  %2 = select i1 %1, float %v1, float %v2
+  ret float %2
+}
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;                  FMIN                  ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+define <4 x float> @_Z4fminDv4_fS_(<4 x float> %v1, <4 x float> %v2) nounwind readonly {
+  %1 = tail call <4 x float> @llvm.arm.neon.vmins.v4f32(<4 x float> %v1, <4 x float> %v2) nounwind readnone
+  ret <4 x float> %1
+}
+
+define <4 x float> @_Z4fminDv4_ff(<4 x float> %v1, float %v2) nounwind readonly {
+  %1 = tail call <4 x float> @smear_4f(float %v2) nounwind readnone
+  %2 = tail call <4 x float> @llvm.arm.neon.vmins.v4f32(<4 x float> %v1, <4 x float> %1) nounwind readnone
+  ret <4 x float> %2
+}
+
+define <3 x float> @_Z4fminDv3_fS_(<3 x float> %v1, <3 x float> %v2) nounwind readonly {
+  %1 = shufflevector <3 x float> %v1, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %2 = shufflevector <3 x float> %v2, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %3 = tail call <4 x float> @llvm.arm.neon.vmins.v4f32(<4 x float> %1, <4 x float> %2) nounwind readnone
+  %4 = shufflevector <4 x float> %3, <4 x float> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x float> %4
+}
+
+define <3 x float> @_Z4fminDv3_ff(<3 x float> %v1, float %v2) nounwind readonly {
+  %1 = shufflevector <3 x float> %v1, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %2 = tail call <4 x float> @smear_4f(float %v2) nounwind readnone
+  %3 = tail call <4 x float> @llvm.arm.neon.vmins.v4f32(<4 x float> %1, <4 x float> %2) nounwind readnone
+  %c = shufflevector <4 x float> %3, <4 x float> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x float> %c
+}
+
+define <2 x float> @_Z4fminDv2_fS_(<2 x float> %v1, <2 x float> %v2) nounwind readonly {
+  %1 = tail call <2 x float> @llvm.arm.neon.vmins.v2f32(<2 x float> %v1, <2 x float> %v2) nounwind readnone
+  ret <2 x float> %1
+}
+
+define <2 x float> @_Z4fminDv2_ff(<2 x float> %v1, float %v2) nounwind readonly {
+  %1 = tail call <2 x float> @smear_2f(float %v2) nounwind readnone
+  %2 = tail call <2 x float> @llvm.arm.neon.vmins.v2f32(<2 x float> %v1, <2 x float> %1) nounwind readnone
+  ret <2 x float> %2
+}
+
+define float @_Z4fminff(float %v1, float %v2) nounwind readnone {
+  %1 = fcmp olt float %v1, %v2
+  %2 = select i1 %1, float %v1, float %v2
+  ret float %2
+}
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;                  MAX                   ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+define signext i8 @_Z3maxcc(i8 signext %v1, i8 signext %v2) nounwind readnone {
+  %1 = icmp sgt i8 %v1, %v2
+  %2 = select i1 %1, i8 %v1, i8 %v2
+  ret i8 %2
+}
+
+define <2 x i8> @_Z3maxDv2_cS_(<2 x i8> %v1, <2 x i8> %v2) nounwind readnone {
+  %1 = sext <2 x i8> %v1 to <2 x i32>
+  %2 = sext <2 x i8> %v2 to <2 x i32>
+  %3 = tail call <2 x i32> @llvm.arm.neon.vmaxs.v2i32(<2 x i32> %1, <2 x i32> %2) nounwind readnone
+  %4 = trunc <2 x i32> %3 to <2 x i8>
+  ret <2 x i8> %4
+}
+
+define <3 x i8> @_Z3maxDv3_cS_(<3 x i8> %v1, <3 x i8> %v2) nounwind readnone {
+  %1 = sext <3 x i8> %v1 to <3 x i32>
+  %2 = sext <3 x i8> %v2 to <3 x i32>
+  %3 = shufflevector <3 x i32> %1, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %4 = shufflevector <3 x i32> %2, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %5 = tail call <4 x i32> @llvm.arm.neon.vmaxs.v4i32(<4 x i32> %3, <4 x i32> %4) nounwind readnone
+  %6 = shufflevector <4 x i32> %5, <4 x i32> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  %7 = trunc <3 x i32> %6 to <3 x i8>
+  ret <3 x i8> %7
+}
+
+define <4 x i8> @_Z3maxDv4_cS_(<4 x i8> %v1, <4 x i8> %v2) nounwind readnone {
+  %1 = sext <4 x i8> %v1 to <4 x i32>
+  %2 = sext <4 x i8> %v2 to <4 x i32>
+  %3 = tail call <4 x i32> @llvm.arm.neon.vmaxs.v4i32(<4 x i32> %1, <4 x i32> %2) nounwind readnone
+  %4 = trunc <4 x i32> %3 to <4 x i8>
+  ret <4 x i8> %4
+}
+
+define signext i16 @_Z3maxss(i16 signext %v1, i16 signext %v2) nounwind readnone {
+  %1 = icmp sgt i16 %v1, %v2
+  %2 = select i1 %1, i16 %v1, i16 %v2
+  ret i16 %2
+}
+
+define <2 x i16> @_Z3maxDv2_sS_(<2 x i16> %v1, <2 x i16> %v2) nounwind readnone {
+  %1 = sext <2 x i16> %v1 to <2 x i32>
+  %2 = sext <2 x i16> %v2 to <2 x i32>
+  %3 = tail call <2 x i32> @llvm.arm.neon.vmaxs.v2i32(<2 x i32> %1, <2 x i32> %2) nounwind readnone
+  %4 = trunc <2 x i32> %3 to <2 x i16>
+  ret <2 x i16> %4
+}
+
+define <3 x i16> @_Z3maxDv3_sS_(<3 x i16> %v1, <3 x i16> %v2) nounwind readnone {
+  %1 = sext <3 x i16> %v1 to <3 x i32>
+  %2 = sext <3 x i16> %v2 to <3 x i32>
+  %3 = shufflevector <3 x i32> %1, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %4 = shufflevector <3 x i32> %2, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %5 = tail call <4 x i32> @llvm.arm.neon.vmaxs.v4i32(<4 x i32> %3, <4 x i32> %4) nounwind readnone
+  %6 = shufflevector <4 x i32> %5, <4 x i32> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  %7 = trunc <3 x i32> %6 to <3 x i16>
+  ret <3 x i16> %7
+}
+
+define <4 x i16> @_Z3maxDv4_sS_(<4 x i16> %v1, <4 x i16> %v2) nounwind readnone {
+  %1 = sext <4 x i16> %v1 to <4 x i32>
+  %2 = sext <4 x i16> %v2 to <4 x i32>
+  %3 = tail call <4 x i32> @llvm.arm.neon.vmaxs.v4i32(<4 x i32> %1, <4 x i32> %2) nounwind readnone
+  %4 = trunc <4 x i32> %3 to <4 x i16>
+  ret <4 x i16> %4
+}
+
+define i32 @_Z3maxii(i32 %v1, i32 %v2) nounwind readnone {
+  %1 = icmp sgt i32 %v1, %v2
+  %2 = select i1 %1, i32 %v1, i32 %v2
+  ret i32 %2
+}
+
+define <2 x i32> @_Z3maxDv2_iS_(<2 x i32> %v1, <2 x i32> %v2) nounwind readnone {
+  %1 = tail call <2 x i32> @llvm.arm.neon.vmaxs.v2i32(<2 x i32> %v1, <2 x i32> %v2) nounwind readnone
+  ret <2 x i32> %1
+}
+
+define <3 x i32> @_Z3maxDv3_iS_(<3 x i32> %v1, <3 x i32> %v2) nounwind readnone {
+  %1 = shufflevector <3 x i32> %v1, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %2 = shufflevector <3 x i32> %v2, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %3 = tail call <4 x i32   > @llvm.arm.neon.vmaxs.v4i32(<4 x i32> %1, <4 x i32> %2) nounwind readnone
+  %4 = shufflevector <4 x i32> %3, <4 x i32> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x i32> %4
+}
+
+define <4 x i32> @_Z3maxDv4_iS_(<4 x i32> %v1, <4 x i32> %v2) nounwind readnone {
+  %1 = tail call <4 x i32> @llvm.arm.neon.vmaxs.v4i32(<4 x i32> %v1, <4 x i32> %v2) nounwind readnone
+  ret <4 x i32> %1
+}
+
+define i64 @_Z3maxxx(i64 %v1, i64 %v2) nounwind readnone {
+  %1 = icmp sgt i64 %v1, %v2
+  %2 = select i1 %1, i64 %v1, i64 %v2
+  ret i64 %2
+}
+
+; TODO:  long vector types
+
+define zeroext i8 @_Z3maxhh(i8 zeroext %v1, i8 zeroext %v2) nounwind readnone {
+  %1 = icmp ugt i8 %v1, %v2
+  %2 = select i1 %1, i8 %v1, i8 %v2
+  ret i8 %2
+}
+
+define <2 x i8> @_Z3maxDv2_hS_(<2 x i8> %v1, <2 x i8> %v2) nounwind readnone {
+  %1 = zext <2 x i8> %v1 to <2 x i32>
+  %2 = zext <2 x i8> %v2 to <2 x i32>
+  %3 = tail call <2 x i32> @llvm.arm.neon.vmaxu.v2i32(<2 x i32> %1, <2 x i32> %2) nounwind readnone
+  %4 = trunc <2 x i32> %3 to <2 x i8>
+  ret <2 x i8> %4
+}
+
+define <3 x i8> @_Z3maxDv3_hS_(<3 x i8> %v1, <3 x i8> %v2) nounwind readnone {
+  %1 = zext <3 x i8> %v1 to <3 x i32>
+  %2 = zext <3 x i8> %v2 to <3 x i32>
+  %3 = shufflevector <3 x i32> %1, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %4 = shufflevector <3 x i32> %2, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %5 = tail call <4 x i32> @llvm.arm.neon.vmaxu.v4i32(<4 x i32> %3, <4 x i32> %4) nounwind readnone
+  %6 = shufflevector <4 x i32> %5, <4 x i32> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  %7 = trunc <3 x i32> %6 to <3 x i8>
+  ret <3 x i8> %7
+}
+
+define <4 x i8> @_Z3maxDv4_hS_(<4 x i8> %v1, <4 x i8> %v2) nounwind readnone {
+  %1 = zext <4 x i8> %v1 to <4 x i32>
+  %2 = zext <4 x i8> %v2 to <4 x i32>
+  %3 = tail call <4 x i32> @llvm.arm.neon.vmaxu.v4i32(<4 x i32> %1, <4 x i32> %2) nounwind readnone
+  %4 = trunc <4 x i32> %3 to <4 x i8>
+  ret <4 x i8> %4
+}
+
+define zeroext i16 @_Z3maxtt(i16 zeroext %v1, i16 zeroext %v2) nounwind readnone {
+  %1 = icmp ugt i16 %v1, %v2
+  %2 = select i1 %1, i16 %v1, i16 %v2
+  ret i16 %2
+}
+
+define <2 x i16> @_Z3maxDv2_tS_(<2 x i16> %v1, <2 x i16> %v2) nounwind readnone {
+  %1 = zext <2 x i16> %v1 to <2 x i32>
+  %2 = zext <2 x i16> %v2 to <2 x i32>
+  %3 = tail call <2 x i32> @llvm.arm.neon.vmaxu.v2i32(<2 x i32> %1, <2 x i32> %2) nounwind readnone
+  %4 = trunc <2 x i32> %3 to <2 x i16>
+  ret <2 x i16> %4
+}
+
+define <3 x i16> @_Z3maxDv3_tS_(<3 x i16> %v1, <3 x i16> %v2) nounwind readnone {
+  %1 = zext <3 x i16> %v1 to <3 x i32>
+  %2 = zext <3 x i16> %v2 to <3 x i32>
+  %3 = shufflevector <3 x i32> %1, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %4 = shufflevector <3 x i32> %2, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %5 = tail call <4 x i32> @llvm.arm.neon.vmaxu.v4i32(<4 x i32> %3, <4 x i32> %4) nounwind readnone
+  %6 = shufflevector <4 x i32> %5, <4 x i32> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  %7 = trunc <3 x i32> %6 to <3 x i16>
+  ret <3 x i16> %7
+}
+
+define <4 x i16> @_Z3maxDv4_tS_(<4 x i16> %v1, <4 x i16> %v2) nounwind readnone {
+  %1 = zext <4 x i16> %v1 to <4 x i32>
+  %2 = zext <4 x i16> %v2 to <4 x i32>
+  %3 = tail call <4 x i32> @llvm.arm.neon.vmaxu.v4i32(<4 x i32> %1, <4 x i32> %2) nounwind readnone
+  %4 = trunc <4 x i32> %3 to <4 x i16>
+  ret <4 x i16> %4
+}
+
+define i32 @_Z3maxjj(i32 %v1, i32 %v2) nounwind readnone {
+  %1 = icmp ugt i32 %v1, %v2
+  %2 = select i1 %1, i32 %v1, i32 %v2
+  ret i32 %2
+}
+
+define <2 x i32> @_Z3maxDv2_jS_(<2 x i32> %v1, <2 x i32> %v2) nounwind readnone {
+  %1 = tail call <2 x i32> @llvm.arm.neon.vmaxu.v2i32(<2 x i32> %v1, <2 x i32> %v2) nounwind readnone
+  ret <2 x i32> %1
+}
+
+define <3 x i32> @_Z3maxDv3_jS_(<3 x i32> %v1, <3 x i32> %v2) nounwind readnone {
+  %1 = shufflevector <3 x i32> %v1, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %2 = shufflevector <3 x i32> %v2, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %3 = tail call <4 x i32   > @llvm.arm.neon.vmaxu.v4i32(<4 x i32> %1, <4 x i32> %2) nounwind readnone
+  %4 = shufflevector <4 x i32> %3, <4 x i32> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x i32> %4
+}
+
+define <4 x i32> @_Z3maxDv4_jS_(<4 x i32> %v1, <4 x i32> %v2) nounwind readnone {
+  %1 = tail call <4 x i32> @llvm.arm.neon.vmaxu.v4i32(<4 x i32> %v1, <4 x i32> %v2) nounwind readnone
+  ret <4 x i32> %1
+}
+
+define i64 @_Z3maxyy(i64 %v1, i64 %v2) nounwind readnone {
+  %1 = icmp ugt i64 %v1, %v2
+  %2 = select i1 %1, i64 %v1, i64 %v2
+  ret i64 %2
+}
+
+; TODO:  long vector types
+
+define float @_Z3maxff(float %v1, float %v2) nounwind readnone {
+  %1 = tail call float @_Z4fmaxff(float %v1, float %v2)
+  ret float %1
+}
+
+define <2 x float> @_Z3maxDv2_fS_(<2 x float> %v1, <2 x float> %v2) nounwind readnone {
+  %1 = tail call <2 x float> @_Z4fmaxDv2_fS_(<2 x float> %v1, <2 x float> %v2)
+  ret <2 x float> %1
+}
+
+define <2 x float> @_Z3maxDv2_ff(<2 x float> %v1, float %v2) nounwind readnone {
+  %1 = tail call <2 x float> @_Z4fmaxDv2_ff(<2 x float> %v1, float %v2)
+  ret <2 x float> %1
+}
+
+define <3 x float> @_Z3maxDv3_fS_(<3 x float> %v1, <3 x float> %v2) nounwind readnone {
+  %1 = tail call <3 x float> @_Z4fmaxDv3_fS_(<3 x float> %v1, <3 x float> %v2)
+  ret <3 x float> %1
+}
+
+define <3 x float> @_Z3maxDv3_ff(<3 x float> %v1, float %v2) nounwind readnone {
+  %1 = tail call <3 x float> @_Z4fmaxDv3_ff(<3 x float> %v1, float %v2)
+  ret <3 x float> %1
+}
+
+define <4 x float> @_Z3maxDv4_fS_(<4 x float> %v1, <4 x float> %v2) nounwind readnone {
+  %1 = tail call <4 x float> @_Z4fmaxDv4_fS_(<4 x float> %v1, <4 x float> %v2)
+  ret <4 x float> %1
+}
+
+define <4 x float> @_Z3maxDv4_ff(<4 x float> %v1, float %v2) nounwind readnone {
+  %1 = tail call <4 x float> @_Z4fmaxDv4_ff(<4 x float> %v1, float %v2)
+  ret <4 x float> %1
+}
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;                  MIN                   ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+define signext i8 @_Z3mincc(i8 signext %v1, i8 signext %v2) nounwind readnone {
+  %1 = icmp slt i8 %v1, %v2
+  %2 = select i1 %1, i8 %v1, i8 %v2
+  ret i8 %2
+}
+
+define <2 x i8> @_Z3minDv2_cS_(<2 x i8> %v1, <2 x i8> %v2) nounwind readnone {
+  %1 = sext <2 x i8> %v1 to <2 x i32>
+  %2 = sext <2 x i8> %v2 to <2 x i32>
+  %3 = tail call <2 x i32> @llvm.arm.neon.vmins.v2i32(<2 x i32> %1, <2 x i32> %2) nounwind readnone
+  %4 = trunc <2 x i32> %3 to <2 x i8>
+  ret <2 x i8> %4
+}
+
+define <3 x i8> @_Z3minDv3_cS_(<3 x i8> %v1, <3 x i8> %v2) nounwind readnone {
+  %1 = sext <3 x i8> %v1 to <3 x i32>
+  %2 = sext <3 x i8> %v2 to <3 x i32>
+  %3 = shufflevector <3 x i32> %1, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %4 = shufflevector <3 x i32> %2, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %5 = tail call <4 x i32> @llvm.arm.neon.vmins.v4i32(<4 x i32> %3, <4 x i32> %4) nounwind readnone
+  %6 = shufflevector <4 x i32> %5, <4 x i32> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  %7 = trunc <3 x i32> %6 to <3 x i8>
+  ret <3 x i8> %7
+}
+
+define <4 x i8> @_Z3minDv4_cS_(<4 x i8> %v1, <4 x i8> %v2) nounwind readnone {
+  %1 = sext <4 x i8> %v1 to <4 x i32>
+  %2 = sext <4 x i8> %v2 to <4 x i32>
+  %3 = tail call <4 x i32> @llvm.arm.neon.vmins.v4i32(<4 x i32> %1, <4 x i32> %2) nounwind readnone
+  %4 = trunc <4 x i32> %3 to <4 x i8>
+  ret <4 x i8> %4
+}
+
+define signext i16 @_Z3minss(i16 signext %v1, i16 signext %v2) nounwind readnone {
+  %1 = icmp slt i16 %v1, %v2
+  %2 = select i1 %1, i16 %v1, i16 %v2
+  ret i16 %2
+}
+
+define <2 x i16> @_Z3minDv2_sS_(<2 x i16> %v1, <2 x i16> %v2) nounwind readnone {
+  %1 = sext <2 x i16> %v1 to <2 x i32>
+  %2 = sext <2 x i16> %v2 to <2 x i32>
+  %3 = tail call <2 x i32> @llvm.arm.neon.vmins.v2i32(<2 x i32> %1, <2 x i32> %2) nounwind readnone
+  %4 = trunc <2 x i32> %3 to <2 x i16>
+  ret <2 x i16> %4
+}
+
+define <3 x i16> @_Z3minDv3_sS_(<3 x i16> %v1, <3 x i16> %v2) nounwind readnone {
+  %1 = sext <3 x i16> %v1 to <3 x i32>
+  %2 = sext <3 x i16> %v2 to <3 x i32>
+  %3 = shufflevector <3 x i32> %1, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %4 = shufflevector <3 x i32> %2, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %5 = tail call <4 x i32> @llvm.arm.neon.vmins.v4i32(<4 x i32> %3, <4 x i32> %4) nounwind readnone
+  %6 = shufflevector <4 x i32> %5, <4 x i32> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  %7 = trunc <3 x i32> %6 to <3 x i16>
+  ret <3 x i16> %7
+}
+
+define <4 x i16> @_Z3minDv4_sS_(<4 x i16> %v1, <4 x i16> %v2) nounwind readnone {
+  %1 = sext <4 x i16> %v1 to <4 x i32>
+  %2 = sext <4 x i16> %v2 to <4 x i32>
+  %3 = tail call <4 x i32> @llvm.arm.neon.vmins.v4i32(<4 x i32> %1, <4 x i32> %2) nounwind readnone
+  %4 = trunc <4 x i32> %3 to <4 x i16>
+  ret <4 x i16> %4
+}
+
+define i32 @_Z3minii(i32 %v1, i32 %v2) nounwind readnone {
+  %1 = icmp slt i32 %v1, %v2
+  %2 = select i1 %1, i32 %v1, i32 %v2
+  ret i32 %2
+}
+
+define <2 x i32> @_Z3minDv2_iS_(<2 x i32> %v1, <2 x i32> %v2) nounwind readnone {
+  %1 = tail call <2 x i32> @llvm.arm.neon.vmins.v2i32(<2 x i32> %v1, <2 x i32> %v2) nounwind readnone
+  ret <2 x i32> %1
+}
+
+define <3 x i32> @_Z3minDv3_iS_(<3 x i32> %v1, <3 x i32> %v2) nounwind readnone {
+  %1 = shufflevector <3 x i32> %v1, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %2 = shufflevector <3 x i32> %v2, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %3 = tail call <4 x i32   > @llvm.arm.neon.vmins.v4i32(<4 x i32> %1, <4 x i32> %2) nounwind readnone
+  %4 = shufflevector <4 x i32> %3, <4 x i32> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x i32> %4
+}
+
+define <4 x i32> @_Z3minDv4_iS_(<4 x i32> %v1, <4 x i32> %v2) nounwind readnone {
+  %1 = tail call <4 x i32> @llvm.arm.neon.vmins.v4i32(<4 x i32> %v1, <4 x i32> %v2) nounwind readnone
+  ret <4 x i32> %1
+}
+
+define i64 @_Z3minxx(i64 %v1, i64 %v2) nounwind readnone {
+  %1 = icmp slt i64 %v1, %v2
+  %2 = select i1 %1, i64 %v1, i64 %v2
+  ret i64 %2
+}
+
+; TODO:  long vector types
+
+define zeroext i8 @_Z3minhh(i8 zeroext %v1, i8 zeroext %v2) nounwind readnone {
+  %1 = icmp ult i8 %v1, %v2
+  %2 = select i1 %1, i8 %v1, i8 %v2
+  ret i8 %2
+}
+
+define <2 x i8> @_Z3minDv2_hS_(<2 x i8> %v1, <2 x i8> %v2) nounwind readnone {
+  %1 = zext <2 x i8> %v1 to <2 x i32>
+  %2 = zext <2 x i8> %v2 to <2 x i32>
+  %3 = tail call <2 x i32> @llvm.arm.neon.vminu.v2i32(<2 x i32> %1, <2 x i32> %2) nounwind readnone
+  %4 = trunc <2 x i32> %3 to <2 x i8>
+  ret <2 x i8> %4
+}
+
+define <3 x i8> @_Z3minDv3_hS_(<3 x i8> %v1, <3 x i8> %v2) nounwind readnone {
+  %1 = zext <3 x i8> %v1 to <3 x i32>
+  %2 = zext <3 x i8> %v2 to <3 x i32>
+  %3 = shufflevector <3 x i32> %1, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %4 = shufflevector <3 x i32> %2, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %5 = tail call <4 x i32> @llvm.arm.neon.vminu.v4i32(<4 x i32> %3, <4 x i32> %4) nounwind readnone
+  %6 = shufflevector <4 x i32> %5, <4 x i32> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  %7 = trunc <3 x i32> %6 to <3 x i8>
+  ret <3 x i8> %7
+}
+
+define <4 x i8> @_Z3minDv4_hS_(<4 x i8> %v1, <4 x i8> %v2) nounwind readnone {
+  %1 = zext <4 x i8> %v1 to <4 x i32>
+  %2 = zext <4 x i8> %v2 to <4 x i32>
+  %3 = tail call <4 x i32> @llvm.arm.neon.vminu.v4i32(<4 x i32> %1, <4 x i32> %2) nounwind readnone
+  %4 = trunc <4 x i32> %3 to <4 x i8>
+  ret <4 x i8> %4
+}
+
+define zeroext i16 @_Z3mintt(i16 zeroext %v1, i16 zeroext %v2) nounwind readnone {
+  %1 = icmp ult i16 %v1, %v2
+  %2 = select i1 %1, i16 %v1, i16 %v2
+  ret i16 %2
+}
+
+define <2 x i16> @_Z3minDv2_tS_(<2 x i16> %v1, <2 x i16> %v2) nounwind readnone {
+  %1 = zext <2 x i16> %v1 to <2 x i32>
+  %2 = zext <2 x i16> %v2 to <2 x i32>
+  %3 = tail call <2 x i32> @llvm.arm.neon.vminu.v2i32(<2 x i32> %1, <2 x i32> %2) nounwind readnone
+  %4 = trunc <2 x i32> %3 to <2 x i16>
+  ret <2 x i16> %4
+}
+
+define <3 x i16> @_Z3minDv3_tS_(<3 x i16> %v1, <3 x i16> %v2) nounwind readnone {
+  %1 = zext <3 x i16> %v1 to <3 x i32>
+  %2 = zext <3 x i16> %v2 to <3 x i32>
+  %3 = shufflevector <3 x i32> %1, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %4 = shufflevector <3 x i32> %2, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %5 = tail call <4 x i32> @llvm.arm.neon.vminu.v4i32(<4 x i32> %3, <4 x i32> %4) nounwind readnone
+  %6 = shufflevector <4 x i32> %5, <4 x i32> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  %7 = trunc <3 x i32> %6 to <3 x i16>
+  ret <3 x i16> %7
+}
+
+define <4 x i16> @_Z3minDv4_tS_(<4 x i16> %v1, <4 x i16> %v2) nounwind readnone {
+  %1 = zext <4 x i16> %v1 to <4 x i32>
+  %2 = zext <4 x i16> %v2 to <4 x i32>
+  %3 = tail call <4 x i32> @llvm.arm.neon.vminu.v4i32(<4 x i32> %1, <4 x i32> %2) nounwind readnone
+  %4 = trunc <4 x i32> %3 to <4 x i16>
+  ret <4 x i16> %4
+}
+
+define i32 @_Z3minjj(i32 %v1, i32 %v2) nounwind readnone {
+  %1 = icmp ult i32 %v1, %v2
+  %2 = select i1 %1, i32 %v1, i32 %v2
+  ret i32 %2
+}
+
+define <2 x i32> @_Z3minDv2_jS_(<2 x i32> %v1, <2 x i32> %v2) nounwind readnone {
+  %1 = tail call <2 x i32> @llvm.arm.neon.vminu.v2i32(<2 x i32> %v1, <2 x i32> %v2) nounwind readnone
+  ret <2 x i32> %1
+}
+
+define <3 x i32> @_Z3minDv3_jS_(<3 x i32> %v1, <3 x i32> %v2) nounwind readnone {
+  %1 = shufflevector <3 x i32> %v1, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %2 = shufflevector <3 x i32> %v2, <3 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %3 = tail call <4 x i32   > @llvm.arm.neon.vminu.v4i32(<4 x i32> %1, <4 x i32> %2) nounwind readnone
+  %4 = shufflevector <4 x i32> %3, <4 x i32> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x i32> %4
+}
+
+define <4 x i32> @_Z3minDv4_jS_(<4 x i32> %v1, <4 x i32> %v2) nounwind readnone {
+  %1 = tail call <4 x i32> @llvm.arm.neon.vminu.v4i32(<4 x i32> %v1, <4 x i32> %v2) nounwind readnone
+  ret <4 x i32> %1
+}
+
+define i64 @_Z3minyy(i64 %v1, i64 %v2) nounwind readnone {
+  %1 = icmp ult i64 %v1, %v2
+  %2 = select i1 %1, i64 %v1, i64 %v2
+  ret i64 %2
+}
+
+; TODO:  long vector types
+
+define float @_Z3minff(float %v1, float %v2) nounwind readnone {
+  %1 = tail call float @_Z4fminff(float %v1, float %v2)
+  ret float %1
+}
+
+define <2 x float> @_Z3minDv2_fS_(<2 x float> %v1, <2 x float> %v2) nounwind readnone {
+  %1 = tail call <2 x float> @_Z4fminDv2_fS_(<2 x float> %v1, <2 x float> %v2)
+  ret <2 x float> %1
+}
+
+define <2 x float> @_Z3minDv2_ff(<2 x float> %v1, float %v2) nounwind readnone {
+  %1 = tail call <2 x float> @_Z4fminDv2_ff(<2 x float> %v1, float %v2)
+  ret <2 x float> %1
+}
+
+define <3 x float> @_Z3minDv3_fS_(<3 x float> %v1, <3 x float> %v2) nounwind readnone {
+  %1 = tail call <3 x float> @_Z4fminDv3_fS_(<3 x float> %v1, <3 x float> %v2)
+  ret <3 x float> %1
+}
+
+define <3 x float> @_Z3minDv3_ff(<3 x float> %v1, float %v2) nounwind readnone {
+  %1 = tail call <3 x float> @_Z4fminDv3_ff(<3 x float> %v1, float %v2)
+  ret <3 x float> %1
+}
+
+define <4 x float> @_Z3minDv4_fS_(<4 x float> %v1, <4 x float> %v2) nounwind readnone {
+  %1 = tail call <4 x float> @_Z4fminDv4_fS_(<4 x float> %v1, <4 x float> %v2)
+  ret <4 x float> %1
+}
+
+define <4 x float> @_Z3minDv4_ff(<4 x float> %v1, float %v2) nounwind readnone {
+  %1 = tail call <4 x float> @_Z4fminDv4_ff(<4 x float> %v1, float %v2)
+  ret <4 x float> %1
+}
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;                  YUV                   ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+@yuv_U = internal constant <4 x i32> <i32 0, i32 -100, i32 516, i32 0>, align 16
+@yuv_V = internal constant <4 x i32> <i32 409, i32 -208, i32 0, i32 0>, align 16
+@yuv_0 = internal constant <4 x i32> <i32 0, i32 0, i32 0, i32 0>, align 16
+@yuv_255 = internal constant <4 x i32> <i32 65535, i32 65535, i32 65535, i32 65535>, align 16
+
+
+define <4 x i8> @_Z18rsYuvToRGBA_uchar4hhh(i8 %pY, i8 %pU, i8 %pV) nounwind readnone alwaysinline {
+  %_sy = zext i8 %pY to i32
+  %_su = zext i8 %pU to i32
+  %_sv = zext i8 %pV to i32
+
+  %_sy2 = add i32 -16, %_sy
+  %_sy3 = mul i32 298, %_sy2
+  %_su2 = add i32 -128, %_su
+  %_sv2 = add i32 -128, %_sv
+  %_y = tail call <4 x i32> @smear_4i32(i32 %_sy3) nounwind readnone
+  %_u = tail call <4 x i32> @smear_4i32(i32 %_su2) nounwind readnone
+  %_v = tail call <4 x i32> @smear_4i32(i32 %_sv2) nounwind readnone
+
+  %mu = load <4 x i32>* @yuv_U, align 8
+  %mv = load <4 x i32>* @yuv_V, align 8
+  %_u2 = mul <4 x i32> %_u, %mu
+  %_v2 = mul <4 x i32> %_v, %mv
+  %_y2 = add <4 x i32> %_y, %_u2
+  %_y3 = add <4 x i32> %_y2, %_v2
+
+ ; %r1 = tail call <4 x i16> @llvm.arm.neon.vqshiftnsu.v4i16(<4 x i32> %_y3, <4 x i32> <i32 8, i32 8, i32 8, i32 8>) nounwind readnone
+;  %r2 = trunc <4 x i16> %r1 to <4 x i8>
+;  ret <4 x i8> %r2
+
+  %c0 = load <4 x i32>* @yuv_0, align 8
+  %c255 = load <4 x i32>* @yuv_255, align 8
+  %r1 = tail call <4 x i32> @llvm.arm.neon.vmaxs.v4i32(<4 x i32> %_y3, <4 x i32> %c0) nounwind readnone
+  %r2 = tail call <4 x i32> @llvm.arm.neon.vmins.v4i32(<4 x i32> %r1, <4 x i32> %c255) nounwind readnone
+  %r3 = lshr <4 x i32> %r2, <i32 8, i32 8, i32 8, i32 8>
+  %r4 = trunc <4 x i32> %r3 to <4 x i8>
+  ret <4 x i8> %r4
+}
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;              half_RECIP              ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+define float @_Z10half_recipf(float %v) {
+  %1 = insertelement <2 x float> undef, float %v, i32 0
+  %2 = tail call <2 x float> @llvm.arm.neon.vrecpe.v2f32(<2 x float> %1) nounwind readnone
+  %3 = extractelement <2 x float> %2, i32 0
+  ret float %3
+}
+
+define <2 x float> @_Z10half_recip2Dv2_h(<2 x float> %v) nounwind readnone {
+  %1 = tail call <2 x float> @llvm.arm.neon.vrecpe.v2f32(<2 x float> %v) nounwind readnone
+  ret <2 x float> %1
+}
+
+define <3 x float> @_Z10half_recip3Dv3_h(<3 x float> %v) nounwind readnone {
+  %1 = shufflevector <3 x float> %v, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %2 = tail call <4 x float> @llvm.arm.neon.vrecpe.v4f32(<4 x float> %1) nounwind readnone
+  %3 = shufflevector <4 x float> %2, <4 x float> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x float> %3
+}
+
+define <4 x float> @_Z10half_recip4Dv4_h(<4 x float> %v) nounwind readnone {
+  %1 = tail call <4 x float> @llvm.arm.neon.vrecpe.v4f32(<4 x float> %v) nounwind readnone
+  ret <4 x float> %1
+}
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;              half_SQRT               ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+define float @_Z9half_sqrtf(float %v) {
+  %1 = insertelement <2 x float> undef, float %v, i32 0
+  %2 = tail call <2 x float> @llvm.arm.neon.vrsqrte.v2f32(<2 x float> %1) nounwind readnone
+  %3 = tail call <2 x float> @llvm.arm.neon.vrecpe.v2f32(<2 x float> %2) nounwind readnone
+  %4 = extractelement <2 x float> %3, i32 0
+  ret float %4
+}
+
+define <2 x float> @_Z9half_sqrt2Dv2_h(<2 x float> %v) nounwind readnone {
+  %1 = tail call <2 x float> @llvm.arm.neon.vrsqrte.v2f32(<2 x float> %v) nounwind readnone
+  %2 = tail call <2 x float> @llvm.arm.neon.vrecpe.v2f32(<2 x float> %1) nounwind readnone
+  ret <2 x float> %2
+}
+
+define <3 x float> @_Z9half_sqrt3Dv3_h(<3 x float> %v) nounwind readnone {
+  %1 = shufflevector <3 x float> %v, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %2 = tail call <4 x float> @llvm.arm.neon.vrsqrte.v4f32(<4 x float> %1) nounwind readnone
+  %3 = tail call <4 x float> @llvm.arm.neon.vrecpe.v4f32(<4 x float> %2) nounwind readnone
+  %4 = shufflevector <4 x float> %3, <4 x float> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x float> %4
+}
+
+define <4 x float> @_Z9half_sqrt4Dv4_h(<4 x float> %v) nounwind readnone {
+  %1 = tail call <4 x float> @llvm.arm.neon.vrsqrte.v4f32(<4 x float> %v) nounwind readnone
+  %2 = tail call <4 x float> @llvm.arm.neon.vrecpe.v4f32(<4 x float> %1) nounwind readnone
+  ret <4 x float> %2
+}
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;              half_RSQRT              ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+define float @_Z10half_rsqrtf(float %v) {
+  %1 = insertelement <2 x float> undef, float %v, i32 0
+  %2 = tail call <2 x float> @llvm.arm.neon.vrsqrte.v2f32(<2 x float> %1) nounwind readnone
+  %3 = extractelement <2 x float> %2, i32 0
+  ret float %3
+}
+
+define <2 x float> @_Z10half_rsqrt2Dv2_h(<2 x float> %v) nounwind readnone {
+  %1 = tail call <2 x float> @llvm.arm.neon.vrsqrte.v2f32(<2 x float> %v) nounwind readnone
+  ret <2 x float> %1
+}
+
+define <3 x float> @_Z10half_rsqrt3Dv3_h(<3 x float> %v) nounwind readnone {
+  %1 = shufflevector <3 x float> %v, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %2 = tail call <4 x float> @llvm.arm.neon.vrsqrte.v4f32(<4 x float> %1) nounwind readnone
+  %3 = shufflevector <4 x float> %2, <4 x float> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x float> %3
+}
+
+define <4 x float> @_Z10half_rsqrt4Dv4_h(<4 x float> %v) nounwind readnone {
+  %1 = tail call <4 x float> @llvm.arm.neon.vrsqrte.v4f32(<4 x float> %v) nounwind readnone
+  ret <4 x float> %1
+}
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;              matrix                    ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+declare <4 x float> @llvm.arm.neon.vld1.v4f32(i8*, i32) nounwind readonly
+
+%struct.rs_matrix4x4 = type { [16 x float] }
+%struct.rs_matrix3x3 = type { [9 x float] }
+%struct.rs_matrix2x2 = type { [4 x float] }
+
+define internal <4 x float> @smear_f(float %in) nounwind readnone alwaysinline {
+  %1 = insertelement <4 x float> undef, float %in, i32 0
+  %2 = insertelement <4 x float> %1, float %in, i32 1
+  %3 = insertelement <4 x float> %2, float %in, i32 2
+  %4 = insertelement <4 x float> %3, float %in, i32 3
+  ret <4 x float> %4
+}
+
+
+define <3 x float> @_Z16rsMatrixMultiplyPK12rs_matrix3x3Dv3_f(%struct.rs_matrix3x3* nocapture %m, <3 x float> %in) nounwind readonly {
+  %x0 = extractelement <3 x float> %in, i32 0
+  %x = tail call <4 x float> @smear_f(float %x0) nounwind readnone
+  %y0 = extractelement <3 x float> %in, i32 1
+  %y = tail call <4 x float> @smear_f(float %y0) nounwind readnone
+  %z0 = extractelement <3 x float> %in, i32 2
+  %z = tail call <4 x float> @smear_f(float %z0) nounwind readnone
+
+  %px = getelementptr inbounds %struct.rs_matrix3x3* %m, i32 0, i32 0, i32 0
+  %px2 = bitcast float* %px to i8*
+  %xm = call <4 x float> @llvm.arm.neon.vld1.v4f32(i8* %px2, i32 4) nounwind
+
+  %py = getelementptr inbounds %struct.rs_matrix3x3* %m, i32 0, i32 0, i32 3
+  %py2 = bitcast float* %py to i8*
+  %ym = call <4 x float> @llvm.arm.neon.vld1.v4f32(i8* %py2, i32 4) nounwind
+
+  %pz = getelementptr inbounds %struct.rs_matrix3x3* %m, i32 0, i32 0, i32 5
+  %pz2 = bitcast float* %pz to i8*
+  %zm2 = call <4 x float> @llvm.arm.neon.vld1.v4f32(i8* %pz2, i32 4) nounwind
+  %zm = shufflevector <4 x float> %zm2, <4 x float> undef, <4 x i32> <i32 1, i32 2, i32 3, i32 4>
+
+  %a1 = fmul <4 x float> %x, %xm
+  %a2 = fmul <4 x float> %y, %ym
+  %a3 = fadd <4 x float> %a1, %a2
+  %a4 = fmul <4 x float> %z, %zm
+  %a5 = fadd <4 x float> %a4, %a3
+  %a6 = shufflevector <4 x float> %a5, <4 x float> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x float> %a6
+}
+
+define <3 x float> @_Z16rsMatrixMultiplyPK12rs_matrix3x3Dv2_f(%struct.rs_matrix3x3* nocapture %m, <2 x float> %in) nounwind readonly {
+  %x0 = extractelement <2 x float> %in, i32 0
+  %x = tail call <4 x float> @smear_f(float %x0) nounwind readnone
+  %y0 = extractelement <2 x float> %in, i32 1
+  %y = tail call <4 x float> @smear_f(float %y0) nounwind readnone
+
+  %px = getelementptr inbounds %struct.rs_matrix3x3* %m, i32 0, i32 0, i32 0
+  %px2 = bitcast float* %px to <4 x float>*
+  %xm = load <4 x float>* %px2, align 4
+  %py = getelementptr inbounds %struct.rs_matrix3x3* %m, i32 0, i32 0, i32 3
+  %py2 = bitcast float* %py to <4 x float>*
+  %ym = load <4 x float>* %py2, align 4
+
+  %a1 = fmul <4 x float> %x, %xm
+  %a2 = fmul <4 x float> %y, %ym
+  %a3 = fadd <4 x float> %a1, %a2
+  %a4 = shufflevector <4 x float> %a3, <4 x float> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x float> %a4
+}
+
+define <4 x float> @_Z16rsMatrixMultiplyPK12rs_matrix4x4Dv4_f(%struct.rs_matrix4x4* nocapture %m, <4 x float> %in) nounwind readonly {
+  %x0 = extractelement <4 x float> %in, i32 0
+  %x = tail call <4 x float> @smear_f(float %x0) nounwind readnone
+  %y0 = extractelement <4 x float> %in, i32 1
+  %y = tail call <4 x float> @smear_f(float %y0) nounwind readnone
+  %z0 = extractelement <4 x float> %in, i32 2
+  %z = tail call <4 x float> @smear_f(float %z0) nounwind readnone
+  %w0 = extractelement <4 x float> %in, i32 3
+  %w = tail call <4 x float> @smear_f(float %w0) nounwind readnone
+
+  %px = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 0
+  %px2 = bitcast float* %px to <4 x float>*
+  %xm = load <4 x float>* %px2, align 4
+  %py = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 4
+  %py2 = bitcast float* %py to <4 x float>*
+  %ym = load <4 x float>* %py2, align 4
+  %pz = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 8
+  %pz2 = bitcast float* %pz to <4 x float>*
+  %zm = load <4 x float>* %pz2, align 4
+  %pw = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 12
+  %pw2 = bitcast float* %pw to <4 x float>*
+  %wm = load <4 x float>* %pw2, align 4
+
+  %a1 = fmul <4 x float> %x, %xm
+  %a2 = fmul <4 x float> %y, %ym
+  %a3 = fadd <4 x float> %a1, %a2
+  %a4 = fmul <4 x float> %z, %zm
+  %a5 = fadd <4 x float> %a3, %a4
+  %a6 = fmul <4 x float> %w, %wm
+  %a7 = fadd <4 x float> %a5, %a6
+  ret <4 x float> %a7
+}
+
+define <4 x float> @_Z16rsMatrixMultiplyPK12rs_matrix4x4Dv3_f(%struct.rs_matrix4x4* nocapture %m, <3 x float> %in) nounwind readonly {
+  %x0 = extractelement <3 x float> %in, i32 0
+  %x = tail call <4 x float> @smear_f(float %x0) nounwind readnone
+  %y0 = extractelement <3 x float> %in, i32 1
+  %y = tail call <4 x float> @smear_f(float %y0) nounwind readnone
+  %z0 = extractelement <3 x float> %in, i32 2
+  %z = tail call <4 x float> @smear_f(float %z0) nounwind readnone
+
+  %px = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 0
+  %px2 = bitcast float* %px to <4 x float>*
+  %xm = load <4 x float>* %px2, align 4
+  %py = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 4
+  %py2 = bitcast float* %py to <4 x float>*
+  %ym = load <4 x float>* %py2, align 4
+  %pz = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 8
+  %pz2 = bitcast float* %pz to <4 x float>*
+  %zm = load <4 x float>* %pz2, align 4
+  %pw = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 12
+  %pw2 = bitcast float* %pw to <4 x float>*
+  %wm = load <4 x float>* %pw2, align 4
+
+  %a1 = fmul <4 x float> %x, %xm
+  %a2 = fadd <4 x float> %wm, %a1
+  %a3 = fmul <4 x float> %y, %ym
+  %a4 = fadd <4 x float> %a2, %a3
+  %a5 = fmul <4 x float> %z, %zm
+  %a6 = fadd <4 x float> %a4, %a5
+  ret <4 x float> %a6
+}
+
+define <4 x float> @_Z16rsMatrixMultiplyPK12rs_matrix4x4Dv2_f(%struct.rs_matrix4x4* nocapture %m, <2 x float> %in) nounwind readonly {
+  %x0 = extractelement <2 x float> %in, i32 0
+  %x = tail call <4 x float> @smear_f(float %x0) nounwind readnone
+  %y0 = extractelement <2 x float> %in, i32 1
+  %y = tail call <4 x float> @smear_f(float %y0) nounwind readnone
+
+  %px = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 0
+  %px2 = bitcast float* %px to <4 x float>*
+  %xm = load <4 x float>* %px2, align 4
+  %py = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 4
+  %py2 = bitcast float* %py to <4 x float>*
+  %ym = load <4 x float>* %py2, align 4
+  %pw = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 12
+  %pw2 = bitcast float* %pw to <4 x float>*
+  %wm = load <4 x float>* %pw2, align 4
+
+  %a1 = fmul <4 x float> %x, %xm
+  %a2 = fadd <4 x float> %wm, %a1
+  %a3 = fmul <4 x float> %y, %ym
+  %a4 = fadd <4 x float> %a2, %a3
+  ret <4 x float> %a4
+}
+
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;              pixel ops                 ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+
+@fc_255.0 = internal constant <4 x float> <float 255.0, float 255.0, float 255.0, float 255.0>, align 16
+@fc_0.5 = internal constant <4 x float> <float 0.5, float 0.5, float 0.5, float 0.5>, align 16
+@fc_0 = internal constant <4 x float> <float 0.0, float 0.0, float 0.0, float 0.0>, align 16
+
+declare <4 x i8> @_Z14convert_uchar4Dv4_f(<4 x float> %in) nounwind readnone
+declare <4 x float> @_Z14convert_float4Dv4_h(<4 x i8> %in) nounwind readnone
+
+; uchar4 __attribute__((overloadable)) rsPackColorTo8888(float4 color)
+define <4 x i8> @_Z17rsPackColorTo8888Dv4_f(<4 x float> %color) nounwind readnone {
+    %f255 = load <4 x float>* @fc_255.0, align 16
+    %f05 = load <4 x float>* @fc_0.5, align 16
+    %f0 = load <4 x float>* @fc_0, align 16
+    %v1 = fmul <4 x float> %f255, %color
+    %v2 = fadd <4 x float> %f05, %v1
+    %v3 = tail call <4 x float> @_Z5clampDv4_fS_S_(<4 x float> %v2, <4 x float> %f0, <4 x float> %f255) nounwind readnone
+    %v4 = tail call <4 x i8> @_Z14convert_uchar4Dv4_f(<4 x float> %v3) nounwind readnone
+    ret <4 x i8> %v4
+}
+
+; uchar4 __attribute__((overloadable)) rsPackColorTo8888(float3 color)
+define <4 x i8> @_Z17rsPackColorTo8888Dv3_f(<3 x float> %color) nounwind readnone {
+    %1 = shufflevector <3 x float> %color, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+    %2 = insertelement <4 x float> %1, float 1.0, i32 3
+    %3 = tail call <4 x i8> @_Z17rsPackColorTo8888Dv4_f(<4 x float> %2) nounwind readnone
+    ret <4 x i8> %3
+}
+
+; uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b)
+define <4 x i8> @_Z17rsPackColorTo8888fff(float %r, float %g, float %b) nounwind readnone {
+    %1 = insertelement <4 x float> undef, float %r, i32 0
+    %2 = insertelement <4 x float> %1, float %g, i32 1
+    %3 = insertelement <4 x float> %2, float %b, i32 2
+    %4 = insertelement <4 x float> %3, float 1.0, i32 3
+    %5 = tail call <4 x i8> @_Z17rsPackColorTo8888Dv4_f(<4 x float> %4) nounwind readnone
+    ret <4 x i8> %5
+}
+
+; uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b, float a)
+define <4 x i8> @_Z17rsPackColorTo8888ffff(float %r, float %g, float %b, float %a) nounwind readnone {
+    %1 = insertelement <4 x float> undef, float %r, i32 0
+    %2 = insertelement <4 x float> %1, float %g, i32 1
+    %3 = insertelement <4 x float> %2, float %b, i32 2
+    %4 = insertelement <4 x float> %3, float %a, i32 3
+    %5 = tail call <4 x i8> @_Z17rsPackColorTo8888Dv4_f(<4 x float> %4) nounwind readnone
+    ret <4 x i8> %5
+}
+
diff --git a/driver/runtime/arch/sqrt.c b/driver/runtime/arch/sqrt.c
new file mode 100755
index 0000000..f1dac5f
--- /dev/null
+++ b/driver/runtime/arch/sqrt.c
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#include "rs_types.rsh"
+
+#define FN_FUNC_FN(fnc)                                         \
+extern float2 __attribute__((overloadable)) fnc(float2 v) { \
+    float2 r;                                                   \
+    r.x = fnc(v.x);                                             \
+    r.y = fnc(v.y);                                             \
+    return r;                                                   \
+}                                                               \
+extern float3 __attribute__((overloadable)) fnc(float3 v) { \
+    float3 r;                                                   \
+    r.x = fnc(v.x);                                             \
+    r.y = fnc(v.y);                                             \
+    r.z = fnc(v.z);                                             \
+    return r;                                                   \
+}                                                               \
+extern float4 __attribute__((overloadable)) fnc(float4 v) { \
+    float4 r;                                                   \
+    r.x = fnc(v.x);                                             \
+    r.y = fnc(v.y);                                             \
+    r.z = fnc(v.z);                                             \
+    r.w = fnc(v.w);                                             \
+    return r;                                                   \
+}
+
+extern float __attribute__((overloadable)) sqrt(float);
+
+FN_FUNC_FN(sqrt)
diff --git a/driver/runtime/arch/x86_clamp.ll b/driver/runtime/arch/x86_clamp.ll
new file mode 100755
index 0000000..422e9f6
--- /dev/null
+++ b/driver/runtime/arch/x86_clamp.ll
@@ -0,0 +1,74 @@
+target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S128"
+target triple = "i386-unknown-linux-gnu"
+
+declare <4 x float> @llvm.x86.sse.min.ps(<4 x float>, <4 x float>)
+declare <4 x float> @llvm.x86.sse.max.ps(<4 x float>, <4 x float>)
+declare <4 x float> @llvm.x86.sse.min.ss(<4 x float>, <4 x float>)
+declare <4 x float> @llvm.x86.sse.max.ss(<4 x float>, <4 x float>)
+
+define <4 x float> @_Z5clampDv4_fS_S_(<4 x float> %in, <4 x float> %low, <4 x float> %high) nounwind readnone alwaysinline {
+  %1 = tail call <4 x float> @llvm.x86.sse.min.ps(<4 x float> %in, <4 x float> %high) nounwind readnone
+  %2 = tail call <4 x float> @llvm.x86.sse.max.ps(<4 x float> %1, <4 x float> %low) nounwind readnone
+  ret <4 x float> %2
+}
+
+define <3 x float> @_Z5clampDv3_fS_S_(<3 x float> %in, <3 x float> %low, <3 x float> %high) nounwind readnone alwaysinline {
+  %1 = shufflevector <3 x float> %in, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %2 = shufflevector <3 x float> %low, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %3 = shufflevector <3 x float> %high, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %4 = tail call <4 x float> @_Z5clampDv4_fS_S_(<4 x float> %1, <4 x float> %2, <4 x float> %3) nounwind readnone
+  %5 = shufflevector <4 x float> %4, <4 x float> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x float> %5
+}
+
+define <2 x float> @_Z5clampDv2_fS_S_(<2 x float> %in, <2 x float> %low, <2 x float> %high) nounwind readnone alwaysinline {
+  %1 = shufflevector <2 x float> %in, <2 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %2 = shufflevector <2 x float> %low, <2 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %3 = shufflevector <2 x float> %high, <2 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %4 = tail call <4 x float> @_Z5clampDv4_fS_S_(<4 x float> %1, <4 x float> %2, <4 x float> %3) nounwind readnone
+  %5 = shufflevector <4 x float> %4, <4 x float> undef, <2 x i32> <i32 0, i32 1>
+  ret <2 x float> %5
+}
+
+define float @_Z5clampfff(float %in, float %low, float %high) nounwind readnone alwaysinline {
+  %1 = insertelement <4 x float> undef, float %in, i32 0
+  %2 = insertelement <4 x float> undef, float %low, i32 0
+  %3 = insertelement <4 x float> undef, float %high, i32 0
+  %4 = tail call <4 x float> @llvm.x86.sse.min.ss(<4 x float> %1, <4 x float> %3) nounwind readnone
+  %5 = tail call <4 x float> @llvm.x86.sse.max.ss(<4 x float> %4, <4 x float> %2) nounwind readnone
+  %6 = extractelement <4 x float> %5, i32 0
+  ret float %6
+}
+
+define <4 x float> @_Z5clampDv4_fff(<4 x float> %in, float %low, float %high) nounwind readonly {
+  %1 = insertelement <4 x float> undef, float %low, i32 0
+  %2 = insertelement <4 x float> %1, float %low, i32 1
+  %3 = insertelement <4 x float> %2, float %low, i32 2
+  %4 = insertelement <4 x float> %3, float %low, i32 3
+  %5 = insertelement <4 x float> undef, float %high, i32 0
+  %6 = insertelement <4 x float> %5, float %high, i32 1
+  %7 = insertelement <4 x float> %6, float %high, i32 2
+  %8 = insertelement <4 x float> %7, float %high, i32 3
+  %9 = tail call <4 x float> @_Z5clampDv4_fS_S_(<4 x float> %in, <4 x float> %4, <4 x float> %8) nounwind readnone
+  ret <4 x float> %9
+}
+
+define <3 x float> @_Z5clampDv3_fff(<3 x float> %in, float %low, float %high) nounwind readonly {
+  %1 = insertelement <3 x float> undef, float %low, i32 0
+  %2 = insertelement <3 x float> %1, float %low, i32 1
+  %3 = insertelement <3 x float> %2, float %low, i32 2
+  %4 = insertelement <3 x float> undef, float %high, i32 0
+  %5 = insertelement <3 x float> %4, float %high, i32 1
+  %6 = insertelement <3 x float> %5, float %high, i32 2
+  %7 = tail call <3 x float> @_Z5clampDv3_fS_S_(<3 x float> %in, <3 x float> %3, <3 x float> %6) nounwind readnone
+  ret <3 x float> %7
+}
+
+define <2 x float> @_Z5clampDv2_fff(<2 x float> %in, float %low, float %high) nounwind readonly {
+  %1 = insertelement <2 x float> undef, float %low, i32 0
+  %2 = insertelement <2 x float> %1, float %low, i32 1
+  %3 = insertelement <2 x float> undef, float %high, i32 0
+  %4 = insertelement <2 x float> %3, float %high, i32 1
+  %5 = tail call <2 x float> @_Z5clampDv2_fS_S_(<2 x float> %in, <2 x float> %2, <2 x float> %4) nounwind readnone
+  ret <2 x float> %5
+}
diff --git a/driver/runtime/arch/x86_dot_length.ll b/driver/runtime/arch/x86_dot_length.ll
new file mode 100644
index 0000000..21f2f3e
--- /dev/null
+++ b/driver/runtime/arch/x86_dot_length.ll
@@ -0,0 +1,75 @@
+target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S128"
+target triple = "i386-unknown-linux-gnu"
+
+declare <2 x i64> @llvm.x86.sse2.psll.dq(<2 x i64>, i32) nounwind readnone
+declare <4 x float> @llvm.x86.sse3.hadd.ps(<4 x float>, <4 x float>) nounwind readnone
+declare float @llvm.sqrt.f32(float) nounwind readnone
+
+define float @_Z3dotDv4_fS_(<4 x float> %lhs, <4 x float> %rhs) nounwind readnone {
+  %1 = fmul <4 x float> %lhs, %rhs
+  %2 = tail call <4 x float> @llvm.x86.sse3.hadd.ps(<4 x float> %1, <4 x float> %1) nounwind readnone
+  %3 = tail call <4 x float> @llvm.x86.sse3.hadd.ps(<4 x float> %2, <4 x float> %2) nounwind readnone
+  %4 = extractelement <4 x float> %3, i32 0
+  ret float %4
+}
+
+define float @_Z3dotDv3_fS_(<3 x float> %lhs, <3 x float> %rhs) nounwind readnone {
+  %1 = fmul <3 x float> %lhs, %rhs
+  %2 = shufflevector <3 x float> %1, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %3 = bitcast <4 x float> %2 to <2 x i64>
+  %4 = tail call <2 x i64> @llvm.x86.sse2.psll.dq(<2 x i64> %3, i32 32)
+  %5 = bitcast <2 x i64> %4 to <4 x float>
+  %6 = tail call <4 x float> @llvm.x86.sse3.hadd.ps(<4 x float> %5, <4 x float> %5) nounwind readnone
+  %7 = tail call <4 x float> @llvm.x86.sse3.hadd.ps(<4 x float> %6, <4 x float> %6) nounwind readnone
+  %8 = extractelement <4 x float> %7, i32 0
+  ret float %8
+}
+
+define float @_Z3dotDv2_fS_(<2 x float> %lhs, <2 x float> %rhs) nounwind readnone {
+  %1 = fmul <2 x float> %lhs, %rhs
+  %2 = shufflevector <2 x float> %1, <2 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %3 = tail call <4 x float> @llvm.x86.sse3.hadd.ps(<4 x float> %2, <4 x float> %2) nounwind readnone
+  %4 = extractelement <4 x float> %3, i32 0
+  ret float %4
+}
+
+define float @_Z3dotff(float %lhs, float %rhs) nounwind readnone {
+  %1 = fmul float %lhs, %rhs
+  ret float %1
+}
+
+define float @_Z6lengthDv4_f(<4 x float> %in) nounwind readnone alwaysinline {
+  %1 = fmul <4 x float> %in, %in
+  %2 = tail call <4 x float> @llvm.x86.sse3.hadd.ps(<4 x float> %1, <4 x float> %1) nounwind readnone
+  %3 = tail call <4 x float> @llvm.x86.sse3.hadd.ps(<4 x float> %2, <4 x float> %2) nounwind readnone
+  %4 = extractelement <4 x float> %3, i32 0
+  %5 = tail call float @llvm.sqrt.f32(float %4) nounwind readnone
+  ret float %5
+}
+
+define float @_Z6lengthDv3_f(<3 x float> %in) nounwind readnone alwaysinline {
+  %1 = fmul <3 x float> %in, %in
+  %2 = shufflevector <3 x float> %1, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %3 = bitcast <4 x float> %2 to <2 x i64>
+  %4 = tail call <2 x i64> @llvm.x86.sse2.psll.dq(<2 x i64> %3, i32 32)
+  %5 = bitcast <2 x i64> %4 to <4 x float>
+  %6 = tail call <4 x float> @llvm.x86.sse3.hadd.ps(<4 x float> %5, <4 x float> %5) nounwind readnone
+  %7 = tail call <4 x float> @llvm.x86.sse3.hadd.ps(<4 x float> %6, <4 x float> %6) nounwind readnone
+  %8 = extractelement <4 x float> %7, i32 0
+  %9 = tail call float @llvm.sqrt.f32(float %8) nounwind readnone
+  ret float %9
+}
+
+define float @_Z6lengthDv2_f(<2 x float> %in) nounwind readnone alwaysinline {
+  %1 = fmul <2 x float> %in, %in
+  %2 = shufflevector <2 x float> %1, <2 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %3 = tail call <4 x float> @llvm.x86.sse3.hadd.ps(<4 x float> %2, <4 x float> %2) nounwind readnone
+  %4 = extractelement <4 x float> %3, i32 0
+  %5 = tail call float @llvm.sqrt.f32(float %4) nounwind readnone
+  ret float %5
+}
+
+define float @_Z6lengthf(float %in) nounwind readnone alwaysinline {
+  ret float %in
+}
+
diff --git a/driver/runtime/arch/x86_generic.c b/driver/runtime/arch/x86_generic.c
new file mode 100644
index 0000000..c46c54a
--- /dev/null
+++ b/driver/runtime/arch/x86_generic.c
@@ -0,0 +1,786 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#include "rs_types.rsh"
+
+extern short __attribute__((overloadable, always_inline)) rsClamp(short amount, short low, short high);
+extern float4 __attribute__((overloadable)) clamp(float4 amount, float4 low, float4 high);
+extern uchar4 __attribute__((overloadable)) convert_uchar4(short4);
+extern float __attribute__((overloadable)) sqrt(float);
+
+/*
+ * FMAX
+ */
+
+extern float __attribute__((overloadable)) fmax(float v1, float v2) {
+    return v1 > v2 ? v1 : v2;
+}
+
+extern float2 __attribute__((overloadable)) fmax(float2 v1, float2 v2) {
+    float2 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern float3 __attribute__((overloadable)) fmax(float3 v1, float3 v2) {
+    float3 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern float4 __attribute__((overloadable)) fmax(float4 v1, float4 v2) {
+    float4 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    r.w = v1.w > v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern float2 __attribute__((overloadable)) fmax(float2 v1, float v2) {
+    float2 r;
+    r.x = v1.x > v2 ? v1.x : v2;
+    r.y = v1.y > v2 ? v1.y : v2;
+    return r;
+}
+
+extern float3 __attribute__((overloadable)) fmax(float3 v1, float v2) {
+    float3 r;
+    r.x = v1.x > v2 ? v1.x : v2;
+    r.y = v1.y > v2 ? v1.y : v2;
+    r.z = v1.z > v2 ? v1.z : v2;
+    return r;
+}
+
+extern float4 __attribute__((overloadable)) fmax(float4 v1, float v2) {
+    float4 r;
+    r.x = v1.x > v2 ? v1.x : v2;
+    r.y = v1.y > v2 ? v1.y : v2;
+    r.z = v1.z > v2 ? v1.z : v2;
+    r.w = v1.w > v2 ? v1.w : v2;
+    return r;
+}
+
+extern float __attribute__((overloadable)) fmin(float v1, float v2) {
+    return v1 < v2 ? v1 : v2;
+}
+
+
+/*
+ * FMIN
+ */
+extern float2 __attribute__((overloadable)) fmin(float2 v1, float2 v2) {
+    float2 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern float3 __attribute__((overloadable)) fmin(float3 v1, float3 v2) {
+    float3 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern float4 __attribute__((overloadable)) fmin(float4 v1, float4 v2) {
+    float4 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    r.w = v1.w < v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern float2 __attribute__((overloadable)) fmin(float2 v1, float v2) {
+    float2 r;
+    r.x = v1.x < v2 ? v1.x : v2;
+    r.y = v1.y < v2 ? v1.y : v2;
+    return r;
+}
+
+extern float3 __attribute__((overloadable)) fmin(float3 v1, float v2) {
+    float3 r;
+    r.x = v1.x < v2 ? v1.x : v2;
+    r.y = v1.y < v2 ? v1.y : v2;
+    r.z = v1.z < v2 ? v1.z : v2;
+    return r;
+}
+
+extern float4 __attribute__((overloadable)) fmin(float4 v1, float v2) {
+    float4 r;
+    r.x = v1.x < v2 ? v1.x : v2;
+    r.y = v1.y < v2 ? v1.y : v2;
+    r.z = v1.z < v2 ? v1.z : v2;
+    r.w = v1.w < v2 ? v1.w : v2;
+    return r;
+}
+
+
+/*
+ * MAX
+ */
+
+extern char __attribute__((overloadable)) max(char v1, char v2) {
+    return v1 > v2 ? v1 : v2;
+}
+
+extern char2 __attribute__((overloadable)) max(char2 v1, char2 v2) {
+    char2 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern char3 __attribute__((overloadable)) max(char3 v1, char3 v2) {
+    char3 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern char4 __attribute__((overloadable)) max(char4 v1, char4 v2) {
+    char4 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    r.w = v1.w > v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern short __attribute__((overloadable)) max(short v1, short v2) {
+    return v1 > v2 ? v1 : v2;
+}
+
+extern short2 __attribute__((overloadable)) max(short2 v1, short2 v2) {
+    short2 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern short3 __attribute__((overloadable)) max(short3 v1, short3 v2) {
+    short3 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern short4 __attribute__((overloadable)) max(short4 v1, short4 v2) {
+    short4 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    r.w = v1.w > v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern int __attribute__((overloadable)) max(int v1, int v2) {
+    return v1 > v2 ? v1 : v2;
+}
+
+extern int2 __attribute__((overloadable)) max(int2 v1, int2 v2) {
+    int2 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern int3 __attribute__((overloadable)) max(int3 v1, int3 v2) {
+    int3 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern int4 __attribute__((overloadable)) max(int4 v1, int4 v2) {
+    int4 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    r.w = v1.w > v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern int64_t __attribute__((overloadable)) max(int64_t v1, int64_t v2) {
+    return v1 > v2 ? v1 : v2;
+}
+
+extern long2 __attribute__((overloadable)) max(long2 v1, long2 v2) {
+    long2 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern long3 __attribute__((overloadable)) max(long3 v1, long3 v2) {
+    long3 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern long4 __attribute__((overloadable)) max(long4 v1, long4 v2) {
+    long4 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    r.w = v1.w > v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern uchar __attribute__((overloadable)) max(uchar v1, uchar v2) {
+    return v1 > v2 ? v1 : v2;
+}
+
+extern uchar2 __attribute__((overloadable)) max(uchar2 v1, uchar2 v2) {
+    uchar2 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern uchar3 __attribute__((overloadable)) max(uchar3 v1, uchar3 v2) {
+    uchar3 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern uchar4 __attribute__((overloadable)) max(uchar4 v1, uchar4 v2) {
+    uchar4 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    r.w = v1.w > v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern ushort __attribute__((overloadable)) max(ushort v1, ushort v2) {
+    return v1 > v2 ? v1 : v2;
+}
+
+extern ushort2 __attribute__((overloadable)) max(ushort2 v1, ushort2 v2) {
+    ushort2 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern ushort3 __attribute__((overloadable)) max(ushort3 v1, ushort3 v2) {
+    ushort3 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern ushort4 __attribute__((overloadable)) max(ushort4 v1, ushort4 v2) {
+    ushort4 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    r.w = v1.w > v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern uint __attribute__((overloadable)) max(uint v1, uint v2) {
+    return v1 > v2 ? v1 : v2;
+}
+
+extern uint2 __attribute__((overloadable)) max(uint2 v1, uint2 v2) {
+    uint2 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern uint3 __attribute__((overloadable)) max(uint3 v1, uint3 v2) {
+    uint3 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern uint4 __attribute__((overloadable)) max(uint4 v1, uint4 v2) {
+    uint4 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    r.w = v1.w > v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern ulong __attribute__((overloadable)) max(ulong v1, ulong v2) {
+    return v1 > v2 ? v1 : v2;
+}
+
+extern ulong2 __attribute__((overloadable)) max(ulong2 v1, ulong2 v2) {
+    ulong2 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern ulong3 __attribute__((overloadable)) max(ulong3 v1, ulong3 v2) {
+    ulong3 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern ulong4 __attribute__((overloadable)) max(ulong4 v1, ulong4 v2) {
+    ulong4 r;
+    r.x = v1.x > v2.x ? v1.x : v2.x;
+    r.y = v1.y > v2.y ? v1.y : v2.y;
+    r.z = v1.z > v2.z ? v1.z : v2.z;
+    r.w = v1.w > v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern float __attribute__((overloadable)) max(float v1, float v2) {
+    return fmax(v1, v2);
+}
+
+extern float2 __attribute__((overloadable)) max(float2 v1, float2 v2) {
+    return fmax(v1, v2);
+}
+
+extern float2 __attribute__((overloadable)) max(float2 v1, float v2) {
+    return fmax(v1, v2);
+}
+
+extern float3 __attribute__((overloadable)) max(float3 v1, float3 v2) {
+    return fmax(v1, v2);
+}
+
+extern float3 __attribute__((overloadable)) max(float3 v1, float v2) {
+    return fmax(v1, v2);
+}
+
+extern float4 __attribute__((overloadable)) max(float4 v1, float4 v2) {
+    return fmax(v1, v2);
+}
+
+extern float4 __attribute__((overloadable)) max(float4 v1, float v2) {
+    return fmax(v1, v2);
+}
+
+
+/*
+ * MIN
+ */
+
+extern int8_t __attribute__((overloadable)) min(int8_t v1, int8_t v2) {
+    return v1 < v2 ? v1 : v2;
+}
+
+extern char2 __attribute__((overloadable)) min(char2 v1, char2 v2) {
+    char2 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern char3 __attribute__((overloadable)) min(char3 v1, char3 v2) {
+    char3 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern char4 __attribute__((overloadable)) min(char4 v1, char4 v2) {
+    char4 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    r.w = v1.w < v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern int16_t __attribute__((overloadable)) min(int16_t v1, int16_t v2) {
+    return v1 < v2 ? v1 : v2;
+}
+
+extern short2 __attribute__((overloadable)) min(short2 v1, short2 v2) {
+    short2 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern short3 __attribute__((overloadable)) min(short3 v1, short3 v2) {
+    short3 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern short4 __attribute__((overloadable)) min(short4 v1, short4 v2) {
+    short4 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    r.w = v1.w < v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern int32_t __attribute__((overloadable)) min(int32_t v1, int32_t v2) {
+    return v1 < v2 ? v1 : v2;
+}
+
+extern int2 __attribute__((overloadable)) min(int2 v1, int2 v2) {
+    int2 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern int3 __attribute__((overloadable)) min(int3 v1, int3 v2) {
+    int3 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern int4 __attribute__((overloadable)) min(int4 v1, int4 v2) {
+    int4 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    r.w = v1.w < v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern int64_t __attribute__((overloadable)) min(int64_t v1, int64_t v2) {
+    return v1 < v2 ? v1 : v2;
+}
+
+extern long2 __attribute__((overloadable)) min(long2 v1, long2 v2) {
+    long2 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern long3 __attribute__((overloadable)) min(long3 v1, long3 v2) {
+    long3 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern long4 __attribute__((overloadable)) min(long4 v1, long4 v2) {
+    long4 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    r.w = v1.w < v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern uchar __attribute__((overloadable)) min(uchar v1, uchar v2) {
+    return v1 < v2 ? v1 : v2;
+}
+
+extern uchar2 __attribute__((overloadable)) min(uchar2 v1, uchar2 v2) {
+    uchar2 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern uchar3 __attribute__((overloadable)) min(uchar3 v1, uchar3 v2) {
+    uchar3 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern uchar4 __attribute__((overloadable)) min(uchar4 v1, uchar4 v2) {
+    uchar4 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    r.w = v1.w < v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern ushort __attribute__((overloadable)) min(ushort v1, ushort v2) {
+    return v1 < v2 ? v1 : v2;
+}
+
+extern ushort2 __attribute__((overloadable)) min(ushort2 v1, ushort2 v2) {
+    ushort2 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern ushort3 __attribute__((overloadable)) min(ushort3 v1, ushort3 v2) {
+    ushort3 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern ushort4 __attribute__((overloadable)) min(ushort4 v1, ushort4 v2) {
+    ushort4 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    r.w = v1.w < v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern uint __attribute__((overloadable)) min(uint v1, uint v2) {
+    return v1 < v2 ? v1 : v2;
+}
+
+extern uint2 __attribute__((overloadable)) min(uint2 v1, uint2 v2) {
+    uint2 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern uint3 __attribute__((overloadable)) min(uint3 v1, uint3 v2) {
+    uint3 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern uint4 __attribute__((overloadable)) min(uint4 v1, uint4 v2) {
+    uint4 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    r.w = v1.w < v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern ulong __attribute__((overloadable)) min(ulong v1, ulong v2) {
+    return v1 < v2 ? v1 : v2;
+}
+
+extern ulong2 __attribute__((overloadable)) min(ulong2 v1, ulong2 v2) {
+    ulong2 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    return r;
+}
+
+extern ulong3 __attribute__((overloadable)) min(ulong3 v1, ulong3 v2) {
+    ulong3 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    return r;
+}
+
+extern ulong4 __attribute__((overloadable)) min(ulong4 v1, ulong4 v2) {
+    ulong4 r;
+    r.x = v1.x < v2.x ? v1.x : v2.x;
+    r.y = v1.y < v2.y ? v1.y : v2.y;
+    r.z = v1.z < v2.z ? v1.z : v2.z;
+    r.w = v1.w < v2.w ? v1.w : v2.w;
+    return r;
+}
+
+extern float __attribute__((overloadable)) min(float v1, float v2) {
+    return fmin(v1, v2);
+}
+
+extern float2 __attribute__((overloadable)) min(float2 v1, float2 v2) {
+    return fmin(v1, v2);
+}
+
+extern float2 __attribute__((overloadable)) min(float2 v1, float v2) {
+    return fmin(v1, v2);
+}
+
+extern float3 __attribute__((overloadable)) min(float3 v1, float3 v2) {
+    return fmin(v1, v2);
+}
+
+extern float3 __attribute__((overloadable)) min(float3 v1, float v2) {
+    return fmin(v1, v2);
+}
+
+extern float4 __attribute__((overloadable)) min(float4 v1, float4 v2) {
+    return fmin(v1, v2);
+}
+
+extern float4 __attribute__((overloadable)) min(float4 v1, float v2) {
+    return fmin(v1, v2);
+}
+
+
+/*
+ * YUV
+ */
+
+extern uchar4 __attribute__((overloadable)) rsYuvToRGBA_uchar4(uchar y, uchar u, uchar v) {
+    short Y = ((short)y) - 16;
+    short U = ((short)u) - 128;
+    short V = ((short)v) - 128;
+
+    short4 p;
+    p.r = (Y * 298 + V * 409 + 128) >> 8;
+    p.g = (Y * 298 - U * 100 - V * 208 + 128) >> 8;
+    p.b = (Y * 298 + U * 516 + 128) >> 8;
+    p.a = 255;
+    p.r = rsClamp(p.r, (short)0, (short)255);
+    p.g = rsClamp(p.g, (short)0, (short)255);
+    p.b = rsClamp(p.b, (short)0, (short)255);
+
+    return convert_uchar4(p);
+}
+
+static float4 yuv_U_values = {0.f, -0.392f * 0.003921569f, +2.02 * 0.003921569f, 0.f};
+static float4 yuv_V_values = {1.603f * 0.003921569f, -0.815f * 0.003921569f, 0.f, 0.f};
+
+extern float4 __attribute__((overloadable)) rsYuvToRGBA_float4(uchar y, uchar u, uchar v) {
+    float4 color = (float)y * 0.003921569f;
+    float4 fU = ((float)u) - 128.f;
+    float4 fV = ((float)v) - 128.f;
+
+    color += fU * yuv_U_values;
+    color += fV * yuv_V_values;
+    color = clamp(color, 0.f, 1.f);
+    return color;
+}
+
+
+/*
+ * half_RECIP
+ */
+
+extern float __attribute__((overloadable)) half_recip(float v) {
+    // FIXME:  actual algorithm for generic approximate reciprocal
+    return 1.f / v;
+}
+
+extern float2 __attribute__((overloadable)) half_recip(float2 v) {
+    float2 r;
+    r.x = half_recip(r.x);
+    r.y = half_recip(r.y);
+    return r;
+}
+
+extern float3 __attribute__((overloadable)) half_recip(float3 v) {
+    float3 r;
+    r.x = half_recip(r.x);
+    r.y = half_recip(r.y);
+    r.z = half_recip(r.z);
+    return r;
+}
+
+extern float4 __attribute__((overloadable)) half_recip(float4 v) {
+    float4 r;
+    r.x = half_recip(r.x);
+    r.y = half_recip(r.y);
+    r.z = half_recip(r.z);
+    r.w = half_recip(r.w);
+    return r;
+}
+
+
+/*
+ * half_SQRT
+ */
+
+extern float __attribute__((overloadable)) half_sqrt(float v) {
+    return sqrt(v);
+}
+
+extern float2 __attribute__((overloadable)) half_sqrt(float2 v) {
+    float2 r;
+    r.x = half_sqrt(v.x);
+    r.y = half_sqrt(v.y);
+    return r;
+}
+
+extern float3 __attribute__((overloadable)) half_sqrt(float3 v) {
+    float3 r;
+    r.x = half_sqrt(v.x);
+    r.y = half_sqrt(v.y);
+    r.z = half_sqrt(v.z);
+    return r;
+}
+
+extern float4 __attribute__((overloadable)) half_sqrt(float4 v) {
+    float4 r;
+    r.x = half_sqrt(v.x);
+    r.y = half_sqrt(v.y);
+    r.z = half_sqrt(v.z);
+    r.w = half_sqrt(v.w);
+    return r;
+}
+
+
+/*
+ * half_rsqrt
+ */
+
+extern float __attribute__((overloadable)) half_rsqrt(float v) {
+    return 1.f / sqrt(v);
+}
+
+extern float2 __attribute__((overloadable)) half_rsqrt(float2 v) {
+    float2 r;
+    r.x = half_rsqrt(v.x);
+    r.y = half_rsqrt(v.y);
+    return r;
+}
+
+extern float3 __attribute__((overloadable)) half_rsqrt(float3 v) {
+    float3 r;
+    r.x = half_rsqrt(v.x);
+    r.y = half_rsqrt(v.y);
+    r.z = half_rsqrt(v.z);
+    return r;
+}
+
+extern float4 __attribute__((overloadable)) half_rsqrt(float4 v) {
+    float4 r;
+    r.x = half_rsqrt(v.x);
+    r.y = half_rsqrt(v.y);
+    r.z = half_rsqrt(v.z);
+    r.w = half_rsqrt(v.w);
+    return r;
+}
+
diff --git a/driver/runtime/arch/x86_math.ll b/driver/runtime/arch/x86_math.ll
new file mode 100755
index 0000000..60add80
--- /dev/null
+++ b/driver/runtime/arch/x86_math.ll
@@ -0,0 +1,40 @@
+target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S128"
+target triple = "i386-unknown-linux-gnu"
+
+declare float @llvm.sqrt.f32(float) nounwind readnone
+declare <2 x float> @llvm.sqrt.v2f32(<2 x float>) nounwind readnone
+declare <3 x float> @llvm.sqrt.v3f32(<3 x float>) nounwind readnone
+declare <4 x float> @llvm.sqrt.v4f32(<4 x float>) nounwind readnone
+declare float @llvm.exp.f32(float) nounwind readonly
+declare float @llvm.pow.f32(float, float) nounwind readonly
+
+define float @_Z4sqrtf(float %in) nounwind readnone alwaysinline {
+  %1 = tail call float @llvm.sqrt.f32(float %in) nounwind readnone
+  ret float %1
+}
+
+define <2 x float> @_Z4sqrtDv2_f(<2 x float> %in) nounwind readnone alwaysinline {
+  %1 = tail call <2 x float> @llvm.sqrt.v2f32(<2 x float> %in) nounwind readnone
+  ret <2 x float> %1
+}
+
+define <3 x float> @_Z4sqrtDv3_f(<3 x float> %in) nounwind readnone alwaysinline {
+  %1 = tail call <3 x float> @llvm.sqrt.v3f32(<3 x float> %in) nounwind readnone
+  ret <3 x float> %1
+}
+
+define <4 x float> @_Z4sqrtDv4_f(<4 x float> %in) nounwind readnone alwaysinline {
+  %1 = tail call <4 x float> @llvm.sqrt.v4f32(<4 x float> %in) nounwind readnone
+  ret <4 x float> %1
+}
+
+define float @_Z3expf(float %in) nounwind readnone {
+  %1 = tail call float @llvm.exp.f32(float %in) nounwind readnone
+  ret float %1
+}
+
+define float @_Z3powff(float %v1, float %v2) nounwind readnone {
+  %1 = tail call float @llvm.pow.f32(float %v1, float %v2) nounwind readnone
+  ret float %1
+}
+
diff --git a/driver/runtime/build_bc_lib.mk b/driver/runtime/build_bc_lib.mk
new file mode 100644
index 0000000..0344983
--- /dev/null
+++ b/driver/runtime/build_bc_lib.mk
@@ -0,0 +1,75 @@
+#
+# Copyright (C) 2012 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+include $(BUILD_SYSTEM)/base_rules.mk
+
+BCC_STRIP_ATTR := $(BUILD_OUT_EXECUTABLES)/bcc_strip_attr$(BUILD_EXECUTABLE_SUFFIX)
+
+# We need to pass the +long64 flag to the underlying version of Clang, since
+# we are generating a library for use with Renderscript (64-bit long type,
+# not 32-bit).
+bc_clang_cc1_cflags := -target-feature +long64
+bc_translated_clang_cc1_cflags := $(addprefix -Xclang , $(bc_clang_cc1_cflags))
+
+bc_cflags := -MD \
+             -DRS_VERSION=$(RS_VERSION) \
+             -std=c99 \
+             -c \
+             -O3 \
+             -fno-builtin \
+             -emit-llvm \
+             -target armv7-none-linux-gnueabi \
+             -fsigned-char \
+             $(LOCAL_CFLAGS) \
+             $(bc_translated_clang_cc1_cflags)
+
+ifeq ($(rs_debug_runtime),1)
+bc_cflags += -DRS_DEBUG_RUNTIME
+endif
+rs_debug_runtime:=
+
+c_sources := $(filter %.c,$(LOCAL_SRC_FILES))
+ll_sources := $(filter %.ll,$(LOCAL_SRC_FILES))
+
+c_bc_files := $(patsubst %.c,%.bc, \
+    $(addprefix $(intermediates)/, $(c_sources)))
+
+ll_bc_files := $(patsubst %.ll,%.bc, \
+    $(addprefix $(intermediates)/, $(ll_sources)))
+
+$(c_bc_files): PRIVATE_INCLUDES := \
+    frameworks/rs/scriptc \
+    external/clang/lib/Headers
+$(c_bc_files): PRIVATE_CFLAGS := $(bc_cflags)
+
+$(c_bc_files): $(intermediates)/%.bc: $(LOCAL_PATH)/%.c  $(CLANG)
+	@mkdir -p $(dir $@)
+	$(hide) $(CLANG) $(addprefix -I, $(PRIVATE_INCLUDES)) $(PRIVATE_CFLAGS) $< -o $@
+
+$(ll_bc_files): $(intermediates)/%.bc: $(LOCAL_PATH)/%.ll $(LLVM_AS)
+	@mkdir -p $(dir $@)
+	$(hide) $(LLVM_AS) $< -o $@
+
+-include $(c_bc_files:%.bc=%.d)
+-include $(ll_bc_files:%.bc=%.d)
+
+$(LOCAL_BUILT_MODULE): PRIVATE_BC_FILES := $(c_bc_files) $(ll_bc_files)
+$(LOCAL_BUILT_MODULE): $(c_bc_files) $(ll_bc_files)
+$(LOCAL_BUILT_MODULE): $(LLVM_LINK) $(clcore_LLVM_LD)
+$(LOCAL_BUILT_MODULE): $(LLVM_AS) $(BCC_STRIP_ATTR)
+	@mkdir -p $(dir $@)
+	$(hide) $(LLVM_LINK) $(PRIVATE_BC_FILES) -o $@.unstripped
+	$(hide) $(BCC_STRIP_ATTR) -o $@ $@.unstripped
diff --git a/driver/runtime/convert.ll b/driver/runtime/convert.ll
new file mode 100644
index 0000000..f45850d
--- /dev/null
+++ b/driver/runtime/convert.ll
@@ -0,0 +1,731 @@
+target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:64:128-a0:0:64-n32-S64"
+target triple = "armv7-none-linux-gnueabi"
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;                  FLOAT                 ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+define <2 x float> @_Z14convert_float2Dv2_h(<2 x i8> %in) nounwind readnone alwaysinline {
+  %1 = uitofp <2 x i8> %in to <2 x float>
+  ret <2 x float> %1
+}
+
+define <3 x float> @_Z14convert_float3Dv3_h(<3 x i8> %in) nounwind readnone alwaysinline {
+  %1 = uitofp <3 x i8> %in to <3 x float>
+  ret <3 x float> %1
+}
+
+define <4 x float> @_Z14convert_float4Dv4_h(<4 x i8> %in) nounwind readnone alwaysinline {
+  %1 = uitofp <4 x i8> %in to <4 x float>
+  ret <4 x float> %1
+}
+
+define <2 x float> @_Z14convert_float2Dv2_c(<2 x i8> %in) nounwind readnone alwaysinline {
+  %1 = sitofp <2 x i8> %in to <2 x float>
+  ret <2 x float> %1
+}
+
+define <3 x float> @_Z14convert_float3Dv3_c(<3 x i8> %in) nounwind readnone alwaysinline {
+  %1 = sitofp <3 x i8> %in to <3 x float>
+  ret <3 x float> %1
+}
+
+define <4 x float> @_Z14convert_float4Dv4_c(<4 x i8> %in) nounwind readnone alwaysinline {
+  %1 = sitofp <4 x i8> %in to <4 x float>
+  ret <4 x float> %1
+}
+
+define <2 x float> @_Z14convert_float2Dv2_t(<2 x i16> %in) nounwind readnone alwaysinline {
+  %1 = uitofp <2 x i16> %in to <2 x float>
+  ret <2 x float> %1
+}
+
+define <3 x float> @_Z14convert_float3Dv3_t(<3 x i16> %in) nounwind readnone alwaysinline {
+  %1 = uitofp <3 x i16> %in to <3 x float>
+  ret <3 x float> %1
+}
+
+define <4 x float> @_Z14convert_float4Dv4_t(<4 x i16> %in) nounwind readnone alwaysinline {
+  %1 = uitofp <4 x i16> %in to <4 x float>
+  ret <4 x float> %1
+}
+
+define <2 x float> @_Z14convert_float2Dv2_s(<2 x i16> %in) nounwind readnone alwaysinline {
+  %1 = sitofp <2 x i16> %in to <2 x float>
+  ret <2 x float> %1
+}
+
+define <3 x float> @_Z14convert_float3Dv3_s(<3 x i16> %in) nounwind readnone alwaysinline {
+  %1 = sitofp <3 x i16> %in to <3 x float>
+  ret <3 x float> %1
+}
+
+define <4 x float> @_Z14convert_float4Dv4_s(<4 x i16> %in) nounwind readnone alwaysinline {
+  %1 = sitofp <4 x i16> %in to <4 x float>
+  ret <4 x float> %1
+}
+
+define <2 x float> @_Z14convert_float2Dv2_j(<2 x i32> %in) nounwind readnone alwaysinline {
+  %1 = uitofp <2 x i32> %in to <2 x float>
+  ret <2 x float> %1
+}
+
+define <3 x float> @_Z14convert_float3Dv3_j(<3 x i32> %in) nounwind readnone alwaysinline {
+  %1 = uitofp <3 x i32> %in to <3 x float>
+  ret <3 x float> %1
+}
+
+define <4 x float> @_Z14convert_float4Dv4_j(<4 x i32> %in) nounwind readnone alwaysinline {
+  %1 = uitofp <4 x i32> %in to <4 x float>
+  ret <4 x float> %1
+}
+
+define <2 x float> @_Z14convert_float2Dv2_i(<2 x i32> %in) nounwind readnone alwaysinline {
+  %1 = sitofp <2 x i32> %in to <2 x float>
+  ret <2 x float> %1
+}
+
+define <3 x float> @_Z14convert_float3Dv3_i(<3 x i32> %in) nounwind readnone alwaysinline {
+  %1 = sitofp <3 x i32> %in to <3 x float>
+  ret <3 x float> %1
+}
+
+define <4 x float> @_Z14convert_float4Dv4_i(<4 x i32> %in) nounwind readnone alwaysinline {
+  %1 = sitofp <4 x i32> %in to <4 x float>
+  ret <4 x float> %1
+}
+
+define <2 x float> @_Z14convert_float2Dv2_f(<2 x float> %in) nounwind readnone alwaysinline {
+  ret <2 x float> %in
+}
+
+define <3 x float> @_Z14convert_float3Dv3_f(<3 x float> %in) nounwind readnone alwaysinline {
+  ret <3 x float> %in
+}
+
+define <4 x float> @_Z14convert_float4Dv4_f(<4 x float> %in) nounwind readnone alwaysinline {
+  ret <4 x float> %in
+}
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;                  CHAR                  ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+define <4 x i8> @_Z13convert_char4Dv4_f(<4 x float> %in) nounwind readnone alwaysinline {
+  %1 = fptosi <4 x float> %in to <4 x i8>
+  ret <4 x i8> %1
+}
+
+define <3 x i8> @_Z13convert_char3Dv3_f(<3 x float> %in) nounwind readnone alwaysinline {
+  %1 = fptosi <3 x float> %in to <3 x i8>
+  ret <3 x i8> %1
+}
+
+define <2 x i8> @_Z13convert_char2Dv2_f(<2 x float> %in) nounwind readnone alwaysinline {
+  %1 = fptosi <2 x float> %in to <2 x i8>
+  ret <2 x i8> %1
+}
+
+define <4 x i8> @_Z13convert_char4Dv4_h(<4 x i8> %in) nounwind readnone alwaysinline {
+  ret <4 x i8> %in
+}
+
+define <3 x i8> @_Z13convert_char3Dv3_h(<3 x i8> %in) nounwind readnone alwaysinline {
+  ret <3 x i8> %in
+}
+
+define <2 x i8> @_Z13convert_char2Dv2_h(<2 x i8> %in) nounwind readnone alwaysinline {
+  ret <2 x i8> %in
+}
+
+define <4 x i8> @_Z13convert_char4Dv4_c(<4 x i8> %in) nounwind readnone alwaysinline {
+  ret <4 x i8> %in
+}
+
+define <3 x i8> @_Z13convert_char3Dv3_c(<3 x i8> %in) nounwind readnone alwaysinline {
+  ret <3 x i8> %in
+}
+
+define <2 x i8> @_Z13convert_char2Dv2_c(<2 x i8> %in) nounwind readnone alwaysinline {
+  ret <2 x i8> %in
+}
+
+define <4 x i8> @_Z13convert_char4Dv4_t(<4 x i16> %in) nounwind readnone alwaysinline {
+  %1 = trunc <4 x i16> %in to <4 x i8>
+  ret <4 x i8> %1
+}
+
+define <3 x i8> @_Z13convert_char3Dv3_t(<3 x i16> %in) nounwind readnone alwaysinline {
+  %1 = trunc <3 x i16> %in to <3 x i8>
+  ret <3 x i8> %1
+}
+
+define <2 x i8> @_Z13convert_char2Dv2_t(<2 x i16> %in) nounwind readnone alwaysinline {
+  %1 = trunc <2 x i16> %in to <2 x i8>
+  ret <2 x i8> %1
+}
+
+define <4 x i8> @_Z13convert_char4Dv4_s(<4 x i16> %in) nounwind readnone alwaysinline {
+  %1 = trunc <4 x i16> %in to <4 x i8>
+  ret <4 x i8> %1
+}
+
+define <3 x i8> @_Z13convert_char3Dv3_s(<3 x i16> %in) nounwind readnone alwaysinline {
+  %1 = trunc <3 x i16> %in to <3 x i8>
+  ret <3 x i8> %1
+}
+
+define <2 x i8> @_Z13convert_char2Dv2_s(<2 x i16> %in) nounwind readnone alwaysinline {
+  %1 = trunc <2 x i16> %in to <2 x i8>
+  ret <2 x i8> %1
+}
+
+define <4 x i8> @_Z13convert_char4Dv4_j(<4 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <4 x i32> %in to <4 x i8>
+  ret <4 x i8> %1
+}
+
+define <3 x i8> @_Z13convert_char3Dv3_j(<3 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <3 x i32> %in to <3 x i8>
+  ret <3 x i8> %1
+}
+
+define <2 x i8> @_Z13convert_char2Dv2_j(<2 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <2 x i32> %in to <2 x i8>
+  ret <2 x i8> %1
+}
+
+define <4 x i8> @_Z13convert_char4Dv4_i(<4 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <4 x i32> %in to <4 x i8>
+  ret <4 x i8> %1
+}
+
+define <3 x i8> @_Z13convert_char3Dv3_i(<3 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <3 x i32> %in to <3 x i8>
+  ret <3 x i8> %1
+}
+
+define <2 x i8> @_Z13convert_char2Dv2_i(<2 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <2 x i32> %in to <2 x i8>
+  ret <2 x i8> %1
+}
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;                  UCHAR                 ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+define <4 x i8> @_Z14convert_uchar4Dv4_f(<4 x float> %in) nounwind readnone alwaysinline {
+  %1 = fptoui <4 x float> %in to <4 x i8>
+  ret <4 x i8> %1
+}
+
+define <3 x i8> @_Z14convert_uchar3Dv3_f(<3 x float> %in) nounwind readnone alwaysinline {
+  %1 = fptoui <3 x float> %in to <3 x i8>
+  ret <3 x i8> %1
+}
+
+define <2 x i8> @_Z14convert_uchar2Dv2_f(<2 x float> %in) nounwind readnone alwaysinline {
+  %1 = fptoui <2 x float> %in to <2 x i8>
+  ret <2 x i8> %1
+}
+
+define <4 x i8> @_Z14convert_uchar4Dv4_h(<4 x i8> %in) nounwind readnone alwaysinline {
+  ret <4 x i8> %in
+}
+
+define <3 x i8> @_Z14convert_uchar3Dv3_h(<3 x i8> %in) nounwind readnone alwaysinline {
+  ret <3 x i8> %in
+}
+
+define <2 x i8> @_Z14convert_uchar2Dv2_h(<2 x i8> %in) nounwind readnone alwaysinline {
+  ret <2 x i8> %in
+}
+
+define <4 x i8> @_Z14convert_uchar4Dv4_c(<4 x i8> %in) nounwind readnone alwaysinline {
+  ret <4 x i8> %in
+}
+
+define <3 x i8> @_Z14convert_uchar3Dv3_c(<3 x i8> %in) nounwind readnone alwaysinline {
+  ret <3 x i8> %in
+}
+
+define <2 x i8> @_Z14convert_uchar2Dv2_c(<2 x i8> %in) nounwind readnone alwaysinline {
+  ret <2 x i8> %in
+}
+
+define <4 x i8> @_Z14convert_uchar4Dv4_t(<4 x i16> %in) nounwind readnone alwaysinline {
+  %1 = trunc <4 x i16> %in to <4 x i8>
+  ret <4 x i8> %1
+}
+
+define <3 x i8> @_Z14convert_uchar3Dv3_t(<3 x i16> %in) nounwind readnone alwaysinline {
+  %1 = trunc <3 x i16> %in to <3 x i8>
+  ret <3 x i8> %1
+}
+
+define <2 x i8> @_Z14convert_uchar2Dv2_t(<2 x i16> %in) nounwind readnone alwaysinline {
+  %1 = trunc <2 x i16> %in to <2 x i8>
+  ret <2 x i8> %1
+}
+
+define <4 x i8> @_Z14convert_uchar4Dv4_s(<4 x i16> %in) nounwind readnone alwaysinline {
+  %1 = trunc <4 x i16> %in to <4 x i8>
+  ret <4 x i8> %1
+}
+
+define <3 x i8> @_Z14convert_uchar3Dv3_s(<3 x i16> %in) nounwind readnone alwaysinline {
+  %1 = trunc <3 x i16> %in to <3 x i8>
+  ret <3 x i8> %1
+}
+
+define <2 x i8> @_Z14convert_uchar2Dv2_s(<2 x i16> %in) nounwind readnone alwaysinline {
+  %1 = trunc <2 x i16> %in to <2 x i8>
+  ret <2 x i8> %1
+}
+
+define <4 x i8> @_Z14convert_uchar4Dv4_j(<4 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <4 x i32> %in to <4 x i8>
+  ret <4 x i8> %1
+}
+
+define <3 x i8> @_Z14convert_uchar3Dv3_j(<3 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <3 x i32> %in to <3 x i8>
+  ret <3 x i8> %1
+}
+
+define <2 x i8> @_Z14convert_uchar2Dv2_j(<2 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <2 x i32> %in to <2 x i8>
+  ret <2 x i8> %1
+}
+
+define <4 x i8> @_Z14convert_uchar4Dv4_i(<4 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <4 x i32> %in to <4 x i8>
+  ret <4 x i8> %1
+}
+
+define <3 x i8> @_Z14convert_uchar3Dv3_i(<3 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <3 x i32> %in to <3 x i8>
+  ret <3 x i8> %1
+}
+
+define <2 x i8> @_Z14convert_uchar2Dv2_i(<2 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <2 x i32> %in to <2 x i8>
+  ret <2 x i8> %1
+}
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;                  SHORT                 ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+define <4 x i16> @_Z14convert_short4Dv4_f(<4 x float> %in) nounwind readnone alwaysinline {
+  %1 = fptosi <4 x float> %in to <4 x i16>
+  ret <4 x i16> %1
+}
+
+define <3 x i16> @_Z14convert_short3Dv3_f(<3 x float> %in) nounwind readnone alwaysinline {
+  %1 = fptosi <3 x float> %in to <3 x i16>
+  ret <3 x i16> %1
+}
+
+define <2 x i16> @_Z14convert_short2Dv2_f(<2 x float> %in) nounwind readnone alwaysinline {
+  %1 = fptosi <2 x float> %in to <2 x i16>
+  ret <2 x i16> %1
+}
+
+define <4 x i16> @_Z14convert_short4Dv4_h(<4 x i8> %in) nounwind readnone alwaysinline {
+  %1 = zext <4 x i8> %in to <4 x i16>
+  ret <4 x i16> %1
+}
+
+define <3 x i16> @_Z14convert_short3Dv3_h(<3 x i8> %in) nounwind readnone alwaysinline {
+  %1 = zext <3 x i8> %in to <3 x i16>
+  ret <3 x i16> %1
+}
+
+define <2 x i16> @_Z14convert_short2Dv2_h(<2 x i8> %in) nounwind readnone alwaysinline {
+  %1 = zext <2 x i8> %in to <2 x i16>
+  ret <2 x i16> %1
+}
+
+define <4 x i16> @_Z14convert_short4Dv4_c(<4 x i8> %in) nounwind readnone alwaysinline {
+  %1 = sext <4 x i8> %in to <4 x i16>
+  ret <4 x i16> %1
+}
+
+define <3 x i16> @_Z14convert_short3Dv3_c(<3 x i8> %in) nounwind readnone alwaysinline {
+  %1 = sext <3 x i8> %in to <3 x i16>
+  ret <3 x i16> %1
+}
+
+define <2 x i16> @_Z14convert_short2Dv2_c(<2 x i8> %in) nounwind readnone alwaysinline {
+  %1 = sext <2 x i8> %in to <2 x i16>
+  ret <2 x i16> %1
+}
+
+define <4 x i16> @_Z14convert_short4Dv4_t(<4 x i16> %in) nounwind readnone alwaysinline {
+  ret <4 x i16> %in
+}
+
+define <3 x i16> @_Z14convert_short3Dv3_t(<3 x i16> %in) nounwind readnone alwaysinline {
+  ret <3 x i16> %in
+}
+
+define <2 x i16> @_Z14convert_short2Dv2_t(<2 x i16> %in) nounwind readnone alwaysinline {
+  ret <2 x i16> %in
+}
+
+define <4 x i16> @_Z14convert_short4Dv4_s(<4 x i16> %in) nounwind readnone alwaysinline {
+  ret <4 x i16> %in
+}
+
+define <3 x i16> @_Z14convert_short3Dv3_s(<3 x i16> %in) nounwind readnone alwaysinline {
+  ret <3 x i16> %in
+}
+
+define <2 x i16> @_Z14convert_short2Dv2_s(<2 x i16> %in) nounwind readnone alwaysinline {
+  ret <2 x i16> %in
+}
+
+define <4 x i16> @_Z14convert_short4Dv4_j(<4 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <4 x i32> %in to <4 x i16>
+  ret <4 x i16> %1
+}
+
+define <3 x i16> @_Z14convert_short3Dv3_j(<3 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <3 x i32> %in to <3 x i16>
+  ret <3 x i16> %1
+}
+
+define <2 x i16> @_Z14convert_short2Dv2_j(<2 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <2 x i32> %in to <2 x i16>
+  ret <2 x i16> %1
+}
+
+define <4 x i16> @_Z14convert_short4Dv4_i(<4 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <4 x i32> %in to <4 x i16>
+  ret <4 x i16> %1
+}
+
+define <3 x i16> @_Z14convert_short3Dv3_i(<3 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <3 x i32> %in to <3 x i16>
+  ret <3 x i16> %1
+}
+
+define <2 x i16> @_Z14convert_short2Dv2_i(<2 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <2 x i32> %in to <2 x i16>
+  ret <2 x i16> %1
+}
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;                 USHORT                 ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+define <4 x i16> @_Z15convert_ushort4Dv4_f(<4 x float> %in) nounwind readnone alwaysinline {
+  %1 = fptoui <4 x float> %in to <4 x i16>
+  ret <4 x i16> %1
+}
+
+define <3 x i16> @_Z15convert_ushort3Dv3_f(<3 x float> %in) nounwind readnone alwaysinline {
+  %1 = fptoui <3 x float> %in to <3 x i16>
+  ret <3 x i16> %1
+}
+
+define <2 x i16> @_Z15convert_ushort2Dv2_f(<2 x float> %in) nounwind readnone alwaysinline {
+  %1 = fptoui <2 x float> %in to <2 x i16>
+  ret <2 x i16> %1
+}
+
+define <4 x i16> @_Z15convert_ushort4Dv4_h(<4 x i8> %in) nounwind readnone alwaysinline {
+  %1 = zext <4 x i8> %in to <4 x i16>
+  ret <4 x i16> %1
+}
+
+define <3 x i16> @_Z15convert_ushort3Dv3_h(<3 x i8> %in) nounwind readnone alwaysinline {
+  %1 = zext <3 x i8> %in to <3 x i16>
+  ret <3 x i16> %1
+}
+
+define <2 x i16> @_Z15convert_ushort2Dv2_h(<2 x i8> %in) nounwind readnone alwaysinline {
+  %1 = zext <2 x i8> %in to <2 x i16>
+  ret <2 x i16> %1
+}
+
+define <4 x i16> @_Z15convert_ushort4Dv4_c(<4 x i8> %in) nounwind readnone alwaysinline {
+  %1 = zext <4 x i8> %in to <4 x i16>
+  ret <4 x i16> %1
+}
+
+define <3 x i16> @_Z15convert_ushort3Dv3_c(<3 x i8> %in) nounwind readnone alwaysinline {
+  %1 = zext <3 x i8> %in to <3 x i16>
+  ret <3 x i16> %1
+}
+
+define <2 x i16> @_Z15convert_ushort2Dv2_c(<2 x i8> %in) nounwind readnone alwaysinline {
+  %1 = zext <2 x i8> %in to <2 x i16>
+  ret <2 x i16> %1
+}
+
+define <4 x i16> @_Z15convert_ushort4Dv4_t(<4 x i16> %in) nounwind readnone alwaysinline {
+  ret <4 x i16> %in
+}
+
+define <3 x i16> @_Z15convert_ushort3Dv3_t(<3 x i16> %in) nounwind readnone alwaysinline {
+  ret <3 x i16> %in
+}
+
+define <2 x i16> @_Z15convert_ushort2Dv2_t(<2 x i16> %in) nounwind readnone alwaysinline {
+  ret <2 x i16> %in
+}
+
+define <4 x i16> @_Z15convert_ushort4Dv4_s(<4 x i16> %in) nounwind readnone alwaysinline {
+  ret <4 x i16> %in
+}
+
+define <3 x i16> @_Z15convert_ushort3Dv3_s(<3 x i16> %in) nounwind readnone alwaysinline {
+  ret <3 x i16> %in
+}
+
+define <2 x i16> @_Z15convert_ushort2Dv2_s(<2 x i16> %in) nounwind readnone alwaysinline {
+  ret <2 x i16> %in
+}
+
+define <4 x i16> @_Z15convert_ushort4Dv4_j(<4 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <4 x i32> %in to <4 x i16>
+  ret <4 x i16> %1
+}
+
+define <3 x i16> @_Z15convert_ushort3Dv3_j(<3 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <3 x i32> %in to <3 x i16>
+  ret <3 x i16> %1
+}
+
+define <2 x i16> @_Z15convert_ushort2Dv2_j(<2 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <2 x i32> %in to <2 x i16>
+  ret <2 x i16> %1
+}
+
+define <4 x i16> @_Z15convert_ushort4Dv4_i(<4 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <4 x i32> %in to <4 x i16>
+  ret <4 x i16> %1
+}
+
+define <3 x i16> @_Z15convert_ushort3Dv3_i(<3 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <3 x i32> %in to <3 x i16>
+  ret <3 x i16> %1
+}
+
+define <2 x i16> @_Z15convert_ushort2Dv2_i(<2 x i32> %in) nounwind readnone alwaysinline {
+  %1 = trunc <2 x i32> %in to <2 x i16>
+  ret <2 x i16> %1
+}
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;                   INT                  ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+define <4 x i32> @_Z12convert_int4Dv4_f(<4 x float> %in) nounwind readnone alwaysinline {
+  %1 = fptosi <4 x float> %in to <4 x i32>
+  ret <4 x i32> %1
+}
+
+define <3 x i32> @_Z12convert_int3Dv3_f(<3 x float> %in) nounwind readnone alwaysinline {
+  %1 = fptosi <3 x float> %in to <3 x i32>
+  ret <3 x i32> %1
+}
+
+define <2 x i32> @_Z12convert_int2Dv2_f(<2 x float> %in) nounwind readnone alwaysinline {
+  %1 = fptosi <2 x float> %in to <2 x i32>
+  ret <2 x i32> %1
+}
+
+define <4 x i32> @_Z12convert_int4Dv4_h(<4 x i8> %in) nounwind readnone alwaysinline {
+  %1 = zext <4 x i8> %in to <4 x i32>
+  ret <4 x i32> %1
+}
+
+define <3 x i32> @_Z12convert_int3Dv3_h(<3 x i8> %in) nounwind readnone alwaysinline {
+  %1 = zext <3 x i8> %in to <3 x i32>
+  ret <3 x i32> %1
+}
+
+define <2 x i32> @_Z12convert_int2Dv2_h(<2 x i8> %in) nounwind readnone alwaysinline {
+  %1 = zext <2 x i8> %in to <2 x i32>
+  ret <2 x i32> %1
+}
+
+define <4 x i32> @_Z12convert_int4Dv4_c(<4 x i8> %in) nounwind readnone alwaysinline {
+  %1 = sext <4 x i8> %in to <4 x i32>
+  ret <4 x i32> %1
+}
+
+define <3 x i32> @_Z12convert_int3Dv3_c(<3 x i8> %in) nounwind readnone alwaysinline {
+  %1 = sext <3 x i8> %in to <3 x i32>
+  ret <3 x i32> %1
+}
+
+define <2 x i32> @_Z12convert_int2Dv2_c(<2 x i8> %in) nounwind readnone alwaysinline {
+  %1 = sext <2 x i8> %in to <2 x i32>
+  ret <2 x i32> %1
+}
+
+define <4 x i32> @_Z12convert_int4Dv4_t(<4 x i16> %in) nounwind readnone alwaysinline {
+  %1 = zext <4 x i16> %in to <4 x i32>
+  ret <4 x i32> %1
+}
+
+define <3 x i32> @_Z12convert_int3Dv3_t(<3 x i16> %in) nounwind readnone alwaysinline {
+  %1 = zext <3 x i16> %in to <3 x i32>
+  ret <3 x i32> %1
+}
+
+define <2 x i32> @_Z12convert_int2Dv2_t(<2 x i16> %in) nounwind readnone alwaysinline {
+  %1 = zext <2 x i16> %in to <2 x i32>
+  ret <2 x i32> %1
+}
+
+define <4 x i32> @_Z12convert_int4Dv4_s(<4 x i16> %in) nounwind readnone alwaysinline {
+  %1 = sext <4 x i16> %in to <4 x i32>
+  ret <4 x i32> %1
+}
+
+define <3 x i32> @_Z12convert_int3Dv3_s(<3 x i16> %in) nounwind readnone alwaysinline {
+  %1 = sext <3 x i16> %in to <3 x i32>
+  ret <3 x i32> %1
+}
+
+define <2 x i32> @_Z12convert_int2Dv2_s(<2 x i16> %in) nounwind readnone alwaysinline {
+  %1 = sext <2 x i16> %in to <2 x i32>
+  ret <2 x i32> %1
+}
+
+define <4 x i32> @_Z12convert_int4Dv4_j(<4 x i32> %in) nounwind readnone alwaysinline {
+  ret <4 x i32> %in
+}
+
+define <3 x i32> @_Z12convert_int3Dv3_j(<3 x i32> %in) nounwind readnone alwaysinline {
+  ret <3 x i32> %in
+}
+
+define <2 x i32> @_Z12convert_int2Dv2_j(<2 x i32> %in) nounwind readnone alwaysinline {
+  ret <2 x i32> %in
+}
+
+define <4 x i32> @_Z12convert_int4Dv4_i(<4 x i32> %in) nounwind readnone alwaysinline {
+  ret <4 x i32> %in
+}
+
+define <3 x i32> @_Z12convert_int3Dv3_i(<3 x i32> %in) nounwind readnone alwaysinline {
+  ret <3 x i32> %in
+}
+
+define <2 x i32> @_Z12convert_int2Dv2_i(<2 x i32> %in) nounwind readnone alwaysinline {
+  ret <2 x i32> %in
+}
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;                  UINT                  ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+define <4 x i32> @_Z13convert_uint4Dv4_f(<4 x float> %in) nounwind readnone alwaysinline {
+  %1 = fptoui <4 x float> %in to <4 x i32>
+  ret <4 x i32> %1
+}
+
+define <3 x i32> @_Z13convert_uint3Dv3_f(<3 x float> %in) nounwind readnone alwaysinline {
+  %1 = fptoui <3 x float> %in to <3 x i32>
+  ret <3 x i32> %1
+}
+
+define <2 x i32> @_Z13convert_uint2Dv2_f(<2 x float> %in) nounwind readnone alwaysinline {
+  %1 = fptoui <2 x float> %in to <2 x i32>
+  ret <2 x i32> %1
+}
+
+define <4 x i32> @_Z13convert_uint4Dv4_h(<4 x i8> %in) nounwind readnone alwaysinline {
+  %1 = zext <4 x i8> %in to <4 x i32>
+  ret <4 x i32> %1
+}
+
+define <3 x i32> @_Z13convert_uint3Dv3_h(<3 x i8> %in) nounwind readnone alwaysinline {
+  %1 = zext <3 x i8> %in to <3 x i32>
+  ret <3 x i32> %1
+}
+
+define <2 x i32> @_Z13convert_uint2Dv2_h(<2 x i8> %in) nounwind readnone alwaysinline {
+  %1 = zext <2 x i8> %in to <2 x i32>
+  ret <2 x i32> %1
+}
+
+define <4 x i32> @_Z13convert_uint4Dv4_c(<4 x i8> %in) nounwind readnone alwaysinline {
+  %1 = zext <4 x i8> %in to <4 x i32>
+  ret <4 x i32> %1
+}
+
+define <3 x i32> @_Z13convert_uint3Dv3_c(<3 x i8> %in) nounwind readnone alwaysinline {
+  %1 = zext <3 x i8> %in to <3 x i32>
+  ret <3 x i32> %1
+}
+
+define <2 x i32> @_Z13convert_uint2Dv2_c(<2 x i8> %in) nounwind readnone alwaysinline {
+  %1 = zext <2 x i8> %in to <2 x i32>
+  ret <2 x i32> %1
+}
+
+define <4 x i32> @_Z13convert_uint4Dv4_t(<4 x i16> %in) nounwind readnone alwaysinline {
+  %1 = zext <4 x i16> %in to <4 x i32>
+  ret <4 x i32> %1
+}
+
+define <3 x i32> @_Z13convert_uint3Dv3_t(<3 x i16> %in) nounwind readnone alwaysinline {
+  %1 = zext <3 x i16> %in to <3 x i32>
+  ret <3 x i32> %1
+}
+
+define <2 x i32> @_Z13convert_uint2Dv2_t(<2 x i16> %in) nounwind readnone alwaysinline {
+  %1 = zext <2 x i16> %in to <2 x i32>
+  ret <2 x i32> %1
+}
+
+define <4 x i32> @_Z13convert_uint4Dv4_s(<4 x i16> %in) nounwind readnone alwaysinline {
+  %1 = zext <4 x i16> %in to <4 x i32>
+  ret <4 x i32> %1
+}
+
+define <3 x i32> @_Z13convert_uint3Dv3_s(<3 x i16> %in) nounwind readnone alwaysinline {
+  %1 = zext <3 x i16> %in to <3 x i32>
+  ret <3 x i32> %1
+}
+
+define <2 x i32> @_Z13convert_uint2Dv2_s(<2 x i16> %in) nounwind readnone alwaysinline {
+  %1 = zext <2 x i16> %in to <2 x i32>
+  ret <2 x i32> %1
+}
+
+define <4 x i32> @_Z13convert_uint4Dv4_j(<4 x i32> %in) nounwind readnone alwaysinline {
+  ret <4 x i32> %in
+}
+
+define <3 x i32> @_Z13convert_uint3Dv3_j(<3 x i32> %in) nounwind readnone alwaysinline {
+  ret <3 x i32> %in
+}
+
+define <2 x i32> @_Z13convert_uint2Dv2_j(<2 x i32> %in) nounwind readnone alwaysinline {
+  ret <2 x i32> %in
+}
+
+define <4 x i32> @_Z13convert_uint4Dv4_i(<4 x i32> %in) nounwind readnone alwaysinline {
+  ret <4 x i32> %in
+}
+
+define <3 x i32> @_Z13convert_uint3Dv3_i(<3 x i32> %in) nounwind readnone alwaysinline {
+  ret <3 x i32> %in
+}
+
+define <2 x i32> @_Z13convert_uint2Dv2_i(<2 x i32> %in) nounwind readnone alwaysinline {
+  ret <2 x i32> %in
+}
diff --git a/driver/runtime/math.ll b/driver/runtime/math.ll
new file mode 100644
index 0000000..f026d15
--- /dev/null
+++ b/driver/runtime/math.ll
@@ -0,0 +1,19 @@
+target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:64:128-a0:0:64-n32-S64"
+target triple = "armv7-none-linux-gnueabi"
+
+declare float @llvm.sqrt.f32(float)
+declare float @llvm.pow.f32(float, float)
+declare float @llvm.fabs.f32(float)
+declare <2 x float> @llvm.fabs.v2f32(<2 x float>)
+declare <3 x float> @llvm.fabs.v3f32(<3 x float>)
+declare <4 x float> @llvm.fabs.v4f32(<4 x float>)
+
+define float @_Z4sqrtf(float %v) nounwind readnone alwaysinline {
+  %1 = tail call float @llvm.sqrt.f32(float %v)
+  ret float %1
+}
+
+define float @_Z3powf(float %v1, float %v2) nounwind readnone alwaysinline {
+  %1 = tail call float @llvm.pow.f32(float  %v1, float %v2)
+  ret float %1
+}
diff --git a/driver/runtime/matrix.ll b/driver/runtime/matrix.ll
new file mode 100644
index 0000000..c56405d
--- /dev/null
+++ b/driver/runtime/matrix.ll
@@ -0,0 +1,176 @@
+target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:64:128-a0:0:64-n32-S64"
+target triple = "armv7-none-linux-gnueabi"
+
+
+%struct.rs_matrix4x4 = type { [16 x float] }
+%struct.rs_matrix3x3 = type { [9 x float] }
+%struct.rs_matrix2x2 = type { [4 x float] }
+
+define internal <4 x float> @smear_f(float %in) nounwind readnone alwaysinline {
+  %1 = insertelement <4 x float> undef, float %in, i32 0
+  %2 = insertelement <4 x float> %1, float %in, i32 1
+  %3 = insertelement <4 x float> %2, float %in, i32 2
+  %4 = insertelement <4 x float> %3, float %in, i32 3
+  ret <4 x float> %4
+}
+
+
+define <3 x float> @_Z16rsMatrixMultiplyPK12rs_matrix3x3Dv3_f(%struct.rs_matrix3x3* nocapture %m, <3 x float> %in) nounwind readonly {
+  %x0 = extractelement <3 x float> %in, i32 0
+  %x = tail call <4 x float> @smear_f(float %x0) nounwind readnone
+  %y0 = extractelement <3 x float> %in, i32 1
+  %y = tail call <4 x float> @smear_f(float %y0) nounwind readnone
+  %z0 = extractelement <3 x float> %in, i32 2
+  %z = tail call <4 x float> @smear_f(float %z0) nounwind readnone
+
+  %px = getelementptr inbounds %struct.rs_matrix3x3* %m, i32 0, i32 0, i32 0
+  %px2 = bitcast float* %px to <4 x float>*
+  %xm = load <4 x float>* %px2, align 4
+  %py = getelementptr inbounds %struct.rs_matrix3x3* %m, i32 0, i32 0, i32 3
+  %py2 = bitcast float* %py to <4 x float>*
+  %ym = load <4 x float>* %py2, align 4
+  %pz = getelementptr inbounds %struct.rs_matrix3x3* %m, i32 0, i32 0, i32 6
+  %pz2 = bitcast float* %pz to <3 x float>*
+  %zm2 = load <3 x float>* %pz2, align 4
+  %zm = shufflevector <3 x float> %zm2, <3 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+
+  %a1 = fmul <4 x float> %x, %xm
+  %a2 = fmul <4 x float> %y, %ym
+  %a3 = fadd <4 x float> %a1, %a2
+  %a4 = fmul <4 x float> %z, %zm
+  %a5 = fadd <4 x float> %a4, %a3
+  %a6 = shufflevector <4 x float> %a5, <4 x float> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x float> %a6
+}
+
+define <3 x float> @_Z16rsMatrixMultiplyP12rs_matrix3x3Dv3_f(%struct.rs_matrix3x3* nocapture %m, <3 x float> %in) nounwind readonly {
+  %r = tail call <3 x float> @_Z16rsMatrixMultiplyPK12rs_matrix3x3Dv3_f(%struct.rs_matrix3x3* nocapture %m, <3 x float> %in) nounwind
+  ret <3 x float> %r
+}
+
+define <3 x float> @_Z16rsMatrixMultiplyPK12rs_matrix3x3Dv2_f(%struct.rs_matrix3x3* nocapture %m, <2 x float> %in) nounwind readonly {
+  %x0 = extractelement <2 x float> %in, i32 0
+  %x = tail call <4 x float> @smear_f(float %x0) nounwind readnone
+  %y0 = extractelement <2 x float> %in, i32 1
+  %y = tail call <4 x float> @smear_f(float %y0) nounwind readnone
+
+  %px = getelementptr inbounds %struct.rs_matrix3x3* %m, i32 0, i32 0, i32 0
+  %px2 = bitcast float* %px to <4 x float>*
+  %xm = load <4 x float>* %px2, align 4
+  %py = getelementptr inbounds %struct.rs_matrix3x3* %m, i32 0, i32 0, i32 3
+  %py2 = bitcast float* %py to <4 x float>*
+  %ym = load <4 x float>* %py2, align 4
+
+  %a1 = fmul <4 x float> %x, %xm
+  %a2 = fmul <4 x float> %y, %ym
+  %a3 = fadd <4 x float> %a1, %a2
+  %a4 = shufflevector <4 x float> %a3, <4 x float> undef, <3 x i32> <i32 0, i32 1, i32 2>
+  ret <3 x float> %a4
+}
+
+define <3 x float> @_Z16rsMatrixMultiplyP12rs_matrix3x3Dv2_f(%struct.rs_matrix3x3* nocapture %m, <2 x float> %in) nounwind readonly {
+  %r = tail call <3 x float> @_Z16rsMatrixMultiplyPK12rs_matrix3x3Dv2_f(%struct.rs_matrix3x3* nocapture %m, <2 x float> %in) nounwind
+  ret <3 x float> %r
+}
+
+define <4 x float> @_Z16rsMatrixMultiplyPK12rs_matrix4x4Dv4_f(%struct.rs_matrix4x4* nocapture %m, <4 x float> %in) nounwind readonly {
+  %x0 = extractelement <4 x float> %in, i32 0
+  %x = tail call <4 x float> @smear_f(float %x0) nounwind readnone
+  %y0 = extractelement <4 x float> %in, i32 1
+  %y = tail call <4 x float> @smear_f(float %y0) nounwind readnone
+  %z0 = extractelement <4 x float> %in, i32 2
+  %z = tail call <4 x float> @smear_f(float %z0) nounwind readnone
+  %w0 = extractelement <4 x float> %in, i32 3
+  %w = tail call <4 x float> @smear_f(float %w0) nounwind readnone
+
+  %px = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 0
+  %px2 = bitcast float* %px to <4 x float>*
+  %xm = load <4 x float>* %px2, align 4
+  %py = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 4
+  %py2 = bitcast float* %py to <4 x float>*
+  %ym = load <4 x float>* %py2, align 4
+  %pz = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 8
+  %pz2 = bitcast float* %pz to <4 x float>*
+  %zm = load <4 x float>* %pz2, align 4
+  %pw = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 12
+  %pw2 = bitcast float* %pw to <4 x float>*
+  %wm = load <4 x float>* %pw2, align 4
+
+  %a1 = fmul <4 x float> %x, %xm
+  %a2 = fmul <4 x float> %y, %ym
+  %a3 = fadd <4 x float> %a1, %a2
+  %a4 = fmul <4 x float> %z, %zm
+  %a5 = fadd <4 x float> %a3, %a4
+  %a6 = fmul <4 x float> %w, %wm
+  %a7 = fadd <4 x float> %a5, %a6
+  ret <4 x float> %a7
+}
+
+define <4 x float> @_Z16rsMatrixMultiplyP12rs_matrix4x4Dv4_f(%struct.rs_matrix4x4* nocapture %m, <4 x float> %in) nounwind readonly {
+  %r = tail call <4 x float> @_Z16rsMatrixMultiplyPK12rs_matrix4x4Dv4_f(%struct.rs_matrix4x4* nocapture %m, <4 x float> %in) nounwind
+  ret <4 x float> %r
+}
+
+define <4 x float> @_Z16rsMatrixMultiplyPK12rs_matrix4x4Dv3_f(%struct.rs_matrix4x4* nocapture %m, <3 x float> %in) nounwind readonly {
+  %x0 = extractelement <3 x float> %in, i32 0
+  %x = tail call <4 x float> @smear_f(float %x0) nounwind readnone
+  %y0 = extractelement <3 x float> %in, i32 1
+  %y = tail call <4 x float> @smear_f(float %y0) nounwind readnone
+  %z0 = extractelement <3 x float> %in, i32 2
+  %z = tail call <4 x float> @smear_f(float %z0) nounwind readnone
+
+  %px = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 0
+  %px2 = bitcast float* %px to <4 x float>*
+  %xm = load <4 x float>* %px2, align 4
+  %py = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 4
+  %py2 = bitcast float* %py to <4 x float>*
+  %ym = load <4 x float>* %py2, align 4
+  %pz = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 8
+  %pz2 = bitcast float* %pz to <4 x float>*
+  %zm = load <4 x float>* %pz2, align 4
+  %pw = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 12
+  %pw2 = bitcast float* %pw to <4 x float>*
+  %wm = load <4 x float>* %pw2, align 4
+
+  %a1 = fmul <4 x float> %x, %xm
+  %a2 = fadd <4 x float> %wm, %a1
+  %a3 = fmul <4 x float> %y, %ym
+  %a4 = fadd <4 x float> %a2, %a3
+  %a5 = fmul <4 x float> %z, %zm
+  %a6 = fadd <4 x float> %a4, %a5
+  ret <4 x float> %a6
+}
+
+define <4 x float> @_Z16rsMatrixMultiplyP12rs_matrix4x4Dv3_f(%struct.rs_matrix4x4* nocapture %m, <3 x float> %in) nounwind readonly {
+  %r = tail call <4 x float> @_Z16rsMatrixMultiplyPK12rs_matrix4x4Dv3_f(%struct.rs_matrix4x4* nocapture %m, <3 x float> %in) nounwind
+  ret <4 x float> %r
+}
+
+define <4 x float> @_Z16rsMatrixMultiplyPK12rs_matrix4x4Dv2_f(%struct.rs_matrix4x4* nocapture %m, <2 x float> %in) nounwind readonly {
+  %x0 = extractelement <2 x float> %in, i32 0
+  %x = tail call <4 x float> @smear_f(float %x0) nounwind readnone
+  %y0 = extractelement <2 x float> %in, i32 1
+  %y = tail call <4 x float> @smear_f(float %y0) nounwind readnone
+
+  %px = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 0
+  %px2 = bitcast float* %px to <4 x float>*
+  %xm = load <4 x float>* %px2, align 4
+  %py = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 4
+  %py2 = bitcast float* %py to <4 x float>*
+  %ym = load <4 x float>* %py2, align 4
+  %pw = getelementptr inbounds %struct.rs_matrix4x4* %m, i32 0, i32 0, i32 12
+  %pw2 = bitcast float* %pw to <4 x float>*
+  %wm = load <4 x float>* %pw2, align 4
+
+  %a1 = fmul <4 x float> %x, %xm
+  %a2 = fadd <4 x float> %wm, %a1
+  %a3 = fmul <4 x float> %y, %ym
+  %a4 = fadd <4 x float> %a2, %a3
+  ret <4 x float> %a4
+}
+
+define <4 x float> @_Z16rsMatrixMultiplyP12rs_matrix4x4Dv2_f(%struct.rs_matrix4x4* nocapture %m, <2 x float> %in) nounwind readonly {
+  %r = tail call <4 x float> @_Z16rsMatrixMultiplyPK12rs_matrix4x4Dv2_f(%struct.rs_matrix4x4* nocapture %m, <2 x float> %in) nounwind
+  ret <4 x float> %r
+}
+
diff --git a/driver/runtime/rsClamp.ll b/driver/runtime/rsClamp.ll
new file mode 100644
index 0000000..eba678a
--- /dev/null
+++ b/driver/runtime/rsClamp.ll
@@ -0,0 +1,60 @@
+target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:64:128-a0:0:64-n32-S64"
+target triple = "armv7-none-linux-gnueabi"
+
+
+define float @_Z7rsClampfff(float %value, float %low, float %high) nounwind readonly {
+  %1 = fcmp olt float %value, %high
+  %2 = select i1 %1, float %value, float %high
+  %3 = fcmp ogt float %2, %low
+  %4 = select i1 %3, float %2, float %low
+  ret float %4
+}
+
+define signext i8 @_Z7rsClampccc(i8 signext %value, i8 signext %low, i8 signext %high) nounwind readonly {
+  %1 = icmp slt i8 %value, %high
+  %2 = select i1 %1, i8 %value, i8 %high
+  %3 = icmp sgt i8 %2, %low
+  %4 = select i1 %3, i8 %2, i8 %low
+  ret i8 %4
+}
+
+define zeroext i8 @_Z7rsClamphhh(i8 zeroext %value, i8 zeroext %low, i8 zeroext %high) nounwind readonly {
+  %1 = icmp ult i8 %value, %high
+  %2 = select i1 %1, i8 %value, i8 %high
+  %3 = icmp ugt i8 %2, %low
+  %4 = select i1 %3, i8 %2, i8 %low
+  ret i8 %4
+}
+
+define signext i16 @_Z7rsClampsss(i16 signext %value, i16 signext %low, i16 signext %high) nounwind readonly {
+  %1 = icmp slt i16 %value, %high
+  %2 = select i1 %1, i16 %value, i16 %high
+  %3 = icmp sgt i16 %2, %low
+  %4 = select i1 %3, i16 %2, i16 %low
+  ret i16 %4
+}
+
+define zeroext i16 @_Z7rsClampttt(i16 zeroext %value, i16 zeroext %low, i16 zeroext %high) nounwind readonly {
+  %1 = icmp ult i16 %value, %high
+  %2 = select i1 %1, i16 %value, i16 %high
+  %3 = icmp ugt i16 %2, %low
+  %4 = select i1 %3, i16 %2, i16 %low
+  ret i16 %4
+}
+
+define i32 @_Z7rsClampiii(i32 %value, i32 %low, i32 %high) nounwind readonly {
+  %1 = icmp slt i32 %value, %high
+  %2 = select i1 %1, i32 %value, i32 %high
+  %3 = icmp sgt i32 %2, %low
+  %4 = select i1 %3, i32 %2, i32 %low
+  ret i32 %4
+}
+
+define i32 @_Z7rsClampjjj(i32 %value, i32 %low, i32 %high) nounwind readonly {
+  %1 = icmp ult i32 %value, %high
+  %2 = select i1 %1, i32 %value, i32 %high
+  %3 = icmp ugt i32 %2, %low
+  %4 = select i1 %3, i32 %2, i32 %low
+  ret i32 %4
+}
+
diff --git a/driver/runtime/rs_allocation.c b/driver/runtime/rs_allocation.c
new file mode 100644
index 0000000..198e9af
--- /dev/null
+++ b/driver/runtime/rs_allocation.c
@@ -0,0 +1,318 @@
+#include "rs_core.rsh"
+#include "rs_graphics.rsh"
+#include "rs_structs.h"
+
+// Opaque Allocation type operations
+extern uint32_t __attribute__((overloadable))
+    rsAllocationGetDimX(rs_allocation a) {
+    Allocation_t *alloc = (Allocation_t *)a.p;
+    return alloc->mHal.drvState.lod[0].dimX;
+}
+
+extern uint32_t __attribute__((overloadable))
+        rsAllocationGetDimY(rs_allocation a) {
+    Allocation_t *alloc = (Allocation_t *)a.p;
+    return alloc->mHal.drvState.lod[0].dimY;
+}
+
+extern uint32_t __attribute__((overloadable))
+        rsAllocationGetDimZ(rs_allocation a) {
+    Allocation_t *alloc = (Allocation_t *)a.p;
+    return alloc->mHal.drvState.lod[0].dimZ;
+}
+
+extern uint32_t __attribute__((overloadable))
+        rsAllocationGetDimLOD(rs_allocation a) {
+    Allocation_t *alloc = (Allocation_t *)a.p;
+    return alloc->mHal.state.hasMipmaps;
+}
+
+extern uint32_t __attribute__((overloadable))
+        rsAllocationGetDimFaces(rs_allocation a) {
+    Allocation_t *alloc = (Allocation_t *)a.p;
+    return alloc->mHal.state.hasFaces;
+}
+
+
+extern rs_element __attribute__((overloadable))
+        rsAllocationGetElement(rs_allocation a) {
+    Allocation_t *alloc = (Allocation_t *)a.p;
+    if (alloc == NULL) {
+        rs_element nullElem = {0};
+        return nullElem;
+    }
+    Type_t *type = (Type_t *)alloc->mHal.state.type;
+    rs_element returnElem = {type->mHal.state.element};
+    return returnElem;
+}
+
+// TODO: this needs to be optimized, obviously
+static void memcpy(void* dst, void* src, size_t size) {
+    char* dst_c = (char*) dst, *src_c = (char*) src;
+    for (; size > 0; size--) {
+        *dst_c++ = *src_c++;
+    }
+}
+
+#ifdef RS_DEBUG_RUNTIME
+#define ELEMENT_AT(T)                                                   \
+    extern void __attribute__((overloadable))                           \
+        rsSetElementAt_##T(rs_allocation a, const T *val, uint32_t x);  \
+    extern void __attribute__((overloadable))                           \
+        rsSetElementAt_##T(rs_allocation a, const T *val, uint32_t x, uint32_t y); \
+    extern void __attribute__((overloadable))                           \
+        rsSetElementAt_##T(rs_allocation a, const T *val, uint32_t x, uint32_t y, uint32_t z); \
+    extern void __attribute__((overloadable))                           \
+        rsGetElementAt_##T(rs_allocation a, T *val, uint32_t x);  \
+    extern void __attribute__((overloadable))                           \
+        rsGetElementAt_##T(rs_allocation a, T *val, uint32_t x, uint32_t y); \
+    extern void __attribute__((overloadable))                           \
+        rsGetElementAt_##T(rs_allocation a, T *val, uint32_t x, uint32_t y, uint32_t z); \
+                                                                        \
+    extern void __attribute__((overloadable))                           \
+    rsSetElementAt_##T(rs_allocation a, T val, uint32_t x) {            \
+        rsSetElementAt_##T(a, &val, x);                                 \
+    }                                                                   \
+    extern void __attribute__((overloadable))                           \
+    rsSetElementAt_##T(rs_allocation a, T val, uint32_t x, uint32_t y) { \
+        rsSetElementAt_##T(a, &val, x, y);                              \
+    }                                                                   \
+    extern void __attribute__((overloadable))                           \
+    rsSetElementAt_##T(rs_allocation a, T val, uint32_t x, uint32_t y, uint32_t z) { \
+        rsSetElementAt_##T(a, &val, x, y, z);                           \
+    }                                                                   \
+    extern T __attribute__((overloadable))                              \
+    rsGetElementAt_##T(rs_allocation a, uint32_t x) {                   \
+        T tmp;                                                          \
+        rsGetElementAt_##T(a, &tmp, x);                                 \
+        return tmp;                                                     \
+    }                                                                   \
+    extern T __attribute__((overloadable))                              \
+    rsGetElementAt_##T(rs_allocation a, uint32_t x, uint32_t y) {       \
+        T tmp;                                                          \
+        rsGetElementAt_##T(a, &tmp, x, y);                              \
+        return tmp;                                                     \
+    }                                                                   \
+    extern T __attribute__((overloadable))                              \
+    rsGetElementAt_##T(rs_allocation a, uint32_t x, uint32_t y, uint32_t z) { \
+        T tmp;                                                          \
+        rsGetElementAt_##T(a, &tmp, x, y, z);                           \
+        return tmp;                                                     \
+    }
+
+#else
+
+uint8_t*
+rsOffset(rs_allocation a, uint32_t sizeOf, uint32_t x, uint32_t y,
+         uint32_t z) {
+    Allocation_t *alloc = (Allocation_t *)a.p;
+    uint8_t *p = (uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
+    const uint32_t stride = alloc->mHal.drvState.lod[0].stride;
+    const uint32_t dimY = alloc->mHal.drvState.lod[0].dimY;
+    uint8_t *dp = &p[(sizeOf * x) + (y * stride) +
+                     (z * stride * dimY)];
+    return dp;
+}
+
+#define ELEMENT_AT(T)                                                   \
+                                                                        \
+    void                                                                \
+    rsSetElementAtImpl_##T(rs_allocation a, T val, uint32_t x,          \
+                           uint32_t y, uint32_t z);                     \
+                                                                        \
+    extern void __attribute__((overloadable))                           \
+    rsSetElementAt_##T(rs_allocation a, T val, uint32_t x) {            \
+        rsSetElementAtImpl_##T(a, val, x, 0, 0);                        \
+    }                                                                   \
+                                                                        \
+    extern void __attribute__((overloadable))                           \
+    rsSetElementAt_##T(rs_allocation a, T val, uint32_t x,              \
+                       uint32_t y) {                                    \
+        rsSetElementAtImpl_##T(a, val, x, y, 0);                        \
+    }                                                                   \
+                                                                        \
+    extern void __attribute__((overloadable))                           \
+    rsSetElementAt_##T(rs_allocation a, T val, uint32_t x, uint32_t y,  \
+                       uint32_t z) {                                    \
+        rsSetElementAtImpl_##T(a, val, x, y, z);                        \
+    }                                                                   \
+                                                                        \
+    T                                                                   \
+    rsGetElementAtImpl_##T(rs_allocation a, uint32_t x, uint32_t y,     \
+                       uint32_t z);                                     \
+                                                                        \
+    extern T __attribute__((overloadable))                              \
+    rsGetElementAt_##T(rs_allocation a, uint32_t x) {                   \
+        return rsGetElementAtImpl_##T(a, x, 0, 0);                      \
+    }                                                                   \
+                                                                        \
+    extern T __attribute__((overloadable))                              \
+    rsGetElementAt_##T(rs_allocation a, uint32_t x, uint32_t y) {       \
+        return rsGetElementAtImpl_##T(a, x, y, 0);                      \
+    }                                                                   \
+                                                                        \
+    extern T __attribute__((overloadable))                              \
+    rsGetElementAt_##T(rs_allocation a, uint32_t x, uint32_t y,         \
+                       uint32_t z) {                                    \
+        return rsGetElementAtImpl_##T(a, x, y, z);                      \
+    }
+
+
+
+extern const void * __attribute__((overloadable))
+        rsGetElementAt(rs_allocation a, uint32_t x) {
+    Allocation_t *alloc = (Allocation_t *)a.p;
+    const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
+    const uint32_t eSize = alloc->mHal.state.elementSizeBytes;
+    return &p[eSize * x];
+}
+
+extern const void * __attribute__((overloadable))
+        rsGetElementAt(rs_allocation a, uint32_t x, uint32_t y) {
+    Allocation_t *alloc = (Allocation_t *)a.p;
+    const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
+    const uint32_t eSize = alloc->mHal.state.elementSizeBytes;
+    const uint32_t stride = alloc->mHal.drvState.lod[0].stride;
+    return &p[(eSize * x) + (y * stride)];
+}
+
+extern const void * __attribute__((overloadable))
+        rsGetElementAt(rs_allocation a, uint32_t x, uint32_t y, uint32_t z) {
+    Allocation_t *alloc = (Allocation_t *)a.p;
+    const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
+    const uint32_t eSize = alloc->mHal.state.elementSizeBytes;
+    const uint32_t stride = alloc->mHal.drvState.lod[0].stride;
+    const uint32_t dimY = alloc->mHal.drvState.lod[0].dimY;
+    return &p[(eSize * x) + (y * stride) + (z * stride * dimY)];
+}
+extern void __attribute__((overloadable))
+        rsSetElementAt(rs_allocation a, void* ptr, uint32_t x) {
+    Allocation_t *alloc = (Allocation_t *)a.p;
+    const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
+    const uint32_t eSize = alloc->mHal.state.elementSizeBytes;
+    memcpy((void*)&p[eSize * x], ptr, eSize);
+}
+
+extern void __attribute__((overloadable))
+        rsSetElementAt(rs_allocation a, void* ptr, uint32_t x, uint32_t y) {
+    Allocation_t *alloc = (Allocation_t *)a.p;
+    const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
+    const uint32_t eSize = alloc->mHal.state.elementSizeBytes;
+    const uint32_t stride = alloc->mHal.drvState.lod[0].stride;
+    memcpy((void*)&p[(eSize * x) + (y * stride)], ptr, eSize);
+}
+
+extern void __attribute__((overloadable))
+        rsSetElementAt(rs_allocation a, void* ptr, uint32_t x, uint32_t y, uint32_t z) {
+    Allocation_t *alloc = (Allocation_t *)a.p;
+    const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
+    const uint32_t eSize = alloc->mHal.state.elementSizeBytes;
+    const uint32_t stride = alloc->mHal.drvState.lod[0].stride;
+    const uint32_t dimY = alloc->mHal.drvState.lod[0].dimY;
+    memcpy((void*)&p[(eSize * x) + (y * stride) + (z * stride * dimY)], ptr, eSize);
+}
+#endif
+
+ELEMENT_AT(char)
+ELEMENT_AT(char2)
+ELEMENT_AT(char3)
+ELEMENT_AT(char4)
+ELEMENT_AT(uchar)
+ELEMENT_AT(uchar2)
+ELEMENT_AT(uchar3)
+ELEMENT_AT(uchar4)
+ELEMENT_AT(short)
+ELEMENT_AT(short2)
+ELEMENT_AT(short3)
+ELEMENT_AT(short4)
+ELEMENT_AT(ushort)
+ELEMENT_AT(ushort2)
+ELEMENT_AT(ushort3)
+ELEMENT_AT(ushort4)
+ELEMENT_AT(int)
+ELEMENT_AT(int2)
+ELEMENT_AT(int3)
+ELEMENT_AT(int4)
+ELEMENT_AT(uint)
+ELEMENT_AT(uint2)
+ELEMENT_AT(uint3)
+ELEMENT_AT(uint4)
+ELEMENT_AT(long)
+ELEMENT_AT(long2)
+ELEMENT_AT(long3)
+ELEMENT_AT(long4)
+ELEMENT_AT(ulong)
+ELEMENT_AT(ulong2)
+ELEMENT_AT(ulong3)
+ELEMENT_AT(ulong4)
+ELEMENT_AT(float)
+ELEMENT_AT(float2)
+ELEMENT_AT(float3)
+ELEMENT_AT(float4)
+ELEMENT_AT(double)
+ELEMENT_AT(double2)
+ELEMENT_AT(double3)
+ELEMENT_AT(double4)
+
+#undef ELEMENT_AT
+
+
+extern const uchar __attribute__((overloadable))
+        rsGetElementAtYuv_uchar_Y(rs_allocation a, uint32_t x, uint32_t y) {
+    return rsGetElementAt_uchar(a, x, y);
+}
+
+extern const uchar __attribute__((overloadable))
+        rsGetElementAtYuv_uchar_U(rs_allocation a, uint32_t x, uint32_t y) {
+
+    Allocation_t *alloc = (Allocation_t *)a.p;
+    const uint32_t yuvID = alloc->mHal.state.yuv;
+    const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[1].mallocPtr;
+    const uint32_t stride = alloc->mHal.drvState.lod[1].stride;
+
+    switch(yuvID) {
+    case 0x32315659: //HAL_PIXEL_FORMAT_YV12:
+        x >>= 1;
+        y >>= 1;
+        return p[x + (y * stride)];
+    case 11: //HAL_PIXEL_FORMAT_YCrCb_420_SP:  // NV21
+        x >>= 1;
+        y >>= 1;
+        return p[(x<<1) + (y * stride)];
+    default:
+        break;
+    }
+
+    return 0;
+}
+
+extern const uchar __attribute__((overloadable))
+        rsGetElementAtYuv_uchar_V(rs_allocation a, uint32_t x, uint32_t y) {
+
+    Allocation_t *alloc = (Allocation_t *)a.p;
+    const uint32_t yuvID = alloc->mHal.state.yuv;
+
+    switch(yuvID) {
+    case 0x32315659: //HAL_PIXEL_FORMAT_YV12:
+        {
+        const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[2].mallocPtr;
+        const uint32_t stride = alloc->mHal.drvState.lod[2].stride;
+        x >>= 1;
+        y >>= 1;
+        return p[x + (y * stride)];
+        }
+    case 11: //HAL_PIXEL_FORMAT_YCrCb_420_SP:  // NV21
+        {
+        const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[1].mallocPtr;
+        const uint32_t stride = alloc->mHal.drvState.lod[1].stride;
+        x >>= 1;
+        y >>= 1;
+        return p[(x<<1) + (y * stride) + 1];
+        }
+    default:
+            break;
+    }
+
+    return 0;
+}
+
diff --git a/driver/runtime/rs_cl.c b/driver/runtime/rs_cl.c
new file mode 100755
index 0000000..b7f9158
--- /dev/null
+++ b/driver/runtime/rs_cl.c
@@ -0,0 +1,1194 @@
+#include "rs_types.rsh"
+
+extern float2 __attribute__((overloadable)) convert_float2(int2 c);
+extern float3 __attribute__((overloadable)) convert_float3(int3 c);
+extern float4 __attribute__((overloadable)) convert_float4(int4 c);
+
+extern int2 __attribute__((overloadable)) convert_int2(float2 c);
+extern int3 __attribute__((overloadable)) convert_int3(float3 c);
+extern int4 __attribute__((overloadable)) convert_int4(float4 c);
+
+
+extern float __attribute__((overloadable)) fmin(float v, float v2);
+extern float2 __attribute__((overloadable)) fmin(float2 v, float v2);
+extern float3 __attribute__((overloadable)) fmin(float3 v, float v2);
+extern float4 __attribute__((overloadable)) fmin(float4 v, float v2);
+
+extern float __attribute__((overloadable)) fmax(float v, float v2);
+extern float2 __attribute__((overloadable)) fmax(float2 v, float v2);
+extern float3 __attribute__((overloadable)) fmax(float3 v, float v2);
+extern float4 __attribute__((overloadable)) fmax(float4 v, float v2);
+
+// Float ops, 6.11.2
+
+#define FN_FUNC_FN(fnc)                                         \
+extern float2 __attribute__((overloadable)) fnc(float2 v) { \
+    float2 r;                                                   \
+    r.x = fnc(v.x);                                             \
+    r.y = fnc(v.y);                                             \
+    return r;                                                   \
+}                                                               \
+extern float3 __attribute__((overloadable)) fnc(float3 v) { \
+    float3 r;                                                   \
+    r.x = fnc(v.x);                                             \
+    r.y = fnc(v.y);                                             \
+    r.z = fnc(v.z);                                             \
+    return r;                                                   \
+}                                                               \
+extern float4 __attribute__((overloadable)) fnc(float4 v) { \
+    float4 r;                                                   \
+    r.x = fnc(v.x);                                             \
+    r.y = fnc(v.y);                                             \
+    r.z = fnc(v.z);                                             \
+    r.w = fnc(v.w);                                             \
+    return r;                                                   \
+}
+
+#define IN_FUNC_FN(fnc)                                         \
+extern int2 __attribute__((overloadable)) fnc(float2 v) {   \
+    int2 r;                                                     \
+    r.x = fnc(v.x);                                             \
+    r.y = fnc(v.y);                                             \
+    return r;                                                   \
+}                                                               \
+extern int3 __attribute__((overloadable)) fnc(float3 v) {   \
+    int3 r;                                                     \
+    r.x = fnc(v.x);                                             \
+    r.y = fnc(v.y);                                             \
+    r.z = fnc(v.z);                                             \
+    return r;                                                   \
+}                                                               \
+extern int4 __attribute__((overloadable)) fnc(float4 v) {   \
+    int4 r;                                                     \
+    r.x = fnc(v.x);                                             \
+    r.y = fnc(v.y);                                             \
+    r.z = fnc(v.z);                                             \
+    r.w = fnc(v.w);                                             \
+    return r;                                                   \
+}
+
+#define FN_FUNC_FN_FN(fnc)                                                  \
+extern float2 __attribute__((overloadable)) fnc(float2 v1, float2 v2) { \
+    float2 r;                                                               \
+    r.x = fnc(v1.x, v2.x);                                                  \
+    r.y = fnc(v1.y, v2.y);                                                  \
+    return r;                                                               \
+}                                                                           \
+extern float3 __attribute__((overloadable)) fnc(float3 v1, float3 v2) { \
+    float3 r;                                                               \
+    r.x = fnc(v1.x, v2.x);                                                  \
+    r.y = fnc(v1.y, v2.y);                                                  \
+    r.z = fnc(v1.z, v2.z);                                                  \
+    return r;                                                               \
+}                                                                           \
+extern float4 __attribute__((overloadable)) fnc(float4 v1, float4 v2) { \
+    float4 r;                                                               \
+    r.x = fnc(v1.x, v2.x);                                                  \
+    r.y = fnc(v1.y, v2.y);                                                  \
+    r.z = fnc(v1.z, v2.z);                                                  \
+    r.w = fnc(v1.w, v2.w);                                                  \
+    return r;                                                               \
+}
+
+#define FN_FUNC_FN_F(fnc)                                                   \
+extern float2 __attribute__((overloadable)) fnc(float2 v1, float v2) {  \
+    float2 r;                                                               \
+    r.x = fnc(v1.x, v2);                                                    \
+    r.y = fnc(v1.y, v2);                                                    \
+    return r;                                                               \
+}                                                                           \
+extern float3 __attribute__((overloadable)) fnc(float3 v1, float v2) {  \
+    float3 r;                                                               \
+    r.x = fnc(v1.x, v2);                                                    \
+    r.y = fnc(v1.y, v2);                                                    \
+    r.z = fnc(v1.z, v2);                                                    \
+    return r;                                                               \
+}                                                                           \
+extern float4 __attribute__((overloadable)) fnc(float4 v1, float v2) {  \
+    float4 r;                                                               \
+    r.x = fnc(v1.x, v2);                                                    \
+    r.y = fnc(v1.y, v2);                                                    \
+    r.z = fnc(v1.z, v2);                                                    \
+    r.w = fnc(v1.w, v2);                                                    \
+    return r;                                                               \
+}
+
+#define FN_FUNC_FN_IN(fnc)                                                  \
+extern float2 __attribute__((overloadable)) fnc(float2 v1, int2 v2) {   \
+    float2 r;                                                               \
+    r.x = fnc(v1.x, v2.x);                                                  \
+    r.y = fnc(v1.y, v2.y);                                                  \
+    return r;                                                               \
+}                                                                           \
+extern float3 __attribute__((overloadable)) fnc(float3 v1, int3 v2) {   \
+    float3 r;                                                               \
+    r.x = fnc(v1.x, v2.x);                                                  \
+    r.y = fnc(v1.y, v2.y);                                                  \
+    r.z = fnc(v1.z, v2.z);                                                  \
+    return r;                                                               \
+}                                                                           \
+extern float4 __attribute__((overloadable)) fnc(float4 v1, int4 v2) {   \
+    float4 r;                                                               \
+    r.x = fnc(v1.x, v2.x);                                                  \
+    r.y = fnc(v1.y, v2.y);                                                  \
+    r.z = fnc(v1.z, v2.z);                                                  \
+    r.w = fnc(v1.w, v2.w);                                                  \
+    return r;                                                               \
+}
+
+#define FN_FUNC_FN_I(fnc)                                                   \
+extern float2 __attribute__((overloadable)) fnc(float2 v1, int v2) {    \
+    float2 r;                                                               \
+    r.x = fnc(v1.x, v2);                                                    \
+    r.y = fnc(v1.y, v2);                                                    \
+    return r;                                                               \
+}                                                                           \
+extern float3 __attribute__((overloadable)) fnc(float3 v1, int v2) {    \
+    float3 r;                                                               \
+    r.x = fnc(v1.x, v2);                                                    \
+    r.y = fnc(v1.y, v2);                                                    \
+    r.z = fnc(v1.z, v2);                                                    \
+    return r;                                                               \
+}                                                                           \
+extern float4 __attribute__((overloadable)) fnc(float4 v1, int v2) {    \
+    float4 r;                                                               \
+    r.x = fnc(v1.x, v2);                                                    \
+    r.y = fnc(v1.y, v2);                                                    \
+    r.z = fnc(v1.z, v2);                                                    \
+    r.w = fnc(v1.w, v2);                                                    \
+    return r;                                                               \
+}
+
+#define FN_FUNC_FN_PFN(fnc)                     \
+extern float2 __attribute__((overloadable)) \
+        fnc(float2 v1, float2 *v2) {            \
+    float2 r;                                   \
+    float t[2];                                 \
+    r.x = fnc(v1.x, &t[0]);                     \
+    r.y = fnc(v1.y, &t[1]);                     \
+    v2->x = t[0];                               \
+    v2->y = t[1];                               \
+    return r;                                   \
+}                                               \
+extern float3 __attribute__((overloadable)) \
+        fnc(float3 v1, float3 *v2) {            \
+    float3 r;                                   \
+    float t[3];                                 \
+    r.x = fnc(v1.x, &t[0]);                     \
+    r.y = fnc(v1.y, &t[1]);                     \
+    r.z = fnc(v1.z, &t[2]);                     \
+    v2->x = t[0];                               \
+    v2->y = t[1];                               \
+    v2->z = t[2];                               \
+    return r;                                   \
+}                                               \
+extern float4 __attribute__((overloadable)) \
+        fnc(float4 v1, float4 *v2) {            \
+    float4 r;                                   \
+    float t[4];                                 \
+    r.x = fnc(v1.x, &t[0]);                     \
+    r.y = fnc(v1.y, &t[1]);                     \
+    r.z = fnc(v1.z, &t[2]);                     \
+    r.w = fnc(v1.w, &t[3]);                     \
+    v2->x = t[0];                               \
+    v2->y = t[1];                               \
+    v2->z = t[2];                               \
+    v2->w = t[3];                               \
+    return r;                                   \
+}
+
+#define FN_FUNC_FN_PIN(fnc)                                                 \
+extern float2 __attribute__((overloadable)) fnc(float2 v1, int2 *v2) {  \
+    float2 r;                                                               \
+    int t[2];                                                               \
+    r.x = fnc(v1.x, &t[0]);                                                 \
+    r.y = fnc(v1.y, &t[1]);                                                 \
+    v2->x = t[0];                                                           \
+    v2->y = t[1];                                                           \
+    return r;                                                               \
+}                                                                           \
+extern float3 __attribute__((overloadable)) fnc(float3 v1, int3 *v2) {  \
+    float3 r;                                                               \
+    int t[3];                                                               \
+    r.x = fnc(v1.x, &t[0]);                                                 \
+    r.y = fnc(v1.y, &t[1]);                                                 \
+    r.z = fnc(v1.z, &t[2]);                                                 \
+    v2->x = t[0];                                                           \
+    v2->y = t[1];                                                           \
+    v2->z = t[2];                                                           \
+    return r;                                                               \
+}                                                                           \
+extern float4 __attribute__((overloadable)) fnc(float4 v1, int4 *v2) {  \
+    float4 r;                                                               \
+    int t[4];                                                               \
+    r.x = fnc(v1.x, &t[0]);                                                 \
+    r.y = fnc(v1.y, &t[1]);                                                 \
+    r.z = fnc(v1.z, &t[2]);                                                 \
+    r.w = fnc(v1.w, &t[3]);                                                 \
+    v2->x = t[0];                                                           \
+    v2->y = t[1];                                                           \
+    v2->z = t[2];                                                           \
+    v2->w = t[3];                                                           \
+    return r;                                                               \
+}
+
+#define FN_FUNC_FN_FN_FN(fnc)                   \
+extern float2 __attribute__((overloadable)) \
+        fnc(float2 v1, float2 v2, float2 v3) {  \
+    float2 r;                                   \
+    r.x = fnc(v1.x, v2.x, v3.x);                \
+    r.y = fnc(v1.y, v2.y, v3.y);                \
+    return r;                                   \
+}                                               \
+extern float3 __attribute__((overloadable)) \
+        fnc(float3 v1, float3 v2, float3 v3) {  \
+    float3 r;                                   \
+    r.x = fnc(v1.x, v2.x, v3.x);                \
+    r.y = fnc(v1.y, v2.y, v3.y);                \
+    r.z = fnc(v1.z, v2.z, v3.z);                \
+    return r;                                   \
+}                                               \
+extern float4 __attribute__((overloadable)) \
+        fnc(float4 v1, float4 v2, float4 v3) {  \
+    float4 r;                                   \
+    r.x = fnc(v1.x, v2.x, v3.x);                \
+    r.y = fnc(v1.y, v2.y, v3.y);                \
+    r.z = fnc(v1.z, v2.z, v3.z);                \
+    r.w = fnc(v1.w, v2.w, v3.w);                \
+    return r;                                   \
+}
+
+#define FN_FUNC_FN_FN_PIN(fnc)                  \
+extern float2 __attribute__((overloadable)) \
+        fnc(float2 v1, float2 v2, int2 *v3) {   \
+    float2 r;                                   \
+    int t[2];                                   \
+    r.x = fnc(v1.x, v2.x, &t[0]);               \
+    r.y = fnc(v1.y, v2.y, &t[1]);               \
+    v3->x = t[0];                               \
+    v3->y = t[1];                               \
+    return r;                                   \
+}                                               \
+extern float3 __attribute__((overloadable)) \
+        fnc(float3 v1, float3 v2, int3 *v3) {   \
+    float3 r;                                   \
+    int t[3];                                   \
+    r.x = fnc(v1.x, v2.x, &t[0]);               \
+    r.y = fnc(v1.y, v2.y, &t[1]);               \
+    r.z = fnc(v1.z, v2.z, &t[2]);               \
+    v3->x = t[0];                               \
+    v3->y = t[1];                               \
+    v3->z = t[2];                               \
+    return r;                                   \
+}                                               \
+extern float4 __attribute__((overloadable)) \
+        fnc(float4 v1, float4 v2, int4 *v3) {   \
+    float4 r;                                   \
+    int t[4];                                   \
+    r.x = fnc(v1.x, v2.x, &t[0]);               \
+    r.y = fnc(v1.y, v2.y, &t[1]);               \
+    r.z = fnc(v1.z, v2.z, &t[2]);               \
+    r.w = fnc(v1.w, v2.w, &t[3]);               \
+    v3->x = t[0];                               \
+    v3->y = t[1];                               \
+    v3->z = t[2];                               \
+    v3->w = t[3];                               \
+    return r;                                   \
+}
+
+static const int iposinf = 0x7f800000;
+static const int ineginf = 0xff800000;
+
+static const float posinf() {
+    float f = *((float*)&iposinf);
+    return f;
+}
+
+static const float neginf() {
+    float f = *((float*)&ineginf);
+    return f;
+}
+
+static bool isinf(float f) {
+    int i = *((int*)(void*)&f);
+    return (i == iposinf) || (i == ineginf);
+}
+
+static bool isnan(float f) {
+    int i = *((int*)(void*)&f);
+    return (((i & 0x7f800000) == 0x7f800000) && (i & 0x007fffff));
+}
+
+static bool isposzero(float f) {
+    int i = *((int*)(void*)&f);
+    return (i == 0x00000000);
+}
+
+static bool isnegzero(float f) {
+    int i = *((int*)(void*)&f);
+    return (i == 0x80000000);
+}
+
+static bool iszero(float f) {
+    return isposzero(f) || isnegzero(f);
+}
+
+
+extern float __attribute__((overloadable)) acos(float);
+FN_FUNC_FN(acos)
+
+extern float __attribute__((overloadable)) acosh(float);
+FN_FUNC_FN(acosh)
+
+
+extern float __attribute__((overloadable)) acospi(float v) {
+    return acos(v) / M_PI;
+}
+FN_FUNC_FN(acospi)
+
+extern float __attribute__((overloadable)) asin(float);
+FN_FUNC_FN(asin)
+
+extern float __attribute__((overloadable)) asinh(float);
+FN_FUNC_FN(asinh)
+
+extern float __attribute__((overloadable)) asinpi(float v) {
+    return asin(v) / M_PI;
+}
+FN_FUNC_FN(asinpi)
+
+extern float __attribute__((overloadable)) atan(float);
+FN_FUNC_FN(atan)
+
+extern float __attribute__((overloadable)) atan2(float, float);
+FN_FUNC_FN_FN(atan2)
+
+extern float __attribute__((overloadable)) atanh(float);
+FN_FUNC_FN(atanh)
+
+extern float __attribute__((overloadable)) atanpi(float v) {
+    return atan(v) / M_PI;
+}
+FN_FUNC_FN(atanpi)
+
+
+extern float __attribute__((overloadable)) atan2pi(float y, float x) {
+    return atan2(y, x) / M_PI;
+}
+FN_FUNC_FN_FN(atan2pi)
+
+extern float __attribute__((overloadable)) cbrt(float);
+FN_FUNC_FN(cbrt)
+
+extern float __attribute__((overloadable)) ceil(float);
+FN_FUNC_FN(ceil)
+
+extern float __attribute__((overloadable)) copysign(float, float);
+FN_FUNC_FN_FN(copysign)
+
+extern float __attribute__((overloadable)) cos(float);
+FN_FUNC_FN(cos)
+
+extern float __attribute__((overloadable)) cosh(float);
+FN_FUNC_FN(cosh)
+
+extern float __attribute__((overloadable)) cospi(float v) {
+    return cos(v * M_PI);
+}
+FN_FUNC_FN(cospi)
+
+extern float __attribute__((overloadable)) erfc(float);
+FN_FUNC_FN(erfc)
+
+extern float __attribute__((overloadable)) erf(float);
+FN_FUNC_FN(erf)
+
+extern float __attribute__((overloadable)) exp(float);
+FN_FUNC_FN(exp)
+
+extern float __attribute__((overloadable)) exp2(float);
+FN_FUNC_FN(exp2)
+
+extern float __attribute__((overloadable)) pow(float, float);
+
+extern float __attribute__((overloadable)) exp10(float v) {
+    return exp2(v * 3.321928095f);
+}
+FN_FUNC_FN(exp10)
+
+extern float __attribute__((overloadable)) expm1(float);
+FN_FUNC_FN(expm1)
+
+extern float __attribute__((overloadable)) fabs(float v) {
+    int i = *((int*)(void*)&v) & 0x7fffffff;
+    return  *((float*)(void*)&i);
+}
+FN_FUNC_FN(fabs)
+
+extern float __attribute__((overloadable)) fdim(float, float);
+FN_FUNC_FN_FN(fdim)
+
+extern float __attribute__((overloadable)) floor(float);
+FN_FUNC_FN(floor)
+
+extern float __attribute__((overloadable)) fma(float, float, float);
+FN_FUNC_FN_FN_FN(fma)
+
+extern float __attribute__((overloadable)) fmin(float, float);
+
+extern float __attribute__((overloadable)) fmod(float, float);
+FN_FUNC_FN_FN(fmod)
+
+extern float __attribute__((overloadable)) fract(float v, float *iptr) {
+    int i = (int)floor(v);
+    if (iptr) {
+        iptr[0] = i;
+    }
+    return fmin(v - i, 0x1.fffffep-1f);
+}
+FN_FUNC_FN_PFN(fract)
+
+extern float __attribute__((overloadable)) frexp(float, int *);
+FN_FUNC_FN_PIN(frexp)
+
+extern float __attribute__((overloadable)) hypot(float, float);
+FN_FUNC_FN_FN(hypot)
+
+extern int __attribute__((overloadable)) ilogb(float);
+IN_FUNC_FN(ilogb)
+
+extern float __attribute__((overloadable)) ldexp(float, int);
+FN_FUNC_FN_IN(ldexp)
+FN_FUNC_FN_I(ldexp)
+
+extern float __attribute__((overloadable)) lgamma(float);
+FN_FUNC_FN(lgamma)
+extern float __attribute__((overloadable)) lgamma(float, int*);
+FN_FUNC_FN_PIN(lgamma)
+
+extern float __attribute__((overloadable)) log(float);
+FN_FUNC_FN(log)
+
+extern float __attribute__((overloadable)) log10(float);
+FN_FUNC_FN(log10)
+
+
+extern float __attribute__((overloadable)) log2(float v) {
+    return log10(v) * 3.321928095f;
+}
+FN_FUNC_FN(log2)
+
+extern float __attribute__((overloadable)) log1p(float);
+FN_FUNC_FN(log1p)
+
+extern float __attribute__((overloadable)) logb(float);
+FN_FUNC_FN(logb)
+
+extern float __attribute__((overloadable)) mad(float a, float b, float c) {
+    return a * b + c;
+}
+extern float2 __attribute__((overloadable)) mad(float2 a, float2 b, float2 c) {
+    return a * b + c;
+}
+extern float3 __attribute__((overloadable)) mad(float3 a, float3 b, float3 c) {
+    return a * b + c;
+}
+extern float4 __attribute__((overloadable)) mad(float4 a, float4 b, float4 c) {
+    return a * b + c;
+}
+
+extern float __attribute__((overloadable)) modf(float, float *);
+FN_FUNC_FN_PFN(modf);
+
+extern float __attribute__((overloadable)) nan(uint v) {
+    float f[1];
+    uint32_t *ip = (uint32_t *)f;
+    *ip = v | 0x7fc00000;
+    return f[0];
+}
+
+extern float __attribute__((overloadable)) nextafter(float, float);
+FN_FUNC_FN_FN(nextafter)
+
+FN_FUNC_FN_FN(pow)
+
+extern float __attribute__((overloadable)) pown(float v, int p) {
+    return pow(v, (float)p);
+}
+extern float2 __attribute__((overloadable)) pown(float2 v, int2 p) {
+    float2 f2 = convert_float2(p);
+    return pow(v, f2);
+}
+extern float3 __attribute__((overloadable)) pown(float3 v, int3 p) {
+    float3 f3 = convert_float3(p);
+    return pow(v, f3);
+}
+extern float4 __attribute__((overloadable)) pown(float4 v, int4 p) {
+    float4 f4 = convert_float4(p);
+    return pow(v, f4);
+}
+
+extern float __attribute__((overloadable)) powr(float v, float p) {
+    return pow(v, p);
+}
+extern float2 __attribute__((overloadable)) powr(float2 v, float2 p) {
+    return pow(v, p);
+}
+extern float3 __attribute__((overloadable)) powr(float3 v, float3 p) {
+    return pow(v, p);
+}
+extern float4 __attribute__((overloadable)) powr(float4 v, float4 p) {
+    return pow(v, p);
+}
+
+extern float __attribute__((overloadable)) remainder(float, float);
+FN_FUNC_FN_FN(remainder)
+
+extern float __attribute__((overloadable)) remquo(float, float, int *);
+FN_FUNC_FN_FN_PIN(remquo)
+
+extern float __attribute__((overloadable)) rint(float);
+FN_FUNC_FN(rint)
+
+extern float __attribute__((overloadable)) rootn(float v, int r) {
+    if (r == 0) {
+        return nan(0);
+    }
+
+    if (iszero(v)) {
+        if (r < 0) {
+            if (r & 1) {
+                return copysign(posinf(), v);
+            } else {
+                return posinf();
+            }
+        } else {
+            if (r & 1) {
+                return copysign(0.f, v);
+            } else {
+                return 0.f;
+            }
+        }
+    }
+
+    if (!isinf(v) && !isnan(v) && (v < 0.f)) {
+        if (r & 1) {
+            return (-1.f * pow(-1.f * v, 1.f / r));
+        } else {
+            return nan(0);
+        }
+    }
+
+    return pow(v, 1.f / r);
+}
+FN_FUNC_FN_IN(rootn);
+
+extern float __attribute__((overloadable)) round(float);
+FN_FUNC_FN(round)
+
+
+extern float __attribute__((overloadable)) sqrt(float);
+extern float __attribute__((overloadable)) rsqrt(float v) {
+    return 1.f / sqrt(v);
+}
+FN_FUNC_FN(rsqrt)
+
+extern float __attribute__((overloadable)) sin(float);
+FN_FUNC_FN(sin)
+
+extern float __attribute__((overloadable)) sincos(float v, float *cosptr) {
+    *cosptr = cos(v);
+    return sin(v);
+}
+extern float2 __attribute__((overloadable)) sincos(float2 v, float2 *cosptr) {
+    *cosptr = cos(v);
+    return sin(v);
+}
+extern float3 __attribute__((overloadable)) sincos(float3 v, float3 *cosptr) {
+    *cosptr = cos(v);
+    return sin(v);
+}
+extern float4 __attribute__((overloadable)) sincos(float4 v, float4 *cosptr) {
+    *cosptr = cos(v);
+    return sin(v);
+}
+
+extern float __attribute__((overloadable)) sinh(float);
+FN_FUNC_FN(sinh)
+
+extern float __attribute__((overloadable)) sinpi(float v) {
+    return sin(v * M_PI);
+}
+FN_FUNC_FN(sinpi)
+
+extern float __attribute__((overloadable)) tan(float);
+FN_FUNC_FN(tan)
+
+extern float __attribute__((overloadable)) tanh(float);
+FN_FUNC_FN(tanh)
+
+extern float __attribute__((overloadable)) tanpi(float v) {
+    return tan(v * M_PI);
+}
+FN_FUNC_FN(tanpi)
+
+
+extern float __attribute__((overloadable)) tgamma(float);
+FN_FUNC_FN(tgamma)
+
+extern float __attribute__((overloadable)) trunc(float);
+FN_FUNC_FN(trunc)
+
+// Int ops (partial), 6.11.3
+
+#define XN_FUNC_YN(typeout, fnc, typein)                                \
+extern typeout __attribute__((overloadable)) fnc(typein);               \
+extern typeout##2 __attribute__((overloadable)) fnc(typein##2 v) {  \
+    typeout##2 r;                                                       \
+    r.x = fnc(v.x);                                                     \
+    r.y = fnc(v.y);                                                     \
+    return r;                                                           \
+}                                                                       \
+extern typeout##3 __attribute__((overloadable)) fnc(typein##3 v) {  \
+    typeout##3 r;                                                       \
+    r.x = fnc(v.x);                                                     \
+    r.y = fnc(v.y);                                                     \
+    r.z = fnc(v.z);                                                     \
+    return r;                                                           \
+}                                                                       \
+extern typeout##4 __attribute__((overloadable)) fnc(typein##4 v) {  \
+    typeout##4 r;                                                       \
+    r.x = fnc(v.x);                                                     \
+    r.y = fnc(v.y);                                                     \
+    r.z = fnc(v.z);                                                     \
+    r.w = fnc(v.w);                                                     \
+    return r;                                                           \
+}
+
+
+#define UIN_FUNC_IN(fnc)          \
+XN_FUNC_YN(uchar, fnc, char)      \
+XN_FUNC_YN(ushort, fnc, short)    \
+XN_FUNC_YN(uint, fnc, int)
+
+#define IN_FUNC_IN(fnc)           \
+XN_FUNC_YN(uchar, fnc, uchar)     \
+XN_FUNC_YN(char, fnc, char)       \
+XN_FUNC_YN(ushort, fnc, ushort)   \
+XN_FUNC_YN(short, fnc, short)     \
+XN_FUNC_YN(uint, fnc, uint)       \
+XN_FUNC_YN(int, fnc, int)
+
+
+#define XN_FUNC_XN_XN_BODY(type, fnc, body)         \
+extern type __attribute__((overloadable))       \
+        fnc(type v1, type v2) {                     \
+    return body;                                    \
+}                                                   \
+extern type##2 __attribute__((overloadable))    \
+        fnc(type##2 v1, type##2 v2) {               \
+    type##2 r;                                      \
+    r.x = fnc(v1.x, v2.x);                          \
+    r.y = fnc(v1.y, v2.y);                          \
+    return r;                                       \
+}                                                   \
+extern type##3 __attribute__((overloadable))    \
+        fnc(type##3 v1, type##3 v2) {               \
+    type##3 r;                                      \
+    r.x = fnc(v1.x, v2.x);                          \
+    r.y = fnc(v1.y, v2.y);                          \
+    r.z = fnc(v1.z, v2.z);                          \
+    return r;                                       \
+}                                                   \
+extern type##4 __attribute__((overloadable))    \
+        fnc(type##4 v1, type##4 v2) {               \
+    type##4 r;                                      \
+    r.x = fnc(v1.x, v2.x);                          \
+    r.y = fnc(v1.y, v2.y);                          \
+    r.z = fnc(v1.z, v2.z);                          \
+    r.w = fnc(v1.w, v2.w);                          \
+    return r;                                       \
+}
+
+#define IN_FUNC_IN_IN_BODY(fnc, body) \
+XN_FUNC_XN_XN_BODY(uchar, fnc, body)  \
+XN_FUNC_XN_XN_BODY(char, fnc, body)   \
+XN_FUNC_XN_XN_BODY(ushort, fnc, body) \
+XN_FUNC_XN_XN_BODY(short, fnc, body)  \
+XN_FUNC_XN_XN_BODY(uint, fnc, body)   \
+XN_FUNC_XN_XN_BODY(int, fnc, body)    \
+XN_FUNC_XN_XN_BODY(float, fnc, body)
+
+
+/**
+ * abs
+ */
+extern uint32_t __attribute__((overloadable)) abs(int32_t v) {
+    if (v < 0)
+        return -v;
+    return v;
+}
+extern uint16_t __attribute__((overloadable)) abs(int16_t v) {
+    if (v < 0)
+        return -v;
+    return v;
+}
+extern uint8_t __attribute__((overloadable)) abs(int8_t v) {
+    if (v < 0)
+        return -v;
+    return v;
+}
+
+/**
+ * clz
+ */
+extern uint32_t __attribute__((overloadable)) clz(uint32_t v) {
+    return __builtin_clz(v);
+}
+extern uint16_t __attribute__((overloadable)) clz(uint16_t v) {
+    return (uint16_t)__builtin_clz(v);
+}
+extern uint8_t __attribute__((overloadable)) clz(uint8_t v) {
+    return (uint8_t)__builtin_clz(v);
+}
+extern int32_t __attribute__((overloadable)) clz(int32_t v) {
+    return (int32_t)__builtin_clz((uint32_t)v);
+}
+extern int16_t __attribute__((overloadable)) clz(int16_t v) {
+    return (int16_t)__builtin_clz(v);
+}
+extern int8_t __attribute__((overloadable)) clz(int8_t v) {
+    return (int8_t)__builtin_clz(v);
+}
+
+
+UIN_FUNC_IN(abs)
+IN_FUNC_IN(clz)
+
+
+// 6.11.4
+
+
+extern float __attribute__((overloadable)) degrees(float radians) {
+    return radians * (180.f / M_PI);
+}
+extern float2 __attribute__((overloadable)) degrees(float2 radians) {
+    return radians * (180.f / M_PI);
+}
+extern float3 __attribute__((overloadable)) degrees(float3 radians) {
+    return radians * (180.f / M_PI);
+}
+extern float4 __attribute__((overloadable)) degrees(float4 radians) {
+    return radians * (180.f / M_PI);
+}
+
+extern float __attribute__((overloadable)) mix(float start, float stop, float amount) {
+    return start + (stop - start) * amount;
+}
+extern float2 __attribute__((overloadable)) mix(float2 start, float2 stop, float2 amount) {
+    return start + (stop - start) * amount;
+}
+extern float3 __attribute__((overloadable)) mix(float3 start, float3 stop, float3 amount) {
+    return start + (stop - start) * amount;
+}
+extern float4 __attribute__((overloadable)) mix(float4 start, float4 stop, float4 amount) {
+    return start + (stop - start) * amount;
+}
+extern float2 __attribute__((overloadable)) mix(float2 start, float2 stop, float amount) {
+    return start + (stop - start) * amount;
+}
+extern float3 __attribute__((overloadable)) mix(float3 start, float3 stop, float amount) {
+    return start + (stop - start) * amount;
+}
+extern float4 __attribute__((overloadable)) mix(float4 start, float4 stop, float amount) {
+    return start + (stop - start) * amount;
+}
+
+extern float __attribute__((overloadable)) radians(float degrees) {
+    return degrees * (M_PI / 180.f);
+}
+extern float2 __attribute__((overloadable)) radians(float2 degrees) {
+    return degrees * (M_PI / 180.f);
+}
+extern float3 __attribute__((overloadable)) radians(float3 degrees) {
+    return degrees * (M_PI / 180.f);
+}
+extern float4 __attribute__((overloadable)) radians(float4 degrees) {
+    return degrees * (M_PI / 180.f);
+}
+
+extern float __attribute__((overloadable)) step(float edge, float v) {
+    return (v < edge) ? 0.f : 1.f;
+}
+extern float2 __attribute__((overloadable)) step(float2 edge, float2 v) {
+    float2 r;
+    r.x = (v.x < edge.x) ? 0.f : 1.f;
+    r.y = (v.y < edge.y) ? 0.f : 1.f;
+    return r;
+}
+extern float3 __attribute__((overloadable)) step(float3 edge, float3 v) {
+    float3 r;
+    r.x = (v.x < edge.x) ? 0.f : 1.f;
+    r.y = (v.y < edge.y) ? 0.f : 1.f;
+    r.z = (v.z < edge.z) ? 0.f : 1.f;
+    return r;
+}
+extern float4 __attribute__((overloadable)) step(float4 edge, float4 v) {
+    float4 r;
+    r.x = (v.x < edge.x) ? 0.f : 1.f;
+    r.y = (v.y < edge.y) ? 0.f : 1.f;
+    r.z = (v.z < edge.z) ? 0.f : 1.f;
+    r.w = (v.w < edge.w) ? 0.f : 1.f;
+    return r;
+}
+extern float2 __attribute__((overloadable)) step(float2 edge, float v) {
+    float2 r;
+    r.x = (v < edge.x) ? 0.f : 1.f;
+    r.y = (v < edge.y) ? 0.f : 1.f;
+    return r;
+}
+extern float3 __attribute__((overloadable)) step(float3 edge, float v) {
+    float3 r;
+    r.x = (v < edge.x) ? 0.f : 1.f;
+    r.y = (v < edge.y) ? 0.f : 1.f;
+    r.z = (v < edge.z) ? 0.f : 1.f;
+    return r;
+}
+extern float4 __attribute__((overloadable)) step(float4 edge, float v) {
+    float4 r;
+    r.x = (v < edge.x) ? 0.f : 1.f;
+    r.y = (v < edge.y) ? 0.f : 1.f;
+    r.z = (v < edge.z) ? 0.f : 1.f;
+    r.w = (v < edge.w) ? 0.f : 1.f;
+    return r;
+}
+
+extern float __attribute__((overloadable)) smoothstep(float, float, float);
+extern float2 __attribute__((overloadable)) smoothstep(float2, float2, float2);
+extern float3 __attribute__((overloadable)) smoothstep(float3, float3, float3);
+extern float4 __attribute__((overloadable)) smoothstep(float4, float4, float4);
+extern float2 __attribute__((overloadable)) smoothstep(float, float, float2);
+extern float3 __attribute__((overloadable)) smoothstep(float, float, float3);
+extern float4 __attribute__((overloadable)) smoothstep(float, float, float4);
+
+extern float __attribute__((overloadable)) sign(float v) {
+    if (v > 0) return 1.f;
+    if (v < 0) return -1.f;
+    return v;
+}
+FN_FUNC_FN(sign)
+
+
+// 6.11.5
+extern float3 __attribute__((overloadable)) cross(float3 lhs, float3 rhs) {
+    float3 r;
+    r.x = lhs.y * rhs.z  - lhs.z * rhs.y;
+    r.y = lhs.z * rhs.x  - lhs.x * rhs.z;
+    r.z = lhs.x * rhs.y  - lhs.y * rhs.x;
+    return r;
+}
+
+extern float4 __attribute__((overloadable)) cross(float4 lhs, float4 rhs) {
+    float4 r;
+    r.x = lhs.y * rhs.z  - lhs.z * rhs.y;
+    r.y = lhs.z * rhs.x  - lhs.x * rhs.z;
+    r.z = lhs.x * rhs.y  - lhs.y * rhs.x;
+    r.w = 0.f;
+    return r;
+}
+
+extern float __attribute__((overloadable)) length(float v);
+extern float __attribute__((overloadable)) length(float2 v);
+extern float __attribute__((overloadable)) length(float3 v);
+extern float __attribute__((overloadable)) length(float4 v);
+
+extern float __attribute__((overloadable)) distance(float lhs, float rhs) {
+    return length(lhs - rhs);
+}
+extern float __attribute__((overloadable)) distance(float2 lhs, float2 rhs) {
+    return length(lhs - rhs);
+}
+extern float __attribute__((overloadable)) distance(float3 lhs, float3 rhs) {
+    return length(lhs - rhs);
+}
+extern float __attribute__((overloadable)) distance(float4 lhs, float4 rhs) {
+    return length(lhs - rhs);
+}
+
+extern float __attribute__((overloadable)) normalize(float v) {
+    return 1.f;
+}
+extern float2 __attribute__((overloadable)) normalize(float2 v) {
+    return v / length(v);
+}
+extern float3 __attribute__((overloadable)) normalize(float3 v) {
+    return v / length(v);
+}
+extern float4 __attribute__((overloadable)) normalize(float4 v) {
+    return v / length(v);
+}
+
+extern float __attribute__((overloadable)) half_sqrt(float);
+
+extern float __attribute__((overloadable)) fast_length(float v) {
+    return fabs(v);
+}
+extern float __attribute__((overloadable)) fast_length(float2 v) {
+    return half_sqrt(v.x*v.x + v.y*v.y);
+}
+extern float __attribute__((overloadable)) fast_length(float3 v) {
+    return half_sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
+}
+extern float __attribute__((overloadable)) fast_length(float4 v) {
+    return half_sqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w);
+}
+
+extern float __attribute__((overloadable)) fast_distance(float lhs, float rhs) {
+    return fast_length(lhs - rhs);
+}
+extern float __attribute__((overloadable)) fast_distance(float2 lhs, float2 rhs) {
+    return fast_length(lhs - rhs);
+}
+extern float __attribute__((overloadable)) fast_distance(float3 lhs, float3 rhs) {
+    return fast_length(lhs - rhs);
+}
+extern float __attribute__((overloadable)) fast_distance(float4 lhs, float4 rhs) {
+    return fast_length(lhs - rhs);
+}
+
+extern float __attribute__((overloadable)) half_rsqrt(float);
+
+extern float __attribute__((overloadable)) fast_normalize(float v) {
+    return 1.f;
+}
+extern float2 __attribute__((overloadable)) fast_normalize(float2 v) {
+    return v * half_rsqrt(v.x*v.x + v.y*v.y);
+}
+extern float3 __attribute__((overloadable)) fast_normalize(float3 v) {
+    return v * half_rsqrt(v.x*v.x + v.y*v.y + v.z*v.z);
+}
+extern float4 __attribute__((overloadable)) fast_normalize(float4 v) {
+    return v * half_rsqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w);
+}
+
+extern float __attribute__((overloadable)) half_recip(float);
+
+/*
+extern float __attribute__((overloadable)) approx_atan(float x) {
+    if (x == 0.f)
+        return 0.f;
+    if (x < 0.f)
+        return -1.f * approx_atan(-1.f * x);
+    if (x > 1.f)
+        return M_PI_2 - approx_atan(approx_recip(x));
+    return x * approx_recip(1.f + 0.28f * x*x);
+}
+FN_FUNC_FN(approx_atan)
+*/
+
+typedef union
+{
+  float fv;
+  int32_t iv;
+} ieee_float_shape_type;
+
+/* Get a 32 bit int from a float.  */
+
+#define GET_FLOAT_WORD(i,d)                 \
+do {                                \
+  ieee_float_shape_type gf_u;                   \
+  gf_u.fv = (d);                     \
+  (i) = gf_u.iv;                      \
+} while (0)
+
+/* Set a float from a 32 bit int.  */
+
+#define SET_FLOAT_WORD(d,i)                 \
+do {                                \
+  ieee_float_shape_type sf_u;                   \
+  sf_u.iv = (i);                      \
+  (d) = sf_u.fv;                     \
+} while (0)
+
+
+
+// Valid -125 to 125
+extern float __attribute__((overloadable)) native_exp2(float v) {
+    int32_t iv = (int)v;
+    int32_t x = iv + (iv >> 31); // ~floor(v)
+    float r = (v - x);
+
+    float fo;
+    SET_FLOAT_WORD(fo, (x + 127) << 23);
+
+    r *= 0.694f; // ~ log(e) / log(2)
+    float r2 = r*r;
+    float adj = 1.f + r + (r2 * 0.5f) + (r2*r * 0.166666f) + (r2*r2 * 0.0416666f);
+    return fo * adj;
+}
+
+extern float2 __attribute__((overloadable)) native_exp2(float2 v) {
+    int2 iv = convert_int2(v);
+    int2 x = iv + (iv >> (int2)31);//floor(v);
+    float2 r = (v - convert_float2(x));
+
+    x += 127;
+
+    float2 fo = (float2)(x << (int2)23);
+
+    r *= 0.694f; // ~ log(e) / log(2)
+    float2 r2 = r*r;
+    float2 adj = 1.f + r + (r2 * 0.5f) + (r2*r * 0.166666f) + (r2*r2 * 0.0416666f);
+    return fo * adj;
+}
+
+extern float4 __attribute__((overloadable)) native_exp2(float4 v) {
+    int4 iv = convert_int4(v);
+    int4 x = iv + (iv >> (int4)31);//floor(v);
+    float4 r = (v - convert_float4(x));
+
+    x += 127;
+
+    float4 fo = (float4)(x << (int4)23);
+
+    r *= 0.694f; // ~ log(e) / log(2)
+    float4 r2 = r*r;
+    float4 adj = 1.f + r + (r2 * 0.5f) + (r2*r * 0.166666f) + (r2*r2 * 0.0416666f);
+    return fo * adj;
+}
+
+extern float3 __attribute__((overloadable)) native_exp2(float3 v) {
+    float4 t = 1.f;
+    t.xyz = v;
+    return native_exp2(t).xyz;
+}
+
+
+extern float __attribute__((overloadable)) native_exp(float v) {
+    return native_exp2(v * 1.442695041f);
+}
+extern float2 __attribute__((overloadable)) native_exp(float2 v) {
+    return native_exp2(v * 1.442695041f);
+}
+extern float3 __attribute__((overloadable)) native_exp(float3 v) {
+    return native_exp2(v * 1.442695041f);
+}
+extern float4 __attribute__((overloadable)) native_exp(float4 v) {
+    return native_exp2(v * 1.442695041f);
+}
+
+extern float __attribute__((overloadable)) native_exp10(float v) {
+    return native_exp2(v * 3.321928095f);
+}
+extern float2 __attribute__((overloadable)) native_exp10(float2 v) {
+    return native_exp2(v * 3.321928095f);
+}
+extern float3 __attribute__((overloadable)) native_exp10(float3 v) {
+    return native_exp2(v * 3.321928095f);
+}
+extern float4 __attribute__((overloadable)) native_exp10(float4 v) {
+    return native_exp2(v * 3.321928095f);
+}
+
+extern float __attribute__((overloadable)) native_log2(float v) {
+    int32_t ibits;
+    GET_FLOAT_WORD(ibits, v);
+
+    int32_t e = (ibits >> 23) & 0xff;
+
+    ibits &= 0x7fffff;
+    ibits |= 127 << 23;
+
+    float ir;
+    SET_FLOAT_WORD(ir, ibits);
+
+    ir -= 1.5f;
+    float ir2 = ir*ir;
+    float adj2 = 0.405465108f + // -0.00009f +
+                 (0.666666667f * ir) -
+                 (0.222222222f * ir2) +
+                 (0.098765432f * ir*ir2) -
+                 (0.049382716f * ir2*ir2) +
+                 (0.026337449f * ir*ir2*ir2) -
+                 (0.014631916f * ir2*ir2*ir2);
+    adj2 *= (1.f / 0.693147181f);
+
+    return (float)(e - 127) + adj2;
+}
+extern float2 __attribute__((overloadable)) native_log2(float2 v) {
+    float2 v2 = {native_log2(v.x), native_log2(v.y)};
+    return v2;
+}
+extern float3 __attribute__((overloadable)) native_log2(float3 v) {
+    float3 v2 = {native_log2(v.x), native_log2(v.y), native_log2(v.z)};
+    return v2;
+}
+extern float4 __attribute__((overloadable)) native_log2(float4 v) {
+    float4 v2 = {native_log2(v.x), native_log2(v.y), native_log2(v.z), native_log2(v.w)};
+    return v2;
+}
+
+extern float __attribute__((overloadable)) native_log(float v) {
+    return native_log2(v) * (1.f / 1.442695041f);
+}
+extern float2 __attribute__((overloadable)) native_log(float2 v) {
+    return native_log2(v) * (1.f / 1.442695041f);
+}
+extern float3 __attribute__((overloadable)) native_log(float3 v) {
+    return native_log2(v) * (1.f / 1.442695041f);
+}
+extern float4 __attribute__((overloadable)) native_log(float4 v) {
+    return native_log2(v) * (1.f / 1.442695041f);
+}
+
+extern float __attribute__((overloadable)) native_log10(float v) {
+    return native_log2(v) * (1.f / 3.321928095f);
+}
+extern float2 __attribute__((overloadable)) native_log10(float2 v) {
+    return native_log2(v) * (1.f / 3.321928095f);
+}
+extern float3 __attribute__((overloadable)) native_log10(float3 v) {
+    return native_log2(v) * (1.f / 3.321928095f);
+}
+extern float4 __attribute__((overloadable)) native_log10(float4 v) {
+    return native_log2(v) * (1.f / 3.321928095f);
+}
+
+
+extern float __attribute__((overloadable)) native_powr(float v, float y) {
+    float v2 = native_log2(v);
+    v2 = fmax(v2, -125.f);
+    return native_exp2(v2 * y);
+}
+extern float2 __attribute__((overloadable)) native_powr(float2 v, float2 y) {
+    float2 v2 = native_log2(v);
+    v2 = fmax(v2, -125.f);
+    return native_exp2(v2 * y);
+}
+extern float3 __attribute__((overloadable)) native_powr(float3 v, float3 y) {
+    float3 v2 = native_log2(v);
+    v2 = fmax(v2, -125.f);
+    return native_exp2(v2 * y);
+}
+extern float4 __attribute__((overloadable)) native_powr(float4 v, float4 y) {
+    float4 v2 = native_log2(v);
+    v2 = fmax(v2, -125.f);
+    return native_exp2(v2 * y);
+}
+
+
+#undef FN_FUNC_FN
+#undef IN_FUNC_FN
+#undef FN_FUNC_FN_FN
+#undef FN_FUNC_FN_F
+#undef FN_FUNC_FN_IN
+#undef FN_FUNC_FN_I
+#undef FN_FUNC_FN_PFN
+#undef FN_FUNC_FN_PIN
+#undef FN_FUNC_FN_FN_FN
+#undef FN_FUNC_FN_FN_PIN
+#undef XN_FUNC_YN
+#undef UIN_FUNC_IN
+#undef IN_FUNC_IN
+#undef XN_FUNC_XN_XN_BODY
+#undef IN_FUNC_IN_IN_BODY
diff --git a/driver/runtime/rs_core.c b/driver/runtime/rs_core.c
new file mode 100644
index 0000000..da51a9a
--- /dev/null
+++ b/driver/runtime/rs_core.c
@@ -0,0 +1,166 @@
+#include "rs_core.rsh"
+#include "rs_graphics.rsh"
+#include "rs_structs.h"
+
+/* Function declarations from libRS */
+extern float4 __attribute__((overloadable)) convert_float4(uchar4 c);
+
+/* Implementation of Core Runtime */
+
+extern float4 rsUnpackColor8888(uchar4 c)
+{
+    return convert_float4(c) * 0.003921569f;
+}
+
+
+extern int32_t __attribute__((overloadable)) rsAtomicCas(volatile int32_t *ptr, int32_t expectedValue, int32_t newValue) {
+    return __sync_val_compare_and_swap(ptr, expectedValue, newValue);
+}
+
+extern uint32_t __attribute__((overloadable)) rsAtomicCas(volatile uint32_t *ptr, uint32_t expectedValue, uint32_t newValue) {
+    return __sync_val_compare_and_swap((volatile int32_t *)ptr, (int32_t)expectedValue, (int32_t)newValue);
+}
+
+extern int32_t __attribute__((overloadable)) rsAtomicInc(volatile int32_t *ptr) {
+    return __sync_fetch_and_add(ptr, 1);
+}
+
+extern int32_t __attribute__((overloadable)) rsAtomicDec(volatile int32_t *ptr) {
+    return __sync_fetch_and_sub(ptr, 1);
+}
+
+extern int32_t __attribute__((overloadable)) rsAtomicAdd(volatile int32_t *ptr, int32_t value) {
+    return __sync_fetch_and_add(ptr, value);
+}
+
+extern int32_t __attribute__((overloadable)) rsAtomicSub(volatile int32_t *ptr, int32_t value) {
+    return __sync_fetch_and_sub(ptr, value);
+}
+
+extern int32_t __attribute__((overloadable)) rsAtomicAnd(volatile int32_t *ptr, int32_t value) {
+    return __sync_fetch_and_and(ptr, value);
+}
+
+extern int32_t __attribute__((overloadable)) rsAtomicOr(volatile int32_t *ptr, int32_t value) {
+    return __sync_fetch_and_or(ptr, value);
+}
+
+extern int32_t __attribute__((overloadable)) rsAtomicXor(volatile int32_t *ptr, int32_t value) {
+    return __sync_fetch_and_xor(ptr, value);
+}
+
+extern uint32_t __attribute__((overloadable)) min(uint32_t, uint32_t);
+extern int32_t __attribute__((overloadable)) min(int32_t, int32_t);
+extern uint32_t __attribute__((overloadable)) max(uint32_t, uint32_t);
+extern int32_t __attribute__((overloadable)) max(int32_t, int32_t);
+
+extern uint32_t __attribute__((overloadable)) rsAtomicMin(volatile uint32_t *ptr, uint32_t value) {
+    uint32_t prev, status;
+    do {
+        prev = *ptr;
+        uint32_t n = min(value, prev);
+        status = rsAtomicCas((volatile int32_t*) ptr, (int32_t) prev, (int32_t)n);
+    } while (status != prev);
+    return prev;
+}
+
+extern int32_t __attribute__((overloadable)) rsAtomicMin(volatile int32_t *ptr, int32_t value) {
+    int32_t prev, status;
+    do {
+        prev = *ptr;
+        int32_t n = min(value, prev);
+        status = rsAtomicCas(ptr, prev, n);
+    } while (status != prev);
+    return prev;
+}
+
+extern uint32_t __attribute__((overloadable)) rsAtomicMax(volatile uint32_t *ptr, uint32_t value) {
+    uint32_t prev, status;
+    do {
+        prev = *ptr;
+        uint32_t n = max(value, prev);
+        status = rsAtomicCas((volatile int32_t*) ptr, (int32_t) prev, (int32_t) n);
+    } while (status != prev);
+    return prev;
+}
+
+extern int32_t __attribute__((overloadable)) rsAtomicMax(volatile int32_t *ptr, int32_t value) {
+    int32_t prev, status;
+    do {
+        prev = *ptr;
+        int32_t n = max(value, prev);
+        status = rsAtomicCas(ptr, prev, n);
+    } while (status != prev);
+    return prev;
+}
+
+
+
+extern int32_t rand();
+#define RAND_MAX 0x7fffffff
+
+
+
+extern float __attribute__((overloadable)) rsRand(float min, float max);/* {
+    float r = (float)rand();
+    r /= RAND_MAX;
+    r = r * (max - min) + min;
+    return r;
+}
+*/
+
+extern float __attribute__((overloadable)) rsRand(float max) {
+    return rsRand(0.f, max);
+    //float r = (float)rand();
+    //r *= max;
+    //r /= RAND_MAX;
+    //return r;
+}
+
+extern int __attribute__((overloadable)) rsRand(int max) {
+    return (int)rsRand((float)max);
+}
+
+extern int __attribute__((overloadable)) rsRand(int min, int max) {
+    return (int)rsRand((float)min, (float)max);
+}
+
+#define PRIM_DEBUG(T)                               \
+extern void __attribute__((overloadable)) rsDebug(const char *, const T *);     \
+void __attribute__((overloadable)) rsDebug(const char *txt, T val) {            \
+    rsDebug(txt, &val);                                                         \
+}
+
+PRIM_DEBUG(char2)
+PRIM_DEBUG(char3)
+PRIM_DEBUG(char4)
+PRIM_DEBUG(uchar2)
+PRIM_DEBUG(uchar3)
+PRIM_DEBUG(uchar4)
+PRIM_DEBUG(short2)
+PRIM_DEBUG(short3)
+PRIM_DEBUG(short4)
+PRIM_DEBUG(ushort2)
+PRIM_DEBUG(ushort3)
+PRIM_DEBUG(ushort4)
+PRIM_DEBUG(int2)
+PRIM_DEBUG(int3)
+PRIM_DEBUG(int4)
+PRIM_DEBUG(uint2)
+PRIM_DEBUG(uint3)
+PRIM_DEBUG(uint4)
+PRIM_DEBUG(long2)
+PRIM_DEBUG(long3)
+PRIM_DEBUG(long4)
+PRIM_DEBUG(ulong2)
+PRIM_DEBUG(ulong3)
+PRIM_DEBUG(ulong4)
+PRIM_DEBUG(float2)
+PRIM_DEBUG(float3)
+PRIM_DEBUG(float4)
+PRIM_DEBUG(double2)
+PRIM_DEBUG(double3)
+PRIM_DEBUG(double4)
+
+#undef PRIM_DEBUG
+
diff --git a/driver/runtime/rs_element.c b/driver/runtime/rs_element.c
new file mode 100644
index 0000000..4db5883
--- /dev/null
+++ b/driver/runtime/rs_element.c
@@ -0,0 +1,111 @@
+#include "rs_core.rsh"
+#include "rs_graphics.rsh"
+#include "rs_structs.h"
+
+/**
+* Element
+*/
+extern uint32_t __attribute__((overloadable))
+        rsElementGetSubElementCount(rs_element e) {
+    Element_t *element = (Element_t *)e.p;
+    if (element == NULL) {
+        return 0;
+    }
+    return element->mHal.state.fieldsCount;
+}
+
+extern rs_element __attribute__((overloadable))
+        rsElementGetSubElement(rs_element e, uint32_t index) {
+    Element_t *element = (Element_t *)e.p;
+    if (element == NULL || index >= element->mHal.state.fieldsCount) {
+        rs_element nullElem = {0};
+        return nullElem;
+    }
+    rs_element returnElem = {element->mHal.state.fields[index]};
+    return returnElem;
+}
+
+extern uint32_t __attribute__((overloadable))
+        rsElementGetSubElementNameLength(rs_element e, uint32_t index) {
+    Element_t *element = (Element_t *)e.p;
+    if (element == NULL || index >= element->mHal.state.fieldsCount) {
+        return 0;
+    }
+    return element->mHal.state.fieldNameLengths[index];
+}
+
+extern uint32_t __attribute__((overloadable))
+        rsElementGetSubElementName(rs_element e, uint32_t index, char *name, uint32_t nameLength) {
+    Element_t *element = (Element_t *)e.p;
+    if (element == NULL || index >= element->mHal.state.fieldsCount ||
+        nameLength == 0 || name == 0) {
+        return 0;
+    }
+
+    uint32_t numToCopy = element->mHal.state.fieldNameLengths[index];
+    if (nameLength < numToCopy) {
+        numToCopy = nameLength;
+    }
+    // Place the null terminator manually, in case of partial string
+    numToCopy --;
+    name[numToCopy] = '\0';
+    const char *nameSource = element->mHal.state.fieldNames[index];
+    for (uint32_t i = 0; i < numToCopy; i ++) {
+        name[i] = nameSource[i];
+    }
+    return numToCopy;
+}
+
+extern uint32_t __attribute__((overloadable))
+        rsElementGetSubElementArraySize(rs_element e, uint32_t index) {
+    Element_t *element = (Element_t *)e.p;
+    if (element == NULL || index >= element->mHal.state.fieldsCount) {
+        return 0;
+    }
+    return element->mHal.state.fieldArraySizes[index];
+}
+
+extern uint32_t __attribute__((overloadable))
+        rsElementGetSubElementOffsetBytes(rs_element e, uint32_t index) {
+    Element_t *element = (Element_t *)e.p;
+    if (element == NULL || index >= element->mHal.state.fieldsCount) {
+        return 0;
+    }
+    return element->mHal.state.fieldOffsetBytes[index];
+}
+
+extern uint32_t __attribute__((overloadable))
+        rsElementGetBytesSize(rs_element e) {
+    Element_t *element = (Element_t *)e.p;
+    if (element == NULL) {
+        return 0;
+    }
+    return element->mHal.state.elementSizeBytes;
+}
+
+extern rs_data_type __attribute__((overloadable))
+        rsElementGetDataType(rs_element e) {
+    Element_t *element = (Element_t *)e.p;
+    if (element == NULL) {
+        return RS_TYPE_INVALID;
+    }
+    return element->mHal.state.dataType;
+}
+
+extern rs_data_kind __attribute__((overloadable))
+        rsElementGetDataKind(rs_element e) {
+    Element_t *element = (Element_t *)e.p;
+    if (element == NULL) {
+        return RS_KIND_INVALID;
+    }
+    return element->mHal.state.dataKind;
+}
+
+extern uint32_t __attribute__((overloadable))
+        rsElementGetVectorSize(rs_element e) {
+    Element_t *element = (Element_t *)e.p;
+    if (element == NULL) {
+        return 0;
+    }
+    return element->mHal.state.vectorSize;
+}
diff --git a/driver/runtime/rs_matrix.c b/driver/runtime/rs_matrix.c
new file mode 100644
index 0000000..3afccc1
--- /dev/null
+++ b/driver/runtime/rs_matrix.c
@@ -0,0 +1,314 @@
+#include "rs_core.rsh"
+#include "rs_graphics.rsh"
+#include "rs_structs.h"
+
+/* Function declarations from libRS */
+extern float4 __attribute__((overloadable)) convert_float4(uchar4 c);
+
+/* Implementation of Core Runtime */
+
+
+/////////////////////////////////////////////////////
+// Matrix ops
+/////////////////////////////////////////////////////
+
+
+extern void __attribute__((overloadable))
+rsMatrixLoadIdentity(rs_matrix4x4 *m) {
+    m->m[0] = 1.f;
+    m->m[1] = 0.f;
+    m->m[2] = 0.f;
+    m->m[3] = 0.f;
+    m->m[4] = 0.f;
+    m->m[5] = 1.f;
+    m->m[6] = 0.f;
+    m->m[7] = 0.f;
+    m->m[8] = 0.f;
+    m->m[9] = 0.f;
+    m->m[10] = 1.f;
+    m->m[11] = 0.f;
+    m->m[12] = 0.f;
+    m->m[13] = 0.f;
+    m->m[14] = 0.f;
+    m->m[15] = 1.f;
+}
+
+extern void __attribute__((overloadable))
+rsMatrixLoadIdentity(rs_matrix3x3 *m) {
+    m->m[0] = 1.f;
+    m->m[1] = 0.f;
+    m->m[2] = 0.f;
+    m->m[3] = 0.f;
+    m->m[4] = 1.f;
+    m->m[5] = 0.f;
+    m->m[6] = 0.f;
+    m->m[7] = 0.f;
+    m->m[8] = 1.f;
+}
+extern void __attribute__((overloadable))
+rsMatrixLoadIdentity(rs_matrix2x2 *m) {
+    m->m[0] = 1.f;
+    m->m[1] = 0.f;
+    m->m[2] = 0.f;
+    m->m[3] = 1.f;
+}
+
+extern void __attribute__((overloadable))
+rsMatrixLoad(rs_matrix4x4 *m, const float *f) {
+    m->m[0] = f[0];
+    m->m[1] = f[1];
+    m->m[2] = f[2];
+    m->m[3] = f[3];
+    m->m[4] = f[4];
+    m->m[5] = f[5];
+    m->m[6] = f[6];
+    m->m[7] = f[7];
+    m->m[8] = f[8];
+    m->m[9] = f[9];
+    m->m[10] = f[10];
+    m->m[11] = f[11];
+    m->m[12] = f[12];
+    m->m[13] = f[13];
+    m->m[14] = f[14];
+    m->m[15] = f[15];
+}
+extern void __attribute__((overloadable))
+rsMatrixLoad(rs_matrix3x3 *m, const float *f) {
+    m->m[0] = f[0];
+    m->m[1] = f[1];
+    m->m[2] = f[2];
+    m->m[3] = f[3];
+    m->m[4] = f[4];
+    m->m[5] = f[5];
+    m->m[6] = f[6];
+    m->m[7] = f[7];
+    m->m[8] = f[8];
+}
+extern void __attribute__((overloadable))
+rsMatrixLoad(rs_matrix2x2 *m, const float *f) {
+    m->m[0] = f[0];
+    m->m[1] = f[1];
+    m->m[2] = f[2];
+    m->m[3] = f[3];
+}
+
+extern void __attribute__((overloadable))
+rsMatrixLoad(rs_matrix4x4 *m, const rs_matrix4x4 *s) {
+    m->m[0] = s->m[0];
+    m->m[1] = s->m[1];
+    m->m[2] = s->m[2];
+    m->m[3] = s->m[3];
+    m->m[4] = s->m[4];
+    m->m[5] = s->m[5];
+    m->m[6] = s->m[6];
+    m->m[7] = s->m[7];
+    m->m[8] = s->m[8];
+    m->m[9] = s->m[9];
+    m->m[10] = s->m[10];
+    m->m[11] = s->m[11];
+    m->m[12] = s->m[12];
+    m->m[13] = s->m[13];
+    m->m[14] = s->m[14];
+    m->m[15] = s->m[15];
+}
+extern void __attribute__((overloadable))
+rsMatrixLoad(rs_matrix4x4 *m, const rs_matrix3x3 *v) {
+    m->m[0] = v->m[0];
+    m->m[1] = v->m[1];
+    m->m[2] = v->m[2];
+    m->m[3] = 0.f;
+    m->m[4] = v->m[3];
+    m->m[5] = v->m[4];
+    m->m[6] = v->m[5];
+    m->m[7] = 0.f;
+    m->m[8] = v->m[6];
+    m->m[9] = v->m[7];
+    m->m[10] = v->m[8];
+    m->m[11] = 0.f;
+    m->m[12] = 0.f;
+    m->m[13] = 0.f;
+    m->m[14] = 0.f;
+    m->m[15] = 1.f;
+}
+extern void __attribute__((overloadable))
+rsMatrixLoad(rs_matrix4x4 *m, const rs_matrix2x2 *v) {
+    m->m[0] = v->m[0];
+    m->m[1] = v->m[1];
+    m->m[2] = 0.f;
+    m->m[3] = 0.f;
+    m->m[4] = v->m[2];
+    m->m[5] = v->m[3];
+    m->m[6] = 0.f;
+    m->m[7] = 0.f;
+    m->m[8] = 0.f;
+    m->m[9] = 0.f;
+    m->m[10] = 1.f;
+    m->m[11] = 0.f;
+    m->m[12] = 0.f;
+    m->m[13] = 0.f;
+    m->m[14] = 0.f;
+    m->m[15] = 1.f;
+}
+extern void __attribute__((overloadable))
+rsMatrixLoad(rs_matrix3x3 *m, const rs_matrix3x3 *s) {
+    m->m[0] = s->m[0];
+    m->m[1] = s->m[1];
+    m->m[2] = s->m[2];
+    m->m[3] = s->m[3];
+    m->m[4] = s->m[4];
+    m->m[5] = s->m[5];
+    m->m[6] = s->m[6];
+    m->m[7] = s->m[7];
+    m->m[8] = s->m[8];
+}
+extern void __attribute__((overloadable))
+rsMatrixLoad(rs_matrix2x2 *m, const rs_matrix2x2 *s) {
+    m->m[0] = s->m[0];
+    m->m[1] = s->m[1];
+    m->m[2] = s->m[2];
+    m->m[3] = s->m[3];
+}
+
+
+extern void __attribute__((overloadable))
+rsMatrixSet(rs_matrix4x4 *m, uint32_t row, uint32_t col, float v) {
+    m->m[row * 4 + col] = v;
+}
+
+extern float __attribute__((overloadable))
+rsMatrixGet(const rs_matrix4x4 *m, uint32_t row, uint32_t col) {
+    return m->m[row * 4 + col];
+}
+
+extern void __attribute__((overloadable))
+rsMatrixSet(rs_matrix3x3 *m, uint32_t row, uint32_t col, float v) {
+    m->m[row * 3 + col] = v;
+}
+
+extern float __attribute__((overloadable))
+rsMatrixGet(const rs_matrix3x3 *m, uint32_t row, uint32_t col) {
+    return m->m[row * 3 + col];
+}
+
+extern void __attribute__((overloadable))
+rsMatrixSet(rs_matrix2x2 *m, uint32_t row, uint32_t col, float v) {
+    m->m[row * 2 + col] = v;
+}
+
+extern float __attribute__((overloadable))
+rsMatrixGet(const rs_matrix2x2 *m, uint32_t row, uint32_t col) {
+    return m->m[row * 2 + col];
+}
+
+extern float2 __attribute__((overloadable))
+rsMatrixMultiply(const rs_matrix2x2 *m, float2 in) {
+    float2 ret;
+    ret.x = (m->m[0] * in.x) + (m->m[2] * in.y);
+    ret.y = (m->m[1] * in.x) + (m->m[3] * in.y);
+    return ret;
+}
+extern float2 __attribute__((overloadable))
+rsMatrixMultiply(rs_matrix2x2 *m, float2 in) {
+    return rsMatrixMultiply((const rs_matrix2x2 *)m, in);
+}
+
+extern float4 __attribute__((overloadable))
+rsMatrixMultiply(rs_matrix4x4 *m, float4 in) {
+    return rsMatrixMultiply((const rs_matrix4x4 *)m, in);
+}
+
+extern float4 __attribute__((overloadable))
+rsMatrixMultiply(rs_matrix4x4 *m, float3 in) {
+    return rsMatrixMultiply((const rs_matrix4x4 *)m, in);
+}
+
+extern float4 __attribute__((overloadable))
+rsMatrixMultiply(rs_matrix4x4 *m, float2 in) {
+    return rsMatrixMultiply((const rs_matrix4x4 *)m, in);
+}
+
+extern float3 __attribute__((overloadable))
+rsMatrixMultiply(rs_matrix3x3 *m, float3 in) {
+    return rsMatrixMultiply((const rs_matrix3x3 *)m, in);
+}
+
+extern float3 __attribute__((overloadable))
+rsMatrixMultiply(rs_matrix3x3 *m, float2 in) {
+    return rsMatrixMultiply((const rs_matrix3x3 *)m, in);
+}
+
+extern void __attribute__((overloadable))
+rsMatrixLoadMultiply(rs_matrix4x4 *ret, const rs_matrix4x4 *lhs, const rs_matrix4x4 *rhs) {
+    for (int i=0 ; i<4 ; i++) {
+        float ri0 = 0;
+        float ri1 = 0;
+        float ri2 = 0;
+        float ri3 = 0;
+        for (int j=0 ; j<4 ; j++) {
+            const float rhs_ij = rsMatrixGet(rhs, i, j);
+            ri0 += rsMatrixGet(lhs, j, 0) * rhs_ij;
+            ri1 += rsMatrixGet(lhs, j, 1) * rhs_ij;
+            ri2 += rsMatrixGet(lhs, j, 2) * rhs_ij;
+            ri3 += rsMatrixGet(lhs, j, 3) * rhs_ij;
+        }
+        rsMatrixSet(ret, i, 0, ri0);
+        rsMatrixSet(ret, i, 1, ri1);
+        rsMatrixSet(ret, i, 2, ri2);
+        rsMatrixSet(ret, i, 3, ri3);
+    }
+}
+
+extern void __attribute__((overloadable))
+rsMatrixMultiply(rs_matrix4x4 *lhs, const rs_matrix4x4 *rhs) {
+    rs_matrix4x4 r;
+    rsMatrixLoadMultiply(&r, lhs, rhs);
+    rsMatrixLoad(lhs, &r);
+}
+
+extern void __attribute__((overloadable))
+rsMatrixLoadMultiply(rs_matrix3x3 *ret, const rs_matrix3x3 *lhs, const rs_matrix3x3 *rhs) {
+    for (int i=0 ; i<3 ; i++) {
+        float ri0 = 0;
+        float ri1 = 0;
+        float ri2 = 0;
+        for (int j=0 ; j<3 ; j++) {
+            const float rhs_ij = rsMatrixGet(rhs, i, j);
+            ri0 += rsMatrixGet(lhs, j, 0) * rhs_ij;
+            ri1 += rsMatrixGet(lhs, j, 1) * rhs_ij;
+            ri2 += rsMatrixGet(lhs, j, 2) * rhs_ij;
+        }
+        rsMatrixSet(ret, i, 0, ri0);
+        rsMatrixSet(ret, i, 1, ri1);
+        rsMatrixSet(ret, i, 2, ri2);
+    }
+}
+
+extern void __attribute__((overloadable))
+rsMatrixMultiply(rs_matrix3x3 *lhs, const rs_matrix3x3 *rhs) {
+    rs_matrix3x3 r;
+    rsMatrixLoadMultiply(&r, lhs, rhs);
+    rsMatrixLoad(lhs, &r);
+}
+
+extern void __attribute__((overloadable))
+rsMatrixLoadMultiply(rs_matrix2x2 *ret, const rs_matrix2x2 *lhs, const rs_matrix2x2 *rhs) {
+    for (int i=0 ; i<2 ; i++) {
+        float ri0 = 0;
+        float ri1 = 0;
+        for (int j=0 ; j<2 ; j++) {
+            const float rhs_ij = rsMatrixGet(rhs, i, j);
+            ri0 += rsMatrixGet(lhs, j, 0) * rhs_ij;
+            ri1 += rsMatrixGet(lhs, j, 1) * rhs_ij;
+        }
+        rsMatrixSet(ret, i, 0, ri0);
+        rsMatrixSet(ret, i, 1, ri1);
+    }
+}
+
+extern void __attribute__((overloadable))
+rsMatrixMultiply(rs_matrix2x2 *lhs, const rs_matrix2x2 *rhs) {
+    rs_matrix2x2 r;
+    rsMatrixLoadMultiply(&r, lhs, rhs);
+    rsMatrixLoad(lhs, &r);
+}
+
diff --git a/driver/runtime/rs_mesh.c b/driver/runtime/rs_mesh.c
new file mode 100644
index 0000000..bb533bc
--- /dev/null
+++ b/driver/runtime/rs_mesh.c
@@ -0,0 +1,55 @@
+#include "rs_core.rsh"
+#include "rs_graphics.rsh"
+#include "rs_structs.h"
+
+/**
+* Mesh
+*/
+extern uint32_t __attribute__((overloadable))
+        rsgMeshGetVertexAllocationCount(rs_mesh m) {
+    Mesh_t *mesh = (Mesh_t *)m.p;
+    if (mesh == NULL) {
+        return 0;
+    }
+    return mesh->mHal.state.vertexBuffersCount;
+}
+
+extern uint32_t __attribute__((overloadable))
+        rsgMeshGetPrimitiveCount(rs_mesh m) {
+    Mesh_t *mesh = (Mesh_t *)m.p;
+    if (mesh == NULL) {
+        return 0;
+    }
+    return mesh->mHal.state.primitivesCount;
+}
+
+extern rs_allocation __attribute__((overloadable))
+        rsgMeshGetVertexAllocation(rs_mesh m, uint32_t index) {
+    Mesh_t *mesh = (Mesh_t *)m.p;
+    if (mesh == NULL || index >= mesh->mHal.state.vertexBuffersCount) {
+        rs_allocation nullAlloc = {0};
+        return nullAlloc;
+    }
+    rs_allocation returnAlloc = {mesh->mHal.state.vertexBuffers[index]};
+    return returnAlloc;
+}
+
+extern rs_allocation __attribute__((overloadable))
+        rsgMeshGetIndexAllocation(rs_mesh m, uint32_t index) {
+    Mesh_t *mesh = (Mesh_t *)m.p;
+    if (mesh == NULL || index >= mesh->mHal.state.primitivesCount) {
+        rs_allocation nullAlloc = {0};
+        return nullAlloc;
+    }
+    rs_allocation returnAlloc = {mesh->mHal.state.indexBuffers[index]};
+    return returnAlloc;
+}
+
+extern rs_primitive __attribute__((overloadable))
+        rsgMeshGetPrimitive(rs_mesh m, uint32_t index) {
+    Mesh_t *mesh = (Mesh_t *)m.p;
+    if (mesh == NULL || index >= mesh->mHal.state.primitivesCount) {
+        return RS_PRIMITIVE_INVALID;
+    }
+    return mesh->mHal.state.primitives[index];
+}
diff --git a/driver/runtime/rs_program.c b/driver/runtime/rs_program.c
new file mode 100644
index 0000000..64c656f
--- /dev/null
+++ b/driver/runtime/rs_program.c
@@ -0,0 +1,108 @@
+#include "rs_core.rsh"
+#include "rs_graphics.rsh"
+#include "rs_structs.h"
+
+/**
+* Program Store
+*/
+extern rs_depth_func __attribute__((overloadable))
+        rsgProgramStoreGetDepthFunc(rs_program_store ps) {
+    ProgramStore_t *prog = (ProgramStore_t *)ps.p;
+    if (prog == NULL) {
+        return RS_DEPTH_FUNC_INVALID;
+    }
+    return prog->mHal.state.depthFunc;
+}
+
+extern bool __attribute__((overloadable))
+        rsgProgramStoreIsDepthMaskEnabled(rs_program_store ps) {
+    ProgramStore_t *prog = (ProgramStore_t *)ps.p;
+    if (prog == NULL) {
+        return false;
+    }
+    return prog->mHal.state.depthWriteEnable;
+}
+
+extern bool __attribute__((overloadable))
+        rsgProgramStoreIsColorMaskRedEnabled(rs_program_store ps) {
+    ProgramStore_t *prog = (ProgramStore_t *)ps.p;
+    if (prog == NULL) {
+        return false;
+    }
+    return prog->mHal.state.colorRWriteEnable;
+}
+
+extern bool __attribute__((overloadable))
+        rsgProgramStoreIsColorMaskGreenEnabled(rs_program_store ps) {
+    ProgramStore_t *prog = (ProgramStore_t *)ps.p;
+    if (prog == NULL) {
+        return false;
+    }
+    return prog->mHal.state.colorGWriteEnable;
+}
+
+extern bool __attribute__((overloadable))
+        rsgProgramStoreIsColorMaskBlueEnabled(rs_program_store ps) {
+    ProgramStore_t *prog = (ProgramStore_t *)ps.p;
+    if (prog == NULL) {
+        return false;
+    }
+    return prog->mHal.state.colorBWriteEnable;
+}
+
+extern bool __attribute__((overloadable))
+        rsgProgramStoreIsColorMaskAlphaEnabled(rs_program_store ps) {
+    ProgramStore_t *prog = (ProgramStore_t *)ps.p;
+    if (prog == NULL) {
+        return false;
+    }
+    return prog->mHal.state.colorAWriteEnable;
+}
+
+extern rs_blend_src_func __attribute__((overloadable))
+        rsgProgramStoreGetBlendSrcFunc(rs_program_store ps) {
+    ProgramStore_t *prog = (ProgramStore_t *)ps.p;
+    if (prog == NULL) {
+        return RS_BLEND_SRC_INVALID;
+    }
+    return prog->mHal.state.blendSrc;
+}
+
+extern rs_blend_dst_func __attribute__((overloadable))
+        rsgProgramStoreGetBlendDstFunc(rs_program_store ps) {
+    ProgramStore_t *prog = (ProgramStore_t *)ps.p;
+    if (prog == NULL) {
+        return RS_BLEND_DST_INVALID;
+    }
+    return prog->mHal.state.blendDst;
+}
+
+extern bool __attribute__((overloadable))
+        rsgProgramStoreIsDitherEnabled(rs_program_store ps) {
+    ProgramStore_t *prog = (ProgramStore_t *)ps.p;
+    if (prog == NULL) {
+        return false;
+    }
+    return prog->mHal.state.ditherEnable;
+}
+
+/**
+* Program Raster
+*/
+extern bool __attribute__((overloadable))
+        rsgProgramRasterIsPointSpriteEnabled(rs_program_raster pr) {
+    ProgramRaster_t *prog = (ProgramRaster_t *)pr.p;
+    if (prog == NULL) {
+        return false;
+    }
+    return prog->mHal.state.pointSprite;
+}
+
+extern rs_cull_mode __attribute__((overloadable))
+        rsgProgramRasterGetCullMode(rs_program_raster pr) {
+    ProgramRaster_t *prog = (ProgramRaster_t *)pr.p;
+    if (prog == NULL) {
+        return RS_CULL_INVALID;
+    }
+    return prog->mHal.state.cull;
+}
diff --git a/driver/runtime/rs_sample.c b/driver/runtime/rs_sample.c
new file mode 100644
index 0000000..2cd5bdc
--- /dev/null
+++ b/driver/runtime/rs_sample.c
@@ -0,0 +1,654 @@
+#include "rs_core.rsh"
+#include "rs_graphics.rsh"
+#include "rs_structs.h"
+
+
+// 565 Conversion bits taken from SkBitmap
+#define SK_R16_BITS     5
+#define SK_G16_BITS     6
+#define SK_B16_BITS     5
+
+#define SK_R16_SHIFT    (SK_B16_BITS + SK_G16_BITS)
+#define SK_G16_SHIFT    (SK_B16_BITS)
+#define SK_B16_SHIFT    0
+
+#define SK_R16_MASK     ((1 << SK_R16_BITS) - 1)
+#define SK_G16_MASK     ((1 << SK_G16_BITS) - 1)
+#define SK_B16_MASK     ((1 << SK_B16_BITS) - 1)
+
+#define SkGetPackedR16(color)   (((unsigned)(color) >> SK_R16_SHIFT) & SK_R16_MASK)
+#define SkGetPackedG16(color)   (((unsigned)(color) >> SK_G16_SHIFT) & SK_G16_MASK)
+#define SkGetPackedB16(color)   (((unsigned)(color) >> SK_B16_SHIFT) & SK_B16_MASK)
+
+static inline unsigned SkR16ToR32(unsigned r) {
+    return (r << (8 - SK_R16_BITS)) | (r >> (2 * SK_R16_BITS - 8));
+}
+
+static inline unsigned SkG16ToG32(unsigned g) {
+    return (g << (8 - SK_G16_BITS)) | (g >> (2 * SK_G16_BITS - 8));
+}
+
+static inline unsigned SkB16ToB32(unsigned b) {
+    return (b << (8 - SK_B16_BITS)) | (b >> (2 * SK_B16_BITS - 8));
+}
+
+#define SkPacked16ToR32(c)      SkR16ToR32(SkGetPackedR16(c))
+#define SkPacked16ToG32(c)      SkG16ToG32(SkGetPackedG16(c))
+#define SkPacked16ToB32(c)      SkB16ToB32(SkGetPackedB16(c))
+
+static float3 getFrom565(uint16_t color) {
+    float3 result;
+    result.x = (float)SkPacked16ToR32(color);
+    result.y = (float)SkPacked16ToG32(color);
+    result.z = (float)SkPacked16ToB32(color);
+    return result;
+}
+
+/**
+* Allocation sampling
+*/
+static inline float __attribute__((overloadable))
+        getElementAt1(const uint8_t *p, int32_t x) {
+    float r = p[x];
+    return r;
+}
+
+static inline float2 __attribute__((overloadable))
+        getElementAt2(const uint8_t *p, int32_t x) {
+    x *= 2;
+    float2 r = {p[x], p[x+1]};
+    return r;
+}
+
+static inline float3 __attribute__((overloadable))
+        getElementAt3(const uint8_t *p, int32_t x) {
+    x *= 4;
+    float3 r = {p[x], p[x+1], p[x+2]};
+    return r;
+}
+
+static inline float4 __attribute__((overloadable))
+        getElementAt4(const uint8_t *p, int32_t x) {
+    x *= 4;
+    const uchar4 *p2 = (const uchar4 *)&p[x];
+    return convert_float4(p2[0]);
+}
+
+static inline float3 __attribute__((overloadable))
+        getElementAt565(const uint8_t *p, int32_t x) {
+    x *= 2;
+    float3 r = getFrom565(((const uint16_t *)p)[0]);
+    return r;
+}
+
+static inline float __attribute__((overloadable))
+        getElementAt1(const uint8_t *p, size_t stride, int32_t x, int32_t y) {
+    p += y * stride;
+    float r = p[x];
+    return r;
+}
+
+static inline float2 __attribute__((overloadable))
+        getElementAt2(const uint8_t *p, size_t stride, int32_t x, int32_t y) {
+    p += y * stride;
+    x *= 2;
+    float2 r = {p[x], p[x+1]};
+    return r;
+}
+
+static inline float3 __attribute__((overloadable))
+        getElementAt3(const uint8_t *p, size_t stride, int32_t x, int32_t y) {
+    p += y * stride;
+    x *= 4;
+    float3 r = {p[x], p[x+1], p[x+2]};
+    return r;
+}
+
+static inline float4 __attribute__((overloadable))
+        getElementAt4(const uint8_t *p, size_t stride, int32_t x, int32_t y) {
+    p += y * stride;
+    x *= 4;
+    float4 r = {p[x], p[x+1], p[x+2], p[x+3]};
+    return r;
+}
+
+static inline float3 __attribute__((overloadable))
+        getElementAt565(const uint8_t *p, size_t stride, int32_t x, int32_t y) {
+    p += y * stride;
+    x *= 2;
+    float3 r = getFrom565(((const uint16_t *)p)[0]);
+    return r;
+}
+
+
+
+
+
+static float4 __attribute__((overloadable))
+            getSample_A(const uint8_t *p, int32_t iPixel,
+                          int32_t next, float w0, float w1) {
+    float p0 = getElementAt1(p, iPixel);
+    float p1 = getElementAt1(p, next);
+    float r = p0 * w0 + p1 * w1;
+    r *= (1.f / 255.f);
+    float4 ret = {0.f, 0.f, 0.f, r};
+    return ret;
+}
+static float4 __attribute__((overloadable))
+            getSample_L(const uint8_t *p, int32_t iPixel,
+                          int32_t next, float w0, float w1) {
+    float p0 = getElementAt1(p, iPixel);
+    float p1 = getElementAt1(p, next);
+    float r = p0 * w0 + p1 * w1;
+    r *= (1.f / 255.f);
+    float4 ret = {r, r, r, 1.f};
+    return ret;
+}
+static float4 __attribute__((overloadable))
+            getSample_LA(const uint8_t *p, int32_t iPixel,
+                           int32_t next, float w0, float w1) {
+    float2 p0 = getElementAt2(p, iPixel);
+    float2 p1 = getElementAt2(p, next);
+    float2 r = p0 * w0 + p1 * w1;
+    r *= (1.f / 255.f);
+    float4 ret = {r.x, r.x, r.x, r.y};
+    return ret;
+}
+static float4 __attribute__((overloadable))
+            getSample_RGB(const uint8_t *p, int32_t iPixel,
+                            int32_t next, float w0, float w1) {
+    float3 p0 = getElementAt3(p, iPixel);
+    float3 p1 = getElementAt3(p, next);
+    float3 r = p0 * w0 + p1 * w1;
+    r *= (1.f / 255.f);
+    float4 ret = {r.x, r.x, r.z, 1.f};
+    return ret;
+}
+static float4 __attribute__((overloadable))
+            getSample_565(const uint8_t *p, int32_t iPixel,
+                           int32_t next, float w0, float w1) {
+    float3 p0 = getElementAt565(p, iPixel);
+    float3 p1 = getElementAt565(p, next);
+    float3 r = p0 * w0 + p1 * w1;
+    r *= (1.f / 255.f);
+    float4 ret = {r.x, r.x, r.z, 1.f};
+    return ret;
+}
+static float4 __attribute__((overloadable))
+            getSample_RGBA(const uint8_t *p, int32_t iPixel,
+                             int32_t next, float w0, float w1) {
+    float4 p0 = getElementAt4(p, iPixel);
+    float4 p1 = getElementAt4(p, next);
+    float4 r = p0 * w0 + p1 * w1;
+    r *= (1.f / 255.f);
+    return r;
+}
+
+
+static float4 __attribute__((overloadable))
+            getSample_A(const uint8_t *p, size_t stride,
+                          int locX, int locY, int nextX, int nextY,
+                          float w0, float w1, float w2, float w3) {
+    float p0 = getElementAt1(p, stride, locX, locY);
+    float p1 = getElementAt1(p, stride, nextX, locY);
+    float p2 = getElementAt1(p, stride, locX, nextY);
+    float p3 = getElementAt1(p, stride, nextX, nextY);
+    float r = p0 * w0 + p1 * w1 + p2 * w2 + p3 * w3;
+    r *= (1.f / 255.f);
+    float4 ret = {0.f, 0.f, 0.f, r};
+    return ret;
+}
+static float4 __attribute__((overloadable))
+            getSample_L(const uint8_t *p, size_t stride,
+                         int locX, int locY, int nextX, int nextY,
+                         float w0, float w1, float w2, float w3) {
+    float p0 = getElementAt1(p, stride, locX, locY);
+    float p1 = getElementAt1(p, stride, nextX, locY);
+    float p2 = getElementAt1(p, stride, locX, nextY);
+    float p3 = getElementAt1(p, stride, nextX, nextY);
+    float r = p0 * w0 + p1 * w1 + p2 * w2 + p3 * w3;
+    r *= (1.f / 255.f);
+    float4 ret = {r, r, r, 1.f};
+    return ret;
+}
+static float4 __attribute__((overloadable))
+            getSample_LA(const uint8_t *p, size_t stride,
+                         int locX, int locY, int nextX, int nextY,
+                         float w0, float w1, float w2, float w3) {
+    float2 p0 = getElementAt2(p, stride, locX, locY);
+    float2 p1 = getElementAt2(p, stride, nextX, locY);
+    float2 p2 = getElementAt2(p, stride, locX, nextY);
+    float2 p3 = getElementAt2(p, stride, nextX, nextY);
+    float2 r = p0 * w0 + p1 * w1 + p2 * w2 + p3 * w3;
+    r *= (1.f / 255.f);
+    float4 ret = {r.x, r.x, r.x, r.y};
+    return ret;
+}
+static float4 __attribute__((overloadable))
+            getSample_RGB(const uint8_t *p, size_t stride,
+                         int locX, int locY, int nextX, int nextY,
+                         float w0, float w1, float w2, float w3) {
+    float4 p0 = getElementAt4(p, stride, locX, locY);
+    float4 p1 = getElementAt4(p, stride, nextX, locY);
+    float4 p2 = getElementAt4(p, stride, locX, nextY);
+    float4 p3 = getElementAt4(p, stride, nextX, nextY);
+    float4 r = p0 * w0 + p1 * w1 + p2 * w2 + p3 * w3;
+    r *= (1.f / 255.f);
+    float4 ret = {r.x, r.y, r.z, 1.f};
+    return ret;
+}
+static float4 __attribute__((overloadable))
+            getSample_RGBA(const uint8_t *p, size_t stride,
+                         int locX, int locY, int nextX, int nextY,
+                         float w0, float w1, float w2, float w3) {
+    float4 p0 = getElementAt4(p, stride, locX, locY);
+    float4 p1 = getElementAt4(p, stride, nextX, locY);
+    float4 p2 = getElementAt4(p, stride, locX, nextY);
+    float4 p3 = getElementAt4(p, stride, nextX, nextY);
+    float4 r = p0 * w0 + p1 * w1 + p2 * w2 + p3 * w3;
+    r *= (1.f / 255.f);
+    return r;
+}
+static float4 __attribute__((overloadable))
+            getSample_565(const uint8_t *p, size_t stride,
+                         int locX, int locY, int nextX, int nextY,
+                         float w0, float w1, float w2, float w3) {
+    float3 p0 = getElementAt565(p, stride, locX, locY);
+    float3 p1 = getElementAt565(p, stride, nextX, locY);
+    float3 p2 = getElementAt565(p, stride, locX, nextY);
+    float3 p3 = getElementAt565(p, stride, nextX, nextY);
+    float3 r = p0 * w0 + p1 * w1 + p2 * w2 + p3 * w3;
+    r *= (1.f / 255.f);
+    float4 ret;
+    ret.rgb = r;
+    ret.w = 1.f;
+    return ret;
+}
+
+static float4 __attribute__((overloadable))
+        getBilinearSample1D(const Allocation_t *alloc, float2 weights,
+                          uint32_t iPixel, uint32_t next,
+                          rs_data_kind dk, rs_data_type dt, uint32_t lod) {
+
+     const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[lod].mallocPtr;
+
+     switch(dk) {
+     case RS_KIND_PIXEL_RGBA:
+         return getSample_RGBA(p, iPixel, next, weights.x, weights.y);
+     case RS_KIND_PIXEL_A:
+         return getSample_A(p, iPixel, next, weights.x, weights.y);
+     case RS_KIND_PIXEL_RGB:
+         if (dt == RS_TYPE_UNSIGNED_5_6_5) {
+             return getSample_565(p, iPixel, next, weights.x, weights.y);
+         }
+         return getSample_RGB(p, iPixel, next, weights.x, weights.y);
+     case RS_KIND_PIXEL_L:
+         return getSample_L(p, iPixel, next, weights.x, weights.y);
+     case RS_KIND_PIXEL_LA:
+         return getSample_LA(p, iPixel, next, weights.x, weights.y);
+
+     default:
+         //__builtin_unreachable();
+         break;
+     }
+
+     //__builtin_unreachable();
+     return 0.f;
+}
+
+static uint32_t wrapI(rs_sampler_value wrap, int32_t coord, int32_t size) {
+    if (wrap == RS_SAMPLER_WRAP) {
+        coord = coord % size;
+        if (coord < 0) {
+            coord += size;
+        }
+    }
+    if (wrap == RS_SAMPLER_MIRRORED_REPEAT) {
+        coord = coord % (size * 2);
+        if (coord < 0) {
+            coord = (size * 2) + coord;
+        }
+        if (coord >= size) {
+            coord = (size * 2) - coord;
+        }
+    }
+    return (uint32_t)max(0, min(coord, size - 1));
+}
+
+static float4 __attribute__((overloadable))
+        getBilinearSample2D(const Allocation_t *alloc, float w0, float w1, float w2, float w3,
+                          int lx, int ly, int nx, int ny,
+                          rs_data_kind dk, rs_data_type dt, uint32_t lod) {
+
+    const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[lod].mallocPtr;
+    size_t stride = alloc->mHal.drvState.lod[lod].stride;
+
+    switch(dk) {
+    case RS_KIND_PIXEL_RGBA:
+        return getSample_RGBA(p, stride, lx, ly, nx, ny, w0, w1, w2, w3);
+    case RS_KIND_PIXEL_A:
+        return getSample_A(p, stride, lx, ly, nx, ny, w0, w1, w2, w3);
+    case RS_KIND_PIXEL_LA:
+        return getSample_LA(p, stride, lx, ly, nx, ny, w0, w1, w2, w3);
+    case RS_KIND_PIXEL_RGB:
+        if (dt == RS_TYPE_UNSIGNED_5_6_5) {
+            return getSample_565(p, stride, lx, ly, nx, ny, w0, w1, w2, w3);
+        }
+        return getSample_RGB(p, stride, lx, ly, nx, ny, w0, w1, w2, w3);
+    case RS_KIND_PIXEL_L:
+        return getSample_L(p, stride, lx, ly, nx, ny, w0, w1, w2, w3);
+
+    default:
+        break;
+    }
+
+    return 0.f;
+}
+
+static float4  __attribute__((overloadable))
+        getNearestSample(const Allocation_t *alloc, uint32_t iPixel, rs_data_kind dk,
+                         rs_data_type dt, uint32_t lod) {
+
+    const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[lod].mallocPtr;
+
+    float4 result = {0.f, 0.f, 0.f, 255.f};
+
+    switch(dk) {
+    case RS_KIND_PIXEL_RGBA:
+        result = getElementAt4(p, iPixel);
+        break;
+    case RS_KIND_PIXEL_A:
+        result.w = getElementAt1(p, iPixel);
+        break;
+    case RS_KIND_PIXEL_LA:
+        result.zw = getElementAt2(p, iPixel);
+        result.xy = result.z;
+        break;
+    case RS_KIND_PIXEL_RGB:
+        if (dt == RS_TYPE_UNSIGNED_5_6_5) {
+            result.xyz = getElementAt565(p, iPixel);
+        } else {
+            result.xyz = getElementAt3(p, iPixel);
+        }
+        break;
+    case RS_KIND_PIXEL_L:
+        result.xyz = getElementAt1(p, iPixel);
+
+    default:
+        //__builtin_unreachable();
+        break;
+    }
+
+    return result * 0.003921569f;
+}
+
+static float4  __attribute__((overloadable))
+        getNearestSample(const Allocation_t *alloc, uint2 iPixel, rs_data_kind dk,
+                         rs_data_type dt, uint32_t lod) {
+
+    const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[lod].mallocPtr;
+    size_t stride = alloc->mHal.drvState.lod[lod].stride;
+
+    float4 result = {0.f, 0.f, 0.f, 255.f};
+
+    switch(dk) {
+    case RS_KIND_PIXEL_RGBA:
+        result = getElementAt4(p, stride, iPixel.x, iPixel.y);
+        break;
+    case RS_KIND_PIXEL_A:
+        result.w = getElementAt1(p, stride, iPixel.x, iPixel.y);
+        break;
+    case RS_KIND_PIXEL_LA:
+        result.zw = getElementAt2(p, stride, iPixel.x, iPixel.y);
+        result.xy = result.z;
+        break;
+    case RS_KIND_PIXEL_RGB:
+        if (dt == RS_TYPE_UNSIGNED_5_6_5) {
+            result.xyz = getElementAt565(p, stride, iPixel.x, iPixel.y);
+        } else {
+            result.xyz = getElementAt3(p, stride, iPixel.x, iPixel.y);
+        }
+        break;
+
+    default:
+        //__builtin_unreachable();
+        break;
+    }
+
+    return result * 0.003921569f;
+}
+
+static float4 __attribute__((overloadable))
+        sample_LOD_LinearPixel(const Allocation_t *alloc,
+                               rs_data_kind dk, rs_data_type dt,
+                               rs_sampler_value wrapS,
+                               float uv, uint32_t lod) {
+
+    const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[lod].mallocPtr;
+
+    int32_t sourceW = alloc->mHal.drvState.lod[lod].dimX;
+    float pixelUV = uv * (float)(sourceW);
+    int32_t iPixel = (int32_t)(pixelUV);
+    float frac = pixelUV - (float)iPixel;
+
+    if (frac < 0.5f) {
+        iPixel -= 1;
+        frac += 0.5f;
+    } else {
+        frac -= 0.5f;
+    }
+
+    float oneMinusFrac = 1.0f - frac;
+
+    float2 weights;
+    weights.x = oneMinusFrac;
+    weights.y = frac;
+
+    uint32_t next = wrapI(wrapS, iPixel + 1, sourceW);
+    uint32_t location = wrapI(wrapS, iPixel, sourceW);
+
+    return getBilinearSample1D(alloc, weights, location, next, dk, dt, lod);
+}
+
+static float4 __attribute__((overloadable))
+        sample_LOD_NearestPixel(const Allocation_t *alloc,
+                                rs_data_kind dk, rs_data_type dt,
+                                rs_sampler_value wrapS,
+                                float uv, uint32_t lod) {
+
+    int32_t sourceW = alloc->mHal.drvState.lod[lod].dimX;
+    int32_t iPixel = (int32_t)(uv * (float)(sourceW));
+    uint32_t location = wrapI(wrapS, iPixel, sourceW);
+
+    return getNearestSample(alloc, location, dk, dt, lod);
+}
+
+static float4 __attribute__((overloadable))
+        sample_LOD_LinearPixel(const Allocation_t *alloc,
+                               rs_data_kind dk, rs_data_type dt,
+                               rs_sampler_value wrapS,
+                               rs_sampler_value wrapT,
+                               float2 uv, uint32_t lod) {
+
+    const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[lod].mallocPtr;
+
+    int sourceW = alloc->mHal.drvState.lod[lod].dimX;
+    int sourceH = alloc->mHal.drvState.lod[lod].dimY;
+
+    float pixelU = uv.x * sourceW;
+    float pixelV = uv.y * sourceH;
+    int iPixelU = pixelU;
+    int iPixelV = pixelV;
+    float fracU = pixelU - iPixelU;
+    float fracV = pixelV - iPixelV;
+
+    if (fracU < 0.5f) {
+        iPixelU -= 1;
+        fracU += 0.5f;
+    } else {
+        fracU -= 0.5f;
+    }
+    if (fracV < 0.5f) {
+        iPixelV -= 1;
+        fracV += 0.5f;
+    } else {
+        fracV -= 0.5f;
+    }
+    float oneMinusFracU = 1.0f - fracU;
+    float oneMinusFracV = 1.0f - fracV;
+
+    float w0 = oneMinusFracU * oneMinusFracV;
+    float w1 = fracU * oneMinusFracV;
+    float w2 = oneMinusFracU * fracV;
+    float w3 = fracU * fracV;
+
+    int nx = wrapI(wrapS, iPixelU + 1, sourceW);
+    int ny = wrapI(wrapT, iPixelV + 1, sourceH);
+    int lx = wrapI(wrapS, iPixelU, sourceW);
+    int ly = wrapI(wrapT, iPixelV, sourceH);
+
+    return getBilinearSample2D(alloc, w0, w1, w2, w3, lx, ly, nx, ny, dk, dt, lod);
+
+}
+
+static float4 __attribute__((overloadable))
+        sample_LOD_NearestPixel(const Allocation_t *alloc,
+                                rs_data_kind dk, rs_data_type dt,
+                                rs_sampler_value wrapS,
+                                rs_sampler_value wrapT,
+                                float2 uv, uint32_t lod) {
+    int sourceW = alloc->mHal.drvState.lod[lod].dimX;
+    int sourceH = alloc->mHal.drvState.lod[lod].dimY;
+
+    float2 dimF;
+    dimF.x = (float)(sourceW);
+    dimF.y = (float)(sourceH);
+    int2 iPixel = convert_int2(uv * dimF);
+
+    uint2 location;
+    location.x = wrapI(wrapS, iPixel.x, sourceW);
+    location.y = wrapI(wrapT, iPixel.y, sourceH);
+    return getNearestSample(alloc, location, dk, dt, lod);
+}
+
+extern const float4 __attribute__((overloadable))
+        rsSample(rs_allocation a, rs_sampler s, float uv, float lod) {
+
+    const Allocation_t *alloc = (const Allocation_t *)a.p;
+    const Sampler_t *prog = (Sampler_t *)s.p;
+    const Type_t *type = (Type_t *)alloc->mHal.state.type;
+    const Element_t *elem = type->mHal.state.element;
+    rs_data_kind dk = elem->mHal.state.dataKind;
+    rs_data_type dt = elem->mHal.state.dataType;
+    rs_sampler_value sampleMin = prog->mHal.state.minFilter;
+    rs_sampler_value sampleMag = prog->mHal.state.magFilter;
+    rs_sampler_value wrapS = prog->mHal.state.wrapS;
+
+    if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE)) {
+        return 0.f;
+    }
+
+    if (lod <= 0.0f) {
+        if (sampleMag == RS_SAMPLER_NEAREST) {
+            return sample_LOD_NearestPixel(alloc, dk, dt, wrapS, uv, 0);
+        }
+        return sample_LOD_LinearPixel(alloc, dk, dt, wrapS, uv, 0);
+    }
+
+    if (sampleMin == RS_SAMPLER_LINEAR_MIP_NEAREST) {
+        uint32_t maxLOD = type->mHal.state.lodCount - 1;
+        lod = min(lod, (float)maxLOD);
+        uint32_t nearestLOD = (uint32_t)round(lod);
+        return sample_LOD_LinearPixel(alloc, dk, dt, wrapS, uv, nearestLOD);
+    }
+
+    if (sampleMin == RS_SAMPLER_LINEAR_MIP_LINEAR) {
+        uint32_t lod0 = (uint32_t)floor(lod);
+        uint32_t lod1 = (uint32_t)ceil(lod);
+        uint32_t maxLOD = type->mHal.state.lodCount - 1;
+        lod0 = min(lod0, maxLOD);
+        lod1 = min(lod1, maxLOD);
+        float4 sample0 = sample_LOD_LinearPixel(alloc, dk, dt, wrapS, uv, lod0);
+        float4 sample1 = sample_LOD_LinearPixel(alloc, dk, dt, wrapS, uv, lod1);
+        float frac = lod - (float)lod0;
+        return sample0 * (1.0f - frac) + sample1 * frac;
+    }
+
+    return sample_LOD_NearestPixel(alloc, dk, dt, wrapS, uv, 0);
+}
+
+extern const float4 __attribute__((overloadable))
+        rsSample(rs_allocation a, rs_sampler s, float location) {
+    return rsSample(a, s, location, 0);
+}
+
+
+extern const float4 __attribute__((overloadable))
+        rsSample(rs_allocation a, rs_sampler s, float2 uv, float lod) {
+
+    const Allocation_t *alloc = (const Allocation_t *)a.p;
+    const Sampler_t *prog = (Sampler_t *)s.p;
+    const Type_t *type = (Type_t *)alloc->mHal.state.type;
+    const Element_t *elem = type->mHal.state.element;
+    rs_data_kind dk = elem->mHal.state.dataKind;
+    rs_data_type dt = elem->mHal.state.dataType;
+    rs_sampler_value sampleMin = prog->mHal.state.minFilter;
+    rs_sampler_value sampleMag = prog->mHal.state.magFilter;
+    rs_sampler_value wrapS = prog->mHal.state.wrapS;
+    rs_sampler_value wrapT = prog->mHal.state.wrapT;
+
+    if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE)) {
+        return 0.f;
+    }
+
+    if (lod <= 0.0f) {
+        if (sampleMag == RS_SAMPLER_NEAREST) {
+            return sample_LOD_NearestPixel(alloc, dk, dt, wrapS, wrapT, uv, 0);
+        }
+        return sample_LOD_LinearPixel(alloc, dk, dt, wrapS, wrapT, uv, 0);
+    }
+
+    if (sampleMin == RS_SAMPLER_LINEAR_MIP_NEAREST) {
+        uint32_t maxLOD = type->mHal.state.lodCount - 1;
+        lod = min(lod, (float)maxLOD);
+        uint32_t nearestLOD = (uint32_t)round(lod);
+        return sample_LOD_LinearPixel(alloc, dk, dt, wrapS, wrapT, uv, nearestLOD);
+    }
+
+    if (sampleMin == RS_SAMPLER_LINEAR_MIP_LINEAR) {
+        uint32_t lod0 = (uint32_t)floor(lod);
+        uint32_t lod1 = (uint32_t)ceil(lod);
+        uint32_t maxLOD = type->mHal.state.lodCount - 1;
+        lod0 = min(lod0, maxLOD);
+        lod1 = min(lod1, maxLOD);
+        float4 sample0 = sample_LOD_LinearPixel(alloc, dk, dt, wrapS, wrapT, uv, lod0);
+        float4 sample1 = sample_LOD_LinearPixel(alloc, dk, dt, wrapS, wrapT, uv, lod1);
+        float frac = lod - (float)lod0;
+        return sample0 * (1.0f - frac) + sample1 * frac;
+    }
+
+    return sample_LOD_NearestPixel(alloc, dk, dt, wrapS, wrapT, uv, 0);
+}
+
+extern const float4 __attribute__((overloadable))
+        rsSample(rs_allocation a, rs_sampler s, float2 uv) {
+
+    const Allocation_t *alloc = (const Allocation_t *)a.p;
+    const Sampler_t *prog = (Sampler_t *)s.p;
+    const Type_t *type = (Type_t *)alloc->mHal.state.type;
+    const Element_t *elem = type->mHal.state.element;
+    rs_data_kind dk = elem->mHal.state.dataKind;
+    rs_data_type dt = elem->mHal.state.dataType;
+    rs_sampler_value wrapS = prog->mHal.state.wrapS;
+    rs_sampler_value wrapT = prog->mHal.state.wrapT;
+
+    if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE)) {
+        return 0.f;
+    }
+
+    if (prog->mHal.state.magFilter == RS_SAMPLER_NEAREST) {
+        return sample_LOD_NearestPixel(alloc, dk, dt, wrapS, wrapT, uv, 0);
+    }
+    return sample_LOD_LinearPixel(alloc, dk, dt, wrapS, wrapT, uv, 0);
+}
+
diff --git a/driver/runtime/rs_sampler.c b/driver/runtime/rs_sampler.c
new file mode 100644
index 0000000..39782de
--- /dev/null
+++ b/driver/runtime/rs_sampler.c
@@ -0,0 +1,51 @@
+#include "rs_core.rsh"
+#include "rs_graphics.rsh"
+#include "rs_structs.h"
+
+/**
+* Sampler
+*/
+extern rs_sampler_value __attribute__((overloadable))
+        rsSamplerGetMinification(rs_sampler s) {
+    Sampler_t *prog = (Sampler_t *)s.p;
+    if (prog == NULL) {
+        return RS_SAMPLER_INVALID;
+    }
+    return prog->mHal.state.minFilter;
+}
+
+extern rs_sampler_value __attribute__((overloadable))
+        rsSamplerGetMagnification(rs_sampler s) {
+    Sampler_t *prog = (Sampler_t *)s.p;
+    if (prog == NULL) {
+        return RS_SAMPLER_INVALID;
+    }
+    return prog->mHal.state.magFilter;
+}
+
+extern rs_sampler_value __attribute__((overloadable))
+        rsSamplerGetWrapS(rs_sampler s) {
+    Sampler_t *prog = (Sampler_t *)s.p;
+    if (prog == NULL) {
+        return RS_SAMPLER_INVALID;
+    }
+    return prog->mHal.state.wrapS;
+}
+
+extern rs_sampler_value __attribute__((overloadable))
+        rsSamplerGetWrapT(rs_sampler s) {
+    Sampler_t *prog = (Sampler_t *)s.p;
+    if (prog == NULL) {
+        return RS_SAMPLER_INVALID;
+    }
+    return prog->mHal.state.wrapT;
+}
+
+extern float __attribute__((overloadable))
+        rsSamplerGetAnisotropy(rs_sampler s) {
+    Sampler_t *prog = (Sampler_t *)s.p;
+    if (prog == NULL) {
+        return 0.0f;
+    }
+    return prog->mHal.state.aniso;
+}
diff --git a/driver/runtime/rs_structs.h b/driver/runtime/rs_structs.h
new file mode 100644
index 0000000..204717c
--- /dev/null
+++ b/driver/runtime/rs_structs.h
@@ -0,0 +1,262 @@
+#ifndef _RS_STRUCTS_H_
+#define _RS_STRUCTS_H_
+
+/*****************************************************************************
+ * CAUTION
+ *
+ * The following structure layout provides a more efficient way to access
+ * internal members of the C++ class Allocation owned by librs. Unfortunately,
+ * since this class has virtual members, we can't simply use offsetof() or any
+ * other compiler trickery to dynamically get the appropriate values at
+ * build-time. This layout may need to be updated whenever
+ * frameworks/base/libs/rs/rsAllocation.h is modified.
+ *
+ * Having the layout information available in this file allows us to
+ * accelerate functionality like rsAllocationGetDimX(). Without this
+ * information, we would not be able to inline the bitcode, thus resulting in
+ * potential runtime performance penalties for tight loops operating on
+ * allocations.
+ *
+ *****************************************************************************/
+typedef enum {
+    RS_ALLOCATION_MIPMAP_NONE = 0,
+    RS_ALLOCATION_MIPMAP_FULL = 1,
+    RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE = 2
+} rs_allocation_mipmap_control;
+
+typedef struct Allocation {
+    char __pad[32];
+    struct {
+        void * drv;
+        struct {
+            const void *type;
+            uint32_t usageFlags;
+            rs_allocation_mipmap_control mipmapControl;
+            uint32_t yuv;
+            uint32_t elementSizeBytes;
+            bool hasMipmaps;
+            bool hasFaces;
+            bool hasReferences;
+            void * usrPtr;
+            int32_t surfaceTextureID;
+            void * nativeBuffer;
+            int64_t timestamp;
+        } state;
+
+        struct DrvState {
+            struct LodState {
+                void * mallocPtr;
+                size_t stride;
+                uint32_t dimX;
+                uint32_t dimY;
+                uint32_t dimZ;
+            } lod[16/*android::renderscript::Allocation::MAX_LOD*/];
+            size_t faceOffset;
+            uint32_t lodCount;
+            uint32_t faceCount;
+        } drvState;
+    } mHal;
+} Allocation_t;
+
+/*****************************************************************************
+ * CAUTION
+ *
+ * The following structure layout provides a more efficient way to access
+ * internal members of the C++ class ProgramStore owned by librs. Unfortunately,
+ * since this class has virtual members, we can't simply use offsetof() or any
+ * other compiler trickery to dynamically get the appropriate values at
+ * build-time. This layout may need to be updated whenever
+ * frameworks/base/libs/rs/rsProgramStore.h is modified.
+ *
+ * Having the layout information available in this file allows us to
+ * accelerate functionality like rsgProgramStoreGetDepthFunc(). Without this
+ * information, we would not be able to inline the bitcode, thus resulting in
+ * potential runtime performance penalties for tight loops operating on
+ * program store.
+ *
+ *****************************************************************************/
+typedef struct ProgramStore {
+    char __pad[40];
+    struct {
+        struct {
+            bool ditherEnable;
+            bool colorRWriteEnable;
+            bool colorGWriteEnable;
+            bool colorBWriteEnable;
+            bool colorAWriteEnable;
+            rs_blend_src_func blendSrc;
+            rs_blend_dst_func blendDst;
+            bool depthWriteEnable;
+            rs_depth_func depthFunc;
+        } state;
+    } mHal;
+} ProgramStore_t;
+
+/*****************************************************************************
+ * CAUTION
+ *
+ * The following structure layout provides a more efficient way to access
+ * internal members of the C++ class ProgramRaster owned by librs. Unfortunately,
+ * since this class has virtual members, we can't simply use offsetof() or any
+ * other compiler trickery to dynamically get the appropriate values at
+ * build-time. This layout may need to be updated whenever
+ * frameworks/base/libs/rs/rsProgramRaster.h is modified.
+ *
+ * Having the layout information available in this file allows us to
+ * accelerate functionality like rsgProgramRasterGetCullMode(). Without this
+ * information, we would not be able to inline the bitcode, thus resulting in
+ * potential runtime performance penalties for tight loops operating on
+ * program raster.
+ *
+ *****************************************************************************/
+typedef struct ProgramRaster {
+    char __pad[36];
+    struct {
+        void * drv;
+        struct {
+            bool pointSprite;
+            rs_cull_mode cull;
+        } state;
+    } mHal;
+} ProgramRaster_t;
+
+/*****************************************************************************
+ * CAUTION
+ *
+ * The following structure layout provides a more efficient way to access
+ * internal members of the C++ class Sampler owned by librs. Unfortunately,
+ * since this class has virtual members, we can't simply use offsetof() or any
+ * other compiler trickery to dynamically get the appropriate values at
+ * build-time. This layout may need to be updated whenever
+ * frameworks/base/libs/rs/rsSampler.h is modified.
+ *
+ * Having the layout information available in this file allows us to
+ * accelerate functionality like rsgProgramRasterGetMagFilter(). Without this
+ * information, we would not be able to inline the bitcode, thus resulting in
+ * potential runtime performance penalties for tight loops operating on
+ * samplers.
+ *
+ *****************************************************************************/
+typedef struct Sampler {
+    char __pad[32];
+    struct {
+        void *drv;
+        struct {
+            rs_sampler_value magFilter;
+            rs_sampler_value minFilter;
+            rs_sampler_value wrapS;
+            rs_sampler_value wrapT;
+            rs_sampler_value wrapR;
+            float aniso;
+        } state;
+    } mHal;
+} Sampler_t;
+
+/*****************************************************************************
+ * CAUTION
+ *
+ * The following structure layout provides a more efficient way to access
+ * internal members of the C++ class Element owned by librs. Unfortunately,
+ * since this class has virtual members, we can't simply use offsetof() or any
+ * other compiler trickery to dynamically get the appropriate values at
+ * build-time. This layout may need to be updated whenever
+ * frameworks/base/libs/rs/rsElement.h is modified.
+ *
+ * Having the layout information available in this file allows us to
+ * accelerate functionality like rsElementGetSubElementCount(). Without this
+ * information, we would not be able to inline the bitcode, thus resulting in
+ * potential runtime performance penalties for tight loops operating on
+ * elements.
+ *
+ *****************************************************************************/
+typedef struct Element {
+    char __pad[32];
+    struct {
+        void *drv;
+        struct {
+            rs_data_type dataType;
+            rs_data_kind dataKind;
+            uint32_t vectorSize;
+            uint32_t elementSizeBytes;
+
+            // Subelements
+            const void **fields;
+            uint32_t *fieldArraySizes;
+            const char **fieldNames;
+            uint32_t *fieldNameLengths;
+            uint32_t *fieldOffsetBytes;
+            uint32_t fieldsCount;
+        } state;
+    } mHal;
+} Element_t;
+
+/*****************************************************************************
+ * CAUTION
+ *
+ * The following structure layout provides a more efficient way to access
+ * internal members of the C++ class Type owned by librs. Unfortunately,
+ * since this class has virtual members, we can't simply use offsetof() or any
+ * other compiler trickery to dynamically get the appropriate values at
+ * build-time. This layout may need to be updated whenever
+ * frameworks/base/libs/rs/rsType.h is modified.
+ *
+ * Having the layout information available in this file allows us to
+ * accelerate functionality like rsAllocationGetElement(). Without this
+ * information, we would not be able to inline the bitcode, thus resulting in
+ * potential runtime performance penalties for tight loops operating on
+ * types.
+ *
+ *****************************************************************************/
+typedef struct Type {
+    char __pad[32];
+    struct {
+        void *drv;
+        struct {
+            const void * element;
+            uint32_t dimX;
+            uint32_t dimY;
+            uint32_t dimZ;
+            uint32_t *lodDimX;
+            uint32_t *lodDimY;
+            uint32_t *lodDimZ;
+            uint32_t *lodOffset;
+            uint32_t lodCount;
+            bool faces;
+        } state;
+    } mHal;
+} Type_t;
+
+/*****************************************************************************
+ * CAUTION
+ *
+ * The following structure layout provides a more efficient way to access
+ * internal members of the C++ class Mesh owned by librs. Unfortunately,
+ * since this class has virtual members, we can't simply use offsetof() or any
+ * other compiler trickery to dynamically get the appropriate values at
+ * build-time. This layout may need to be updated whenever
+ * frameworks/base/libs/rs/rsMesh.h is modified.
+ *
+ * Having the layout information available in this file allows us to
+ * accelerate functionality like rsMeshGetVertexAllocationCount(). Without this
+ * information, we would not be able to inline the bitcode, thus resulting in
+ * potential runtime performance penalties for tight loops operating on
+ * meshes.
+ *
+ *****************************************************************************/
+typedef struct Mesh {
+    char __pad[32];
+    struct {
+        void *drv;
+        struct {
+            void **vertexBuffers;
+            uint32_t vertexBuffersCount;
+
+            // indexBuffers[i] could be NULL, in which case only primitives[i] is used
+            void **indexBuffers;
+            uint32_t indexBuffersCount;
+            rs_primitive *primitives;
+            uint32_t primitivesCount;
+        } state;
+    } mHal;
+} Mesh_t;
+#endif // _RS_CORE_H_
diff --git a/driver/linkloader/tests/images/clean-testcases.sh b/java/tests/GenImages/Android.mk
old mode 100755
new mode 100644
similarity index 63%
rename from driver/linkloader/tests/images/clean-testcases.sh
rename to java/tests/GenImages/Android.mk
index 1c2d0be..69ef52c
--- a/driver/linkloader/tests/images/clean-testcases.sh
+++ b/java/tests/GenImages/Android.mk
@@ -1,7 +1,5 @@
-#!/bin/bash -e
-
-
-# Copyright (C) 2011 The Android Open Source Project
+#
+# Copyright (C) 2013 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -14,5 +12,16 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
+#
 
-rm -rf *.o *.bc
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := tests
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src) \
+                   $(call all-renderscript-files-under, src)
+
+LOCAL_PACKAGE_NAME := RsGenImages
+
+include $(BUILD_PACKAGE)
diff --git a/java/tests/GenImages/AndroidManifest.xml b/java/tests/GenImages/AndroidManifest.xml
new file mode 100644
index 0000000..c7b690e
--- /dev/null
+++ b/java/tests/GenImages/AndroidManifest.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.android.rs.genimage"
+    android:versionCode="1"
+    android:versionName="1.0" >
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+
+    <uses-sdk android:minSdkVersion="14" />
+    <!-- Tell the system this app requires OpenGL ES 2.0. -->
+    <uses-feature android:glEsVersion="0x00020000" android:required="true" />
+
+    <application
+        android:icon="@drawable/ic_launcher"
+        android:label="@string/app_name" >
+        <activity
+            android:name="com.android.rs.genimage.GenImageAct"
+            android:label="@string/app_name" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+
+</manifest>
diff --git a/java/tests/GenImages/res/drawable-nodpi/test_pattern.png b/java/tests/GenImages/res/drawable-nodpi/test_pattern.png
new file mode 100644
index 0000000..e7d1455
--- /dev/null
+++ b/java/tests/GenImages/res/drawable-nodpi/test_pattern.png
Binary files differ
diff --git a/java/tests/GenImages/res/drawable/ic_launcher.png b/java/tests/GenImages/res/drawable/ic_launcher.png
new file mode 100644
index 0000000..359047d
--- /dev/null
+++ b/java/tests/GenImages/res/drawable/ic_launcher.png
Binary files differ
diff --git a/java/tests/GenImages/res/layout/main.xml b/java/tests/GenImages/res/layout/main.xml
new file mode 100644
index 0000000..b8db6d8
--- /dev/null
+++ b/java/tests/GenImages/res/layout/main.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="fill_parent"
+    android:layout_height="fill_parent"
+    android:orientation="vertical" >
+
+    <TextView
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content"
+        android:text="@string/hello" />
+
+</LinearLayout>
diff --git a/java/tests/GenImages/res/values/strings.xml b/java/tests/GenImages/res/values/strings.xml
new file mode 100644
index 0000000..a0cc805
--- /dev/null
+++ b/java/tests/GenImages/res/values/strings.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<resources>
+
+    <string name="hello">Hello</string>
+    <string name="app_name">RS Image Generator</string>
+
+</resources>
diff --git a/java/tests/GenImages/src/com/android/rs/genimage/GenImage.java b/java/tests/GenImages/src/com/android/rs/genimage/GenImage.java
new file mode 100644
index 0000000..f238095
--- /dev/null
+++ b/java/tests/GenImages/src/com/android/rs/genimage/GenImage.java
@@ -0,0 +1,271 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.rs.genimage;
+
+import android.content.Context;
+
+import java.io.FileOutputStream;
+import java.nio.ByteBuffer;
+import java.nio.IntBuffer;
+import java.nio.ByteOrder;
+import java.nio.FloatBuffer;
+
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+
+import javax.microedition.khronos.egl.EGLConfig;
+import javax.microedition.khronos.opengles.GL10;
+import android.opengl.GLUtils;
+
+import android.opengl.GLES20;
+import android.opengl.GLSurfaceView;
+
+public class GenImage implements GLSurfaceView.Renderer {
+    private Bitmap mTestImage;
+
+    private Triangle mTriangle;
+
+
+    private Bitmap loadBitmap(Context context, int resource) {
+        final BitmapFactory.Options options = new BitmapFactory.Options();
+        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
+        return BitmapFactory.decodeResource(context.getResources(), resource, options);
+    }
+
+    GenImage(Context context) {
+
+        mTestImage = loadBitmap(context, R.drawable.test_pattern);
+
+    }
+
+    @Override
+    public void onSurfaceCreated(GL10 unused, EGLConfig config) {
+
+        // Set the background frame color
+        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+
+        mTriangle = new Triangle(mTestImage);
+    }
+
+    @Override
+    public void onDrawFrame(GL10 unused) {
+
+        // Draw background color
+        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
+
+        // Draw triangle
+        mTriangle.draw();
+    }
+
+    @Override
+    public void onSurfaceChanged(GL10 unused, int width, int height) {
+        // Adjust the viewport based on geometry changes,
+        // such as screen rotation
+        GLES20.glViewport(0, 0, 512, 512);
+    }
+
+    public static int loadShader(int type, String shaderCode){
+
+        // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
+        // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
+        int shader = GLES20.glCreateShader(type);
+
+        // add the source code to the shader and compile it
+        GLES20.glShaderSource(shader, shaderCode);
+        GLES20.glCompileShader(shader);
+
+        return shader;
+    }
+
+}
+
+
+
+class Triangle {
+    int mTextureIDs[] = new int[1];
+
+    private final String vertexShaderCode =
+        "varying vec2 vTex0;" +
+        "varying vec2 vPos0;" +
+        "attribute vec4 aPosition;" +
+        "void main() {" +
+        "  gl_Position = aPosition;" +
+        "  vPos0 = aPosition.xy;" +
+        "  vTex0 = ((aPosition.xy + 1.0) * 0.6);" +
+        //"  vTex0 = (aPosition.xy * 1.7) + 0.5;" +
+        "}";
+
+    private final String fragmentShaderCode =
+        "precision mediump float;" +
+        "varying vec2 vTex0;" +
+        "varying vec2 vPos0;" +
+        "uniform sampler2D uSamp;" +
+        "void main() {" +
+        "  vec2 tc = vTex0;" +
+        //"  tc.x *= pow(vPos0.y + 1.0, 2.0);" +
+        //"  tc.y *= pow(vPos0.x + 1.0, 2.0);" +
+        "  vec4 c = texture2D(uSamp, tc);" +
+        "  c.a = 1.0;" +
+        "  gl_FragColor = c;" +
+        "}";
+
+    private final FloatBuffer vertexBuffer;
+    private final int mProgram;
+
+    // number of coordinates per vertex in this array
+    static float triangleCoords[] = { // in counterclockwise order:
+       -1.0f,  1.0f, 0.0f,   // top left
+       -1.0f, -1.0f, 0.0f,   // bottom left
+        1.0f, -1.0f, 0.0f,   // bottom right
+
+       -1.0f,  1.0f, 0.0f,   // top left
+        1.0f, -1.0f, 0.0f,   // bottom right
+        1.0f,  1.0f, 0.0f    // top right
+    };
+
+    FloatBuffer createFloatBuffer(float buf[]) {
+        ByteBuffer bb = ByteBuffer.allocateDirect(buf.length * 4);
+        bb.order(ByteOrder.nativeOrder());
+        FloatBuffer fb = bb.asFloatBuffer();
+        fb.put(buf);
+        fb.position(0);
+        return fb;
+    }
+
+    public String setup(int key) {
+        String s = new String();
+        int tmp;
+
+        tmp = key % 2;
+        key /= 2;
+        if (tmp != 0) {
+            s += "N";
+            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
+        } else {
+            s += "L";
+            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
+        }
+
+        tmp = key % 2;
+        key /= 2;
+        if (tmp != 0) {
+            s += "N";
+            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
+        } else {
+            s += "L";
+            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
+        }
+
+        tmp = key % 3;
+        key /= 3;
+        switch(tmp) {
+        case 0:
+            s += "_CE";
+            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
+            break;
+        case 1:
+            s += "_RE";
+            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
+            break;
+        case 2:
+            s += "_MR";
+            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_MIRRORED_REPEAT);
+            break;
+        }
+
+        tmp = key % 3;
+        key /= 3;
+        switch(tmp) {
+        case 0:
+            s += "_CE";
+            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
+            break;
+        case 1:
+            s += "_RE";
+            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
+            break;
+        case 2:
+            s += "_MR";
+            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_MIRRORED_REPEAT);
+            break;
+        }
+
+        if (key > 0) done = true;
+        return s;
+    }
+
+    public Triangle(Bitmap testImage) {
+        vertexBuffer = createFloatBuffer(triangleCoords);
+
+        // prepare shaders and OpenGL program
+        int vertexShader = GenImage.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
+        int fragmentShader = GenImage.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
+
+        mProgram = GLES20.glCreateProgram();             // create empty OpenGL Program
+        GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
+        GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
+        GLES20.glLinkProgram(mProgram);                  // create OpenGL program executables
+
+        GLES20.glGenTextures(1, mTextureIDs, 0);
+
+        // Bind to the texture in OpenGL
+        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureIDs[0]);
+        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, testImage, 0);
+    }
+
+    boolean done = false;
+    int key = 0;
+
+    public void draw() {
+        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
+        String ext = setup(key++);
+
+        // Add program to OpenGL environment
+        GLES20.glUseProgram(mProgram);
+
+        int posA = GLES20.glGetAttribLocation(mProgram, "aPosition");
+        GLES20.glEnableVertexAttribArray(posA);
+        GLES20.glVertexAttribPointer(posA, 3, GLES20.GL_FLOAT, false, 12, vertexBuffer);
+
+        int sampUni = GLES20.glGetUniformLocation(mProgram, "uSamp");
+        GLES20.glUniform1i(sampUni, 0);
+
+        // Draw the triangle
+        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, triangleCoords.length / 3);
+
+        if (!done) {
+            IntBuffer ib = IntBuffer.allocate(512*512);
+            ib.position(0);
+            GLES20.glReadPixels(0,0, 512, 512, GLES20.GL_RGBA,
+                                GLES20.GL_UNSIGNED_BYTE, ib);
+
+            Bitmap bmp = Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888);
+            bmp.setPixels(ib.array(), 0, 512, 0, 0, 512, 512);
+
+            try {
+                String s = new String("/sdcard/imgs/RsSampImg_");
+                s += ext + ".png";
+                FileOutputStream out = new FileOutputStream(s);
+                bmp.compress(Bitmap.CompressFormat.PNG, 95, out);
+                out.close();
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+            bmp.recycle();
+        }
+    }
+}
diff --git a/java/tests/GenImages/src/com/android/rs/genimage/GenImageAct.java b/java/tests/GenImages/src/com/android/rs/genimage/GenImageAct.java
new file mode 100644
index 0000000..ea60d59
--- /dev/null
+++ b/java/tests/GenImages/src/com/android/rs/genimage/GenImageAct.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.rs.genimage;
+
+import android.app.Activity;
+import android.content.Context;
+import android.opengl.GLSurfaceView;
+import android.os.Bundle;
+
+public class GenImageAct extends Activity {
+
+    private GLSurfaceView mGLView;
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        // Create a GLSurfaceView instance and set it
+        // as the ContentView for this Activity
+        mGLView = new MyGLSurfaceView(this);
+        setContentView(mGLView);
+    }
+
+    @Override
+    protected void onPause() {
+        super.onPause();
+        // The following call pauses the rendering thread.
+        // If your OpenGL application is memory intensive,
+        // you should consider de-allocating objects that
+        // consume significant memory here.
+        mGLView.onPause();
+    }
+
+    @Override
+    protected void onResume() {
+        super.onResume();
+        // The following call resumes a paused rendering thread.
+        // If you de-allocated graphic objects for onPause()
+        // this is a good place to re-allocate them.
+        mGLView.onResume();
+    }
+}
+
+class MyGLSurfaceView extends GLSurfaceView {
+
+    public MyGLSurfaceView(Context context) {
+        super(context);
+
+        // Create an OpenGL ES 2.0 context.
+        setEGLContextClientVersion(2);
+
+        // Set the Renderer for drawing on the GLSurfaceView
+        setRenderer(new GenImage(context));
+
+        // Render the view only when there is a change in the drawing data
+        setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
+    }
+}
diff --git a/java/tests/ImageProcessing/src/com/android/rs/image/Artistic1.java b/java/tests/ImageProcessing/src/com/android/rs/image/Artistic1.java
new file mode 100644
index 0000000..f6288d3
--- /dev/null
+++ b/java/tests/ImageProcessing/src/com/android/rs/image/Artistic1.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.rs.image;
+
+import java.lang.Math;
+
+import android.renderscript.*;
+import android.util.Log;
+
+public class Artistic1 extends TestBase {
+    private ScriptC_artistic1 mScript;
+    private Allocation mBlured;
+
+    public void createTest(android.content.res.Resources res) {
+        mScript = new ScriptC_artistic1(mRS);
+        mBlured = Allocation.createTyped(mRS, mInPixelsAllocation.getType());
+        mScript.set_gBlur(mBlured);
+
+        ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(mRS, Element.U8_4(mRS));
+        blur.setRadius(20);
+        blur.setInput(mInPixelsAllocation);
+        blur.forEach(mBlured);
+    }
+
+    public void runTest() {
+        mScript.invoke_setup();
+        mScript.forEach_process(mInPixelsAllocation, mOutPixelsAllocation);
+    }
+
+}
diff --git a/java/tests/ImageProcessing/src/com/android/rs/image/Blend.java b/java/tests/ImageProcessing/src/com/android/rs/image/Blend.java
index 2303fc3..f385bdb 100644
--- a/java/tests/ImageProcessing/src/com/android/rs/image/Blend.java
+++ b/java/tests/ImageProcessing/src/com/android/rs/image/Blend.java
@@ -25,7 +25,6 @@
 import android.renderscript.RenderScript;
 import android.renderscript.Script;
 import android.renderscript.ScriptC;
-import android.renderscript.ScriptGroup;
 import android.renderscript.ScriptIntrinsicBlend;
 import android.renderscript.Type;
 import android.util.Log;
diff --git a/java/tests/ImageProcessing/src/com/android/rs/image/ColorCube.java b/java/tests/ImageProcessing/src/com/android/rs/image/ColorCube.java
index f313c46..d7f58c7 100644
--- a/java/tests/ImageProcessing/src/com/android/rs/image/ColorCube.java
+++ b/java/tests/ImageProcessing/src/com/android/rs/image/ColorCube.java
@@ -24,7 +24,6 @@
 import android.renderscript.RenderScript;
 import android.renderscript.Script;
 import android.renderscript.ScriptC;
-import android.renderscript.ScriptGroup;
 import android.renderscript.ScriptIntrinsic3DLUT;
 import android.renderscript.ScriptIntrinsicColorMatrix;
 import android.renderscript.Type;
diff --git a/java/tests/ImageProcessing/src/com/android/rs/image/ColorMatrix.java b/java/tests/ImageProcessing/src/com/android/rs/image/ColorMatrix.java
index 2ac40a1..4e8e06c 100644
--- a/java/tests/ImageProcessing/src/com/android/rs/image/ColorMatrix.java
+++ b/java/tests/ImageProcessing/src/com/android/rs/image/ColorMatrix.java
@@ -24,7 +24,6 @@
 import android.renderscript.RenderScript;
 import android.renderscript.Script;
 import android.renderscript.ScriptC;
-import android.renderscript.ScriptGroup;
 import android.renderscript.ScriptIntrinsicColorMatrix;
 import android.renderscript.Type;
 import android.util.Log;
diff --git a/java/tests/ImageProcessing/src/com/android/rs/image/Convolve3x3.java b/java/tests/ImageProcessing/src/com/android/rs/image/Convolve3x3.java
index 18e9b43..76706c9 100644
--- a/java/tests/ImageProcessing/src/com/android/rs/image/Convolve3x3.java
+++ b/java/tests/ImageProcessing/src/com/android/rs/image/Convolve3x3.java
@@ -24,7 +24,6 @@
 import android.renderscript.RenderScript;
 import android.renderscript.Script;
 import android.renderscript.ScriptC;
-import android.renderscript.ScriptGroup;
 import android.renderscript.ScriptIntrinsicConvolve3x3;
 import android.renderscript.Type;
 import android.util.Log;
diff --git a/java/tests/ImageProcessing/src/com/android/rs/image/Convolve5x5.java b/java/tests/ImageProcessing/src/com/android/rs/image/Convolve5x5.java
index 03b3bb8..a0f4fd1 100644
--- a/java/tests/ImageProcessing/src/com/android/rs/image/Convolve5x5.java
+++ b/java/tests/ImageProcessing/src/com/android/rs/image/Convolve5x5.java
@@ -24,7 +24,6 @@
 import android.renderscript.RenderScript;
 import android.renderscript.Script;
 import android.renderscript.ScriptC;
-import android.renderscript.ScriptGroup;
 import android.renderscript.ScriptIntrinsicConvolve5x5;
 import android.renderscript.Type;
 import android.util.Log;
diff --git a/java/tests/ImageProcessing/src/com/android/rs/image/Histogram.java b/java/tests/ImageProcessing/src/com/android/rs/image/Histogram.java
new file mode 100644
index 0000000..5035561
--- /dev/null
+++ b/java/tests/ImageProcessing/src/com/android/rs/image/Histogram.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.rs.image;
+
+import java.lang.Math;
+
+import android.renderscript.*;
+import android.util.Log;
+
+public class Histogram extends TestBase {
+    private ScriptC_histogram mScript;
+    private ScriptIntrinsicHistogram mHist;
+    private Allocation mSum;
+    private Allocation mSums;
+
+    public void createTest(android.content.res.Resources res) {
+        mScript = new ScriptC_histogram(mRS);
+        mHist = ScriptIntrinsicHistogram.create(mRS, Element.U8_4(mRS));
+
+        int w = mInPixelsAllocation.getType().getX();
+        int h = mInPixelsAllocation.getType().getY();
+        int step = 8;
+        int steps = (h + step - 1) / step;
+
+        mScript.set_gWidth(w);
+        mScript.set_gHeight(h);
+        mScript.set_gStep(step);
+        mScript.set_gSteps(steps);
+
+        Type.Builder tb = new Type.Builder(mRS, Element.I32(mRS));
+        tb.setX(256).setY(steps);
+        Type t = tb.create();
+        mSums = Allocation.createTyped(mRS, t);
+        mSum = Allocation.createSized(mRS, Element.I32(mRS), 256);
+
+        mScript.set_gSums(mSums);
+        mScript.set_gSum(mSum);
+        mScript.set_gSrc(mInPixelsAllocation);
+        mScript.set_gDest(mOutPixelsAllocation);
+
+        mScript.forEach_clear(mOutPixelsAllocation);
+    }
+
+
+
+    public void runTest() {
+        Script.LaunchOptions lo = new Script.LaunchOptions();
+        lo.setX(0, 1);
+        //mScript.forEach_pass1(mSums, lo);
+        //mScript.forEach_pass2(mSum);
+
+        mHist.setOutput(mSum);
+        mHist.forEach_dot(mInPixelsAllocation);
+
+        mScript.invoke_rescale();
+
+        lo.setX(0, 1024);
+        mScript.forEach_draw(mOutPixelsAllocation, lo);
+    }
+
+}
diff --git a/java/tests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java b/java/tests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
index 975027a..14543c9 100644
--- a/java/tests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
+++ b/java/tests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
@@ -98,7 +98,9 @@
         WHITE_BALANCE ("White Balance"),
         COLOR_CUBE ("Color Cube"),
         COLOR_CUBE_3D_INTRINSIC ("Color Cube (3D LUT intrinsic)"),
-        USAGE_IO ("Usage io)");
+        USAGE_IO ("Usage io"),
+        ARTISTIC_1("Artistic 1"),
+        HISTOGRAM ("Histogram");
 
 
         private final String name;
@@ -359,6 +361,12 @@
         case USAGE_IO:
             mTest = new UsageIO();
             break;
+        case ARTISTIC_1:
+            mTest = new Artistic1();
+            break;
+        case HISTOGRAM:
+            mTest = new Histogram();
+            break;
         }
 
         mTest.createBaseTest(this, mBitmapIn, mBitmapIn2, mBitmapOut);
diff --git a/java/tests/ImageProcessing/src/com/android/rs/image/LevelsV4.java b/java/tests/ImageProcessing/src/com/android/rs/image/LevelsV4.java
index 9eb5647..98c6460 100644
--- a/java/tests/ImageProcessing/src/com/android/rs/image/LevelsV4.java
+++ b/java/tests/ImageProcessing/src/com/android/rs/image/LevelsV4.java
@@ -109,7 +109,7 @@
     public boolean onBar4Setup(SeekBar b, TextView t) {
         b.setMax(128);
         b.setProgress(128);
-        t.setText("Out White");
+        t.setText("In White");
         return true;
     }
     public boolean onBar5Setup(SeekBar b, TextView t) {
diff --git a/java/tests/ImageProcessing/src/com/android/rs/image/UsageIO.java b/java/tests/ImageProcessing/src/com/android/rs/image/UsageIO.java
index 3f86311..9c29329 100644
--- a/java/tests/ImageProcessing/src/com/android/rs/image/UsageIO.java
+++ b/java/tests/ImageProcessing/src/com/android/rs/image/UsageIO.java
@@ -26,7 +26,6 @@
 import android.renderscript.ScriptIntrinsicColorMatrix;
 import android.renderscript.Type;
 import android.renderscript.Matrix4f;
-import android.renderscript.ScriptGroup;
 import android.util.Log;
 
 public class UsageIO extends TestBase {
diff --git a/java/tests/ImageProcessing/src/com/android/rs/image/WhiteBalance.java b/java/tests/ImageProcessing/src/com/android/rs/image/WhiteBalance.java
index a836067..e7f12d6 100644
--- a/java/tests/ImageProcessing/src/com/android/rs/image/WhiteBalance.java
+++ b/java/tests/ImageProcessing/src/com/android/rs/image/WhiteBalance.java
@@ -19,18 +19,24 @@
 import java.lang.Math;
 
 import android.renderscript.Allocation;
+import android.renderscript.Element;
+import android.renderscript.ScriptIntrinsicHistogram;
 
 public class WhiteBalance extends TestBase {
     private ScriptC_wbalance mScript;
+    private ScriptIntrinsicHistogram mHist;
+    private Allocation mSums;
 
     public void createTest(android.content.res.Resources res) {
         mScript = new ScriptC_wbalance(mRS);
+        mHist = ScriptIntrinsicHistogram.create(mRS, Element.U8_4(mRS));
+        mSums = Allocation.createSized(mRS, Element.I32_3(mRS), 256);
+        mHist.setOutput(mSums);
+        mScript.set_histogramValues(mSums);
     }
 
     public void runTest() {
-        mScript.set_histogramSource(mInPixelsAllocation);
-        mScript.set_histogramWidth(mInPixelsAllocation.getType().getX());
-        mScript.set_histogramHeight(mInPixelsAllocation.getType().getY());
+        mHist.forEach(mInPixelsAllocation);
         mScript.invoke_prepareWhiteBalance();
         mScript.forEach_whiteBalanceKernel(mInPixelsAllocation, mOutPixelsAllocation);
     }
diff --git a/java/tests/ImageProcessing/src/com/android/rs/image/artistic1.rs b/java/tests/ImageProcessing/src/com/android/rs/image/artistic1.rs
new file mode 100644
index 0000000..8051f29
--- /dev/null
+++ b/java/tests/ImageProcessing/src/com/android/rs/image/artistic1.rs
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ip.rsh"
+#pragma rs_fp_relaxed
+
+rs_allocation gBlur;
+
+static float gOverWm1;
+static float gOverHm1;
+static uchar gLutR[256];
+static uchar gLutG[256];
+static uchar gLutB[256];
+
+void setup() {
+    int w = rsAllocationGetDimX(gBlur);
+    int h = rsAllocationGetDimY(gBlur);
+    gOverWm1 = 1.f / w;
+    gOverHm1 = 1.f / h;
+
+    for (int x=0; x < 256; x++) {
+        gLutR[x] = x;//255-x;
+        gLutG[x] = x;//255-x;
+        gLutB[x] = x;//255-x;
+    }
+}
+
+uchar4 __attribute__((kernel)) process(uchar4 in, uint32_t x, uint32_t y) {
+    float2 xyDist;
+    xyDist.x = (x * gOverWm1 - 0.5f);
+    xyDist.y = (y * gOverHm1 - 0.5f);
+
+    // color
+    float4 v1 = rsUnpackColor8888(in);
+    float4 v2 = rsUnpackColor8888(rsGetElementAt_uchar4(gBlur, x, y));
+
+    float dist = dot(xyDist, xyDist) * 1.4f;
+    float pdist = native_powr(dist, 2.7f * 0.5f);
+    //float pdist = powr(dist, 2.7f * 0.5f);
+
+    pdist = clamp(pdist, 0.f, 1.f);
+    v1 = mix(v1, v2, dist * 2.f);
+    v1 *= 1.f - pdist;
+
+    // apply curve
+    uchar4 out = rsPackColorTo8888(v1);
+
+    out.r = gLutR[out.r];
+    out.g = gLutG[out.g];
+    out.b = gLutB[out.b];
+    return out;
+}
+
+
diff --git a/java/tests/ImageProcessing/src/com/android/rs/image/colorcube.rs b/java/tests/ImageProcessing/src/com/android/rs/image/colorcube.rs
index 4f1e73e..c0d6ace 100644
--- a/java/tests/ImageProcessing/src/com/android/rs/image/colorcube.rs
+++ b/java/tests/ImageProcessing/src/com/android/rs/image/colorcube.rs
@@ -56,16 +56,16 @@
     uint4 v011 = convert_uint4(rsGetElementAt_uchar4(gCube, coord1.x, coord2.y, coord2.z));
     uint4 v111 = convert_uint4(rsGetElementAt_uchar4(gCube, coord2.x, coord2.y, coord2.z));
 
-    uint4 yz00 = ((v000 * weight1.x) + (v100 * weight2.x)) >> (int4)8;
-    uint4 yz10 = ((v010 * weight1.x) + (v110 * weight2.x)) >> (int4)8;
-    uint4 yz01 = ((v001 * weight1.x) + (v101 * weight2.x)) >> (int4)8;
-    uint4 yz11 = ((v011 * weight1.x) + (v111 * weight2.x)) >> (int4)8;
+    uint4 yz00 = ((v000 * weight1.x) + (v100 * weight2.x)) >> (uint4)8;
+    uint4 yz10 = ((v010 * weight1.x) + (v110 * weight2.x)) >> (uint4)8;
+    uint4 yz01 = ((v001 * weight1.x) + (v101 * weight2.x)) >> (uint4)8;
+    uint4 yz11 = ((v011 * weight1.x) + (v111 * weight2.x)) >> (uint4)8;
 
-    uint4 z0 = ((yz00 * weight1.y) + (yz10 * weight2.y)) >> (int4)16;
-    uint4 z1 = ((yz01 * weight1.y) + (yz11 * weight2.y)) >> (int4)16;
+    uint4 z0 = ((yz00 * weight1.y) + (yz10 * weight2.y)) >> (uint4)16;
+    uint4 z1 = ((yz01 * weight1.y) + (yz11 * weight2.y)) >> (uint4)16;
 
-    uint4 v = ((z0 * weight1.z) + (z1 * weight2.z)) >> (int4)16;
-    uint4 v2 = (v + 0x7f) >> (int4)8;
+    uint4 v = ((z0 * weight1.z) + (z1 * weight2.z)) >> (uint4)16;
+    uint4 v2 = (v + 0x7f) >> (uint4)8;
 
     *out = convert_uchar4(v2);
     out->a = 0xff;
diff --git a/java/tests/ImageProcessing/src/com/android/rs/image/contrast.rs b/java/tests/ImageProcessing/src/com/android/rs/image/contrast.rs
index 5b67252..ef6fd63 100644
--- a/java/tests/ImageProcessing/src/com/android/rs/image/contrast.rs
+++ b/java/tests/ImageProcessing/src/com/android/rs/image/contrast.rs
@@ -27,12 +27,6 @@
 
 void contrast(const uchar4 *in, uchar4 *out)
 {
-#if 0
-    out->r = rsClamp((int)(brightM * in->r + brightC), 0, 255);
-    out->g = rsClamp((int)(brightM * in->g + brightC), 0, 255);
-    out->b = rsClamp((int)(brightM * in->b + brightC), 0, 255);
-#else
     float3 v = convert_float3(in->rgb) * brightM + brightC;
     out->rgb = convert_uchar3(clamp(v, 0.f, 255.f));
-#endif
 }
diff --git a/java/tests/ImageProcessing/src/com/android/rs/image/convolve3x3.fs b/java/tests/ImageProcessing/src/com/android/rs/image/convolve3x3.fs
index 177e86e..4f8b4d8 100644
--- a/java/tests/ImageProcessing/src/com/android/rs/image/convolve3x3.fs
+++ b/java/tests/ImageProcessing/src/com/android/rs/image/convolve3x3.fs
@@ -28,6 +28,8 @@
     uint32_t y1 = min((int32_t)y+1, gHeight-1);
     uint32_t y2 = max((int32_t)y-1, 0);
 
+    float4 result;
+
     float4 p00 = convert_float4(rsGetElementAt_uchar4(gIn, x1, y1));
     float4 p01 = convert_float4(rsGetElementAt_uchar4(gIn, x, y1));
     float4 p02 = convert_float4(rsGetElementAt_uchar4(gIn, x2, y1));
@@ -37,29 +39,19 @@
     float4 p20 = convert_float4(rsGetElementAt_uchar4(gIn, x1, y2));
     float4 p21 = convert_float4(rsGetElementAt_uchar4(gIn, x, y2));
     float4 p22 = convert_float4(rsGetElementAt_uchar4(gIn, x2, y2));
-    p00 *= gCoeffs[0];
-    p01 *= gCoeffs[1];
-    p02 *= gCoeffs[2];
-    p10 *= gCoeffs[3];
-    p11 *= gCoeffs[4];
-    p12 *= gCoeffs[5];
-    p20 *= gCoeffs[6];
-    p21 *= gCoeffs[7];
-    p22 *= gCoeffs[8];
 
-    p00 += p01;
-    p02 += p10;
-    p11 += p12;
-    p20 += p21;
+    result = p00 * gCoeffs[0];
+    result += p01 * gCoeffs[1];
+    result += p02 * gCoeffs[2];
+    result += p10 * gCoeffs[3];
+    result += p11 * gCoeffs[4];
+    result += p12 * gCoeffs[5];
+    result += p20 * gCoeffs[6];
+    result += p21 * gCoeffs[7];
+    result += p22 * gCoeffs[8];
 
-    p22 += p00;
-    p02 += p11;
-
-    p20 += p22;
-    p20 += p02;
-
-    p20 = clamp(p20, 0.f, 255.f);
-    return convert_uchar4(p20);
+    result = clamp(result, 0.f, 255.f);
+    return convert_uchar4(result);
 }
 
 
diff --git a/java/tests/ImageProcessing/src/com/android/rs/image/exposure.rs b/java/tests/ImageProcessing/src/com/android/rs/image/exposure.rs
index d9a8f62..88bb1d6 100644
--- a/java/tests/ImageProcessing/src/com/android/rs/image/exposure.rs
+++ b/java/tests/ImageProcessing/src/com/android/rs/image/exposure.rs
@@ -23,10 +23,11 @@
     bright = 255.f / (255.f - v);
 }
 
-void exposure(const uchar4 *in, uchar4 *out)
+uchar4 __attribute__((kernel)) exposure(uchar4 in)
 {
-    out->r = rsClamp((int)(bright * in->r), 0, 255);
-    out->g = rsClamp((int)(bright * in->g), 0, 255);
-    out->b = rsClamp((int)(bright * in->b), 0, 255);
+    uchar4 out = {0, 0, 0, 255};
+    float3 t = convert_float3(in.rgb);
+    out.rgb = convert_uchar3(clamp(convert_int3(t * bright), 0, 255));
+    return out;
 }
 
diff --git a/java/tests/ImageProcessing/src/com/android/rs/image/histogram.rs b/java/tests/ImageProcessing/src/com/android/rs/image/histogram.rs
new file mode 100644
index 0000000..dc0ec59
--- /dev/null
+++ b/java/tests/ImageProcessing/src/com/android/rs/image/histogram.rs
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ip.rsh"
+
+rs_allocation gSrc;
+rs_allocation gDest;
+rs_allocation gSums;
+rs_allocation gSum;
+
+int gWidth;
+int gHeight;
+int gStep;
+int gSteps;
+
+void __attribute__((kernel)) pass1(int in, uint x, uint y) {
+    for (int i=0; i < (256); i++) {
+        rsSetElementAt_int(gSums, 0, i, y);
+    }
+
+    for (int i = 0; i < gStep; i++) {
+        int py = y*gStep + i;
+        if (py >= gHeight) return;
+
+        for (int px=0; px < gWidth; px++) {
+            uchar4 c = rsGetElementAt_uchar4(gSrc, px, py);
+            int lum = (77 * c.r + 150 * c.g + 29 * c.b) >> 8;
+
+            int old = rsGetElementAt_int(gSums, lum, y);
+            rsSetElementAt_int(gSums, old+1, lum, y);
+        }
+    }
+}
+
+int __attribute__((kernel)) pass2(uint x) {
+    int sum = 0;
+    for (int i=0; i < gSteps; i++) {
+        sum += rsGetElementAt_int(gSums, x, i);
+    }
+    return sum;
+}
+
+void rescale() {
+    int maxv = 0;
+
+    for (int i=0; i < 256; i++) {
+        maxv = max(maxv, rsGetElementAt_int(gSum, i));
+    }
+    float overMax = (1.f / maxv) * gHeight;
+
+    for (int i=0; i < 256; i++) {
+        int t = rsGetElementAt_int(gSum, i);
+        t = gHeight - (overMax * rsGetElementAt_int(gSum, i));
+        t = max(0, t);
+        rsSetElementAt_int(gSum, t, i);
+    }
+}
+
+static const uchar4 gClear = {0, 0, 0, 0xff};
+
+uchar4 __attribute__((kernel)) clear() {
+    return gClear;
+}
+
+uchar4 __attribute__((kernel)) draw(uint x, uint y) {
+    int l = rsGetElementAt_int(gSum, x >> 2);
+    if (y > l) {
+        return 0xff;
+    }
+    return gClear;
+}
diff --git a/java/tests/ImageProcessing/src/com/android/rs/image/vibrance.rs b/java/tests/ImageProcessing/src/com/android/rs/image/vibrance.rs
index 174c2c3..b82e1d3 100644
--- a/java/tests/ImageProcessing/src/com/android/rs/image/vibrance.rs
+++ b/java/tests/ImageProcessing/src/com/android/rs/image/vibrance.rs
@@ -23,11 +23,6 @@
 static const float Gf = 0.587f;
 static const float Bf = 0.114f;
 
-static float S  = 0.f;
-static float MS = 0.f;
-static float Rt = 0.f;
-static float Gt = 0.f;
-static float Bt = 0.f;
 static float Vib = 0.f;
 
 void vibranceKernel(const uchar4 *in, uchar4 *out) {
@@ -37,14 +32,13 @@
     int r = in->r;
     int g = in->g;
     int b = in->b;
-    float red = (r-max(g, b))/256.f;
-    float sx = (float)(Vib/(1+native_exp(-red*3)));
-    S = sx+1;
-    MS = 1.0f - S;
-    Rt = Rf * MS;
-    Gt = Gf * MS;
-    Bt = Bf * MS;
-    int t = (r + g) / 2;
+    float red = (r-max(g, b)) * (1.f / 256.f);
+    float S = (float)(Vib/(1+native_exp(-red*3)))+1;
+    float MS = 1.0f - S;
+    float Rt = Rf * MS;
+    float Gt = Gf * MS;
+    float Bt = Bf * MS;
+    int t = (r + g) >> 1;
     R = r;
     G = g;
     B = b;
@@ -60,12 +54,5 @@
 }
 
 void prepareVibrance() {
-
     Vib = vibrance/100.f;
-    S  = Vib + 1;
-    MS = 1.0f - S;
-    Rt = Rf * MS;
-    Gt = Gf * MS;
-    Bt = Bf * MS;
-
 }
diff --git a/java/tests/ImageProcessing/src/com/android/rs/image/wbalance.rs b/java/tests/ImageProcessing/src/com/android/rs/image/wbalance.rs
index b5ab14f..461204c 100644
--- a/java/tests/ImageProcessing/src/com/android/rs/image/wbalance.rs
+++ b/java/tests/ImageProcessing/src/com/android/rs/image/wbalance.rs
@@ -17,39 +17,19 @@
 #include "ip.rsh"
 #pragma rs_fp_relaxed
 
-static int histR[256] = {0}, histG[256] = {0}, histB[256] = {0};
-
-rs_allocation histogramSource;
-uint32_t histogramHeight;
-uint32_t histogramWidth;
-
-static float scaleR;
-static float scaleG;
-static float scaleB;
+rs_allocation histogramValues;
+static float3 scale;
 
 static uchar4 estimateWhite() {
-
-    for (int i = 0; i < 256; i++) {
-        histR[i] = 0; histG[i] = 0; histB[i] = 0;
-    }
-
-    for (uint32_t i = 0; i < histogramHeight; i++) {
-        for (uint32_t j = 0; j < histogramWidth; j++) {
-            uchar4 in = rsGetElementAt_uchar4(histogramSource, j, i);
-            histR[in.r]++;
-            histG[in.g]++;
-            histB[in.b]++;
-        }
-    }
-
     int min_r = -1, min_g = -1, min_b = -1;
     int max_r =  0, max_g =  0, max_b =  0;
     int sum_r =  0, sum_g =  0, sum_b =  0;
 
     for (int i = 1; i < 255; i++) {
-        int r = histR[i];
-        int g = histG[i];
-        int b = histB[i];
+        int4 hv = rsGetElementAt_int4(histogramValues, i);
+        int r = hv.r;
+        int g = hv.g;
+        int b = hv.b;
         sum_r += r;
         sum_g += g;
         sum_b += b;
@@ -73,9 +53,10 @@
     int tmp_r = 0, tmp_g = 0, tmp_b = 0;
 
     for (int i = 254; i >0; i--) {
-        int r = histR[i];
-        int g = histG[i];
-        int b = histB[i];
+        int4 hv = rsGetElementAt_int4(histogramValues, i);
+        int r = hv.r;
+        int g = hv.g;
+        int b = hv.b;
         tmp_r += r;
         tmp_g += g;
         tmp_b += b;
@@ -115,28 +96,18 @@
     int maximum = max(estimation.r, max(estimation.g, estimation.b));
     float avg = (minimum + maximum) / 2.f;
 
-    scaleR =  avg/estimation.r;
-    scaleG =  avg/estimation.g;
-    scaleB =  avg/estimation.b;
-
+    scale.r =  avg / estimation.r;
+    scale.g =  avg / estimation.g;
+    scale.b =  avg / estimation.b;
 }
 
-static unsigned char contrastClamp(int c)
-{
-    int N = 255;
-    c &= ~(c >> 31);
-    c -= N;
-    c &= (c >> 31);
-    c += N;
-    return  (unsigned char) c;
-}
+uchar4 __attribute__((kernel)) whiteBalanceKernel(uchar4 in) {
+    float3 t = convert_float3(in.rgb);
+    t *= scale;
+    t = min(t, 255.f);
 
-void whiteBalanceKernel(const uchar4 *in, uchar4 *out) {
-    float Rc =  in->r*scaleR;
-    float Gc =  in->g*scaleG;
-    float Bc =  in->b*scaleB;
-
-    out->r = contrastClamp(Rc);
-    out->g = contrastClamp(Gc);
-    out->b = contrastClamp(Bc);
+    uchar4 out;
+    out.rgb = convert_uchar3(t);
+    out.a = 255;
+    return out;
 }
diff --git a/java/tests/ImageProcessing2/src/com/android/rs/image/colorcube.rs b/java/tests/ImageProcessing2/src/com/android/rs/image/colorcube.rs
index 4f1e73e..c0d6ace 100644
--- a/java/tests/ImageProcessing2/src/com/android/rs/image/colorcube.rs
+++ b/java/tests/ImageProcessing2/src/com/android/rs/image/colorcube.rs
@@ -56,16 +56,16 @@
     uint4 v011 = convert_uint4(rsGetElementAt_uchar4(gCube, coord1.x, coord2.y, coord2.z));
     uint4 v111 = convert_uint4(rsGetElementAt_uchar4(gCube, coord2.x, coord2.y, coord2.z));
 
-    uint4 yz00 = ((v000 * weight1.x) + (v100 * weight2.x)) >> (int4)8;
-    uint4 yz10 = ((v010 * weight1.x) + (v110 * weight2.x)) >> (int4)8;
-    uint4 yz01 = ((v001 * weight1.x) + (v101 * weight2.x)) >> (int4)8;
-    uint4 yz11 = ((v011 * weight1.x) + (v111 * weight2.x)) >> (int4)8;
+    uint4 yz00 = ((v000 * weight1.x) + (v100 * weight2.x)) >> (uint4)8;
+    uint4 yz10 = ((v010 * weight1.x) + (v110 * weight2.x)) >> (uint4)8;
+    uint4 yz01 = ((v001 * weight1.x) + (v101 * weight2.x)) >> (uint4)8;
+    uint4 yz11 = ((v011 * weight1.x) + (v111 * weight2.x)) >> (uint4)8;
 
-    uint4 z0 = ((yz00 * weight1.y) + (yz10 * weight2.y)) >> (int4)16;
-    uint4 z1 = ((yz01 * weight1.y) + (yz11 * weight2.y)) >> (int4)16;
+    uint4 z0 = ((yz00 * weight1.y) + (yz10 * weight2.y)) >> (uint4)16;
+    uint4 z1 = ((yz01 * weight1.y) + (yz11 * weight2.y)) >> (uint4)16;
 
-    uint4 v = ((z0 * weight1.z) + (z1 * weight2.z)) >> (int4)16;
-    uint4 v2 = (v + 0x7f) >> (int4)8;
+    uint4 v = ((z0 * weight1.z) + (z1 * weight2.z)) >> (uint4)16;
+    uint4 v2 = (v + 0x7f) >> (uint4)8;
 
     *out = convert_uchar4(v2);
     out->a = 0xff;
diff --git a/java/tests/LivePreview/src/com/android/rs/livepreview/CameraPreviewActivity.java b/java/tests/LivePreview/src/com/android/rs/livepreview/CameraPreviewActivity.java
index 62dcaa8..be0af4b 100644
--- a/java/tests/LivePreview/src/com/android/rs/livepreview/CameraPreviewActivity.java
+++ b/java/tests/LivePreview/src/com/android/rs/livepreview/CameraPreviewActivity.java
@@ -228,11 +228,14 @@
 
 
         // Set initial values
+	//
+        int initialSize = mPreviewSizes.size() - 1;
 
-        mNextPreviewSize = mPreviewSizes.get(15);
-        mResolutionSpinner.setSelection(15);
+	mNextPreviewSize = mPreviewSizes.get(initialSize);
+        mResolutionSpinner.setSelection(initialSize);
 
-        if (mPreviewTexture != null) {
+	if(mPreviewTexture != null)
+	{
             startPreview();
         }
     }
@@ -371,4 +374,4 @@
 
 
 
-}
\ No newline at end of file
+}
diff --git a/java/tests/RSTest_CompatLib/src/com/android/rs/test/RSTest.java b/java/tests/RSTest_CompatLib/src/com/android/rs/test/RSTest.java
index b76f21e..cc07e9c 100644
--- a/java/tests/RSTest_CompatLib/src/com/android/rs/test/RSTest.java
+++ b/java/tests/RSTest_CompatLib/src/com/android/rs/test/RSTest.java
@@ -40,7 +40,7 @@
 
     private static final String LOG_TAG = "RSTest_Compat";
     private static final boolean DEBUG  = false;
-    private static final boolean LOG_ENABLED = false;
+    private static final boolean LOG_ENABLED = true;
 
     private RenderScript mRS;
     private RSTestCore RSTC;
@@ -54,10 +54,6 @@
 
         RSTC = new RSTestCore(this);
         RSTC.init(mRS, getResources());
-
-
-
-
     }
 
     static void log(String message) {
diff --git a/java/tests/RSTest_CompatLib/src/com/android/rs/test/RSTestCore.java b/java/tests/RSTest_CompatLib/src/com/android/rs/test/RSTestCore.java
index 51f8a4d..2ee2a33 100644
--- a/java/tests/RSTest_CompatLib/src/com/android/rs/test/RSTestCore.java
+++ b/java/tests/RSTest_CompatLib/src/com/android/rs/test/RSTestCore.java
@@ -58,6 +58,7 @@
         unitTests = new ArrayList<UnitTest>();
 
         unitTests.add(new UT_primitives(this, mRes, mCtx));
+        unitTests.add(new UT_instance(this, mRes, mCtx));
         unitTests.add(new UT_constant(this, mRes, mCtx));
         unitTests.add(new UT_vector(this, mRes, mCtx));
         unitTests.add(new UT_unsigned(this, mRes, mCtx));
diff --git a/java/tests/RSTest_CompatLib/src/com/android/rs/test/UT_instance.java b/java/tests/RSTest_CompatLib/src/com/android/rs/test/UT_instance.java
new file mode 100644
index 0000000..071b80e
--- /dev/null
+++ b/java/tests/RSTest_CompatLib/src/com/android/rs/test/UT_instance.java
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.rs.test_compat;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.support.v8.renderscript.*;
+
+public class UT_instance extends UnitTest {
+    private Resources mRes;
+
+    protected UT_instance(RSTestCore rstc, Resources res, Context ctx) {
+        super(rstc, "Instance", ctx);
+        mRes = res;
+    }
+
+    void assertEquals(int e, int v) {
+        if (e != v) {
+            RSTest.log("Assertion failed! Expected: <" + e + "> Got: <" + v + ">");
+            failTest();
+        }
+    }
+
+    public void run() {
+        RenderScript mRS = RenderScript.create(mCtx);
+        mRS.setMessageHandler(mRsMessage);
+
+        ScriptC_instance instance_1 = new ScriptC_instance(mRS);
+        ScriptC_instance instance_2 = new ScriptC_instance(mRS);
+        ScriptC_instance instance_3 = new ScriptC_instance(mRS);
+        ScriptC_instance instance_4 = new ScriptC_instance(mRS);
+        ScriptC_instance instance_5 = new ScriptC_instance(mRS);
+
+        Type t = new Type.Builder(mRS, Element.I32(mRS)).setX(1).create();
+        Allocation ai1 = Allocation.createTyped(mRS, t);
+        Allocation ai2 = Allocation.createTyped(mRS, t);
+        Allocation ai3 = Allocation.createTyped(mRS, t);
+        Allocation ai4 = Allocation.createTyped(mRS, t);
+        Allocation ai5 = Allocation.createTyped(mRS, t);
+
+        instance_1.set_i(1);
+        instance_2.set_i(2);
+        instance_3.set_i(3);
+        instance_4.set_i(4);
+        instance_5.set_i(5);
+        instance_1.set_ai(ai1);
+        instance_2.set_ai(ai2);
+        instance_3.set_ai(ai3);
+        instance_4.set_ai(ai4);
+        instance_5.set_ai(ai5);
+
+        // We now check to ensure that the global is not being shared across
+        // our separate script instances. Our invoke here merely sets the
+        // instanced allocation with the instanced global variable's value.
+        // If globals are being shared (i.e. not instancing scripts), then
+        // both instanced allocations will have the same resulting value
+        // (depending on the order in which the invokes complete).
+        instance_1.invoke_instance_test();
+        instance_2.invoke_instance_test();
+        instance_3.invoke_instance_test();
+        instance_4.invoke_instance_test();
+        instance_5.invoke_instance_test();
+
+        int i1[] = new int[1];
+        int i2[] = new int[1];
+        int i3[] = new int[1];
+        int i4[] = new int[1];
+        int i5[] = new int[1];
+
+        ai1.copyTo(i1);
+        ai2.copyTo(i2);
+        ai3.copyTo(i3);
+        ai4.copyTo(i4);
+        ai5.copyTo(i5);
+
+        assertEquals(1, i1[0]);
+        assertEquals(2, i2[0]);
+        assertEquals(3, i3[0]);
+        assertEquals(4, i4[0]);
+        assertEquals(5, i5[0]);
+        assertEquals(1, i1[0]);
+        assertEquals(2, i2[0]);
+        assertEquals(3, i3[0]);
+        assertEquals(4, i4[0]);
+        assertEquals(5, i5[0]);
+        passTest();  // Set to pass (as long as existing checks didn't fail).
+    }
+}
diff --git a/java/tests/RSTest_CompatLib/src/com/android/rs/test/instance.rs b/java/tests/RSTest_CompatLib/src/com/android/rs/test/instance.rs
new file mode 100644
index 0000000..65ce032
--- /dev/null
+++ b/java/tests/RSTest_CompatLib/src/com/android/rs/test/instance.rs
@@ -0,0 +1,9 @@
+#include "shared.rsh"
+
+int i;
+rs_allocation ai;
+
+void instance_test() {
+    // Set our allocation based on the global input value.
+    rsSetElementAt_int(ai, i, 0);
+}
diff --git a/java/tests/RsTest/src/com/android/rs/test/RSTestCore.java b/java/tests/RsTest/src/com/android/rs/test/RSTestCore.java
index c0eeeea..d5f56b3 100644
--- a/java/tests/RsTest/src/com/android/rs/test/RSTestCore.java
+++ b/java/tests/RsTest/src/com/android/rs/test/RSTestCore.java
@@ -82,6 +82,7 @@
         unitTests.add(new UT_rstime(this, mRes, mCtx));
         unitTests.add(new UT_rstypes(this, mRes, mCtx));
         unitTests.add(new UT_alloc(this, mRes, mCtx));
+        unitTests.add(new UT_check_dims(this, mRes, mCtx));
         unitTests.add(new UT_static_globals(this, mRes, mCtx));
         unitTests.add(new UT_refcount(this, mRes, mCtx));
         unitTests.add(new UT_foreach(this, mRes, mCtx));
diff --git a/java/tests/RsTest/src/com/android/rs/test/UT_check_dims.java b/java/tests/RsTest/src/com/android/rs/test/UT_check_dims.java
new file mode 100644
index 0000000..71752a3
--- /dev/null
+++ b/java/tests/RsTest/src/com/android/rs/test/UT_check_dims.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.rs.test;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.renderscript.*;
+
+public class UT_check_dims extends UnitTest {
+    private Resources mRes;
+    byte mFailedArr[];
+    int mData[];
+    Allocation mA;
+    static final int Pattern = 0xA5A5A5A5;
+
+    protected UT_check_dims(RSTestCore rstc, Resources res, Context ctx) {
+        super(rstc, "Check Dims", ctx);
+        mRes = res;
+    }
+
+    private void initializeGlobals(RenderScript RS, ScriptC_check_dims s) {
+        Type.Builder typeBuilder = new Type.Builder(RS, Element.U8(RS));
+        typeBuilder.setX(1);
+        Allocation AFailed = Allocation.createTyped(RS, typeBuilder.create());
+        s.set_aFailed(AFailed);
+
+        mFailedArr = new byte[1];
+        mFailedArr[0] = 0;
+        AFailed.copyFrom(mFailedArr);
+
+        typeBuilder = new Type.Builder(RS, Element.I32(RS));
+        int X = 5;
+        int Y = 7;
+        typeBuilder.setX(X).setY(Y);
+        mA = Allocation.createTyped(RS, typeBuilder.create());
+        s.set_pattern(Pattern);
+
+        mData = new int[X*Y];
+        for (int i = 0; i < X*Y; i++) {
+            mData[i] = Pattern;
+        }
+        mA.copyFrom(mData);
+
+        return;
+    }
+
+    public void run() {
+        RenderScript pRS = RenderScript.create(mCtx);
+        ScriptC_check_dims s = new ScriptC_check_dims(pRS);
+        pRS.setMessageHandler(mRsMessage);
+        initializeGlobals(pRS, s);
+        s.forEach_root(mA);
+        s.invoke_check_dims_test();
+        pRS.finish();
+        waitForMessage();
+        pRS.destroy();
+    }
+}
diff --git a/java/tests/RsTest/src/com/android/rs/test/check_dims.rs b/java/tests/RsTest/src/com/android/rs/test/check_dims.rs
new file mode 100644
index 0000000..b18796c
--- /dev/null
+++ b/java/tests/RsTest/src/com/android/rs/test/check_dims.rs
@@ -0,0 +1,26 @@
+#include "shared.rsh"
+
+int pattern;
+
+rs_allocation aFailed;
+
+// This test checks to see that we only work on the cells specified for the
+// input allocation (i.e. don't affect anything between dimX and stride for
+// each row). If we don't see the pattern that we wrote, we know that we
+// are definitely working outside our proper bounds.
+void root(const int *o, uint32_t x, uint32_t y) {
+    if (*o != pattern) {
+        rsSetElementAt_uchar(aFailed, 1, 0);
+    }
+}
+
+void check_dims_test() {
+    bool failed = rsGetElementAt_uchar(aFailed, 0);
+    if (failed) {
+        rsSendToClientBlocking(RS_MSG_TEST_FAILED);
+    }
+    else {
+        rsSendToClientBlocking(RS_MSG_TEST_PASSED);
+    }
+}
+
diff --git a/rs.h b/rs.h
index 7f592f7..8a0761a 100644
--- a/rs.h
+++ b/rs.h
@@ -22,9 +22,8 @@
 
 #include "rsDefines.h"
 
-//
-// A3D loading and object update code.
-// Should only be called at object creation, not thread safe
+// Legacy graphics functions
+// Not extern C because not used from C++ API
 RsObjectBase rsaFileA3DGetEntryByIndex(RsContext, uint32_t idx, RsFile);
 RsFile rsaFileA3DCreateFromMemory(RsContext, const void *data, uint32_t len);
 RsFile rsaFileA3DCreateFromAsset(RsContext, void *asset);
@@ -32,30 +31,32 @@
 void rsaFileA3DGetNumIndexEntries(RsContext, int32_t *numEntries, RsFile);
 void rsaFileA3DGetIndexEntries(RsContext, RsFileIndexEntry *fileEntries,
                                uint32_t numEntries, RsFile);
-void rsaGetName(RsContext, void * obj, const char **name);
 // Mesh update functions
 void rsaMeshGetVertexBufferCount(RsContext, RsMesh, int32_t *vtxCount);
 void rsaMeshGetIndexCount(RsContext, RsMesh, int32_t *idxCount);
 void rsaMeshGetVertices(RsContext, RsMesh, RsAllocation *vtxData, uint32_t vtxDataCount);
 void rsaMeshGetIndices(RsContext, RsMesh, RsAllocation *va,
                        uint32_t *primType, uint32_t idxDataCount);
-// Allocation update
-const void* rsaAllocationGetType(RsContext con, RsAllocation va);
-// Type update
-void rsaTypeGetNativeData(RsContext, RsType, uint32_t *typeData, uint32_t typeDataSize);
-// Element update
-void rsaElementGetNativeData(RsContext, RsElement, uint32_t *elemData, uint32_t elemDataSize);
-void rsaElementGetSubElements(RsContext, RsElement, uint32_t *ids, const char **names,
-                              uint32_t *arraySizes, uint32_t dataSize);
-
-RsDevice rsDeviceCreate();
-void rsDeviceDestroy(RsDevice dev);
-void rsDeviceSetConfig(RsDevice dev, RsDeviceParam p, int32_t value);
-RsContext rsContextCreate(RsDevice dev, uint32_t version, uint32_t sdkVersion,
-                          RsContextType ct, bool forceCpu, bool synchronous);
 RsContext rsContextCreateGL(RsDevice dev, uint32_t version, uint32_t sdkVersion,
                             RsSurfaceConfig sc, uint32_t dpi);
 
+extern "C" {
+    void rsaGetName(RsContext, void * obj, const char **name);
+    // Allocation update
+    const void* rsaAllocationGetType(RsContext con, RsAllocation va);
+    // Type update
+    void rsaTypeGetNativeData(RsContext, RsType, uint32_t *typeData, uint32_t typeDataSize);
+    // Element update
+    void rsaElementGetNativeData(RsContext, RsElement, uint32_t *elemData, uint32_t elemDataSize);
+    void rsaElementGetSubElements(RsContext, RsElement, uint32_t *ids, const char **names,
+                                  uint32_t *arraySizes, uint32_t dataSize);
+
+    RsDevice rsDeviceCreate();
+    void rsDeviceDestroy(RsDevice dev);
+    void rsDeviceSetConfig(RsDevice dev, RsDeviceParam p, int32_t value);
+    RsContext rsContextCreate(RsDevice dev, uint32_t version, uint32_t sdkVersion,
+                              RsContextType ct, bool forceCpu, bool synchronous);
+}
 #include "rsgApiFuncDecl.h"
 
 #endif // RENDER_SCRIPT_H
diff --git a/rs.spec b/rs.spec
index 385622f..aab3d46 100644
--- a/rs.spec
+++ b/rs.spec
@@ -95,7 +95,7 @@
     }
 
 ContextDestroyWorker {
-        sync
+    sync
 }
 
 AssignName {
@@ -171,16 +171,6 @@
     param size_t stride
     }
 
-Allocation2DElementData {
-    param RsAllocation va
-    param uint32_t x
-    param uint32_t y
-    param uint32_t lod
-    param RsAllocationCubemapFace face
-    param const void *data
-    param size_t element_offset
-    }
-
 AllocationGenerateMipmaps {
     param RsAllocation va
 }
@@ -210,12 +200,12 @@
     param size_t stride
 }
 
+
 AllocationSyncAll {
     param RsAllocation va
     param RsAllocationUsageType src
 }
 
-
 AllocationResize1D {
     param RsAllocation va
     param uint32_t dimX
@@ -401,5 +391,11 @@
     param RsScriptGroup group
 }
 
+AllocationIoSend {
+    param RsAllocation alloc
+    }
 
+AllocationIoReceive {
+    param RsAllocation alloc
+    }
 
diff --git a/rsAllocation.cpp b/rsAllocation.cpp
index 0c271c1..9bf8709 100644
--- a/rsAllocation.cpp
+++ b/rsAllocation.cpp
@@ -79,6 +79,13 @@
 }
 
 Allocation::~Allocation() {
+#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
+    if (mGrallocConsumer.get()) {
+        mGrallocConsumer->unlockBuffer();
+        mGrallocConsumer = NULL;
+    }
+#endif
+
     freeChildrenUnlocked();
     mRSC->mHal.funcs.allocation.destroy(mRSC, this);
 }
@@ -236,13 +243,14 @@
 
 void Allocation::dumpLOGV(const char *prefix) const {
     ObjectBase::dumpLOGV(prefix);
+    char buf[1024];
 
-    String8 s(prefix);
-    s.append(" type ");
-    if (mHal.state.type) {
-        mHal.state.type->dumpLOGV(s.string());
+    if ((strlen(prefix) + 10) < sizeof(buf)) {
+        sprintf(buf, "%s type ", prefix);
+        if (mHal.state.type) {
+            mHal.state.type->dumpLOGV(buf);
+        }
     }
-
     ALOGV("%s allocation ptr=%p  mUsageFlags=0x04%x, mMipmapControl=0x%04x",
          prefix, mHal.drvState.lod[0].mallocPtr, mHal.state.usageFlags, mHal.state.mipmapControl);
 }
@@ -327,9 +335,7 @@
 void Allocation::serialize(Context *rsc, OStream *stream) const {
     // Need to identify ourselves
     stream->addU32((uint32_t)getClassId());
-
-    String8 name(getName());
-    stream->addString(&name);
+    stream->addString(getName());
 
     // First thing we need to serialize is the type object since it will be needed
     // to initialize the class
@@ -358,8 +364,7 @@
         return NULL;
     }
 
-    String8 name;
-    stream->loadString(&name);
+    const char *name = stream->loadString();
 
     Type *type = Type::createFromStream(rsc, stream);
     if (!type) {
@@ -382,8 +387,7 @@
         return NULL;
     }
 
-    alloc->setName(name.string(), name.size());
-
+    alloc->assignName(name);
     if (dataSize == type->getSizeBytes()) {
         uint32_t count = dataSize / type->getElementSizeBytes();
         // Read in all of our allocation data
@@ -454,8 +458,31 @@
     ALOGE("not implemented");
 }
 
+#ifndef RS_COMPATIBILITY_LIB
+void Allocation::NewBufferListener::onFrameAvailable() {
+    intptr_t ip = (intptr_t)alloc;
+    rsc->sendMessageToClient(NULL, RS_MESSAGE_TO_CLIENT_NEW_BUFFER, ip, 0, true);
+}
+#endif
+
 void * Allocation::getSurface(const Context *rsc) {
-    return rsc->mHal.funcs.allocation.getSurface(rsc, this);
+#ifndef RS_COMPATIBILITY_LIB
+    // Configure GrallocConsumer to be in asynchronous mode
+    sp<BufferQueue> bq = new BufferQueue();
+    mGrallocConsumer = new GrallocConsumer(this, bq);
+    sp<IGraphicBufferProducer> bp = bq;
+    bp->incStrong(NULL);
+
+    mBufferListener = new NewBufferListener();
+    mBufferListener->rsc = rsc;
+    mBufferListener->alloc = this;
+
+    mGrallocConsumer->setFrameAvailableListener(mBufferListener);
+    return bp.get();
+#else
+    return NULL;
+#endif
+    //return rsc->mHal.funcs.allocation.getSurface(rsc, this);
 }
 
 void Allocation::setSurface(const Context *rsc, RsNativeWindow sur) {
@@ -468,7 +495,22 @@
 }
 
 void Allocation::ioReceive(const Context *rsc) {
-    rsc->mHal.funcs.allocation.ioReceive(rsc, this);
+    void *ptr = NULL;
+    size_t stride = 0;
+#ifndef RS_COMPATIBILITY_LIB
+    if (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
+        status_t ret = mGrallocConsumer->lockNextBuffer();
+
+        if (ret == OK) {
+            rsc->mHal.funcs.allocation.ioReceive(rsc, this);
+        } else if (ret == BAD_VALUE) {
+            // No new frame, don't do anything
+        } else {
+            rsc->setError(RS_ERROR_DRIVER, "Error receiving IO input buffer.");
+        }
+
+    }
+#endif
 }
 
 
@@ -690,7 +732,7 @@
 }
 }
 
-const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
+extern "C" const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
     Allocation *a = static_cast<Allocation *>(va);
     a->getType()->incUserRef();
 
diff --git a/rsAllocation.h b/rsAllocation.h
index dffa440..440246e 100644
--- a/rsAllocation.h
+++ b/rsAllocation.h
@@ -19,6 +19,14 @@
 
 #include "rsType.h"
 
+#include <ui/GraphicBuffer.h>
+
+#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
+#include "rsGrallocConsumer.h"
+#include "gui/CpuConsumer.h"
+#include "gui/GLConsumer.h"
+#endif
+
 // ---------------------------------------------------------------------------
 namespace android {
 
@@ -58,8 +66,8 @@
             bool hasReferences;
             void * userProvidedPtr;
             int32_t surfaceTextureID;
-            void *deprecated01;
-            void *deprecated02;
+            ANativeWindowBuffer *nativeBuffer;
+            int64_t timestamp;
         };
         State state;
 
@@ -157,6 +165,20 @@
         mHal.state.type = t;
     }
 
+#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
+    class NewBufferListener : public android::ConsumerBase::FrameAvailableListener {
+    public:
+        const android::renderscript::Context *rsc;
+        const android::renderscript::Allocation *alloc;
+
+        virtual void onFrameAvailable();
+    };
+
+    sp<NewBufferListener> mBufferListener;
+    sp< GrallocConsumer > mGrallocConsumer;
+#endif
+
+
 private:
     void freeChildrenUnlocked();
     Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr);
diff --git a/rsContext.cpp b/rsContext.cpp
index 74c40c5..44f2d74 100644
--- a/rsContext.cpp
+++ b/rsContext.cpp
@@ -34,7 +34,7 @@
 #include <dlfcn.h>
 #include <unistd.h>
 
-#if !defined(RS_SERVER)
+#if !defined(RS_SERVER) && defined(HAVE_ANDROID_OS)
 #include <cutils/properties.h>
 #endif
 
@@ -205,7 +205,7 @@
 #endif
 
 static uint32_t getProp(const char *str) {
-#ifndef RS_SERVER
+#if !defined(RS_SERVER) && defined(HAVE_ANDROID_OS)
     char buf[PROPERTY_VALUE_MAX];
     property_get(str, buf, "0");
     return atoi(buf);
@@ -868,7 +868,6 @@
     //ALOGV("%p rsContextDestroy done", rsc);
 }
 
-
 RsMessageToClientType rsi_ContextPeekMessage(Context *rsc,
                                            size_t * receiveLen, size_t receiveLen_length,
                                            uint32_t * subID, size_t subID_length) {
@@ -898,8 +897,8 @@
 }
 }
 
-RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion,
-                          RsContextType ct, bool forceCpu, bool synchronous) {
+extern "C" RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion,
+                                     RsContextType ct, bool forceCpu, bool synchronous) {
     //ALOGV("rsContextCreate dev=%p", vdev);
     Device * dev = static_cast<Device *>(vdev);
     Context *rsc = Context::createContext(dev, NULL, ct, forceCpu, synchronous);
diff --git a/rsContext.h b/rsContext.h
index abc9b27..a29313e 100644
--- a/rsContext.h
+++ b/rsContext.h
@@ -26,6 +26,15 @@
 #include "rsScriptGroup.h"
 #include "rsSampler.h"
 
+#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
+#define ATRACE_TAG ATRACE_TAG_RS
+#include "utils/Trace.h"
+#else
+#define ATRACE_ENABLED(...) false
+#define ATRACE_NAME(...)
+#define ATRACE_CALL(...)
+#endif
+
 #ifndef RS_COMPATIBILITY_LIB
 #include "rsFont.h"
 #include "rsPath.h"
@@ -34,6 +43,7 @@
 #include "rsProgramRaster.h"
 #include "rsProgramVertex.h"
 #include "rsFBOCache.h"
+
 #endif
 
 
@@ -62,6 +72,8 @@
 #define CHECK_OBJ_OR_NULL(o)
 #endif
 
+
+
 class Context {
 public:
     struct Hal {
diff --git a/rsCppUtils.cpp b/rsCppUtils.cpp
new file mode 100644
index 0000000..59ec9bb
--- /dev/null
+++ b/rsCppUtils.cpp
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "rsUtils.h"
+#include "rsCppUtils.h"
+
+namespace android {
+namespace renderscript {
+
+const char * rsuCopyString(const char *name) {
+    return rsuCopyString(name, strlen(name));
+}
+
+const char * rsuCopyString(const char *name, size_t len) {
+    char *n = new char[len+1];
+    memcpy(n, name, len);
+    n[len] = 0;
+    return n;
+}
+
+
+}
+}
diff --git a/rsCppUtils.h b/rsCppUtils.h
index ced13c1..36b52aa 100644
--- a/rsCppUtils.h
+++ b/rsCppUtils.h
@@ -38,6 +38,9 @@
 
 #if defined(RS_SERVER) || defined(RS_COMPATIBILITY_LIB)
 
+#define ATRACE_TAG
+#define ATRACE_CALL(...)
+
 #include <string>
 #include <vector>
 #include <algorithm>
@@ -185,6 +188,9 @@
 namespace android {
 namespace renderscript {
 
+const char * rsuCopyString(const char *name);
+const char * rsuCopyString(const char *name, size_t len);
+
 #if 1
 #define rsAssert(v) do {if(!(v)) ALOGE("rsAssert failed: %s, in %s at %i", #v, __FILE__, __LINE__);} while (0)
 #else
diff --git a/rsDefines.h b/rsDefines.h
index 6e080de..0287f67 100644
--- a/rsDefines.h
+++ b/rsDefines.h
@@ -363,7 +363,8 @@
     RS_SCRIPT_INTRINSIC_ID_BLUR = 5,
     RS_SCRIPT_INTRINSIC_ID_YUV_TO_RGB = 6,
     RS_SCRIPT_INTRINSIC_ID_BLEND = 7,
-    RS_SCRIPT_INTRINSIC_ID_3DLUT = 8
+    RS_SCRIPT_INTRINSIC_ID_3DLUT = 8,
+    RS_SCRIPT_INTRINSIC_ID_HISTOGRAM = 9
 };
 
 typedef struct {
diff --git a/rsDevice.cpp b/rsDevice.cpp
index d7d03f6..2688890 100644
--- a/rsDevice.cpp
+++ b/rsDevice.cpp
@@ -40,17 +40,17 @@
     }
 }
 
-RsDevice rsDeviceCreate() {
+extern "C" RsDevice rsDeviceCreate() {
     Device * d = new Device();
     return d;
 }
 
-void rsDeviceDestroy(RsDevice dev) {
+extern "C" void rsDeviceDestroy(RsDevice dev) {
     Device * d = static_cast<Device *>(dev);
     delete d;
 }
 
-void rsDeviceSetConfig(RsDevice dev, RsDeviceParam p, int32_t value) {
+extern "C" void rsDeviceSetConfig(RsDevice dev, RsDeviceParam p, int32_t value) {
     Device * d = static_cast<Device *>(dev);
     if (p == RS_DEVICE_PARAM_FORCE_SOFTWARE_GL) {
         d->mForceSW = value != 0;
diff --git a/rsElement.cpp b/rsElement.cpp
index 712f17d..a81df48 100644
--- a/rsElement.cpp
+++ b/rsElement.cpp
@@ -44,7 +44,12 @@
 }
 
 void Element::clear() {
-    delete [] mFields;
+    if (mFields) {
+        for (size_t i = 0; i < mFieldCount; i++) {
+            delete[] mFields[i].name;
+        }
+        delete [] mFields;
+    }
     mFields = NULL;
     mFieldCount = 0;
     mHasReference = false;
@@ -87,7 +92,7 @@
     for (uint32_t ct = 0; ct < mFieldCount; ct++) {
         ALOGV("%s Element field index: %u ------------------", prefix, ct);
         ALOGV("%s name: %s, offsetBits: %u, arraySize: %u",
-             prefix, mFields[ct].name.string(), mFields[ct].offsetBits, mFields[ct].arraySize);
+             prefix, mFields[ct].name, mFields[ct].offsetBits, mFields[ct].arraySize);
         mFields[ct].e->dumpLOGV(prefix);
     }
 }
@@ -95,16 +100,14 @@
 void Element::serialize(Context *rsc, OStream *stream) const {
     // Need to identify ourselves
     stream->addU32((uint32_t)getClassId());
-
-    String8 name(getName());
-    stream->addString(&name);
+    stream->addString(getName());
 
     mComponent.serialize(stream);
 
     // Now serialize all the fields
     stream->addU32(mFieldCount);
     for (uint32_t ct = 0; ct < mFieldCount; ct++) {
-        stream->addString(&mFields[ct].name);
+        stream->addString(mFields[ct].name);
         stream->addU32(mFields[ct].arraySize);
         mFields[ct].e->serialize(rsc, stream);
     }
@@ -118,8 +121,7 @@
         return NULL;
     }
 
-    String8 name;
-    stream->loadString(&name);
+    const char *name = stream->loadString();
 
     Component component;
     component.loadFromStream(stream);
@@ -138,13 +140,9 @@
     size_t *subElemNamesLengths = new size_t[fieldCount];
     uint32_t *arraySizes = new uint32_t[fieldCount];
 
-    String8 elemName;
     for (uint32_t ct = 0; ct < fieldCount; ct ++) {
-        stream->loadString(&elemName);
-        subElemNamesLengths[ct] = elemName.length();
-        char *tmpName = new char[subElemNamesLengths[ct]];
-        memcpy(tmpName, elemName.string(), subElemNamesLengths[ct]);
-        subElemNames[ct] = tmpName;
+        subElemNames[ct] = stream->loadString();
+        subElemNamesLengths[ct] = strlen(subElemNames[ct]);
         arraySizes[ct] = stream->loadU32();
         subElems[ct] = Element::createFromStream(rsc, stream);
     }
@@ -155,6 +153,7 @@
         delete [] subElemNames[ct];
         subElems[ct]->decUserRef();
     }
+    delete[] name;
     delete[] subElems;
     delete[] subElemNames;
     delete[] subElemNamesLengths;
@@ -179,7 +178,7 @@
 
     uint32_t noPaddingFieldCount = 0;
     for (uint32_t ct = 0; ct < mFieldCount; ct ++) {
-        if (mFields[ct].name.string()[0] != '#') {
+        if (mFields[ct].name[0] != '#') {
             noPaddingFieldCount ++;
         }
     }
@@ -203,14 +202,14 @@
             mHasReference = true;
         }
 
-        if (mFields[ct].name.string()[0] == '#') {
+        if (mFields[ct].name[0] == '#') {
             continue;
         }
 
         mHal.state.fields[ctNoPadding] = mFields[ct].e.get();
         mHal.state.fieldArraySizes[ctNoPadding] = mFields[ct].arraySize;
-        mHal.state.fieldNames[ctNoPadding] = mFields[ct].name.string();
-        mHal.state.fieldNameLengths[ctNoPadding] = mFields[ct].name.length() + 1; // to include 0
+        mHal.state.fieldNames[ctNoPadding] = mFields[ct].name;
+        mHal.state.fieldNameLengths[ctNoPadding] = strlen(mFields[ct].name) + 1; // to include 0
         mHal.state.fieldOffsetBytes[ctNoPadding] = mFields[ct].offsetBits >> 3;
 
         ctNoPadding ++;
@@ -262,10 +261,21 @@
         if (ee->getFieldCount() == count) {
             bool match = true;
             for (uint32_t i=0; i < count; i++) {
+                size_t len;
+                uint32_t asize = 1;
+                if (lengths) {
+                    len = lengths[i];
+                } else {
+                    len = strlen(nin[i]);
+                }
+                if (asin) {
+                    asize = asin[i];
+                }
+
                 if ((ee->mFields[i].e.get() != ein[i]) ||
-                    (ee->mFields[i].name.length() != lengths[i]) ||
-                    (ee->mFields[i].name != nin[i]) ||
-                    (ee->mFields[i].arraySize != asin[i])) {
+                    (strlen(ee->mFields[i].name) != len) ||
+                    strcmp(ee->mFields[i].name, nin[i]) ||
+                    (ee->mFields[i].arraySize != asize)) {
                     match = false;
                     break;
                 }
@@ -284,9 +294,20 @@
     e->mFields = new ElementField_t [count];
     e->mFieldCount = count;
     for (size_t ct=0; ct < count; ct++) {
+        size_t len;
+        uint32_t asize = 1;
+        if (lengths) {
+            len = lengths[ct];
+        } else {
+            len = strlen(nin[ct]);
+        }
+        if (asin) {
+            asize = asin[ct];
+        }
+
         e->mFields[ct].e.set(ein[ct]);
-        e->mFields[ct].name.setTo(nin[ct], lengths[ct]);
-        e->mFields[ct].arraySize = asin[ct];
+        e->mFields[ct].name = rsuCopyString(nin[ct], len);
+        e->mFields[ct].arraySize = asize;
     }
     e->compute();
 
@@ -341,33 +362,6 @@
     }
 }
 
-Element::Builder::Builder() {
-    const uint32_t initialCapacity = 32;
-    mBuilderElementRefs.setCapacity(initialCapacity);
-    mBuilderElements.setCapacity(initialCapacity);
-    mBuilderNameStrings.setCapacity(initialCapacity);
-    mBuilderNameLengths.setCapacity(initialCapacity);
-    mBuilderArrays.setCapacity(initialCapacity);
-}
-
-void Element::Builder::add(const Element *e, const char *nameStr, uint32_t arraySize) {
-    mBuilderElementRefs.push(ObjectBaseRef<const Element>(e));
-    mBuilderElements.push(e);
-    mBuilderNameStrings.push(nameStr);
-    mBuilderNameLengths.push(strlen(nameStr));
-    mBuilderArrays.push(arraySize);
-
-}
-
-ObjectBaseRef<const Element> Element::Builder::create(Context *rsc) {
-    return Element::createRef(rsc, mBuilderElements.size(),
-                              &(mBuilderElements.editArray()[0]),
-                              &(mBuilderNameStrings.editArray()[0]),
-                              mBuilderNameLengths.editArray(),
-                              mBuilderArrays.editArray());
-}
-
-
 ElementState::ElementState() {
 }
 
@@ -407,7 +401,7 @@
 }
 }
 
-void rsaElementGetNativeData(RsContext con, RsElement elem,
+extern "C" void rsaElementGetNativeData(RsContext con, RsElement elem,
                              uint32_t *elemData, uint32_t elemDataSize) {
     rsAssert(elemDataSize == 5);
     // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
@@ -420,7 +414,7 @@
     (*elemData++) = e->getFieldCount();
 }
 
-void rsaElementGetSubElements(RsContext con, RsElement elem, uintptr_t *ids,
+extern "C" void rsaElementGetSubElements(RsContext con, RsElement elem, uintptr_t *ids,
                               const char **names, size_t *arraySizes, uint32_t dataSize) {
     Element *e = static_cast<Element *>(elem);
     rsAssert(e->getFieldCount() == dataSize);
diff --git a/rsElement.h b/rsElement.h
index 1eae46d..b97dfe5 100644
--- a/rsElement.h
+++ b/rsElement.h
@@ -57,18 +57,6 @@
     };
     Hal mHal;
 
-    class Builder {
-    public:
-        Builder();
-        void add(const Element *e, const char *nameStr, uint32_t arraySize);
-        ObjectBaseRef<const Element> create(Context *rsc);
-    private:
-        Vector<ObjectBaseRef<const Element> > mBuilderElementRefs;
-        Vector<const Element *> mBuilderElements;
-        Vector<const char*> mBuilderNameStrings;
-        Vector<size_t> mBuilderNameLengths;
-        Vector<uint32_t> mBuilderArrays;
-    };
     uint32_t getGLType() const;
     uint32_t getGLFormat() const;
 
@@ -95,7 +83,7 @@
 
     uint32_t getFieldCount() const {return mFieldCount;}
     const Element * getField(uint32_t idx) const {return mFields[idx].e.get();}
-    const char * getFieldName(uint32_t idx) const {return mFields[idx].name.string();}
+    const char * getFieldName(uint32_t idx) const {return mFields[idx].name;}
     uint32_t getFieldArraySize(uint32_t idx) const {return mFields[idx].arraySize;}
 
     const Component & getComponent() const {return mComponent;}
@@ -133,8 +121,8 @@
     static const Element* create(Context *rsc, size_t count,
                                  const Element **ein,
                                  const char **nin,
-                                 const size_t * lengths,
-                                 const uint32_t *asin) {
+                                 const size_t * lengths = NULL,
+                                 const uint32_t *asin = NULL) {
         ObjectBaseRef<const Element> elem = createRef(rsc, count, ein, nin, lengths, asin);
         elem->incUserRef();
         return elem.get();
@@ -149,7 +137,7 @@
     void clear();
 
     typedef struct {
-        String8 name;
+        const char *name;
         ObjectBaseRef<const Element> e;
         uint32_t offsetBits;
         uint32_t offsetBitsUnpadded;
diff --git a/rsFileA3D.cpp b/rsFileA3D.cpp
index c79d008..3b963fe 100644
--- a/rsFileA3D.cpp
+++ b/rsFileA3D.cpp
@@ -73,7 +73,8 @@
     uint32_t numIndexEntries = headerStream->loadU32();
     for (uint32_t i = 0; i < numIndexEntries; i ++) {
         A3DIndexEntry *entry = new A3DIndexEntry();
-        headerStream->loadString(&entry->mObjectName);
+        entry->mObjectName = headerStream->loadString();
+
         //ALOGV("Header data, entry name = %s", entry->mObjectName.string());
         entry->mType = (RsA3DClassID)headerStream->loadU32();
         if (mUse64BitOffsets){
@@ -214,6 +215,10 @@
     return mIndex.size();
 }
 
+FileA3D::A3DIndexEntry::~A3DIndexEntry() {
+    delete[] mObjectName;
+}
+
 const FileA3D::A3DIndexEntry *FileA3D::getIndexEntry(size_t index) const {
     if (index < mIndex.size()) {
         return mIndex[index];
@@ -320,7 +325,7 @@
     uint32_t writeIndexSize = mWriteIndex.size();
     headerStream.addU32(writeIndexSize);
     for (uint32_t i = 0; i < writeIndexSize; i ++) {
-        headerStream.addString(&mWriteIndex[i]->mObjectName);
+        headerStream.addString(mWriteIndex[i]->mObjectName);
         headerStream.addU32((uint32_t)mWriteIndex[i]->mType);
         if (mUse64BitOffsets){
             headerStream.addOffset(mWriteIndex[i]->mOffset);
@@ -334,8 +339,7 @@
     }
 
     // Write our magic string so we know we are reading the right file
-    String8 magicString(A3D_MAGIC_KEY);
-    fwrite(magicString.string(), sizeof(char), magicString.size(), writeHandle);
+    fwrite(A3D_MAGIC_KEY, sizeof(char), strlen(A3D_MAGIC_KEY), writeHandle);
 
     // Store the size of the header to make it easier to parse when we read it
     uint64_t headerSize = headerStream.getPos();
@@ -369,7 +373,7 @@
         mWriteStream = new OStream(initialStreamSize, false);
     }
     A3DIndexEntry *indexEntry = new A3DIndexEntry();
-    indexEntry->mObjectName.setTo(obj->getName());
+    indexEntry->mObjectName = rsuCopyString(obj->getName());
     indexEntry->mType = obj->getClassId();
     indexEntry->mOffset = mWriteStream->getPos();
     indexEntry->mRsObj = obj;
@@ -420,7 +424,7 @@
     for (uint32_t i = 0; i < numFileEntries; i ++) {
         const FileA3D::A3DIndexEntry *entry = fa3d->getIndexEntry(i);
         fileEntries[i].classID = entry->getType();
-        fileEntries[i].objectName = entry->getObjectName().string();
+        fileEntries[i].objectName = rsuCopyString(entry->getObjectName());
     }
 }
 
diff --git a/rsFileA3D.h b/rsFileA3D.h
index 06b90d7..8bf36b9 100644
--- a/rsFileA3D.h
+++ b/rsFileA3D.h
@@ -19,7 +19,6 @@
 
 #include "rsMesh.h"
 
-#include <utils/String8.h>
 #include "rsStream.h"
 #include <stdio.h>
 
@@ -43,19 +42,21 @@
     bool mUse64BitOffsets;
 
     class A3DIndexEntry {
-        String8 mObjectName;
+        const char *mObjectName;
         RsA3DClassID mType;
         uint64_t mOffset;
         uint64_t mLength;
         ObjectBase *mRsObj;
     public:
         friend class FileA3D;
-        const String8 &getObjectName() const {
+        const char *getObjectName() const {
             return mObjectName;
         }
         RsA3DClassID getType() const {
             return mType;
         }
+
+        ~A3DIndexEntry();
     };
 
     bool load(FILE *f);
diff --git a/rsFont.cpp b/rsFont.cpp
index 3665a3d..d302182 100644
--- a/rsFont.cpp
+++ b/rsFont.cpp
@@ -20,7 +20,9 @@
 #include "rsFont.h"
 #include "rsProgramFragment.h"
 #include "rsMesh.h"
+#ifdef HAVE_ANDROID_OS
 #include <cutils/properties.h>
+#endif
 
 #ifndef ANDROID_RS_SERIALIZE
 #include <ft2build.h>
@@ -56,7 +58,7 @@
         return false;
     }
 
-    mFontName = name;
+    mFontName = rsuCopyString(name);
     mFontSize = fontSize;
     mDpi = dpi;
 
@@ -337,27 +339,31 @@
     mLibrary = NULL;
 #endif //ANDROID_RS_SERIALIZE
 
+    float gamma = DEFAULT_TEXT_GAMMA;
+    int32_t blackThreshold = DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD;
+    int32_t whiteThreshold = DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD;
+
+#ifdef HAVE_ANDROID_OS
     // Get the renderer properties
     char property[PROPERTY_VALUE_MAX];
 
     // Get the gamma
-    float gamma = DEFAULT_TEXT_GAMMA;
     if (property_get(PROPERTY_TEXT_GAMMA, property, NULL) > 0) {
         gamma = atof(property);
     }
 
     // Get the black gamma threshold
-    int32_t blackThreshold = DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD;
     if (property_get(PROPERTY_TEXT_BLACK_GAMMA_THRESHOLD, property, NULL) > 0) {
         blackThreshold = atoi(property);
     }
-    mBlackThreshold = (float)(blackThreshold) / 255.0f;
 
     // Get the white gamma threshold
-    int32_t whiteThreshold = DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD;
     if (property_get(PROPERTY_TEXT_WHITE_GAMMA_THRESHOLD, property, NULL) > 0) {
         whiteThreshold = atoi(property);
     }
+#endif
+
+    mBlackThreshold = (float)(blackThreshold) / 255.0f;
     mWhiteThreshold = (float)(whiteThreshold) / 255.0f;
 
     // Compute the gamma tables
@@ -486,13 +492,13 @@
 #endif //ANDROID_RS_SERIALIZE
 
 void FontState::initRenderState() {
-    String8 shaderString("varying vec2 varTex0;\n");
-    shaderString.append("void main() {\n");
-    shaderString.append("  lowp vec4 col = UNI_Color;\n");
-    shaderString.append("  col.a = texture2D(UNI_Tex0, varTex0.xy).a;\n");
-    shaderString.append("  col.a = pow(col.a, UNI_Gamma);\n");
-    shaderString.append("  gl_FragColor = col;\n");
-    shaderString.append("}\n");
+    const char *shaderString = "varying vec2 varTex0;\n"
+                               "void main() {\n"
+                               "  lowp vec4 col = UNI_Color;\n"
+                               "  col.a = texture2D(UNI_Tex0, varTex0.xy).a;\n"
+                               "  col.a = pow(col.a, UNI_Gamma);\n"
+                               "  gl_FragColor = col;\n"
+                               "}\n";
 
     const char *textureNames[] = { "Tex0" };
     const size_t textureNamesLengths[] = { 4 };
@@ -502,10 +508,10 @@
                                                                 RS_KIND_USER, false, 4);
     ObjectBaseRef<const Element> gammaElem = Element::createRef(mRSC, RS_TYPE_FLOAT_32,
                                                                 RS_KIND_USER, false, 1);
-    Element::Builder builder;
-    builder.add(colorElem.get(), "Color", 1);
-    builder.add(gammaElem.get(), "Gamma", 1);
-    ObjectBaseRef<const Element> constInput = builder.create(mRSC);
+
+    const char *ebn1[] = { "Color", "Gamma" };
+    const Element *ebe1[] = {colorElem.get(), gammaElem.get()};
+    ObjectBaseRef<const Element> constInput = Element::create(mRSC, 2, ebe1, ebn1);
 
     ObjectBaseRef<Type> inputType = Type::getTypeRef(mRSC, constInput.get(), 1, 0, 0, false, false, 0);
 
@@ -518,7 +524,7 @@
     mFontShaderFConstant.set(Allocation::createAllocation(mRSC, inputType.get(),
                                                           RS_ALLOCATION_USAGE_SCRIPT |
                                                           RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS));
-    ProgramFragment *pf = new ProgramFragment(mRSC, shaderString.string(), shaderString.length(),
+    ProgramFragment *pf = new ProgramFragment(mRSC, shaderString, strlen(shaderString),
                                               textureNames, numTextures, textureNamesLengths,
                                               tmp, 4);
     mFontShaderF.set(pf);
@@ -601,10 +607,9 @@
     ObjectBaseRef<const Element> posElem = Element::createRef(mRSC, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 3);
     ObjectBaseRef<const Element> texElem = Element::createRef(mRSC, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 2);
 
-    Element::Builder builder;
-    builder.add(posElem.get(), "position", 1);
-    builder.add(texElem.get(), "texture0", 1);
-    ObjectBaseRef<const Element> vertexDataElem = builder.create(mRSC);
+    const char *ebn1[] = { "position", "texture0" };
+    const Element *ebe1[] = {posElem.get(), texElem.get()};
+    ObjectBaseRef<const Element> vertexDataElem = Element::create(mRSC, 2, ebe1, ebn1);
 
     ObjectBaseRef<Type> vertexDataType = Type::getTypeRef(mRSC, vertexDataElem.get(),
                                                           mMaxNumberOfQuads * 4,
@@ -634,11 +639,9 @@
     initVertexArrayBuffers();
 
     // We store a string with letters in a rough frequency of occurrence
-    mLatinPrecache = String8(" eisarntolcdugpmhbyfvkwzxjq");
-    mLatinPrecache += String8("EISARNTOLCDUGPMHBYFVKWZXJQ");
-    mLatinPrecache += String8(",.?!()-+@;:`'");
-    mLatinPrecache += String8("0123456789");
-
+    mLatinPrecache = " eisarntolcdugpmhbyfvkwzxjq"
+                     "EISARNTOLCDUGPMHBYFVKWZXJQ"
+                     ",.?!()-+@;:`'0123456789";
     mInitialized = true;
 }
 
@@ -734,7 +737,8 @@
     // Remaining capacity is measured in %
     uint32_t remainingCapacity = getRemainingCacheCapacity();
     uint32_t precacheIdx = 0;
-    while (remainingCapacity > 25 && precacheIdx < mLatinPrecache.size()) {
+    const size_t l = strlen(mLatinPrecache);
+    while ((remainingCapacity > 25) && (precacheIdx < l)) {
         font->getCachedUTFChar((int32_t)mLatinPrecache[precacheIdx]);
         remainingCapacity = getRemainingCacheCapacity();
         precacheIdx ++;
@@ -753,11 +757,12 @@
     Font *currentFont = mRSC->getFont();
     if (!currentFont) {
         if (!mDefault.get()) {
-            String8 fontsDir("/fonts/Roboto-Regular.ttf");
-            String8 fullPath(getenv("ANDROID_ROOT"));
-            fullPath += fontsDir;
-
-            mDefault.set(Font::create(mRSC, fullPath.string(), 8, mRSC->getDPI()));
+            char fullPath[1024];
+            const char * root = getenv("ANDROID_ROOT");
+            rsAssert(strlen(root) < 256);
+            strcpy(fullPath, root);
+            strcat(fullPath, "/fonts/Roboto-Regular.ttf");
+            mDefault.set(Font::create(mRSC, fullPath, 8, mRSC->getDPI()));
         }
         currentFont = mDefault.get();
     }
diff --git a/rsFont.h b/rsFont.h
index 8f43a2a..7bac508 100644
--- a/rsFont.h
+++ b/rsFont.h
@@ -18,7 +18,6 @@
 #define ANDROID_RS_FONT_H
 
 #include "rsStream.h"
-#include <utils/String8.h>
 #include <utils/Vector.h>
 #include <utils/KeyedVector.h>
 
@@ -113,7 +112,7 @@
         int32_t mBitmapTop;
     };
 
-    String8 mFontName;
+    const char *mFontName;
     float mFontSize;
     uint32_t mDpi;
 
@@ -183,7 +182,7 @@
     uint32_t getRemainingCacheCapacity();
 
     void precacheLatin(Font *font);
-    String8 mLatinPrecache;
+    const char *mLatinPrecache;
 
     Context *mRSC;
 
diff --git a/rsGrallocConsumer.cpp b/rsGrallocConsumer.cpp
new file mode 100644
index 0000000..e3bd9d4
--- /dev/null
+++ b/rsGrallocConsumer.cpp
@@ -0,0 +1,187 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define ATRACE_TAG ATRACE_TAG_RS
+
+#include "rsContext.h"
+#include "rsAllocation.h"
+#include "rsAdapter.h"
+#include "rs_hal.h"
+
+#include <cutils/compiler.h>
+#include <utils/Log.h>
+#include "rsGrallocConsumer.h"
+#include <ui/GraphicBuffer.h>
+
+
+namespace android {
+namespace renderscript {
+
+GrallocConsumer::GrallocConsumer(Allocation *a, const sp<IGraphicBufferConsumer>& bq) :
+    ConsumerBase(bq, true)
+{
+    mAlloc = a;
+    mConsumer->setConsumerUsageBits(GRALLOC_USAGE_SW_READ_OFTEN);
+    mConsumer->setMaxAcquiredBufferCount(2);
+
+    uint32_t y = a->mHal.drvState.lod[0].dimY;
+    if (y < 1) y = 1;
+    mConsumer->setDefaultBufferSize(a->mHal.drvState.lod[0].dimX, y);
+
+    //mBufferQueue->setDefaultBufferFormat(defaultFormat);
+    //mBufferQueue->setConsumerName(name);
+}
+
+GrallocConsumer::~GrallocConsumer() {
+    // ConsumerBase destructor does all the work.
+}
+
+
+
+status_t GrallocConsumer::lockNextBuffer() {
+    Mutex::Autolock _l(mMutex);
+    status_t err;
+
+    if (mAcquiredBuffer.mSlot != BufferQueue::INVALID_BUFFER_SLOT) {
+        err = releaseAcquiredBufferLocked();
+        if (err) {
+            return err;
+        }
+    }
+
+    BufferQueue::BufferItem b;
+
+    err = acquireBufferLocked(&b, 0);
+    if (err != OK) {
+        if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
+            return BAD_VALUE;
+        } else {
+            ALOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
+            return err;
+        }
+    }
+
+    int buf = b.mBuf;
+
+    if (b.mFence.get()) {
+        err = b.mFence->waitForever("GrallocConsumer::lockNextBuffer");
+        if (err != OK) {
+            ALOGE("Failed to wait for fence of acquired buffer: %s (%d)",
+                    strerror(-err), err);
+            return err;
+        }
+    }
+
+    void *bufferPointer = NULL;
+    android_ycbcr ycbcr = android_ycbcr();
+
+    if (mSlots[buf].mGraphicBuffer->getPixelFormat() ==
+            HAL_PIXEL_FORMAT_YCbCr_420_888) {
+        err = mSlots[buf].mGraphicBuffer->lockYCbCr(
+            GraphicBuffer::USAGE_SW_READ_OFTEN,
+            b.mCrop,
+            &ycbcr);
+
+        if (err != OK) {
+            ALOGE("Unable to lock YCbCr buffer for CPU reading: %s (%d)",
+                    strerror(-err), err);
+            return err;
+        }
+        bufferPointer = ycbcr.y;
+    } else {
+        err = mSlots[buf].mGraphicBuffer->lock(
+            GraphicBuffer::USAGE_SW_READ_OFTEN,
+            b.mCrop,
+            &bufferPointer);
+
+        if (err != OK) {
+            ALOGE("Unable to lock buffer for CPU reading: %s (%d)",
+                    strerror(-err), err);
+            return err;
+        }
+    }
+
+    size_t lockedIdx = 0;
+    assert(mAcquiredBuffer.mSlot == BufferQueue::INVALID_BUFFER_SLOT);
+
+    mAcquiredBuffer.mSlot = buf;
+    mAcquiredBuffer.mBufferPointer = bufferPointer;
+    mAcquiredBuffer.mGraphicBuffer = mSlots[buf].mGraphicBuffer;
+
+    mAlloc->mHal.drvState.lod[0].mallocPtr = reinterpret_cast<uint8_t*>(bufferPointer);
+    mAlloc->mHal.drvState.lod[0].stride = mSlots[buf].mGraphicBuffer->getStride() *
+            mAlloc->mHal.state.type->getElementSizeBytes();
+    mAlloc->mHal.state.nativeBuffer = mAcquiredBuffer.mGraphicBuffer->getNativeBuffer();
+    mAlloc->mHal.state.timestamp = b.mTimestamp;
+
+    assert(mAlloc->mHal.drvState.lod[0].dimX ==
+           mSlots[buf].mGraphicBuffer->getWidth());
+    assert(mAlloc->mHal.drvState.lod[0].dimY ==
+           mSlots[buf].mGraphicBuffer->getHeight());
+
+    //mAlloc->format = mSlots[buf].mGraphicBuffer->getPixelFormat();
+
+    //mAlloc->crop        = b.mCrop;
+    //mAlloc->transform   = b.mTransform;
+    //mAlloc->scalingMode = b.mScalingMode;
+    //mAlloc->frameNumber = b.mFrameNumber;
+
+    if (mAlloc->mHal.state.yuv) {
+        mAlloc->mHal.drvState.lod[1].mallocPtr = ycbcr.cb;
+        mAlloc->mHal.drvState.lod[2].mallocPtr = ycbcr.cr;
+
+        mAlloc->mHal.drvState.lod[0].stride = ycbcr.ystride;
+        mAlloc->mHal.drvState.lod[1].stride = ycbcr.cstride;
+        mAlloc->mHal.drvState.lod[2].stride = ycbcr.cstride;
+    }
+
+    return OK;
+}
+
+status_t GrallocConsumer::unlockBuffer() {
+    Mutex::Autolock _l(mMutex);
+    return releaseAcquiredBufferLocked();
+}
+
+status_t GrallocConsumer::releaseAcquiredBufferLocked() {
+    status_t err;
+
+    err = mAcquiredBuffer.mGraphicBuffer->unlock();
+    if (err != OK) {
+        ALOGE("%s: Unable to unlock graphic buffer", __FUNCTION__);
+        return err;
+    }
+    int buf = mAcquiredBuffer.mSlot;
+
+    // release the buffer if it hasn't already been freed by the BufferQueue.
+    // This can happen, for example, when the producer of this buffer
+    // disconnected after this buffer was acquired.
+    if (CC_LIKELY(mAcquiredBuffer.mGraphicBuffer ==
+            mSlots[buf].mGraphicBuffer)) {
+        releaseBufferLocked(
+                buf, mAcquiredBuffer.mGraphicBuffer,
+                EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
+    }
+
+    mAcquiredBuffer.mSlot = BufferQueue::INVALID_BUFFER_SLOT;
+    mAcquiredBuffer.mBufferPointer = NULL;
+    mAcquiredBuffer.mGraphicBuffer.clear();
+    return OK;
+}
+
+} // namespace renderscript
+} // namespace android
+
diff --git a/rsGrallocConsumer.h b/rsGrallocConsumer.h
new file mode 100644
index 0000000..9e4fc58
--- /dev/null
+++ b/rsGrallocConsumer.h
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_RS_GRALLOC_CONSUMER_H
+#define ANDROID_RS_GRALLOC_CONSUMER_H
+
+#include <gui/ConsumerBase.h>
+
+#include <ui/GraphicBuffer.h>
+
+#include <utils/String8.h>
+#include <utils/Vector.h>
+#include <utils/threads.h>
+
+
+// ---------------------------------------------------------------------------
+namespace android {
+namespace renderscript {
+
+class Allocation;
+
+/**
+ * CpuConsumer is a BufferQueue consumer endpoint that allows direct CPU
+ * access to the underlying gralloc buffers provided by BufferQueue. Multiple
+ * buffers may be acquired by it at once, to be used concurrently by the
+ * CpuConsumer owner. Sets gralloc usage flags to be software-read-only.
+ * This queue is synchronous by default.
+ */
+class GrallocConsumer : public ConsumerBase
+{
+  public:
+    typedef ConsumerBase::FrameAvailableListener FrameAvailableListener;
+
+    GrallocConsumer(Allocation *, const sp<IGraphicBufferConsumer>& bq);
+
+    virtual ~GrallocConsumer();
+    status_t lockNextBuffer();
+    status_t unlockBuffer();
+
+  private:
+    status_t releaseAcquiredBufferLocked();
+    Allocation *mAlloc;
+
+    // Tracking for buffers acquired by the user
+    struct AcquiredBuffer {
+        // Need to track the original mSlot index and the buffer itself because
+        // the mSlot entry may be freed/reused before the acquired buffer is
+        // released.
+        int mSlot;
+        sp<GraphicBuffer> mGraphicBuffer;
+        void *mBufferPointer;
+
+        AcquiredBuffer() :
+                mSlot(BufferQueue::INVALID_BUFFER_SLOT),
+                mBufferPointer(NULL) {
+        }
+    };
+    AcquiredBuffer mAcquiredBuffer;
+};
+
+} // namespace renderscript
+} // namespace android
+
+#endif // ANDROID_RS_GRALLOC_CONSUMER_H
+
diff --git a/rsMesh.cpp b/rsMesh.cpp
index 651a8f3..8decfc5 100644
--- a/rsMesh.cpp
+++ b/rsMesh.cpp
@@ -81,9 +81,7 @@
 void Mesh::serialize(Context *rsc, OStream *stream) const {
     // Need to identify ourselves
     stream->addU32((uint32_t)getClassId());
-
-    String8 name(getName());
-    stream->addString(&name);
+    stream->addString(getName());
 
     // Store number of vertex streams
     stream->addU32(mHal.state.vertexBuffersCount);
@@ -113,8 +111,7 @@
         return NULL;
     }
 
-    String8 name;
-    stream->loadString(&name);
+    const char *name = stream->loadString();
 
     uint32_t vertexBuffersCount = stream->loadU32();
     ObjectBaseRef<Allocation> *vertexBuffers = NULL;
@@ -148,7 +145,7 @@
     }
 
     Mesh *mesh = new Mesh(rsc, vertexBuffersCount, primitivesCount);
-    mesh->setName(name.string(), name.size());
+    mesh->assignName(name);
     for (uint32_t vCount = 0; vCount < vertexBuffersCount; vCount ++) {
         mesh->setVertexBuffer(vertexBuffers[vCount].get(), vCount);
     }
diff --git a/rsObjectBase.cpp b/rsObjectBase.cpp
index c8d7349..bf5d3b5 100644
--- a/rsObjectBase.cpp
+++ b/rsObjectBase.cpp
@@ -284,4 +284,3 @@
     asyncUnlock();
     return false;
 }
-
diff --git a/rsObjectBase.h b/rsObjectBase.h
index 12c89a3..85f8fd8 100644
--- a/rsObjectBase.h
+++ b/rsObjectBase.h
@@ -44,6 +44,7 @@
     const char * getName() const {
         return mName;
     }
+    void assignName(const char *s) {mName = s;}
     void setName(const char *);
     void setName(const char *, uint32_t len);
 
diff --git a/rsProgram.cpp b/rsProgram.cpp
index 7114f29..806d1de 100644
--- a/rsProgram.cpp
+++ b/rsProgram.cpp
@@ -77,7 +77,9 @@
         shaderText += internalTokenLen;
         shaderLength -= internalTokenLen;
     }
-    mUserShader.setTo(shaderText, shaderLength);
+
+    mUserShader = rsuCopyString(shaderText, shaderLength);
+    mUserShaderLen = shaderLength;
 }
 
 Program::~Program() {
@@ -98,6 +100,12 @@
     mHal.state.inputElementsCount = 0;
     mHal.state.constantsCount = 0;
     mHal.state.texturesCount = 0;
+
+    if (mUserShader != NULL) {
+        delete[] mUserShader;
+        mUserShader = NULL;
+    }
+    mUserShaderLen = 0;
 }
 
 bool Program::freeChildren() {
@@ -134,6 +142,9 @@
     mConstants = NULL;
 
     mIsInternal = false;
+
+    mUserShader = NULL;
+    mUserShaderLen = 0;
 }
 
 void Program::bindAllocation(Context *rsc, Allocation *alloc, uint32_t slot) {
diff --git a/rsProgram.h b/rsProgram.h
index d032930..be5631f 100644
--- a/rsProgram.h
+++ b/rsProgram.h
@@ -78,7 +78,8 @@
     ObjectBaseRef<Element> *mInputElements;
 
     bool mIsInternal;
-    String8 mUserShader;
+    const char *mUserShader;
+    size_t mUserShaderLen;
     void initMemberVars();
 };
 
diff --git a/rsProgramFragment.cpp b/rsProgramFragment.cpp
index f2885a9..877b8bd 100644
--- a/rsProgramFragment.cpp
+++ b/rsProgramFragment.cpp
@@ -30,7 +30,7 @@
     mConstantColor[2] = 1.f;
     mConstantColor[3] = 1.f;
 
-    mRSC->mHal.funcs.fragment.init(mRSC, this, mUserShader.string(), mUserShader.length(),
+    mRSC->mHal.funcs.fragment.init(mRSC, this, mUserShader, mUserShaderLen,
                                    textureNames, textureNamesCount, textureNamesLength);
 }
 
@@ -93,18 +93,20 @@
 }
 
 void ProgramFragmentState::init(Context *rsc) {
-    String8 shaderString(RS_SHADER_INTERNAL);
-    shaderString.append("varying lowp vec4 varColor;\n");
-    shaderString.append("varying vec2 varTex0;\n");
-    shaderString.append("void main() {\n");
-    shaderString.append("  lowp vec4 col = UNI_Color;\n");
-    shaderString.append("  gl_FragColor = col;\n");
-    shaderString.append("}\n");
+    const char *shaderString =
+            RS_SHADER_INTERNAL
+            "varying lowp vec4 varColor;\n"
+            "varying vec2 varTex0;\n"
+            "void main() {\n"
+            "  lowp vec4 col = UNI_Color;\n"
+            "  gl_FragColor = col;\n"
+            "}\n";
 
     ObjectBaseRef<const Element> colorElem = Element::createRef(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4);
-    Element::Builder builder;
-    builder.add(colorElem.get(), "Color", 1);
-    ObjectBaseRef<const Element> constInput = builder.create(rsc);
+
+    const char *enames[] = { "Color" };
+    const Element *eins[] = {colorElem.get()};
+    ObjectBaseRef<const Element> constInput = Element::create(rsc, 1, eins, enames);
 
     ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1, 0, 0, false, false, 0);
 
@@ -114,7 +116,7 @@
 
     Allocation *constAlloc = Allocation::createAllocation(rsc, inputType.get(),
                               RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
-    ProgramFragment *pf = new ProgramFragment(rsc, shaderString.string(), shaderString.length(),
+    ProgramFragment *pf = new ProgramFragment(rsc, shaderString, strlen(shaderString),
                                               NULL, 0, NULL, tmp, 2);
     pf->bindAllocation(rsc, constAlloc, 0);
     pf->setConstantColor(rsc, 1.0f, 1.0f, 1.0f, 1.0f);
diff --git a/rsProgramVertex.cpp b/rsProgramVertex.cpp
index 5265216..549dcae 100644
--- a/rsProgramVertex.cpp
+++ b/rsProgramVertex.cpp
@@ -27,7 +27,7 @@
 
                              const uint32_t * params, size_t paramLength)
     : Program(rsc, shaderText, shaderLength, params, paramLength) {
-    mRSC->mHal.funcs.vertex.init(mRSC, this, mUserShader.string(), mUserShader.length(),
+    mRSC->mHal.funcs.vertex.init(mRSC, this, mUserShader, mUserShaderLen,
                                  textureNames, textureNamesCount, textureNamesLength);
 }
 
@@ -174,31 +174,27 @@
     ObjectBaseRef<const Element> f4Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
                                                              RS_KIND_USER, false, 4);
 
-    Element::Builder constBuilder;
-    constBuilder.add(matrixElem.get(), "MV", 1);
-    constBuilder.add(matrixElem.get(), "P", 1);
-    constBuilder.add(matrixElem.get(), "TexMatrix", 1);
-    constBuilder.add(matrixElem.get(), "MVP", 1);
-    ObjectBaseRef<const Element> constInput = constBuilder.create(rsc);
+    const char *ebn1[] = { "MV", "P", "TexMatrix", "MVP" };
+    const Element *ebe1[] = {matrixElem.get(), matrixElem.get(),
+            matrixElem.get(), matrixElem.get()};
+    ObjectBaseRef<const Element> constInput = Element::create(rsc, 4, ebe1, ebn1);
 
-    Element::Builder inputBuilder;
-    inputBuilder.add(f4Elem.get(), "position", 1);
-    inputBuilder.add(f4Elem.get(), "color", 1);
-    inputBuilder.add(f3Elem.get(), "normal", 1);
-    inputBuilder.add(f2Elem.get(), "texture0", 1);
-    ObjectBaseRef<const Element> attrElem = inputBuilder.create(rsc);
+    const char *ebn2[] = { "position", "color", "normal", "texture0" };
+    const Element *ebe2[] = {f4Elem.get(), f4Elem.get(), f3Elem.get(), f2Elem.get()};
+    ObjectBaseRef<const Element> attrElem = Element::create(rsc, 4, ebe2, ebn2);
 
     ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1, 0, 0, false, false, 0);
 
-    String8 shaderString(RS_SHADER_INTERNAL);
-    shaderString.append("varying vec4 varColor;\n");
-    shaderString.append("varying vec2 varTex0;\n");
-    shaderString.append("void main() {\n");
-    shaderString.append("  gl_Position = UNI_MVP * ATTRIB_position;\n");
-    shaderString.append("  gl_PointSize = 1.0;\n");
-    shaderString.append("  varColor = ATTRIB_color;\n");
-    shaderString.append("  varTex0 = ATTRIB_texture0;\n");
-    shaderString.append("}\n");
+    const char *shaderString =
+            RS_SHADER_INTERNAL
+            "varying vec4 varColor;\n"
+            "varying vec2 varTex0;\n"
+            "void main() {\n"
+            "  gl_Position = UNI_MVP * ATTRIB_position;\n"
+            "  gl_PointSize = 1.0;\n"
+            "  varColor = ATTRIB_color;\n"
+            "  varTex0 = ATTRIB_texture0;\n"
+            "}\n";
 
     uint32_t tmp[4];
     tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
@@ -206,7 +202,7 @@
     tmp[2] = RS_PROGRAM_PARAM_INPUT;
     tmp[3] = (uint32_t)attrElem.get();
 
-    ProgramVertex *pv = new ProgramVertex(rsc, shaderString.string(), shaderString.length(),
+    ProgramVertex *pv = new ProgramVertex(rsc, shaderString, strlen(shaderString),
                                           NULL, 0, NULL, tmp, 4);
     Allocation *alloc = Allocation::createAllocation(rsc, inputType.get(),
                               RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
diff --git a/rsRuntime.h b/rsRuntime.h
index 2939176..1d81ffb 100644
--- a/rsRuntime.h
+++ b/rsRuntime.h
@@ -108,6 +108,10 @@
                     int32_t *left, int32_t *right, int32_t *top, int32_t *bottom);
 void rsrBindFont(Context *, Font *);
 void rsrFontColor(Context *, float r, float g, float b, float a);
+
+void rsrAllocationIoSend(Context *, Allocation *);
+void rsrAllocationIoReceive(Context *, Allocation *);
+
 #endif
 
 //////////////////////////////////////////////////////////////////////////////
diff --git a/rsSampler.cpp b/rsSampler.cpp
index fededb1..2a8476a 100644
--- a/rsSampler.cpp
+++ b/rsSampler.cpp
@@ -98,7 +98,13 @@
     }
     ObjectBase::asyncUnlock();
 
-    Sampler *s = new Sampler(rsc, magFilter, minFilter, wrapS, wrapT, wrapR, aniso);
+    void* allocMem = rsc->mHal.funcs.allocRuntimeMem(sizeof(Sampler), 0);
+    if (!allocMem) {
+        rsc->setError(RS_ERROR_FATAL_DRIVER, "Couldn't allocate memory for Allocation");
+        return NULL;
+    }
+
+    Sampler *s = new (allocMem) Sampler(rsc, magFilter, minFilter, wrapS, wrapT, wrapR, aniso);
     returnRef.set(s);
 
     ObjectBase::asyncLock();
@@ -108,6 +114,14 @@
     return returnRef;
 }
 
+void Sampler::operator delete(void* ptr) {
+    if (ptr) {
+        Sampler *s = (Sampler*) ptr;
+        s->getContext()->mHal.funcs.freeRuntimeMem(ptr);
+    }
+}
+
+
 ////////////////////////////////
 
 namespace android {
diff --git a/rsSampler.h b/rsSampler.h
index 81220a8..2fdf707 100644
--- a/rsSampler.h
+++ b/rsSampler.h
@@ -51,6 +51,8 @@
     };
     Hal mHal;
 
+    void operator delete(void* ptr);
+
     static ObjectBaseRef<Sampler> getSampler(Context *,
                                              RsSamplerValue magFilter,
                                              RsSamplerValue minFilter,
diff --git a/rsScript.h b/rsScript.h
index a1360b5..c868aaa 100644
--- a/rsScript.h
+++ b/rsScript.h
@@ -19,6 +19,7 @@
 
 #include "rsAllocation.h"
 
+#include <utility>
 
 // ---------------------------------------------------------------------------
 namespace android {
@@ -72,6 +73,7 @@
             size_t exportedPragmaCount;
             char const **exportedPragmaKeyList;
             char const **exportedPragmaValueList;
+            const std::pair<const char *, uint32_t> *exportedForeachFuncList;
 
             int (* root)();
         };
diff --git a/rsScriptC.cpp b/rsScriptC.cpp
index d8f356c..49c9b57 100644
--- a/rsScriptC.cpp
+++ b/rsScriptC.cpp
@@ -26,6 +26,7 @@
 
 #if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
 #include "utils/Timers.h"
+#include "cutils/trace.h"
 #endif
 
 #include <sys/stat.h>
@@ -166,15 +167,32 @@
                          const void * usr,
                          size_t usrBytes,
                          const RsScriptCall *sc) {
+    // Trace this function call.
+    // To avoid overhead, we only build the string, if tracing is actually
+    // enabled.
+    String8 *AString = NULL;
+    const char *String = "";
+    if (ATRACE_ENABLED()) {
+        AString = new String8("runForEach_");
+        AString->append(mHal.info.exportedForeachFuncList[slot].first);
+        String = AString->string();
+    }
+    ATRACE_NAME(String);
+    (void)String;
 
     Context::PushState ps(rsc);
 
     setupGLState(rsc);
     setupScript(rsc);
     rsc->mHal.funcs.script.invokeForEach(rsc, this, slot, ain, aout, usr, usrBytes, sc);
+
+    if (AString)
+        delete AString;
 }
 
 void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, size_t len) {
+    ATRACE_CALL();
+
     if (slot >= mHal.info.exportedFunctionCount) {
         rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
         return;
@@ -229,7 +247,7 @@
                           const char *cacheDir,
                           const uint8_t *bitcode,
                           size_t bitcodeLen) {
-
+    ATRACE_CALL();
     //ALOGE("runCompiler %p %p %p %p %p %i", rsc, this, resName, cacheDir, bitcode, bitcodeLen);
 #ifndef RS_COMPATIBILITY_LIB
 #ifndef ANDROID_RS_SERIALIZE
diff --git a/rsScriptC_Lib.cpp b/rsScriptC_Lib.cpp
index 123b8b3..842d8d2 100644
--- a/rsScriptC_Lib.cpp
+++ b/rsScriptC_Lib.cpp
@@ -171,6 +171,13 @@
     return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, true);
 }
 
+void rsrAllocationIoSend(Context *rsc, Allocation *src) {
+    src->ioSend(rsc);
+}
+
+void rsrAllocationIoReceive(Context *rsc, Allocation *src) {
+    src->ioReceive(rsc);
+}
 
 void rsrForEach(Context *rsc,
                 Script *target,
diff --git a/rsStream.cpp b/rsStream.cpp
index b9df0cc..a9587c1 100644
--- a/rsStream.cpp
+++ b/rsStream.cpp
@@ -43,10 +43,11 @@
     return loadU32();
 }
 
-void IStream::loadString(String8 *s) {
+const char * IStream::loadString() {
     uint32_t len = loadU32();
-    s->setTo((const char *)&mData[mPos], len);
+    const char *s = rsuCopyString((const char *)&mData[mPos], len);
     mPos += len;
+    return s;
 }
 
 // Output stream implementation
@@ -89,19 +90,20 @@
     }
 }
 
-void OStream::addString(String8 *s) {
-    uint32_t len = s->size();
+void OStream::addString(const char *s, size_t len) {
     addU32(len);
     if (mPos + len*sizeof(char) >= mLength) {
         growSize();
     }
     char *stringData = reinterpret_cast<char *>(&mData[mPos]);
-    for (uint32_t i = 0; i < len; i ++) {
-        stringData[i] = s->string()[i];
-    }
+    memcpy(stringData, s, len);
     mPos += len*sizeof(char);
 }
 
+void OStream::addString(const char *s) {
+    addString(s, strlen(s));
+}
+
 void OStream::growSize() {
     uint8_t *newData = (uint8_t*)malloc(mLength*2);
     memcpy(newData, mData, mLength*sizeof(uint8_t));
diff --git a/rsStream.h b/rsStream.h
index 8a192e6..d0ed62e 100644
--- a/rsStream.h
+++ b/rsStream.h
@@ -59,7 +59,7 @@
     }
     void loadByteArray(void *dest, size_t numBytes);
     uint64_t loadOffset();
-    void loadString(String8 *s);
+    const char * loadString();
     uint64_t getPos() const {
         return mPos;
     }
@@ -132,7 +132,8 @@
     }
     void addByteArray(const void *src, size_t numBytes);
     void addOffset(uint64_t v);
-    void addString(String8 *s);
+    void addString(const char *name);
+    void addString(const char *name, size_t len);
     uint64_t getPos() const {
         return mPos;
     }
diff --git a/rsType.cpp b/rsType.cpp
index 9719a04..15e2550 100644
--- a/rsType.cpp
+++ b/rsType.cpp
@@ -188,9 +188,7 @@
 void Type::serialize(Context *rsc, OStream *stream) const {
     // Need to identify ourselves
     stream->addU32((uint32_t)getClassId());
-
-    String8 name(getName());
-    stream->addString(&name);
+    stream->addString(getName());
 
     mElement->serialize(rsc, stream);
 
@@ -210,8 +208,7 @@
         return NULL;
     }
 
-    String8 name;
-    stream->loadString(&name);
+    const char *name = stream->loadString();
 
     Element *elem = Element::createFromStream(rsc, stream);
     if (!elem) {
@@ -225,6 +222,8 @@
     uint8_t faces = stream->loadU8();
     Type *type = Type::getType(rsc, elem, x, y, z, lod != 0, faces !=0, 0);
     elem->decUserRef();
+
+    delete [] name;
     return type;
 }
 
@@ -346,7 +345,7 @@
 }
 }
 
-void rsaTypeGetNativeData(RsContext con, RsType type, uintptr_t *typeData, uint32_t typeDataSize) {
+extern "C" void rsaTypeGetNativeData(RsContext con, RsType type, uintptr_t *typeData, uint32_t typeDataSize) {
     rsAssert(typeDataSize == 6);
     // Pack the data in the follofing way mHal.state.dimX; mHal.state.dimY; mHal.state.dimZ;
     // mHal.state.lodCount; mHal.state.faces; mElement; into typeData
diff --git a/rs_hal.h b/rs_hal.h
index 1ed26dc..16bd890 100644
--- a/rs_hal.h
+++ b/rs_hal.h
@@ -145,15 +145,23 @@
     struct {
         bool (*init)(const Context *rsc, Allocation *alloc, bool forceZero);
         void (*destroy)(const Context *rsc, Allocation *alloc);
+        uint32_t (*grallocBits)(const Context *rsc, Allocation *alloc);
 
         void (*resize)(const Context *rsc, const Allocation *alloc, const Type *newType,
                        bool zeroNew);
         void (*syncAll)(const Context *rsc, const Allocation *alloc, RsAllocationUsageType src);
         void (*markDirty)(const Context *rsc, const Allocation *alloc);
 
-        void * (*getSurface)(const Context *rsc, const Allocation *alloc);
         void (*setSurface)(const Context *rsc, Allocation *alloc, ANativeWindow *sur);
         void (*ioSend)(const Context *rsc, Allocation *alloc);
+
+        /**
+         * A new gralloc buffer is in use. The pointers and strides in
+         * mHal.drvState.lod[0-2] will be updated with the new values.
+         *
+         * The new gralloc handle is provided in mHal.state.nativeBuffer
+         *
+         */
         void (*ioReceive)(const Context *rsc, Allocation *alloc);
 
         void (*data1D)(const Context *rsc, const Allocation *alloc,
diff --git a/rs_native.spec b/rs_native.spec
index fcede40..e69de29 100644
--- a/rs_native.spec
+++ b/rs_native.spec
@@ -1,9 +0,0 @@
-
-AllocationIoSend {
-    param RsAllocation alloc
-    }
-
-AllocationIoReceive {
-    param RsAllocation alloc
-    }
-
diff --git a/rsg_generator.c b/rsg_generator.c
index ca26851..fd531b6 100644
--- a/rsg_generator.c
+++ b/rsg_generator.c
@@ -123,9 +123,12 @@
     fprintf(f, ")");
 }
 
-void printFuncDecls(FILE *f, const char *prefix, int addContext) {
+void printFuncDecls(FILE *f, const char *prefix, int addContext, int externC) {
     int ct;
     for (ct=0; ct < apiCount; ct++) {
+        if (externC) {
+            fprintf(f, "extern \"C\" ");
+        }
         printFuncDecl(f, &apis[ct], prefix, addContext, 0);
         fprintf(f, ";\n");
     }
@@ -400,6 +403,8 @@
         int needFlush = 0;
         const ApiEntry * api = &apis[ct];
 
+        fprintf(f, "extern \"C\" ");
+
         printFuncDecl(f, api, "rs", 0, 0);
         fprintf(f, "\n{\n");
         fprintf(f, "    ");
@@ -655,7 +660,7 @@
             fprintf(f, "namespace android {\n");
             fprintf(f, "namespace renderscript {\n");
             printStructures(f);
-            printFuncDecls(f, "rsi_", 1);
+            printFuncDecls(f, "rsi_", 1, 0);
             printPlaybackFuncs(f, "rsp_");
             fprintf(f, "\n\ntypedef struct RsPlaybackRemoteHeaderRec {\n");
             fprintf(f, "    uint32_t command;\n");
@@ -673,7 +678,7 @@
 
         case '1': // rsgApiFuncDecl.h
         {
-            printFuncDecls(f, "rs", 0);
+            printFuncDecls(f, "rs", 0, 1);
         }
         break;
 
diff --git a/scriptc/rs_allocation.rsh b/scriptc/rs_allocation.rsh
index 0f5e79d..7dffa54 100644
--- a/scriptc/rs_allocation.rsh
+++ b/scriptc/rs_allocation.rsh
@@ -134,17 +134,17 @@
  * Extract a single element from an allocation.
  */
 extern const void * __attribute__((overloadable))
-    rsGetElementAt(rs_allocation, uint32_t x);
+    rsGetElementAt(rs_allocation a, uint32_t x);
 /**
  * \overload
  */
 extern const void * __attribute__((overloadable))
-    rsGetElementAt(rs_allocation, uint32_t x, uint32_t y);
+    rsGetElementAt(rs_allocation a, uint32_t x, uint32_t y);
 /**
  * \overload
  */
 extern const void * __attribute__((overloadable))
-    rsGetElementAt(rs_allocation, uint32_t x, uint32_t y, uint32_t z);
+    rsGetElementAt(rs_allocation a, uint32_t x, uint32_t y, uint32_t z);
 
 
 #if (defined(RS_VERSION) && (RS_VERSION >= 18))
diff --git a/scriptc/rs_cl.rsh b/scriptc/rs_cl.rsh
index 788aea8..2ff5d8b 100644
--- a/scriptc/rs_cl.rsh
+++ b/scriptc/rs_cl.rsh
@@ -25,11 +25,11 @@
 
 // Conversions
 #define CVT_FUNC_2(typeout, typein)                             \
-_RS_RUNTIME typeout##2 __attribute__((overloadable))            \
+_RS_RUNTIME typeout##2 __attribute__((const, overloadable))     \
         convert_##typeout##2(typein##2 v);                      \
-_RS_RUNTIME typeout##3 __attribute__((overloadable))            \
+_RS_RUNTIME typeout##3 __attribute__((const, overloadable))     \
         convert_##typeout##3(typein##3 v);                      \
-_RS_RUNTIME typeout##4 __attribute__((overloadable))            \
+_RS_RUNTIME typeout##4 __attribute__((const, overloadable))     \
         convert_##typeout##4(typein##4 v);
 
 
@@ -92,98 +92,119 @@
 
 // Float ops, 6.11.2
 
-#define FN_FUNC_FN(fnc)                                         \
-_RS_RUNTIME float2 __attribute__((overloadable)) fnc(float2 v); \
-_RS_RUNTIME float3 __attribute__((overloadable)) fnc(float3 v); \
-_RS_RUNTIME float4 __attribute__((overloadable)) fnc(float4 v);
+#ifdef DOXYGEN
 
-#define F_FUNC_FN(fnc)                                          \
-_RS_RUNTIME float __attribute__((overloadable)) fnc(float2 v);  \
-_RS_RUNTIME float __attribute__((overloadable)) fnc(float3 v);  \
-_RS_RUNTIME float __attribute__((overloadable)) fnc(float4 v);
+#define FN_FUNC_FN(fnc)
+#define F_FUNC_FN(fnc)
+#define IN_FUNC_FN(fnc)
+#define FN_FUNC_FN_FN(fnc)
+#define F_FUNC_FN_FN(fnc)
+#define FN_FUNC_FN_F(fnc)
+#define FN_FUNC_FN_IN(fnc)
+#define FN_FUNC_FN_I(fnc)
+#define FN_FUNC_FN_PFN(fnc)
+#define FN_FUNC_FN_PIN(fnc)
+#define FN_FUNC_FN_FN_FN(fnc)
+#define FN_FUNC_FN_FN_F(fnc)
+#define FN_FUNC_FN_F_F(fnc)
+#define FN_FUNC_FN_FN_PIN(fnc)
 
-#define IN_FUNC_FN(fnc)                                         \
-_RS_RUNTIME int2 __attribute__((overloadable)) fnc(float2 v);   \
-_RS_RUNTIME int3 __attribute__((overloadable)) fnc(float3 v);   \
-_RS_RUNTIME int4 __attribute__((overloadable)) fnc(float4 v);
+#else
 
-#define FN_FUNC_FN_FN(fnc)                                                  \
-_RS_RUNTIME float2 __attribute__((overloadable)) fnc(float2 v1, float2 v2); \
-_RS_RUNTIME float3 __attribute__((overloadable)) fnc(float3 v1, float3 v2); \
-_RS_RUNTIME float4 __attribute__((overloadable)) fnc(float4 v1, float4 v2);
+#define FN_FUNC_FN(fnc)                                                \
+_RS_RUNTIME float2 __attribute__((const, overloadable)) fnc(float2 v); \
+_RS_RUNTIME float3 __attribute__((const, overloadable)) fnc(float3 v); \
+_RS_RUNTIME float4 __attribute__((const, overloadable)) fnc(float4 v);
 
-#define F_FUNC_FN_FN(fnc)                                                   \
-_RS_RUNTIME float __attribute__((overloadable)) fnc(float2 v1, float2 v2);  \
-_RS_RUNTIME float __attribute__((overloadable)) fnc(float3 v1, float3 v2);  \
-_RS_RUNTIME float __attribute__((overloadable)) fnc(float4 v1, float4 v2);
+#define F_FUNC_FN(fnc)                                                \
+_RS_RUNTIME float __attribute__((const, overloadable)) fnc(float2 v); \
+_RS_RUNTIME float __attribute__((const, overloadable)) fnc(float3 v); \
+_RS_RUNTIME float __attribute__((const, overloadable)) fnc(float4 v);
 
-#define FN_FUNC_FN_F(fnc)                                                   \
-_RS_RUNTIME float2 __attribute__((overloadable)) fnc(float2 v1, float v2);  \
-_RS_RUNTIME float3 __attribute__((overloadable)) fnc(float3 v1, float v2);  \
-_RS_RUNTIME float4 __attribute__((overloadable)) fnc(float4 v1, float v2);
+#define IN_FUNC_FN(fnc)                                              \
+_RS_RUNTIME int2 __attribute__((const, overloadable)) fnc(float2 v); \
+_RS_RUNTIME int3 __attribute__((const, overloadable)) fnc(float3 v); \
+_RS_RUNTIME int4 __attribute__((const, overloadable)) fnc(float4 v);
 
-#define FN_FUNC_FN_IN(fnc)                                                  \
-_RS_RUNTIME float2 __attribute__((overloadable)) fnc(float2 v1, int2 v2);   \
-_RS_RUNTIME float3 __attribute__((overloadable)) fnc(float3 v1, int3 v2);   \
-_RS_RUNTIME float4 __attribute__((overloadable)) fnc(float4 v1, int4 v2);   \
+#define FN_FUNC_FN_FN(fnc)                                                         \
+_RS_RUNTIME float2 __attribute__((const, overloadable)) fnc(float2 v1, float2 v2); \
+_RS_RUNTIME float3 __attribute__((const, overloadable)) fnc(float3 v1, float3 v2); \
+_RS_RUNTIME float4 __attribute__((const, overloadable)) fnc(float4 v1, float4 v2);
 
-#define FN_FUNC_FN_I(fnc)                                                   \
-_RS_RUNTIME float2 __attribute__((overloadable)) fnc(float2 v1, int v2);    \
-_RS_RUNTIME float3 __attribute__((overloadable)) fnc(float3 v1, int v2);    \
-_RS_RUNTIME float4 __attribute__((overloadable)) fnc(float4 v1, int v2);
+#define F_FUNC_FN_FN(fnc)                                                         \
+_RS_RUNTIME float __attribute__((const, overloadable)) fnc(float2 v1, float2 v2); \
+_RS_RUNTIME float __attribute__((const, overloadable)) fnc(float3 v1, float3 v2); \
+_RS_RUNTIME float __attribute__((const, overloadable)) fnc(float4 v1, float4 v2);
 
-#define FN_FUNC_FN_PFN(fnc)                         \
-_RS_RUNTIME float2 __attribute__((overloadable))    \
-        fnc(float2 v1, float2 *v2);                 \
-_RS_RUNTIME float3 __attribute__((overloadable))    \
-        fnc(float3 v1, float3 *v2);                 \
-_RS_RUNTIME float4 __attribute__((overloadable))    \
+#define FN_FUNC_FN_F(fnc)                                                         \
+_RS_RUNTIME float2 __attribute__((const, overloadable)) fnc(float2 v1, float v2); \
+_RS_RUNTIME float3 __attribute__((const, overloadable)) fnc(float3 v1, float v2); \
+_RS_RUNTIME float4 __attribute__((const, overloadable)) fnc(float4 v1, float v2);
+
+#define FN_FUNC_FN_IN(fnc)                                                       \
+_RS_RUNTIME float2 __attribute__((const, overloadable)) fnc(float2 v1, int2 v2); \
+_RS_RUNTIME float3 __attribute__((const, overloadable)) fnc(float3 v1, int3 v2); \
+_RS_RUNTIME float4 __attribute__((const, overloadable)) fnc(float4 v1, int4 v2);
+
+#define FN_FUNC_FN_I(fnc)                                                       \
+_RS_RUNTIME float2 __attribute__((const, overloadable)) fnc(float2 v1, int v2); \
+_RS_RUNTIME float3 __attribute__((const, overloadable)) fnc(float3 v1, int v2); \
+_RS_RUNTIME float4 __attribute__((const, overloadable)) fnc(float4 v1, int v2);
+
+#define FN_FUNC_FN_PFN(fnc)                            \
+_RS_RUNTIME float2 __attribute__((pure, overloadable)) \
+        fnc(float2 v1, float2 *v2);                    \
+_RS_RUNTIME float3 __attribute__((pure, overloadable)) \
+        fnc(float3 v1, float3 *v2);                    \
+_RS_RUNTIME float4 __attribute__((pure, overloadable)) \
         fnc(float4 v1, float4 *v2);
 
-#define FN_FUNC_FN_PIN(fnc)                                                 \
-_RS_RUNTIME float2 __attribute__((overloadable)) fnc(float2 v1, int2 *v2);  \
-_RS_RUNTIME float3 __attribute__((overloadable)) fnc(float3 v1, int3 *v2);  \
-_RS_RUNTIME float4 __attribute__((overloadable)) fnc(float4 v1, int4 *v2);
+#define FN_FUNC_FN_PIN(fnc)                                                      \
+_RS_RUNTIME float2 __attribute__((pure, overloadable)) fnc(float2 v1, int2 *v2); \
+_RS_RUNTIME float3 __attribute__((pure, overloadable)) fnc(float3 v1, int3 *v2); \
+_RS_RUNTIME float4 __attribute__((pure, overloadable)) fnc(float4 v1, int4 *v2);
 
-#define FN_FUNC_FN_FN_FN(fnc)                       \
-_RS_RUNTIME float2 __attribute__((overloadable))    \
-        fnc(float2 v1, float2 v2, float2 v3);       \
-_RS_RUNTIME float3 __attribute__((overloadable))    \
-        fnc(float3 v1, float3 v2, float3 v3);       \
-_RS_RUNTIME float4 __attribute__((overloadable))    \
+#define FN_FUNC_FN_FN_FN(fnc)                           \
+_RS_RUNTIME float2 __attribute__((const, overloadable)) \
+        fnc(float2 v1, float2 v2, float2 v3);           \
+_RS_RUNTIME float3 __attribute__((const, overloadable)) \
+        fnc(float3 v1, float3 v2, float3 v3);           \
+_RS_RUNTIME float4 __attribute__((const, overloadable)) \
         fnc(float4 v1, float4 v2, float4 v3);
 
-#define FN_FUNC_FN_FN_F(fnc)                        \
-_RS_RUNTIME float2 __attribute__((overloadable))    \
-        fnc(float2 v1, float2 v2, float v3);        \
-_RS_RUNTIME float3 __attribute__((overloadable))    \
-        fnc(float3 v1, float3 v2, float v3);        \
-_RS_RUNTIME float4 __attribute__((overloadable))    \
+#define FN_FUNC_FN_FN_F(fnc)                            \
+_RS_RUNTIME float2 __attribute__((const, overloadable)) \
+        fnc(float2 v1, float2 v2, float v3);            \
+_RS_RUNTIME float3 __attribute__((const, overloadable)) \
+        fnc(float3 v1, float3 v2, float v3);            \
+_RS_RUNTIME float4 __attribute__((const, overloadable)) \
         fnc(float4 v1, float4 v2, float v3);
 
-#define FN_FUNC_FN_F_F(fnc)                         \
-_RS_RUNTIME float2 __attribute__((overloadable))    \
-        fnc(float2 v1, float v2, float v3);         \
-_RS_RUNTIME float3 __attribute__((overloadable))    \
-        fnc(float3 v1, float v2, float v3);         \
-_RS_RUNTIME float4 __attribute__((overloadable))    \
+#define FN_FUNC_FN_F_F(fnc)                             \
+_RS_RUNTIME float2 __attribute__((const, overloadable)) \
+        fnc(float2 v1, float v2, float v3);             \
+_RS_RUNTIME float3 __attribute__((const, overloadable)) \
+        fnc(float3 v1, float v2, float v3);             \
+_RS_RUNTIME float4 __attribute__((const, overloadable)) \
         fnc(float4 v1, float v2, float v3);
 
-#define FN_FUNC_FN_FN_PIN(fnc)                      \
-_RS_RUNTIME float2 __attribute__((overloadable))    \
-        fnc(float2 v1, float2 v2, int2 *v3);        \
-_RS_RUNTIME float3 __attribute__((overloadable))    \
-        fnc(float3 v1, float3 v2, int3 *v3);        \
-_RS_RUNTIME float4 __attribute__((overloadable))    \
+#define FN_FUNC_FN_FN_PIN(fnc)                         \
+_RS_RUNTIME float2 __attribute__((pure, overloadable)) \
+        fnc(float2 v1, float2 v2, int2 *v3);           \
+_RS_RUNTIME float3 __attribute__((pure, overloadable)) \
+        fnc(float3 v1, float3 v2, int3 *v3);           \
+_RS_RUNTIME float4 __attribute__((pure, overloadable)) \
         fnc(float4 v1, float4 v2, int4 *v3);
 
+#endif  // DOXYGEN
+
 
 /**
  * Return the inverse cosine.
  *
  * Supports float, float2, float3, float4
  */
-extern float __attribute__((overloadable)) acos(float);
+extern float __attribute__((const, overloadable)) acos(float);
 FN_FUNC_FN(acos)
 
 /**
@@ -191,7 +212,7 @@
  *
  * Supports float, float2, float3, float4
  */
-extern float __attribute__((overloadable)) acosh(float);
+extern float __attribute__((const, overloadable)) acosh(float);
 FN_FUNC_FN(acosh)
 
 /**
@@ -199,7 +220,7 @@
  *
  * Supports float, float2, float3, float4
  */
-_RS_RUNTIME float __attribute__((overloadable)) acospi(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) acospi(float v);
 FN_FUNC_FN(acospi)
 
 /**
@@ -207,7 +228,7 @@
  *
  * Supports float, float2, float3, float4
  */
-extern float __attribute__((overloadable)) asin(float);
+extern float __attribute__((const, overloadable)) asin(float);
 FN_FUNC_FN(asin)
 
 /**
@@ -215,7 +236,7 @@
  *
  * Supports float, float2, float3, float4
  */
-extern float __attribute__((overloadable)) asinh(float);
+extern float __attribute__((const, overloadable)) asinh(float);
 FN_FUNC_FN(asinh)
 
 
@@ -224,7 +245,7 @@
  *
  * Supports float, float2, float3, float4
  */
-_RS_RUNTIME float __attribute__((overloadable)) asinpi(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) asinpi(float v);
 FN_FUNC_FN(asinpi)
 
 /**
@@ -232,7 +253,7 @@
  *
  * Supports float, float2, float3, float4
  */
-extern float __attribute__((overloadable)) atan(float);
+extern float __attribute__((const, overloadable)) atan(float);
 FN_FUNC_FN(atan)
 
 /**
@@ -244,7 +265,7 @@
  * @param y
  * @param x
  */
-extern float __attribute__((overloadable)) atan2(float y, float x);
+extern float __attribute__((const, overloadable)) atan2(float y, float x);
 FN_FUNC_FN_FN(atan2)
 
 /**
@@ -252,7 +273,7 @@
  *
  * Supports float, float2, float3, float4
  */
-extern float __attribute__((overloadable)) atanh(float);
+extern float __attribute__((const, overloadable)) atanh(float);
 FN_FUNC_FN(atanh)
 
 /**
@@ -260,7 +281,7 @@
  *
  * Supports float, float2, float3, float4
  */
-_RS_RUNTIME float __attribute__((overloadable)) atanpi(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) atanpi(float v);
 FN_FUNC_FN(atanpi)
 
 /**
@@ -272,7 +293,7 @@
  * @param y
  * @param x
  */
-_RS_RUNTIME float __attribute__((overloadable)) atan2pi(float y, float x);
+_RS_RUNTIME float __attribute__((const, overloadable)) atan2pi(float y, float x);
 FN_FUNC_FN_FN(atan2pi)
 
 
@@ -281,7 +302,7 @@
  *
  * Supports float, float2, float3, float4.
  */
-extern float __attribute__((overloadable)) cbrt(float);
+extern float __attribute__((const, overloadable)) cbrt(float);
 FN_FUNC_FN(cbrt)
 
 /**
@@ -289,7 +310,7 @@
  *
  * Supports float, float2, float3, float4.
  */
-extern float __attribute__((overloadable)) ceil(float);
+extern float __attribute__((const, overloadable)) ceil(float);
 FN_FUNC_FN(ceil)
 
 /**
@@ -301,7 +322,7 @@
  * @param x
  * @param y
  */
-extern float __attribute__((overloadable)) copysign(float x, float y);
+extern float __attribute__((const, overloadable)) copysign(float x, float y);
 FN_FUNC_FN_FN(copysign)
 
 /**
@@ -309,7 +330,7 @@
  *
  * Supports float, float2, float3, float4.
  */
-extern float __attribute__((overloadable)) cos(float);
+extern float __attribute__((const, overloadable)) cos(float);
 FN_FUNC_FN(cos)
 
 /**
@@ -317,7 +338,7 @@
  *
  * Supports float, float2, float3, float4.
  */
-extern float __attribute__((overloadable)) cosh(float);
+extern float __attribute__((const, overloadable)) cosh(float);
 FN_FUNC_FN(cosh)
 
 /**
@@ -325,7 +346,7 @@
  *
  * Supports float, float2, float3, float4.
  */
-_RS_RUNTIME float __attribute__((overloadable)) cospi(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) cospi(float v);
 FN_FUNC_FN(cospi)
 
 /**
@@ -333,7 +354,7 @@
  *
  * Supports float, float2, float3, float4.
  */
-extern float __attribute__((overloadable)) erfc(float);
+extern float __attribute__((const, overloadable)) erfc(float);
 FN_FUNC_FN(erfc)
 
 /**
@@ -341,7 +362,7 @@
  *
  * Supports float, float2, float3, float4.
  */
-extern float __attribute__((overloadable)) erf(float);
+extern float __attribute__((const, overloadable)) erf(float);
 FN_FUNC_FN(erf)
 
 /**
@@ -349,7 +370,7 @@
  *
  * Supports float, float2, float3, float4.
  */
-extern float __attribute__((overloadable)) exp(float);
+extern float __attribute__((const, overloadable)) exp(float);
 FN_FUNC_FN(exp)
 
 /**
@@ -357,7 +378,7 @@
  *
  * Supports float, float2, float3, float4.
  */
-extern float __attribute__((overloadable)) exp2(float);
+extern float __attribute__((const, overloadable)) exp2(float);
 FN_FUNC_FN(exp2)
 
 /**
@@ -366,7 +387,7 @@
  * Supports float, float2, float3, float4. Both arguments must be of the same
  * type.
  */
-extern float __attribute__((overloadable)) pow(float x, float y);
+extern float __attribute__((const, overloadable)) pow(float x, float y);
 FN_FUNC_FN_FN(pow)
 
 /**
@@ -374,7 +395,7 @@
  *
  * Supports float, float2, float3, float4.
  */
-_RS_RUNTIME float __attribute__((overloadable)) exp10(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) exp10(float v);
 FN_FUNC_FN(exp10)
 
 /**
@@ -382,7 +403,7 @@
  *
  * Supports float, float2, float3, float4.
  */
-extern float __attribute__((overloadable)) expm1(float);
+extern float __attribute__((const, overloadable)) expm1(float);
 FN_FUNC_FN(expm1)
 
 /**
@@ -390,7 +411,7 @@
  *
  * Supports float, float2, float3, float4.
  */
-extern float __attribute__((overloadable)) fabs(float);
+extern float __attribute__((const, overloadable)) fabs(float);
 FN_FUNC_FN(fabs)
 
 /**
@@ -399,7 +420,7 @@
  * Supports float, float2, float3, float4.  Both arguments must be of the same
  * type.
  */
-extern float __attribute__((overloadable)) fdim(float, float);
+extern float __attribute__((const, overloadable)) fdim(float, float);
 FN_FUNC_FN_FN(fdim)
 
 /**
@@ -407,7 +428,7 @@
  *
  * Supports float, float2, float3, float4.
  */
-extern float __attribute__((overloadable)) floor(float);
+extern float __attribute__((const, overloadable)) floor(float);
 FN_FUNC_FN(floor)
 
 /**
@@ -415,7 +436,7 @@
  *
  * Supports float, float2, float3, float4.
  */
-extern float __attribute__((overloadable)) fma(float a, float b, float c);
+extern float __attribute__((const, overloadable)) fma(float a, float b, float c);
 FN_FUNC_FN_FN_FN(fma)
 
 /**
@@ -425,7 +446,7 @@
  * @param x: may be float, float2, float3, float4
  * @param y: may be float or vector.  If vector must match type of x.
  */
-extern float __attribute__((overloadable)) fmax(float x, float y);
+extern float __attribute__((const, overloadable)) fmax(float x, float y);
 FN_FUNC_FN_FN(fmax);
 FN_FUNC_FN_F(fmax);
 
@@ -435,7 +456,7 @@
  * @param x: may be float, float2, float3, float4
  * @param y: may be float or vector.  If vector must match type of x.
  */
-extern float __attribute__((overloadable)) fmin(float x, float y);
+extern float __attribute__((const, overloadable)) fmin(float x, float y);
 FN_FUNC_FN_FN(fmin);
 FN_FUNC_FN_F(fmin);
 
@@ -444,7 +465,7 @@
  *
  * Supports float, float2, float3, float4.
  */
-extern float __attribute__((overloadable)) fmod(float x, float y);
+extern float __attribute__((const, overloadable)) fmod(float x, float y);
 FN_FUNC_FN_FN(fmod)
 
 /**
@@ -453,7 +474,7 @@
  * @param iptr  iptr[0] will be set to the floor of the input value.
  * Supports float, float2, float3, float4.
  */
-_RS_RUNTIME float __attribute__((overloadable)) fract(float v, float *iptr);
+_RS_RUNTIME float __attribute__((pure, overloadable)) fract(float v, float *iptr);
 FN_FUNC_FN_PFN(fract)
 
 /**
@@ -461,22 +482,22 @@
  *
  * Supports float, float2, float3, float4.
  */
-static inline float __attribute__((overloadable)) fract(float v) {
+static inline float __attribute__((const, overloadable)) fract(float v) {
     float unused;
     return fract(v, &unused);
 }
 
-static inline float2 __attribute__((overloadable)) fract(float2 v) {
+static inline float2 __attribute__((const, overloadable)) fract(float2 v) {
     float2 unused;
     return fract(v, &unused);
 }
 
-static inline float3 __attribute__((overloadable)) fract(float3 v) {
+static inline float3 __attribute__((const, overloadable)) fract(float3 v) {
     float3 unused;
     return fract(v, &unused);
 }
 
-static inline float4 __attribute__((overloadable)) fract(float4 v) {
+static inline float4 __attribute__((const, overloadable)) fract(float4 v) {
     float4 unused;
     return fract(v, &unused);
 }
@@ -487,7 +508,7 @@
  * @param v Supports float, float2, float3, float4.
  * @param iptr  Must have the same vector size as v.
  */
-extern float __attribute__((overloadable)) frexp(float v, int *iptr);
+extern float __attribute__((pure, overloadable)) frexp(float v, int *iptr);
 FN_FUNC_FN_PIN(frexp)
 
 /**
@@ -495,7 +516,7 @@
  *
  * Supports float, float2, float3, float4.
  */
-extern float __attribute__((overloadable)) hypot(float x, float y);
+extern float __attribute__((const, overloadable)) hypot(float x, float y);
 FN_FUNC_FN_FN(hypot)
 
 /**
@@ -503,7 +524,7 @@
  *
  * Supports 1,2,3,4 components
  */
-extern int __attribute__((overloadable)) ilogb(float);
+extern int __attribute__((const, overloadable)) ilogb(float);
 IN_FUNC_FN(ilogb)
 
 /**
@@ -512,7 +533,7 @@
  * @param x Supports 1,2,3,4 components
  * @param y Supports single component or matching vector.
  */
-extern float __attribute__((overloadable)) ldexp(float x, int y);
+extern float __attribute__((const, overloadable)) ldexp(float x, int y);
 FN_FUNC_FN_IN(ldexp)
 FN_FUNC_FN_I(ldexp)
 
@@ -521,7 +542,7 @@
  *
  * Supports 1,2,3,4 components
  */
-extern float __attribute__((overloadable)) lgamma(float);
+extern float __attribute__((const, overloadable)) lgamma(float);
 FN_FUNC_FN(lgamma)
 
 /**
@@ -530,7 +551,7 @@
  * @param x Supports 1,2,3,4 components
  * @param y Supports matching vector.
  */
-extern float __attribute__((overloadable)) lgamma(float x, int* y);
+extern float __attribute__((pure, overloadable)) lgamma(float x, int* y);
 FN_FUNC_FN_PIN(lgamma)
 
 /**
@@ -538,7 +559,7 @@
  *
  * Supports 1,2,3,4 components
  */
-extern float __attribute__((overloadable)) log(float);
+extern float __attribute__((const, overloadable)) log(float);
 FN_FUNC_FN(log)
 
 /**
@@ -546,7 +567,7 @@
  *
  * Supports 1,2,3,4 components
  */
-extern float __attribute__((overloadable)) log10(float);
+extern float __attribute__((const, overloadable)) log10(float);
 FN_FUNC_FN(log10)
 
 /**
@@ -554,7 +575,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) log2(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) log2(float v);
 FN_FUNC_FN(log2)
 
 /**
@@ -562,7 +583,7 @@
  *
  * Supports 1,2,3,4 components
  */
-extern float __attribute__((overloadable)) log1p(float v);
+extern float __attribute__((const, overloadable)) log1p(float v);
 FN_FUNC_FN(log1p)
 
 /**
@@ -570,7 +591,7 @@
  *
  * Supports 1,2,3,4 components
  */
-extern float __attribute__((overloadable)) logb(float);
+extern float __attribute__((const, overloadable)) logb(float);
 FN_FUNC_FN(logb)
 
 /**
@@ -578,7 +599,7 @@
  *
  * Supports 1,2,3,4 components
  */
-extern float __attribute__((overloadable)) mad(float a, float b, float c);
+extern float __attribute__((const, overloadable)) mad(float a, float b, float c);
 FN_FUNC_FN_FN_FN(mad)
 
 /**
@@ -589,17 +610,17 @@
  * @param iret iret[0] will be set to the integral portion of the number.
  * @return The floating point portion of the value.
  */
-extern float __attribute__((overloadable)) modf(float x, float *iret);
+extern float __attribute__((pure, overloadable)) modf(float x, float *iret);
 FN_FUNC_FN_PFN(modf);
 
-extern float __attribute__((overloadable)) nan(uint);
+extern float __attribute__((const, overloadable)) nan(uint);
 
 /**
  * Return the next floating point number from x towards y.
  *
  * Supports 1,2,3,4 components
  */
-extern float __attribute__((overloadable)) nextafter(float x, float y);
+extern float __attribute__((const, overloadable)) nextafter(float x, float y);
 FN_FUNC_FN_FN(nextafter)
 
 /**
@@ -607,7 +628,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) pown(float v, int p);
+_RS_RUNTIME float __attribute__((const, overloadable)) pown(float v, int p);
 FN_FUNC_FN_IN(pown)
 
 /**
@@ -616,7 +637,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) powr(float v, float p);
+_RS_RUNTIME float __attribute__((const, overloadable)) powr(float v, float p);
 FN_FUNC_FN_FN(powr)
 
 /**
@@ -624,11 +645,11 @@
  *
  * Supports 1,2,3,4 components
  */
-extern float __attribute__((overloadable)) remainder(float x, float y);
+extern float __attribute__((const, overloadable)) remainder(float x, float y);
 FN_FUNC_FN_FN(remainder)
 
 // document once we know the precision of bionic
-extern float __attribute__((overloadable)) remquo(float, float, int *);
+extern float __attribute__((pure, overloadable)) remquo(float, float, int *);
 FN_FUNC_FN_FN_PIN(remquo)
 
 /**
@@ -636,7 +657,7 @@
  *
  * Supports 1,2,3,4 components
  */
-extern float __attribute__((overloadable)) rint(float);
+extern float __attribute__((const, overloadable)) rint(float);
 FN_FUNC_FN(rint)
 
 /**
@@ -644,7 +665,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) rootn(float v, int n);
+_RS_RUNTIME float __attribute__((const, overloadable)) rootn(float v, int n);
 FN_FUNC_FN_IN(rootn)
 
 /**
@@ -652,7 +673,7 @@
  *
  * Supports 1,2,3,4 components
  */
-extern float __attribute__((overloadable)) round(float);
+extern float __attribute__((const, overloadable)) round(float);
 FN_FUNC_FN(round)
 
 /**
@@ -660,7 +681,7 @@
  *
  * Supports 1,2,3,4 components
  */
-extern float __attribute__((overloadable)) sqrt(float);
+extern float __attribute__((const, overloadable)) sqrt(float);
 FN_FUNC_FN(sqrt)
 
 /**
@@ -668,7 +689,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) rsqrt(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) rsqrt(float v);
 FN_FUNC_FN(rsqrt)
 
 /**
@@ -677,7 +698,7 @@
  * @param v The incoming value in radians
  * Supports 1,2,3,4 components
  */
-extern float __attribute__((overloadable)) sin(float v);
+extern float __attribute__((const, overloadable)) sin(float v);
 FN_FUNC_FN(sin)
 
 /**
@@ -689,7 +710,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) sincos(float v, float *cosptr);
+_RS_RUNTIME float __attribute__((pure, overloadable)) sincos(float v, float *cosptr);
 FN_FUNC_FN_PFN(sincos);
 
 /**
@@ -697,7 +718,7 @@
  *
  * Supports 1,2,3,4 components
  */
-extern float __attribute__((overloadable)) sinh(float);
+extern float __attribute__((const, overloadable)) sinh(float);
 FN_FUNC_FN(sinh)
 
 /**
@@ -705,7 +726,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) sinpi(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) sinpi(float v);
 FN_FUNC_FN(sinpi)
 
 /**
@@ -714,7 +735,7 @@
  * Supports 1,2,3,4 components
  * @param v The incoming value in radians
  */
-extern float __attribute__((overloadable)) tan(float v);
+extern float __attribute__((const, overloadable)) tan(float v);
 FN_FUNC_FN(tan)
 
 /**
@@ -723,7 +744,7 @@
  * Supports 1,2,3,4 components
  * @param v The incoming value in radians
  */
-extern float __attribute__((overloadable)) tanh(float);
+extern float __attribute__((const, overloadable)) tanh(float);
 FN_FUNC_FN(tanh)
 
 /**
@@ -731,7 +752,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) tanpi(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) tanpi(float v);
 FN_FUNC_FN(tanpi)
 
 /**
@@ -739,7 +760,7 @@
  *
  * Supports 1,2,3,4 components
  */
-extern float __attribute__((overloadable)) tgamma(float);
+extern float __attribute__((const, overloadable)) tgamma(float);
 FN_FUNC_FN(tgamma)
 
 /**
@@ -747,15 +768,37 @@
  *
  * Supports 1,2,3,4 components
  */
-extern float __attribute__((overloadable)) trunc(float);
+extern float __attribute__((const, overloadable)) trunc(float);
 FN_FUNC_FN(trunc)
 
+#ifdef DOXYGEN
 
 #define XN_FUNC_YN(typeout, fnc, typein)                                \
-extern typeout __attribute__((overloadable)) fnc(typein);               \
-_RS_RUNTIME typeout##2 __attribute__((overloadable)) fnc(typein##2 v);  \
-_RS_RUNTIME typeout##3 __attribute__((overloadable)) fnc(typein##3 v);  \
-_RS_RUNTIME typeout##4 __attribute__((overloadable)) fnc(typein##4 v);
+extern typeout __attribute__((overloadable)) fnc(typein v);
+
+#define XN_FUNC_XN_XN_BODY(type, fnc, body)         \
+_RS_RUNTIME type __attribute__((overloadable))      \
+        fnc(type v1, type v2);
+
+#else
+
+#define XN_FUNC_YN(typeout, fnc, typein)                                      \
+extern typeout __attribute__((const, overloadable)) fnc(typein v);            \
+_RS_RUNTIME typeout##2 __attribute__((const, overloadable)) fnc(typein##2 v); \
+_RS_RUNTIME typeout##3 __attribute__((const, overloadable)) fnc(typein##3 v); \
+_RS_RUNTIME typeout##4 __attribute__((const, overloadable)) fnc(typein##4 v);
+
+#define XN_FUNC_XN_XN_BODY(type, fnc, body)              \
+_RS_RUNTIME type __attribute__((const, overloadable))    \
+        fnc(type v1, type v2);                           \
+_RS_RUNTIME type##2 __attribute__((const, overloadable)) \
+        fnc(type##2 v1, type##2 v2);                     \
+_RS_RUNTIME type##3 __attribute__((const, overloadable)) \
+        fnc(type##3 v1, type##3 v2);                     \
+_RS_RUNTIME type##4 __attribute__((const, overloadable)) \
+        fnc(type##4 v1, type##4 v2);
+
+#endif  // DOXYGEN
 
 #define UIN_FUNC_IN(fnc)          \
 XN_FUNC_YN(uchar, fnc, char)      \
@@ -770,17 +813,6 @@
 XN_FUNC_YN(uint, fnc, uint)       \
 XN_FUNC_YN(int, fnc, int)
 
-
-#define XN_FUNC_XN_XN_BODY(type, fnc, body)         \
-_RS_RUNTIME type __attribute__((overloadable))      \
-        fnc(type v1, type v2);                      \
-_RS_RUNTIME type##2 __attribute__((overloadable))   \
-        fnc(type##2 v1, type##2 v2);                \
-_RS_RUNTIME type##3 __attribute__((overloadable))   \
-        fnc(type##3 v1, type##3 v2);                \
-_RS_RUNTIME type##4 __attribute__((overloadable))   \
-        fnc(type##4 v1, type##4 v2);
-
 #define IN_FUNC_IN_IN_BODY(fnc, body)   \
 XN_FUNC_XN_XN_BODY(uchar, fnc, body)    \
 XN_FUNC_XN_XN_BODY(char, fnc, body)     \
@@ -791,6 +823,7 @@
 XN_FUNC_XN_XN_BODY(float, fnc, body)
 
 /**
+ * \fn uchar abs(char)
  * Return the absolute value of a value.
  *
  * Supports 1,2,3,4 components of char, short, int.
@@ -827,16 +860,41 @@
  * @param low Lower bound, must be scalar or matching vector.
  * @param high High bound, must match type of low
  */
-_RS_RUNTIME float __attribute__((overloadable)) clamp(float amount, float low, float high);
+
+#if !defined(RS_VERSION) || (RS_VERSION < 19)
+_RS_RUNTIME float __attribute__((const, overloadable)) clamp(float amount, float low, float high);
 FN_FUNC_FN_FN_FN(clamp)
 FN_FUNC_FN_F_F(clamp)
+#else
+#define _CLAMP(T)                                                                   \
+extern T __attribute__((overloadable)) clamp(T amount, T low, T high);              \
+extern T##2 __attribute__((overloadable)) clamp(T##2 amount, T##2 low, T##2 high);  \
+extern T##3 __attribute__((overloadable)) clamp(T##3 amount, T##3 low, T##3 high);  \
+extern T##4 __attribute__((overloadable)) clamp(T##4 amount, T##4 low, T##4 high);  \
+extern T##2 __attribute__((overloadable)) clamp(T##2 amount, T low, T high);        \
+extern T##3 __attribute__((overloadable)) clamp(T##3 amount, T low, T high);        \
+extern T##4 __attribute__((overloadable)) clamp(T##4 amount, T low, T high)
+
+_CLAMP(float);
+_CLAMP(double);
+_CLAMP(char);
+_CLAMP(uchar);
+_CLAMP(short);
+_CLAMP(ushort);
+_CLAMP(int);
+_CLAMP(uint);
+_CLAMP(long);
+_CLAMP(ulong);
+
+#undef _CLAMP
+#endif
 
 /**
  * Convert from radians to degrees.
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) degrees(float radians);
+_RS_RUNTIME float __attribute__((const, overloadable)) degrees(float radians);
 FN_FUNC_FN(degrees)
 
 /**
@@ -844,7 +902,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) mix(float start, float stop, float amount);
+_RS_RUNTIME float __attribute__((const, overloadable)) mix(float start, float stop, float amount);
 FN_FUNC_FN_FN_FN(mix)
 FN_FUNC_FN_FN_F(mix)
 
@@ -853,7 +911,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) radians(float degrees);
+_RS_RUNTIME float __attribute__((const, overloadable)) radians(float degrees);
 FN_FUNC_FN(radians)
 
 /**
@@ -864,18 +922,18 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) step(float edge, float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) step(float edge, float v);
 FN_FUNC_FN_FN(step)
 FN_FUNC_FN_F(step)
 
 // not implemented
-extern float __attribute__((overloadable)) smoothstep(float, float, float);
-extern float2 __attribute__((overloadable)) smoothstep(float2, float2, float2);
-extern float3 __attribute__((overloadable)) smoothstep(float3, float3, float3);
-extern float4 __attribute__((overloadable)) smoothstep(float4, float4, float4);
-extern float2 __attribute__((overloadable)) smoothstep(float, float, float2);
-extern float3 __attribute__((overloadable)) smoothstep(float, float, float3);
-extern float4 __attribute__((overloadable)) smoothstep(float, float, float4);
+extern float __attribute__((const, overloadable)) smoothstep(float, float, float);
+extern float2 __attribute__((const, overloadable)) smoothstep(float2, float2, float2);
+extern float3 __attribute__((const, overloadable)) smoothstep(float3, float3, float3);
+extern float4 __attribute__((const, overloadable)) smoothstep(float4, float4, float4);
+extern float2 __attribute__((const, overloadable)) smoothstep(float, float, float2);
+extern float3 __attribute__((const, overloadable)) smoothstep(float, float, float3);
+extern float4 __attribute__((const, overloadable)) smoothstep(float, float, float4);
 
 /**
  * Return the sign of a value.
@@ -886,7 +944,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) sign(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) sign(float v);
 FN_FUNC_FN(sign)
 
 /**
@@ -894,15 +952,15 @@
  *
  * Supports 3,4 components
  */
-_RS_RUNTIME float3 __attribute__((overloadable)) cross(float3 lhs, float3 rhs);
-_RS_RUNTIME float4 __attribute__((overloadable)) cross(float4 lhs, float4 rhs);
+_RS_RUNTIME float3 __attribute__((const, overloadable)) cross(float3 lhs, float3 rhs);
+_RS_RUNTIME float4 __attribute__((const, overloadable)) cross(float4 lhs, float4 rhs);
 
 /**
  * Compute the dot product of two vectors.
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) dot(float lhs, float rhs);
+_RS_RUNTIME float __attribute__((const, overloadable)) dot(float lhs, float rhs);
 F_FUNC_FN_FN(dot)
 
 /**
@@ -910,7 +968,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) length(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) length(float v);
 F_FUNC_FN(length)
 
 /**
@@ -918,7 +976,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) distance(float lhs, float rhs);
+_RS_RUNTIME float __attribute__((const, overloadable)) distance(float lhs, float rhs);
 F_FUNC_FN_FN(distance)
 
 /**
@@ -926,7 +984,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) normalize(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) normalize(float v);
 FN_FUNC_FN(normalize)
 
 
@@ -938,7 +996,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) half_recip(float);
+_RS_RUNTIME float __attribute__((const, overloadable)) half_recip(float);
 FN_FUNC_FN(half_recip)
 
 /**
@@ -946,7 +1004,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) half_sqrt(float);
+_RS_RUNTIME float __attribute__((const, overloadable)) half_sqrt(float);
 FN_FUNC_FN(half_sqrt)
 
 /**
@@ -954,7 +1012,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) half_rsqrt(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) half_rsqrt(float v);
 FN_FUNC_FN(half_rsqrt)
 
 /**
@@ -962,7 +1020,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) fast_length(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) fast_length(float v);
 F_FUNC_FN(fast_length)
 
 /**
@@ -970,7 +1028,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) fast_distance(float lhs, float rhs);
+_RS_RUNTIME float __attribute__((const, overloadable)) fast_distance(float lhs, float rhs);
 F_FUNC_FN_FN(fast_distance)
 
 /**
@@ -978,7 +1036,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) fast_normalize(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) fast_normalize(float v);
 F_FUNC_FN(fast_normalize)
 
 #endif  // (defined(RS_VERSION) && (RS_VERSION >= 17))
@@ -996,7 +1054,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) native_exp2(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) native_exp2(float v);
 FN_FUNC_FN(native_exp2)
 
 /**
@@ -1006,7 +1064,7 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) native_exp(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) native_exp(float v);
 FN_FUNC_FN(native_exp)
 
 /**
@@ -1016,21 +1074,21 @@
  *
  * Supports 1,2,3,4 components
  */
-_RS_RUNTIME float __attribute__((overloadable)) native_exp10(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) native_exp10(float v);
 FN_FUNC_FN(native_exp10)
 
 
-_RS_RUNTIME float __attribute__((overloadable)) native_log2(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) native_log2(float v);
 FN_FUNC_FN(native_log2)
 
-_RS_RUNTIME float __attribute__((overloadable)) native_log(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) native_log(float v);
 FN_FUNC_FN(native_log)
 
-_RS_RUNTIME float __attribute__((overloadable)) native_log10(float v);
+_RS_RUNTIME float __attribute__((const, overloadable)) native_log10(float v);
 FN_FUNC_FN(native_log10)
 
 
-_RS_RUNTIME float __attribute__((overloadable)) native_powr(float v, float y);
+_RS_RUNTIME float __attribute__((const, overloadable)) native_powr(float v, float y);
 FN_FUNC_FN_FN(native_powr)
 
 
diff --git a/scriptc/rs_math.rsh b/scriptc/rs_math.rsh
index 73040b3..4d3124c 100644
--- a/scriptc/rs_math.rsh
+++ b/scriptc/rs_math.rsh
@@ -49,7 +49,7 @@
 /**
  * Returns the fractional part of a float
  */
-extern float __attribute__((overloadable))
+extern float __attribute__((const, overloadable))
     rsFrac(float);
 
 
@@ -64,28 +64,28 @@
  * @param low
  * @param high
  */
-_RS_RUNTIME uint __attribute__((overloadable, always_inline)) rsClamp(uint amount, uint low, uint high);
+_RS_RUNTIME uint __attribute__((const, overloadable, always_inline)) rsClamp(uint amount, uint low, uint high);
 
 /**
  * \overload
  */
-_RS_RUNTIME int __attribute__((overloadable, always_inline)) rsClamp(int amount, int low, int high);
+_RS_RUNTIME int __attribute__((const, overloadable, always_inline)) rsClamp(int amount, int low, int high);
 /**
  * \overload
  */
-_RS_RUNTIME ushort __attribute__((overloadable, always_inline)) rsClamp(ushort amount, ushort low, ushort high);
+_RS_RUNTIME ushort __attribute__((const, overloadable, always_inline)) rsClamp(ushort amount, ushort low, ushort high);
 /**
  * \overload
  */
-_RS_RUNTIME short __attribute__((overloadable, always_inline)) rsClamp(short amount, short low, short high);
+_RS_RUNTIME short __attribute__((const, overloadable, always_inline)) rsClamp(short amount, short low, short high);
 /**
  * \overload
  */
-_RS_RUNTIME uchar __attribute__((overloadable, always_inline)) rsClamp(uchar amount, uchar low, uchar high);
+_RS_RUNTIME uchar __attribute__((const, overloadable, always_inline)) rsClamp(uchar amount, uchar low, uchar high);
 /**
  * \overload
  */
-_RS_RUNTIME char __attribute__((overloadable, always_inline)) rsClamp(char amount, char low, char high);
+_RS_RUNTIME char __attribute__((const, overloadable, always_inline)) rsClamp(char amount, char low, char high);
 
 
 /**
@@ -202,7 +202,7 @@
  *
  * @return uchar4
  */
-_RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b);
+_RS_RUNTIME uchar4 __attribute__((const, overloadable)) rsPackColorTo8888(float r, float g, float b);
 
 /**
  * Pack floating point (0-1) RGBA values into a uchar4.
@@ -214,7 +214,7 @@
  *
  * @return uchar4
  */
-_RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b, float a);
+_RS_RUNTIME uchar4 __attribute__((const, overloadable)) rsPackColorTo8888(float r, float g, float b, float a);
 
 /**
  * Pack floating point (0-1) RGB values into a uchar4.  The alpha component is
@@ -224,7 +224,7 @@
  *
  * @return uchar4
  */
-_RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float3 color);
+_RS_RUNTIME uchar4 __attribute__((const, overloadable)) rsPackColorTo8888(float3 color);
 
 /**
  * Pack floating point (0-1) RGBA values into a uchar4.
@@ -233,7 +233,7 @@
  *
  * @return uchar4
  */
-_RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float4 color);
+_RS_RUNTIME uchar4 __attribute__((const, overloadable)) rsPackColorTo8888(float4 color);
 
 /**
  * Unpack a uchar4 color to float4.  The resulting float range will be (0-1).
@@ -242,10 +242,10 @@
  *
  * @return float4
  */
-_RS_RUNTIME float4 rsUnpackColor8888(uchar4 c);
+_RS_RUNTIME float4 __attribute__((const)) rsUnpackColor8888(uchar4 c);
 
-_RS_RUNTIME uchar4 __attribute__((overloadable)) rsYuvToRGBA_uchar4(uchar y, uchar u, uchar v);
-_RS_RUNTIME float4 __attribute__((overloadable)) rsYuvToRGBA_float4(uchar y, uchar u, uchar v);
+_RS_RUNTIME uchar4 __attribute__((const, overloadable)) rsYuvToRGBA_uchar4(uchar y, uchar u, uchar v);
+_RS_RUNTIME float4 __attribute__((const, overloadable)) rsYuvToRGBA_float4(uchar y, uchar u, uchar v);
 
 
 #endif
diff --git a/tests/cppbasic/Android.mk b/tests/cppbasic/Android.mk
index 5c2c53a..bf7725a 100644
--- a/tests/cppbasic/Android.mk
+++ b/tests/cppbasic/Android.mk
@@ -6,18 +6,18 @@
 	compute.cpp
 
 LOCAL_SHARED_LIBRARIES := \
-	libRS \
-	libRScpp \
 	libz \
-	libcutils \
-	libutils \
 	libEGL \
 	libGLESv1_CM \
 	libGLESv2 \
 	libui \
 	libbcc \
 	libbcinfo \
-	libgui
+	libgui \
+	libdl \
+	libRScpp \
+	libstlport
+
 
 LOCAL_MODULE:= rstest-compute
 
@@ -25,6 +25,7 @@
 
 intermediates := $(call intermediates-dir-for,STATIC_LIBRARIES,libRS,TARGET,)
 
+LOCAL_C_INCLUDES += external/stlport/stlport bionic/ bionic/libstdc++/include
 LOCAL_C_INCLUDES += frameworks/rs/cpp
 LOCAL_C_INCLUDES += frameworks/rs
 LOCAL_C_INCLUDES += $(intermediates)
diff --git a/tests/cppbasic/compute.cpp b/tests/cppbasic/compute.cpp
index d1647c4..bc80c7e 100644
--- a/tests/cppbasic/compute.cpp
+++ b/tests/cppbasic/compute.cpp
@@ -32,7 +32,7 @@
     sp<Allocation> aout = Allocation::createTyped(rs, t);
     printf("Allocation %p %p\n", ain.get(), aout.get());
 
-    sp<ScriptC_mono> sc = new ScriptC_mono(rs, NULL, 0);
+    ScriptC_mono* sc = new ScriptC_mono(rs);
     printf("new script\n");
 
     // We read back the status from the script-side via a "failed" allocation.
@@ -66,8 +66,8 @@
     {
         sp<const Element> e = Element::I32(rs);
         Type::Builder tb(rs, e);
-        tb.setX(5);
-        tb.setY(5);
+        tb.setX(8);
+        tb.setY(8);
         sp<const Type> t = tb.create();
         sp<Allocation> kern1_in = Allocation::createTyped(rs, t);
         sp<Allocation> kern1_out = Allocation::createTyped(rs, t);
@@ -84,21 +84,10 @@
 
         rs->finish();
         failed_alloc->copy1DTo(&failed);
-
-        e.clear();
-        t.clear();
-        kern1_in.clear();
-        kern1_out.clear();
     }
 
     printf("Deleting stuff\n");
-    sc.clear();
-    t.clear();
-    a1.clear();
-    e.clear();
-    ain.clear();
-    aout.clear();
-    //    delete rs;
+    delete sc;
     printf("Delete OK\n");
 
     if (failed) {
diff --git a/tests/cppbasic/mono.rs b/tests/cppbasic/mono.rs
index d49d0f7..f134b36 100644
--- a/tests/cppbasic/mono.rs
+++ b/tests/cppbasic/mono.rs
@@ -15,7 +15,7 @@
  */
 
 #pragma version(1)
-#pragma rs java_package_name(com.android.rs.image)
+#pragma rs java_package_name(com.android.rs.cppbasic)
 #pragma rs_fp_relaxed
 
 int g_i = 4;