Update samples

The motivation of this update was to remove libnativehelper which was made
implicitly available recently.  But since we are on it, update samples
source to recent one which allows same bitcode to compile/run on
ARM/X86/MIPS.  Tiny arch-specific *so are still required to call libbcc.
Future llvm ABI in ndk will remove this restriction and phase out this.

Change-Id: Ic32d7dcfbbe421c07911590b22dfd865cdeb2fb0
diff --git a/platforms/android-portable/arch-llvm/usr/include b/platforms/android-portable/arch-llvm/usr/include
index 914bdcf..6872ae4 120000
--- a/platforms/android-portable/arch-llvm/usr/include
+++ b/platforms/android-portable/arch-llvm/usr/include
@@ -1 +1 @@
-../../../../../ndk/platforms/android-9/arch-arm/usr/include
\ No newline at end of file
+include.arm
\ No newline at end of file
diff --git a/platforms/android-portable/arch-llvm/usr/include.arm b/platforms/android-portable/arch-llvm/usr/include.arm
new file mode 120000
index 0000000..914bdcf
--- /dev/null
+++ b/platforms/android-portable/arch-llvm/usr/include.arm
@@ -0,0 +1 @@
+../../../../../ndk/platforms/android-9/arch-arm/usr/include
\ No newline at end of file
diff --git a/platforms/android-portable/arch-llvm/usr/include.mips b/platforms/android-portable/arch-llvm/usr/include.mips
new file mode 120000
index 0000000..ca09cc3
--- /dev/null
+++ b/platforms/android-portable/arch-llvm/usr/include.mips
@@ -0,0 +1 @@
+../../../../../ndk/platforms/android-9/arch-mips/usr/include
\ No newline at end of file
diff --git a/platforms/android-portable/arch-llvm/usr/include.x86 b/platforms/android-portable/arch-llvm/usr/include.x86
new file mode 120000
index 0000000..3fb9a68
--- /dev/null
+++ b/platforms/android-portable/arch-llvm/usr/include.x86
@@ -0,0 +1 @@
+../../../../../ndk/platforms/android-9/arch-x86/usr/include
\ No newline at end of file
diff --git a/samples/PhotoEditor/jni/Android-portable.mk b/samples/PhotoEditor/jni/Android-portable.mk
index 861ff01..2a83f8a 100644
--- a/samples/PhotoEditor/jni/Android-portable.mk
+++ b/samples/PhotoEditor/jni/Android-portable.mk
@@ -40,6 +40,8 @@
     LOCAL_SDK_VERSION := 9
 endif
 
+LOCAL_CFLAGS := -D__GDK__
+
 LOCAL_C_INCLUDES := $(OUT)/../../../../frameworks/compile/libbcc/include
 #LOCAL_LDFLAGS := -L$(OUT)/system/lib
 
diff --git a/samples/PhotoEditor/jni/Android.mk b/samples/PhotoEditor/jni/Android.mk
index c94794a..794c7b8 100644
--- a/samples/PhotoEditor/jni/Android.mk
+++ b/samples/PhotoEditor/jni/Android.mk
@@ -41,8 +41,7 @@
 endif
 
 LOCAL_CFLAGS := -Werror \
-    -I$(OUT)/../../../../frameworks/compile/libbcc/include \
-    -I$(OUT)/../../../../dalvik/libnativehelper/include 
+    -I$(OUT)/../../../../frameworks/compile/libbcc/include 
 
 LOCAL_LDFLAGS := -L$(OUT)/system/lib
 
diff --git a/samples/PhotoEditor/jni/Application.mk b/samples/PhotoEditor/jni/Application.mk
index f05229c..bb9ec4d 100644
--- a/samples/PhotoEditor/jni/Application.mk
+++ b/samples/PhotoEditor/jni/Application.mk
@@ -1 +1,5 @@
 APP_ABI := armeabi armeabi-v7a
+#APP_ABI := mips mips-r2
+#APP_ABI := x86
+APP_PLATFORM := android-9
+
diff --git a/samples/PhotoEditor/jni/_jni.cpp b/samples/PhotoEditor/jni/_jni.cpp
index 87263ff..63fd3d6 100644
--- a/samples/PhotoEditor/jni/_jni.cpp
+++ b/samples/PhotoEditor/jni/_jni.cpp
@@ -1,10 +1,11 @@
-#if !defined(__clang__)
 
-#include <nativehelper/jni.h>
+#include <jni.h>
 #include <android/log.h>
+
+#if !defined(__GDK__) && !defined(__NOGDK__)
 #include <bcc/bcc.h>
 #include <dlfcn.h>
-
+#endif // !__GDK__ && !__NOGDK__
 
 #define LOG_TAG "libjni_photoeditor"
 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
@@ -30,14 +31,27 @@
     return JNI_VERSION_1_4;
 }
 
+
+#if !defined(__GDK__) && !defined(__NOGDK__)
 static void* lookupSymbol(void* pContext, const char* name)
 {
     return (void*) dlsym(RTLD_DEFAULT, name);
 }
+#endif // !__GDK__ && !__NOGDK__
+
+extern "C" JNIEXPORT jboolean JNICALL Java_com_android_photoeditor_filters_ImageUtils_gdk(JNIEnv *env, jobject obj)
+{
+#if !defined(__NOGDK__)
+   return JNI_TRUE;
+#else
+   return JNI_FALSE;
+#endif
+}
 
 extern "C" JNIEXPORT jboolean JNICALL Java_com_android_photoeditor_filters_ImageUtils_init(
     JNIEnv *env, jobject obj, jbyteArray scriptRef, jint length)
 {   
+#if !defined(__GDK__) && !defined(__NOGDK__)
     void *new_func_ptr[JNI_max];
     int i, all_func_found = 1;
 
@@ -58,10 +72,19 @@
     }
   #endif
     bccRegisterSymbolCallback(script_ref, lookupSymbol, NULL);
-    if (bccPrepareExecutableEx(script_ref, ".", "/data/data/com.android.photoeditor/photoeditorLLVM", 0)) {
-        LOGE("Error! Cannot bccPrepareExecutableEx");
+   
+  #ifdef OLD_BCC
+    if (bccPrepareExecutable(script_ref, "/data/data/com.android.photoeditor/photoeditorLLVM.oBCC", 0)) {
+        LOGE("Error! Cannot bccPrepareExecutable");
         return JNI_FALSE;
     }
+  #else 
+    if (bccPrepareExecutable(script_ref, "/data/data/com.android.photoeditor/", "photoeditorLLVM", 0)) {
+        LOGE("Error! Cannot bccPrepareExecutable");
+        return JNI_FALSE;
+    }
+  #endif // OLD_BCC
+   
     for(i=0; i<JNI_max; i++) {
         new_func_ptr[i] = bccGetFuncAddr(script_ref, JNIFunc[i].func_name);
         if (new_func_ptr[i] == NULL) {
@@ -82,6 +105,10 @@
     }
 
     return JNI_TRUE;
+#else
+   
+    return JNI_FALSE;
+
+#endif // !__GDK__ && !__NOGDK__
 }
 
-#endif // __clang__
\ No newline at end of file
diff --git a/samples/PhotoEditor/src/com/android/photoeditor/PhotoEditor.java b/samples/PhotoEditor/src/com/android/photoeditor/PhotoEditor.java
index 928f523..074b882 100644
--- a/samples/PhotoEditor/src/com/android/photoeditor/PhotoEditor.java
+++ b/samples/PhotoEditor/src/com/android/photoeditor/PhotoEditor.java
@@ -41,6 +41,8 @@
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
+       
+        if (ImageUtils.gdk())
 	{
           // HACK: create faked view in order to read bitcode in resource
                 View view = new View(getApplication());
diff --git a/samples/PhotoEditor/src/com/android/photoeditor/filters/ImageUtils.java b/samples/PhotoEditor/src/com/android/photoeditor/filters/ImageUtils.java
index e37a30f..a6bce71 100644
--- a/samples/PhotoEditor/src/com/android/photoeditor/filters/ImageUtils.java
+++ b/samples/PhotoEditor/src/com/android/photoeditor/filters/ImageUtils.java
@@ -29,6 +29,7 @@
         System.loadLibrary("jni_photoeditor");
     }
   
+    public static native boolean gdk();
     public static native boolean init(byte[] pgm, int pgmLength);
    
     public static native void nativeBacklight(Bitmap src, Bitmap dst, float backlight);
diff --git a/samples/bitmap-plasma-llvm/.classpath b/samples/bitmap-plasma-llvm/.classpath
deleted file mode 100644
index 6efcbb7..0000000
--- a/samples/bitmap-plasma-llvm/.classpath
+++ /dev/null
@@ -1,7 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
-	<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
-	<classpathentry kind="src" path="src"/>
-	<classpathentry kind="src" path="gen"/>
-	<classpathentry kind="output" path="bin"/>
-</classpath>
diff --git a/samples/bitmap-plasma-llvm/.project b/samples/bitmap-plasma-llvm/.project
deleted file mode 100644
index 1240c70..0000000
--- a/samples/bitmap-plasma-llvm/.project
+++ /dev/null
@@ -1,33 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<projectDescription>
-	<name>PlasmaLLVM</name>
-	<comment></comment>
-	<projects>
-	</projects>
-	<buildSpec>
-		<buildCommand>
-			<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
-			<arguments>
-			</arguments>
-		</buildCommand>
-		<buildCommand>
-			<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
-			<arguments>
-			</arguments>
-		</buildCommand>
-		<buildCommand>
-			<name>org.eclipse.jdt.core.javabuilder</name>
-			<arguments>
-			</arguments>
-		</buildCommand>
-		<buildCommand>
-			<name>com.android.ide.eclipse.adt.ApkBuilder</name>
-			<arguments>
-			</arguments>
-		</buildCommand>
-	</buildSpec>
-	<natures>
-		<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
-		<nature>org.eclipse.jdt.core.javanature</nature>
-	</natures>
-</projectDescription>
diff --git a/samples/bitmap-plasma-llvm/default.properties b/samples/bitmap-plasma-llvm/default.properties
index 3ac2523..e2e8061 100644
--- a/samples/bitmap-plasma-llvm/default.properties
+++ b/samples/bitmap-plasma-llvm/default.properties
@@ -8,4 +8,4 @@
 # project structure.
 
 # Project target.
-target=android-12
+target=android-8
diff --git a/samples/bitmap-plasma-llvm/jni/Android-portable.mk b/samples/bitmap-plasma-llvm/jni/Android-portable.mk
index e3e99b5..099ad1e 100644
--- a/samples/bitmap-plasma-llvm/jni/Android-portable.mk
+++ b/samples/bitmap-plasma-llvm/jni/Android-portable.mk
@@ -3,6 +3,8 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE    := plasma_portable
-LOCAL_SRC_FILES := libplasma.c
+LOCAL_SRC_FILES := plasma.cpp
+
+LOCAL_CFLAGS := -D__GDK__
 
 include $(BUILD_BITCODE)
diff --git a/samples/bitmap-plasma-llvm/jni/Android.mk b/samples/bitmap-plasma-llvm/jni/Android.mk
index 68cb8f6..08e4058 100644
--- a/samples/bitmap-plasma-llvm/jni/Android.mk
+++ b/samples/bitmap-plasma-llvm/jni/Android.mk
@@ -6,7 +6,7 @@
 
 LOCAL_MODULE_TAGS := optional
 
-LOCAL_SRC_FILES := plasmaLLVM.cpp
+LOCAL_SRC_FILES := plasma.cpp
 
 LOCAL_C_INCLUDES := $(OUT)/../../../../frameworks/compile/libbcc/include
 
diff --git a/samples/bitmap-plasma-llvm/jni/Application.mk b/samples/bitmap-plasma-llvm/jni/Application.mk
index 59fdfab..5b6cab6 100644
--- a/samples/bitmap-plasma-llvm/jni/Application.mk
+++ b/samples/bitmap-plasma-llvm/jni/Application.mk
@@ -1,3 +1,5 @@
 # The ARMv7 is significanly faster due to the use of the hardware FPU
-APP_ABI := armeabi armeabi-v7a
-APP_PLATFORM := android-12
+APP_ABI := armeabi armeabi-v7a 
+#APP_ABI := mips mips-r2
+#APP_ABI := x86
+APP_PLATFORM := android-8
diff --git a/samples/bitmap-plasma-llvm/jni/libplasma.c b/samples/bitmap-plasma-llvm/jni/libplasma.c
deleted file mode 100644
index 1922bfe..0000000
--- a/samples/bitmap-plasma-llvm/jni/libplasma.c
+++ /dev/null
@@ -1,224 +0,0 @@
-#include <stddef.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-
-/* Set to 1 to enable debug log traces. */
-#define DEBUG 0
-
-/* Set to 1 to optimize memory stores when generating plasma. */
-#define OPTIMIZE_WRITES  1
-
-/* We're going to perform computations for every pixel of the target
- * bitmap. floating-point operations are very slow on ARMv5, and not
- * too bad on ARMv7 with the exception of trigonometric functions.
- *
- * For better performance on all platforms, we're going to use fixed-point
- * arithmetic and all kinds of tricks
- */
-
-typedef int32_t  Fixed;
-
-#define  FIXED_BITS           16
-#define  FIXED_ONE            (1 << FIXED_BITS)
-#define  FIXED_AVERAGE(x,y)   (((x) + (y)) >> 1)
-
-#define  FIXED_FROM_INT(x)    ((x) << FIXED_BITS)
-#define  FIXED_TO_INT(x)      ((x) >> FIXED_BITS)
-
-#define  FIXED_FROM_FLOAT(x)  ((Fixed)((x)*FIXED_ONE))
-#define  FIXED_TO_FLOAT(x)    ((x)/(1.*FIXED_ONE))
-
-#define  FIXED_MUL(x,y)       (((int64_t)(x) * (y)) >> FIXED_BITS)
-#define  FIXED_DIV(x,y)       (((int64_t)(x) * FIXED_ONE) / (y))
-
-#define  FIXED_DIV2(x)        ((x) >> 1)
-#define  FIXED_AVERAGE(x,y)   (((x) + (y)) >> 1)
-
-#define  FIXED_FRAC(x)        ((x) & ((1 << FIXED_BITS)-1))
-#define  FIXED_TRUNC(x)       ((x) & ~((1 << FIXED_BITS)-1))
-
-#define  FIXED_FROM_INT_FLOAT(x,f)   (Fixed)((x)*(FIXED_ONE*(f)))
-
-typedef int32_t  Angle;
-
-#define  ANGLE_BITS              9
-
-#if ANGLE_BITS < 8
-#  error ANGLE_BITS must be at least 8
-#endif
-
-#define  ANGLE_2PI               (1 << ANGLE_BITS)
-#define  ANGLE_PI                (1 << (ANGLE_BITS-1))
-#define  ANGLE_PI2               (1 << (ANGLE_BITS-2))
-#define  ANGLE_PI4               (1 << (ANGLE_BITS-3))
-
-#define  ANGLE_FROM_FLOAT(x)   (Angle)((x)*ANGLE_PI/M_PI)
-#define  ANGLE_TO_FLOAT(x)     ((x)*M_PI/ANGLE_PI)
-
-#if ANGLE_BITS <= FIXED_BITS
-#  define  ANGLE_FROM_FIXED(x)     (Angle)((x) >> (FIXED_BITS - ANGLE_BITS))
-#  define  ANGLE_TO_FIXED(x)       (Fixed)((x) << (FIXED_BITS - ANGLE_BITS))
-#else
-#  define  ANGLE_FROM_FIXED(x)     (Angle)((x) << (ANGLE_BITS - FIXED_BITS))
-#  define  ANGLE_TO_FIXED(x)       (Fixed)((x) >> (ANGLE_BITS - FIXED_BITS))
-#endif
-
-static Fixed  *angle_sin_tab;
-//static Fixed  angle_sin_tab[ANGLE_2PI+1];
-
-static __inline__ Fixed angle_sin( Angle  a )
-{
-    return angle_sin_tab[(uint32_t)a & (ANGLE_2PI-1)];
-}
-
-static __inline__ Fixed angle_cos( Angle  a )
-{
-    return angle_sin(a + ANGLE_PI2);
-}
-
-static __inline__ Fixed fixed_sin( Fixed  f )
-{
-    return angle_sin(ANGLE_FROM_FIXED(f));
-}
-
-static __inline__ Fixed  fixed_cos( Fixed  f )
-{
-    return angle_cos(ANGLE_FROM_FIXED(f));
-}
-
-/* Color palette used for rendering the plasma */
-#define  PALETTE_BITS   8
-#define  PALETTE_SIZE   (1 << PALETTE_BITS)
-
-#if PALETTE_BITS > FIXED_BITS
-#  error PALETTE_BITS must be smaller than FIXED_BITS
-#endif
-
-#if 0
-static uint16_t  make565(int red, int green, int blue)
-{
-    return (uint16_t)( ((red   << 8) & 0xf800) |
-                       ((green << 2) & 0x03e0) |
-                       ((blue  >> 3) & 0x001f) );
-}
-
-static void init_palette(uint16_t *palette)
-{
-    int  nn, mm = 0;
-    /* fun with colors */
-    for (nn = 0; nn < PALETTE_SIZE/4; nn++) {
-        int  jj = (nn-mm)*4*255/PALETTE_SIZE;
-        palette[nn] = make565(255, jj, 255-jj);
-    }
-
-    for ( mm = nn; nn < PALETTE_SIZE/2; nn++ ) {
-        int  jj = (nn-mm)*4*255/PALETTE_SIZE;
-        palette[nn] = make565(255-jj, 255, jj);
-    }
-
-    for ( mm = nn; nn < PALETTE_SIZE*3/4; nn++ ) {
-        int  jj = (nn-mm)*4*255/PALETTE_SIZE;
-        palette[nn] = make565(0, 255-jj, 255);
-    }
-
-    for ( mm = nn; nn < PALETTE_SIZE; nn++ ) {
-        int  jj = (nn-mm)*4*255/PALETTE_SIZE;
-        palette[nn] = make565(jj, 0, 255);
-    }
-}
-#endif
-static __inline__ uint16_t  palette_from_fixed(uint16_t* palette, Fixed  x )
-{
-    if (x < 0) x = -x;
-    if (x >= FIXED_ONE) x = FIXED_ONE-1;
-    int  idx = FIXED_FRAC(x) >> (FIXED_BITS - PALETTE_BITS);
-    return palette[idx & (PALETTE_SIZE-1)];
-}
-
-
-extern void root(uint32_t width, uint32_t height, uint32_t stride, double  t, uint16_t* palette, void* pixels, void *_angle_sin_tab)
-{
-    angle_sin_tab = _angle_sin_tab;
-    Fixed ft  = FIXED_FROM_FLOAT(t/1000.);
-    Fixed yt1 = FIXED_FROM_FLOAT(t/1230.);
-    Fixed yt2 = yt1;
-    Fixed xt10 = FIXED_FROM_FLOAT(t/3000.);
-    Fixed xt20 = xt10;
-
-#define  YT1_INCR   FIXED_FROM_FLOAT(1/100.)
-#define  YT2_INCR   FIXED_FROM_FLOAT(1/163.)
-
-    int  yy;
-    for (yy = 0; yy < height; yy++) {
-        uint16_t*  line = (uint16_t*)pixels;
-        Fixed      base = fixed_sin(yt1) + fixed_sin(yt2);
-        Fixed      xt1 = xt10;
-        Fixed      xt2 = xt20;
-
-        yt1 += YT1_INCR;
-        yt2 += YT2_INCR;
-
-#define  XT1_INCR  FIXED_FROM_FLOAT(1/173.)
-#define  XT2_INCR  FIXED_FROM_FLOAT(1/242.)
-
-        uint16_t*  line_end = line + width;
-
-        if (line < line_end) {
-            if (((uint32_t)line & 3) != 0) {
-                Fixed ii = base + fixed_sin(xt1) + fixed_sin(xt2);
-
-                xt1 += XT1_INCR;
-                xt2 += XT2_INCR;
-
-                line[0] = palette_from_fixed(palette, ii >> 2);
-                line++;
-            }
-
-            while (line + 2 <= line_end) {
-                Fixed i1 = base + fixed_sin(xt1) + fixed_sin(xt2);
-                xt1 += XT1_INCR;
-                xt2 += XT2_INCR;
-
-                Fixed i2 = base + fixed_sin(xt1) + fixed_sin(xt2);
-                xt1 += XT1_INCR;
-                xt2 += XT2_INCR;
-
-                uint32_t  pixel = ((uint32_t)palette_from_fixed(palette, i1 >> 2) << 16) |
-                                   (uint32_t)palette_from_fixed(palette, i2 >> 2);
-
-                ((uint32_t*)line)[0] = pixel;
-                line += 2;
-            }
-
-            if (line < line_end) {
-                Fixed ii = base + fixed_sin(xt1) + fixed_sin(xt2);
-                line[0] = palette_from_fixed(palette, ii >> 2);
-                line++;
-            }
-        }
-
-        // go to next line
-        pixels = (char*)pixels + stride;
-    }
-}
-
-/* simple stats management */
-typedef struct {
-    double  renderTime;
-    double  frameTime;
-} FrameStats;
-
-#define  MAX_FRAME_STATS  200
-#define  MAX_PERIOD_MS    1500
-
-typedef struct {
-    double  firstTime;
-    double  lastTime;
-    double  frameTime;
-
-    int         firstFrame;
-    int         numFrames;
-    FrameStats  frames[ MAX_FRAME_STATS ];
-} Stats;
-
diff --git a/samples/bitmap-plasma-llvm/jni/plasmaLLVM.cpp b/samples/bitmap-plasma-llvm/jni/plasma.cpp
similarity index 84%
rename from samples/bitmap-plasma-llvm/jni/plasmaLLVM.cpp
rename to samples/bitmap-plasma-llvm/jni/plasma.cpp
index 04dbbbe..d05990f 100644
--- a/samples/bitmap-plasma-llvm/jni/plasmaLLVM.cpp
+++ b/samples/bitmap-plasma-llvm/jni/plasma.cpp
@@ -14,16 +14,22 @@
  * limitations under the License.
  */
 
-#include <jni.h>
-#include <time.h>
-#include <android/log.h>
-#include <android/bitmap.h>
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
 
-#include "bcc/bcc.h"
+#if !defined(__GDK__) && !defined(__NOGDK__)
+#include <bcc/bcc.h>
+#include <dlfcn.h>
+#endif // !__GDK__ && !__NOGDK__
+
+#if !defined(__GDK__)
+#include <jni.h>
+#include <time.h>
+#include <android/bitmap.h>
+#endif // !__GDK__
+
+#include <android/log.h>
 
 #define  LOG_TAG    "libplasma"
 #define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
@@ -35,13 +41,6 @@
 /* Set to 1 to optimize memory stores when generating plasma. */
 #define OPTIMIZE_WRITES  1
 
-/* Return current time in milliseconds */
-static double now_ms(void)
-{
-    struct timeval tv;
-    gettimeofday(&tv, NULL);
-    return tv.tv_sec*1000. + tv.tv_usec/1000.;
-}
 
 /* We're going to perform computations for every pixel of the target
  * bitmap. floating-point operations are very slow on ARMv5, and not
@@ -98,7 +97,11 @@
 #  define  ANGLE_TO_FIXED(x)       (Fixed)((x) >> (ANGLE_BITS - FIXED_BITS))
 #endif
 
+#if defined(__GDK__)
+static Fixed  *angle_sin_tab;
+#else
 static Fixed  angle_sin_tab[ANGLE_2PI+1];
+#endif // !__GDK__
 
 static void init_angles(void)
 {
@@ -171,7 +174,7 @@
     }
 }
 
-static __inline__ uint16_t  palette_from_fixed( Fixed  x )
+static __inline__ uint16_t  palette_from_fixed( uint16_t* palette, Fixed  x )
 {
     if (x < 0) x = -x;
     if (x >= FIXED_ONE) x = FIXED_ONE-1;
@@ -187,8 +190,13 @@
     init_angles();
 }
 
-static void fill_plasma( AndroidBitmapInfo*  info, void*  pixels, double  t )
+
+extern "C" void fill_plasma( 
+    uint32_t width, uint32_t height, uint32_t stride, double t, uint16_t* palette, void* pixels, Fixed *_angle_sin_tab )
 {
+  #if defined(__GDK__)
+    angle_sin_tab = _angle_sin_tab;
+  #endif // !__GDK__
     Fixed ft  = FIXED_FROM_FLOAT(t/1000.);
     Fixed yt1 = FIXED_FROM_FLOAT(t/1230.);
     Fixed yt2 = yt1;
@@ -199,7 +207,7 @@
 #define  YT2_INCR   FIXED_FROM_FLOAT(1/163.)
 
     int  yy;
-    for (yy = 0; yy < info->height; yy++) {
+    for (yy = 0; yy < height; yy++) {
         uint16_t*  line = (uint16_t*)pixels;
         Fixed      base = fixed_sin(yt1) + fixed_sin(yt2);
         Fixed      xt1 = xt10;
@@ -215,7 +223,7 @@
         /* optimize memory writes by generating one aligned 32-bit store
          * for every pair of pixels.
          */
-        uint16_t*  line_end = line + info->width;
+        uint16_t*  line_end = line + width;
 
         if (line < line_end) {
             if (((uint32_t)line & 3) != 0) {
@@ -224,7 +232,7 @@
                 xt1 += XT1_INCR;
                 xt2 += XT2_INCR;
 
-                line[0] = palette_from_fixed(ii >> 2);
+                line[0] = palette_from_fixed(palette, ii >> 2);
                 line++;
             }
 
@@ -237,8 +245,8 @@
                 xt1 += XT1_INCR;
                 xt2 += XT2_INCR;
 
-                uint32_t  pixel = ((uint32_t)palette_from_fixed(i1 >> 2) << 16) |
-                                   (uint32_t)palette_from_fixed(i2 >> 2);
+                uint32_t  pixel = ((uint32_t)palette_from_fixed(palette, i1 >> 2) << 16) |
+                                   (uint32_t)palette_from_fixed(palette, i2 >> 2);
 
                 ((uint32_t*)line)[0] = pixel;
                 line += 2;
@@ -246,28 +254,38 @@
 
             if (line < line_end) {
                 Fixed ii = base + fixed_sin(xt1) + fixed_sin(xt2);
-                line[0] = palette_from_fixed(ii >> 2);
+                line[0] = palette_from_fixed(palette, ii >> 2);
                 line++;
             }
         }
 #else /* !OPTIMIZE_WRITES */
         int xx;
-        for (xx = 0; xx < info->width; xx++) {
+        for (xx = 0; xx < width; xx++) {
 
             Fixed ii = base + fixed_sin(xt1) + fixed_sin(xt2);
 
             xt1 += XT1_INCR;
             xt2 += XT2_INCR;
 
-            line[xx] = palette_from_fixed(ii / 4);
+            line[xx] = palette_from_fixed(palette, ii / 4);
         }
 #endif /* !OPTIMIZE_WRITES */
 
         // go to next line
-        pixels = (char*)pixels + info->stride;
+        pixels = (char*)pixels + stride;
     }
 }
 
+#if !defined(__GDK__)
+
+/* Return current time in milliseconds */
+static double now_ms(void)
+{
+    struct timeval tv;
+    gettimeofday(&tv, NULL);
+    return tv.tv_sec*1000. + tv.tv_usec/1000.;
+}
+
 /* simple stats management */
 typedef struct {
     double  renderTime;
@@ -363,7 +381,23 @@
     s->lastTime = now;
 }
 
-typedef void (*pPlasmaType)(uint32_t, uint32_t, uint32_t, double, uint16_t*, void*, void*);
+typedef void (*pPlasmaType)(uint32_t, uint32_t, uint32_t, double, uint16_t*, void*, Fixed*);
+
+#if !defined(__GDK__) && !defined(__NOGDK__)
+static void* lookupSymbol(void* pContext, const char* name)
+{
+    return (void*) dlsym(RTLD_DEFAULT, name);
+}
+#endif // !__GDK__ && !__NOGDK__
+
+extern "C" JNIEXPORT jboolean JNICALL Java_com_example_plasma_llvm_PlasmaView_gdk(JNIEnv *env, jobject obj)
+{
+#if !defined(__NOGDK__)
+   return JNI_TRUE;
+#else
+   return JNI_FALSE;
+#endif
+}
 
 extern "C" JNIEXPORT jint JNICALL Java_com_example_plasma_llvm_PlasmaView_nativeRenderPlasma
     (JNIEnv * env, jobject  obj,
@@ -376,6 +410,7 @@
     static int         init;
     static double      time_sum = 0;
     static int         count = 0;
+#if !defined(__NOGDK__)
     static bool        last_mode = false;
     static pPlasmaType native_function = NULL;
     static BCCScriptRef script_ref;
@@ -383,7 +418,8 @@
     if (last_mode != use_llvm)
       count = 0, time_sum = 0;
     last_mode = use_llvm;
-
+#endif // !__NOGDK__
+   
     if (!init) {
         init_tables();
         stats_init(&stats);
@@ -403,8 +439,8 @@
     if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) < 0) {
         LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
     }
-
-
+   
+#if !defined(__NOGDK__)
     if (use_llvm) {
       double start_jit = now_ms();
 
@@ -422,16 +458,28 @@
           env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr, 0);
         }
 
+     #if 0
         if (bccLinkFile(script_ref, "/system/lib/libclcore.bc", 0)) {
           LOGE("Error! Cannot bccLinkBC");
           return -1;
         }
-
+     #endif	 
+	 
+	bccRegisterSymbolCallback(script_ref, lookupSymbol, NULL);
+	 
+     #ifdef OLD_BCC 
+        if (bccPrepareExecutable(script_ref, "/data/data/com.example.plasma.llvm/plasmaLLVM.oBCC", 0)) {
+          LOGE("Error! Cannot bccPrepareExecutable");
+          return -1;
+        }
+     #else        	 
         if (bccPrepareExecutable(script_ref, "/data/data/com.example.plasma.llvm/", "plasmaLLVM", 0)) {
           LOGE("Error! Cannot bccPrepareExecutable");
           return -1;
         }
-        native_function = (pPlasmaType)bccGetFuncAddr(script_ref, "root");
+     #endif // OLD_BCC
+	 
+        native_function = (pPlasmaType)bccGetFuncAddr(script_ref, "fill_plasma");
         if (native_function == NULL) {
           LOGE("Error! Cannot find fill_plasma()");
           return -1;
@@ -441,13 +489,19 @@
       double start_run = now_ms();
       native_function(info.width, info.height, info.stride, time_ms, palette, pixels, angle_sin_tab);
       double diff = now_ms()-start_run;
-      LOGI("LLVM Time JIT: %.2lf , Run: %.2lf, Avg: %.2lf", start_run-start_jit, diff, time_sum / count);
+      if (((count+1) % 30) == 0)
+          LOGI("LLVM Time JIT: %.2lf , Run: %.2lf, Avg: %.2lf, count=%d", start_run-start_jit, diff, time_sum / count, count+1);
       time_sum += diff + start_run - start_jit;
-    } else {
+    } 
+    else 
+#endif // !__NOGDK__
+
+    {
       double start_run = now_ms();
-      fill_plasma(&info, pixels, time_ms );
+      fill_plasma(info.width, info.height, info.stride, time_ms, palette, pixels, angle_sin_tab);
       double diff = now_ms()-start_run;
-      LOGI("GCC Time Run: %.2lf, Avg: %.2lf", diff, time_sum / count);
+      if (((count+1) % 30) == 0)
+          LOGI("GCC Time Run: %.2lf, Avg: %.2lf, count=%d", diff, time_sum / count, count+1);
       time_sum += diff;
     }
     count++;
@@ -456,3 +510,5 @@
 
     return count * 1000.0 / time_sum;
 }
+
+#endif // !__GDK
diff --git a/samples/bitmap-plasma-llvm/res/raw/gdb.setup b/samples/bitmap-plasma-llvm/res/raw/gdb.setup
deleted file mode 100644
index d7abec1..0000000
--- a/samples/bitmap-plasma-llvm/res/raw/gdb.setup
+++ /dev/null
@@ -1,2 +0,0 @@
-set solib-search-path /home/nowar/cyanogen/gdk/samples/bitmap-plasma-llvm/obj/local/armeabi-v7a
-directory /home/nowar/cyanogen/gdk/platforms/android-9/arch-arm/usr/include /home/nowar/cyanogen/gdk/samples/bitmap-plasma-llvm/jni /home/nowar/cyanogen/gdk/sources/cxx-stl/system
diff --git a/samples/bitmap-plasma-llvm/res/raw/gdbserver b/samples/bitmap-plasma-llvm/res/raw/gdbserver
deleted file mode 100755
index 39634a8..0000000
--- a/samples/bitmap-plasma-llvm/res/raw/gdbserver
+++ /dev/null
Binary files differ
diff --git a/samples/bitmap-plasma-llvm/src/com/example/plasma/llvm/Plasma.java b/samples/bitmap-plasma-llvm/src/com/example/plasma/llvm/Plasma.java
index 676ca05..a627a1c 100644
--- a/samples/bitmap-plasma-llvm/src/com/example/plasma/llvm/Plasma.java
+++ b/samples/bitmap-plasma-llvm/src/com/example/plasma/llvm/Plasma.java
@@ -77,49 +77,56 @@
     private long mStartTime;
 
     /* implementend by libplasma.so */
+    private static native boolean gdk();
     private static native int nativeRenderPlasma(Bitmap  bitmap, long time_ms, byte[] script, int scriptLength, boolean useLLVM);
 
     private byte[] pgm;
     private int pgmLength;
 
-    private boolean mode = true;
+    private boolean llvm_mode = false;
     private Paint paint = null;
 
     public void switchMode() {
-        mode = !mode;
+        if (gdk())
+            llvm_mode = !llvm_mode;
     }
 
     public PlasmaView(Context context) {
         super(context);
+       
+        llvm_mode = gdk();
 
         mStartTime = System.currentTimeMillis();
-
-        InputStream is = null;
-        is = getResources().openRawResource(R.raw.libplasma_portable);
-        try {
+        if (llvm_mode)
+        {
+            InputStream is = null;
+            is = getResources().openRawResource(R.raw.libplasma_portable);
             try {
-                pgm = new byte[1024];
-                pgmLength = 0;
-                while(true) {
-                    int bytesLeft = pgm.length - pgmLength;
-                    if (bytesLeft == 0) {
-                        byte[] buf2 = new byte[pgm.length * 2];
-                        System.arraycopy(pgm, 0, buf2, 0, pgm.length);
-                        pgm = buf2;
-                        bytesLeft = pgm.length - pgmLength;
+                try {
+                    pgm = new byte[1024];
+                    pgmLength = 0;
+                    while(true) {
+                        int bytesLeft = pgm.length - pgmLength;
+                        if (bytesLeft == 0) {
+                            byte[] buf2 = new byte[pgm.length * 2];
+                            System.arraycopy(pgm, 0, buf2, 0, pgm.length);
+                            pgm = buf2;
+                            bytesLeft = pgm.length - pgmLength;
+                        }
+                        int bytesRead = is.read(pgm, pgmLength, bytesLeft);
+                        if (bytesRead <= 0) {
+                            break;
+                        }
+                        pgmLength += bytesRead;
                     }
-                    int bytesRead = is.read(pgm, pgmLength, bytesLeft);
-                    if (bytesRead <= 0) {
-                        break;
-                    }
-                    pgmLength += bytesRead;
+                } finally {
+                    is.close();
                 }
-            } finally {
-                is.close();
+            } catch(IOException e) {
+                throw new Resources.NotFoundException();
             }
-        } catch(IOException e) {
-            throw new Resources.NotFoundException();
-        }
+	}
+       
         paint = new Paint();
         paint.setTextSize(40);
     }
@@ -128,9 +135,9 @@
         if (mBitmap == null || mBitmap.getWidth() != getWidth() || mBitmap.getHeight() != getHeight())
             mBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.RGB_565);
 
-        int frameRate = nativeRenderPlasma(mBitmap, System.currentTimeMillis() - mStartTime, pgm, pgmLength, mode);
+        int frameRate = nativeRenderPlasma(mBitmap, System.currentTimeMillis() - mStartTime, pgm, pgmLength, llvm_mode);
         canvas.drawBitmap(mBitmap, 0, 0, null);
-        canvas.drawText((mode ? "LLVM" : "GCC") + " Frame: " + Integer.toString(frameRate), 100, 100, paint);
+        canvas.drawText((llvm_mode ? "LLVM/GDK" : "Native") + " Frame: " + Integer.toString(frameRate), 100, 100, paint);
 
         // force a redraw, with a different time-based pattern.
         invalidate();
diff --git a/samples/quake/jni/Android-portable.mk b/samples/quake/jni/Android-portable.mk
index a2fd8b1..1cfacb0 100644
--- a/samples/quake/jni/Android-portable.mk
+++ b/samples/quake/jni/Android-portable.mk
@@ -63,12 +63,11 @@
   world.cpp \
   zone.cpp
 
-LOCAL_CFLAGS := -fno-zero-initialized-in-bss \
-  -I$(OUT)/../../../../dalvik/libnativehelper/include \
-  -I$(OUT)/../../../../frameworks/base/include \
+LOCAL_CFLAGS := -D__GDK__ \
+  -fno-zero-initialized-in-bss \
+  -I$(OUT)/../../../../frameworks/native/include \
   -I$(OUT)/../../../../system/core/include
 
 LOCAL_C_INCLUDES := $(call include-path-for, wilhelm)
 
-
 include $(BUILD_BITCODE)
diff --git a/samples/quake/jni/Android.mk b/samples/quake/jni/Android.mk
index 1881080..27dd381 100644
--- a/samples/quake/jni/Android.mk
+++ b/samples/quake/jni/Android.mk
@@ -34,8 +34,7 @@
 
 LOCAL_CFLAGS := -Werror \
 	-I$(OUT)/../../../../frameworks/compile/libbcc/include \
-	-I$(OUT)/../../../../dalvik/libnativehelper/include \
-	-I$(OUT)/../../../../frameworks/base/include \
+	-I$(OUT)/../../../../frameworks/native/include \
 	-I$(OUT)/../../../../system/core/include
 
 LOCAL_C_INCLUDES := $(call include-path-for, wilhelm)
diff --git a/samples/quake/jni/Application.mk b/samples/quake/jni/Application.mk
new file mode 100644
index 0000000..a64dbba
--- /dev/null
+++ b/samples/quake/jni/Application.mk
@@ -0,0 +1,5 @@
+APP_ABI := armeabi armeabi-v7a 
+#APP_ABI := mips mips-r2 
+#APP_ABI := x86
+APP_PLATFORM := android-9
+
diff --git a/samples/quake/jni/gl_draw.cpp b/samples/quake/jni/gl_draw.cpp
index e822a65..775ac77 100644
--- a/samples/quake/jni/gl_draw.cpp
+++ b/samples/quake/jni/gl_draw.cpp
@@ -116,6 +116,7 @@
 #include <sys/mman.h>
 #include <fcntl.h>
 #include <errno.h>
+int errno_portable();
 
 // Allow named textures to be evicted from memory.
 
@@ -288,25 +289,25 @@
         mFileId = open(fullpath, O_RDWR | O_CREAT, 0666);
         if ( mFileId == -1 ) {
             Sys_Error("Could not open texture store file %s: %d", fullpath,
-                    errno);
+                    errno_portable());
         }
 
         if (-1 == lseek(mFileId, TEXTURE_STORE_SIZE-1, SEEK_SET)) {
             Sys_Error("Could not extend the texture store file size. %d",
-                    errno);
+                    errno_portable());
         }
         char end;
         end = 0;
         if (-1 == write(mFileId, &end, 1)) {
             Sys_Error("Could not write last byte of the texture store file. %d",
-                    errno);
+                    errno_portable());
         }
 
         mBase = (byte*) mmap((caddr_t)0, TEXTURE_STORE_SIZE,
                 PROT_READ | PROT_WRITE, MAP_PRIVATE, mFileId, 0);
 
         if (mBase == (byte*) -1) {
-            Sys_Error("Could not mmap file %s: %d", fullpath, errno);
+            Sys_Error("Could not mmap file %s: %d", fullpath, errno_portable());
         }
         mLength = 0;
         mCapacity = TEXTURE_STORE_SIZE;
diff --git a/samples/quake/jni/gl_vidandroid.cpp b/samples/quake/jni/gl_vidandroid.cpp
index 02588e5..c8d3847 100644
--- a/samples/quake/jni/gl_vidandroid.cpp
+++ b/samples/quake/jni/gl_vidandroid.cpp
@@ -490,7 +490,7 @@
 // Called from Java to process an event.
 // Return non-zero if the event is handled.
 
-#if !defined(__clang__)
+#if !defined(__GDK__)
 int AndroidEvent2(int type, int value)
 #else
 extern "C" int AndroidEvent2_LLVM(int type, int value)
@@ -660,7 +660,7 @@
 
 GestureDetector gGestureDetector;
 
-#if !defined(__clang__)
+#if !defined(__GDK__)
 int AndroidMotionEvent
 #else
 extern "C" int AndroidMotionEvent_LLVM
@@ -681,7 +681,7 @@
     return true;
 }
 
-#if !defined(__clang__)
+#if !defined(__GDK__)
 int AndroidTrackballEvent
 #else
 extern "C" int AndroidTrackballEvent_LLVM
diff --git a/samples/quake/jni/masterMain.cpp b/samples/quake/jni/masterMain.cpp
index e513171..67e9697 100644
--- a/samples/quake/jni/masterMain.cpp
+++ b/samples/quake/jni/masterMain.cpp
@@ -15,22 +15,23 @@
 ** limitations under the License.
 */
 
-#include <nativehelper/jni.h>
+#include <jni.h>
 #include <stdio.h>
 #include <assert.h>
-#include <dlfcn.h>
 
-#if !defined(__clang__)
+#if !defined(__GDK__)
 #include <bcc/bcc.h>
+#include <dlfcn.h>
 #endif
 
 #include <android/log.h>
+#include "sys.h"
 
 #define LOG_TAG "Quake masterMain"
 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
 
-#if !defined(__clang__)
+#if !defined(__GDK__)
 int AndroidInit();
 int AndroidEvent2(int type, int value);
 int AndroidMotionEvent(unsigned long long eventTime, int action, float x, float y, float pressure, float size, int deviceId);
@@ -91,14 +92,27 @@
     return pAndroidQuit();
 }
 
+jboolean 
+gdk(JNIEnv *env, jobject obj)
+{
+#if !defined(__NOGDK__)
+   return JNI_TRUE;
+#else
+   return JNI_FALSE;
+#endif
+}
+
+#if !defined(__GDK__) && !defined(__NOGDK__)
 static void* lookupSymbol(void* pContext, const char* name)
 {
     return (void*) dlsym(RTLD_DEFAULT, name);
 }
+#endif // !__GDK__ && !__NOGDK__
 
 jboolean
 qcompile_bc(JNIEnv *env, jobject thiz, jbyteArray scriptRef, jint length)
 {
+#if !defined(__GDK__) && !defined(__NOGDK__)
     if (!use_llvm)
        return JNI_TRUE;
 
@@ -110,6 +124,8 @@
     pAndroidQuitType new_pAndroidQuit;
     int all_func_found = 1;
 
+    double time = Sys_FloatTime ();
+
     BCCScriptRef script_ref = bccCreateScript();
     jbyte* script_ptr = (jbyte *)env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
     LOGI("BCC Script Len: %d", length);
@@ -126,11 +142,28 @@
         return JNI_FALSE;
     }
   #endif
+
+    double newtime = Sys_FloatTime ();
+    double readtime = newtime - time;
+    time = newtime;
+
     bccRegisterSymbolCallback(script_ref, lookupSymbol, NULL);
-    if (bccPrepareExecutableEx(script_ref, ".", "/data/data/com.android.quake.llvm/quakeLLVM", 0)) {
-        LOGE("Error! Cannot bccPrepareExecutableEx");
+   
+ #ifdef OLD_BCC
+    if (bccPrepareExecutable(script_ref, "/data/data/com.android.quake.llvm/quakeLLVM.oBCC", 0)) {
+        LOGE("Error! Cannot bccPrepareExecutable");
         return JNI_FALSE;
     }
+ #else  
+    if (bccPrepareExecutable(script_ref, "/data/data/com.android.quake.llvm/", "quakeLLVM", 0)) {
+        LOGE("Error! Cannot bccPrepareExecutable");
+        return JNI_FALSE;
+    }
+ #endif // OLD_BCC
+
+    newtime = Sys_FloatTime ();
+    double compiletime = newtime - time;
+    time = newtime;
 
     new_pAndroidInit = (pAndroidInitType)bccGetFuncAddr(script_ref, "AndroidInit_LLVM");
     if (new_pAndroidInit == NULL) {
@@ -182,7 +215,6 @@
 
     //bccDisposeScript(script_ref);
 
-  //Uncomment the following
     if (all_func_found)
     {
         LOGI("Use LLVM version");
@@ -193,14 +225,25 @@
         pAndroidStep = new_pAndroidStep;
         pAndroidQuit = new_pAndroidQuit;
     }
+    newtime = Sys_FloatTime ();
+    double findptrtime = newtime - time;
+    
+    LOGI("LLVM JIT time = %lf (%lf + %lf + %lf)\n", readtime+compiletime+findptrtime, 
+	   readtime, compiletime, findptrtime);
 
     return JNI_TRUE;
+#else
+   
+    return JNI_FALSE;
+   
+#endif // !__GDK__ && !__NOGDK__
 }
 
 
 static const char *classPathName = "com/android/quake/llvm/QuakeLib";
 
 static JNINativeMethod methods[] = {
+  {"gdk", "()Z", (void*)gdk },
   {"compile_bc", "([BI)Z", (void*)qcompile_bc },
   {"init", "()Z", (void*)qinit },
   {"event", "(II)Z", (void*)qevent },
@@ -293,4 +336,4 @@
 }
 
 
-#endif // __clang__
+#endif // __GDK__
diff --git a/samples/quake/jni/net_udp.cpp b/samples/quake/jni/net_udp.cpp
index 68fba41..aaf470f 100644
--- a/samples/quake/jni/net_udp.cpp
+++ b/samples/quake/jni/net_udp.cpp
@@ -51,6 +51,7 @@
 
 #include "net_udp.h"
 
+
 // **** Start of Android-specific code ****
 // copied from ifc_utils.c
 //
@@ -61,14 +62,109 @@
 #include <sys/ioctl.h>
 #include <net/if.h>
 
+//
+// system dependent 
+//
+
+#define SOCK_DGRAM_PORTABLE    2
+#define SOCK_STREAM_PORTABLE   1
+#define FIONBIO_PORTABLE       0x5421
+#define FIONREAD_PORTABLE      0x541B
+#define ECONNREFUSED_PORTABLE  111
+#define SOL_SOCKET_PORTABLE     1 
+#define SO_BROADCAST_PORTABLE   6 
+
+#define SOCK_DGRAM_MIPS    1
+#define SOCK_STREAM_MIPS   2
+#define FIONBIO_MIPS       0x667e
+#define FIONREAD_MIPS      0x467f
+#define ECONNREFUSED_MIPS  146
+#define SOL_SOCKET_MIPS     0xffff 
+#define SO_BROADCAST_MIPS   0x0020
+
+
+int socket_portable(int domain, int type, int protocol);
+int ioctl_portable(int fd, int cmd, void *);  // ToDo: ioctl_portable(int fd, int cmd, ...)
+int setsockopt_portable(int s, int level, int optname, const void *optval, socklen_t optlen);
+int errno_portable();
+
+
+#if !defined(__GDK__)
+int socket_portable(int domain, int type, int protocol)
+{
+#if defined(__mips__) 
+   switch(type) {
+   case SOCK_DGRAM_PORTABLE: 
+      type = SOCK_DGRAM_MIPS; 
+      break;
+   case SOCK_STREAM_PORTABLE:
+      type = SOCK_STREAM_MIPS; 
+      break;
+   }
+#endif // __mips__
+   return socket(domain, type, protocol);
+}
+
+int ioctl_portable(int fd, int cmd, void *arg)
+{
+#if defined(__mips__)
+   switch(cmd) {
+    case FIONBIO_PORTABLE:
+      cmd = FIONBIO_MIPS; 
+      break;
+    case FIONREAD_PORTABLE:
+      cmd = FIONREAD_MIPS;
+    //ToDo: there are a lot more diff!  
+   }
+#endif // __mips__
+   return ioctl(fd, cmd, arg);
+}
+   
+int errno_portable()
+{
+   int ret = errno;
+#if defined(__mips__)
+   switch(ret) {
+   case ECONNREFUSED_PORTABLE: 
+      ret = ECONNREFUSED_MIPS;
+      break;
+     //ToDo: there are a lot more diff!
+   }
+#endif // __mips__   
+   return ret;
+}
+   
+int setsockopt_portable(int s, int level, int optname, const void *optval, socklen_t optlen)
+{
+#if defined(__mips__)
+   switch(level) {
+   case SOL_SOCKET_PORTABLE: 
+      level = SOL_SOCKET_MIPS;
+      break;
+     //ToDo: there are a lot more diff!
+   }
+   
+   switch(optname) {
+   case SO_BROADCAST_PORTABLE: 
+      optname = SO_BROADCAST_MIPS;
+      break;
+     //ToDo: there are a lot more diff!
+   }
+#endif   
+   return setsockopt(s, level, optname, optval, optlen);
+}
+
+#endif // !__GDK__
+
+
 static int ifc_ctl_sock = -1;
 
 int ifc_init(void)
 {
     if (ifc_ctl_sock == -1) {
-        ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
+        ifc_ctl_sock = socket_portable(AF_INET, SOCK_DGRAM_PORTABLE, 0);
         if (ifc_ctl_sock < 0) {
-        	Con_Printf("socket() failed: %s\n", strerror(errno));
+        	Con_Printf("socket() failed: %s\n", strerror(errno_portable()));
         }
     }
     return ifc_ctl_sock < 0 ? -1 : 0;
@@ -95,7 +191,7 @@
     ifc_init_ifr(name, &ifr);
 
     if (addr != NULL) {
-        if(ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr) < 0) {
+        if(ioctl_portable(ifc_ctl_sock, SIOCGIFADDR, &ifr) < 0) {
             *addr = 0;
         } else {
             *addr = ((struct sockaddr_in*) (void*) &ifr.ifr_addr)->sin_addr.s_addr;
@@ -103,7 +199,7 @@
     }
 
     if (mask != NULL) {
-        if(ioctl(ifc_ctl_sock, SIOCGIFNETMASK, &ifr) < 0) {
+        if(ioctl_portable(ifc_ctl_sock, SIOCGIFNETMASK, &ifr) < 0) {
             *mask = 0;
         } else {
             *mask = ((struct sockaddr_in*) (void*) &ifr.ifr_addr)->sin_addr.s_addr;
@@ -111,7 +207,7 @@
     }
 
     if (flags != NULL) {
-        if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) {
+        if(ioctl_portable(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) {
             *flags = 0;
         } else {
             *flags = ifr.ifr_flags;
@@ -233,10 +329,10 @@
 	} address;
 	qboolean _true = true;
 
-	if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
+	if ((newsocket = socket_portable(PF_INET, SOCK_DGRAM_PORTABLE, IPPROTO_UDP)) == -1)
 		return -1;
 
-	if (ioctl (newsocket, FIONBIO, (char *)&_true) == -1)
+	if (ioctl_portable(newsocket, FIONBIO_PORTABLE, (char *)&_true) == -1)
 		goto ErrorReturn;
 
 	address.in.sin_family = AF_INET;
@@ -335,7 +431,7 @@
 	if (net_acceptsocket == -1)
 		return -1;
 
-	if (ioctl (net_acceptsocket, FIONREAD, &available) == -1)
+	if (ioctl_portable(net_acceptsocket, FIONREAD_PORTABLE, &available) == -1)
 		Sys_Error ("UDP: ioctlsocket (FIONREAD) failed\n");
 	if (available)
 		return net_acceptsocket;
@@ -350,7 +446,7 @@
 	int ret;
 
 	ret = recvfrom (socket, buf, len, 0, (struct sockaddr *)addr, (socklen_t*) &addrlen);
-	if (ret == -1 && (errno == EWOULDBLOCK || errno == ECONNREFUSED))
+	if (ret == -1 && (errno_portable() == EWOULDBLOCK || errno_portable() == ECONNREFUSED_PORTABLE))
 		return 0;
 	return ret;
 }
@@ -362,7 +458,7 @@
 	int				i = 1;
 
 	// make this socket broadcast capable
-	if (setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) < 0)
+	if (setsockopt_portable(socket, SOL_SOCKET_PORTABLE, SO_BROADCAST_PORTABLE, (char *)&i, sizeof(i)) < 0)
 		return -1;
 	net_broadcastsocket = socket;
 
@@ -397,7 +493,7 @@
 	int ret;
 
 	ret = sendto (socket, buf, len, 0, (struct sockaddr *)addr, sizeof(struct qsockaddr));
-	if (ret == -1 && errno == EWOULDBLOCK)
+	if (ret == -1 && errno_portable() == EWOULDBLOCK)
 		return 0;
 	return ret;
 }
diff --git a/samples/quake/jni/snd_dma.cpp b/samples/quake/jni/snd_dma.cpp
index e46469f..7e11149 100644
--- a/samples/quake/jni/snd_dma.cpp
+++ b/samples/quake/jni/snd_dma.cpp
@@ -19,8 +19,14 @@
 */
 // snd_dma.c -- main control for any streaming sound output device
 
+#include <android/log.h>
 #include "quakedef.h"
 
+#define LOG_TAG "Quake snd_dma"
+#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
+#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
+
+
 #ifdef _WIN32
 #include "winquake.h"
 #endif
@@ -144,7 +150,6 @@
 	if (!fakedma)
 	{
 		rc = SNDDMA_Init();
-
 		if (!rc)
 		{
 #ifndef	_WIN32
@@ -199,8 +204,6 @@
 		Con_Printf ("loading all sounds as 8bit\n");
 	}
 
-
-
 	snd_initialized = true;
 
 	S_Startup ();
diff --git a/samples/quake/jni/sys_android.cpp b/samples/quake/jni/sys_android.cpp
index 90f0134..5d418ea 100644
--- a/samples/quake/jni/sys_android.cpp
+++ b/samples/quake/jni/sys_android.cpp
@@ -39,6 +39,7 @@
 #include <sys/wait.h>
 #include <sys/mman.h>
 #include <errno.h>
+int errno_portable();
 #include <dirent.h>
 
 #include <android/log.h>
@@ -64,6 +65,142 @@
 
 cvar_t  sys_linerefresh = CVAR2("sys_linerefresh","0");// set for entity display
 
+
+//
+// system dependent 
+//    
+
+struct stat_portable {
+    unsigned long long st_dev;
+    unsigned char __pad0[4];
+
+    unsigned long __st_ino;  
+    unsigned int st_mode;
+    unsigned int st_nlink;
+
+    unsigned long st_uid;
+    unsigned long st_gid;
+
+    unsigned long long st_rdev;
+    unsigned char __pad3[4];
+
+    long long st_size;
+    unsigned long st_blksize;
+    unsigned long long st_blocks;
+
+    unsigned long st_atime;
+    unsigned long st_atime_nsec;
+
+    unsigned long st_mtime;
+    unsigned long st_mtime_nsec;
+
+    unsigned long st_ctime;
+    unsigned long st_ctime_nsec;
+
+    unsigned long long st_ino;
+};
+
+#if defined(__mips__)
+struct stat_mips {
+    unsigned long st_dev;
+    unsigned long __pad0[3];
+
+    unsigned long long st_ino;
+
+    unsigned int st_mode;
+    unsigned int st_nlink;
+
+    unsigned long st_uid;
+    unsigned long st_gid;
+
+    unsigned long st_rdev;
+    unsigned long __pad1[3];
+
+    long long st_size;
+
+    unsigned long st_atime;
+    unsigned long st_atime_nsec;
+
+    unsigned long st_mtime;
+    unsigned long st_mtime_nsec;
+
+    unsigned long st_ctime;
+    unsigned long st_ctime_nsec;
+
+    unsigned long st_blksize;
+    unsigned long __pad2;
+
+    unsigned long long st_blocks;
+};
+#endif  //__mips__
+
+int stat_portable(const char *path, struct stat_portable *st);
+int fstat_portable(int fd, struct stat_portable *st);
+
+
+#if !defined(__GDK__)
+
+#if defined(__mips__)
+static void __copy_mips_stat_to_portable(struct stat_portable *pst_portable, struct stat/*_mips*/ *pst_mips)
+{
+   pst_portable->st_dev = pst_mips->st_dev;  //ToDo: sizeof st_dev is different !
+   pst_portable->__st_ino = 0;             //ToDo: missing in mips!
+   pst_portable->st_mode = pst_mips->st_mode;
+   pst_portable->st_nlink = pst_mips->st_nlink;
+   pst_portable->st_uid = pst_mips->st_uid;
+   pst_portable->st_gid = pst_mips->st_gid;
+   pst_portable->st_rdev = pst_mips->st_rdev;  //ToDo: st_rdev is different !
+   pst_portable->st_size = pst_mips->st_size;
+   pst_portable->st_blksize = pst_mips->st_blksize;
+   pst_portable->st_blocks = pst_mips->st_blocks;
+   pst_portable->st_atime = pst_mips->st_atime;
+   pst_portable->st_atime_nsec = pst_mips->st_atime_nsec;
+   pst_portable->st_mtime = pst_mips->st_mtime;
+   pst_portable->st_mtime_nsec = pst_mips->st_mtime_nsec;
+   pst_portable->st_ctime = pst_mips->st_ctime;
+   pst_portable->st_ctime_nsec = pst_mips->st_ctime_nsec;
+   pst_portable->st_ino = pst_mips->st_ino; 
+}
+#endif // __mips__
+			
+int stat_portable(const char *path, struct stat_portable *st)
+{
+#if !defined(__mips__)
+   struct stat st_orig;
+   //assert(sizeof(st_orig) == sizeof(*st));   //ToDo: and offset of each field !
+   
+   return stat(path, (struct stat*)st);
+#else
+   struct stat/*_mips*/ st_mips;
+   //assert(sizeof(st_mips) == sizeof(*st));   //ToDo: and offset of each field !
+   
+   int ret = stat(path, &st_mips);
+   __copy_mips_stat_to_portable((struct stat_portable *)st, &st_mips);
+   
+   return ret;
+#endif   
+}
+
+int fstat_portable(int fd, struct stat_portable *st)
+{
+#if !defined(__mips__)
+   struct stat st_orig;
+   //assert(sizeof(st_orig) == sizeof(*st));   //ToDo: and offset of each field !
+   
+   return fstat(fd, (struct stat*)st);
+#else
+   struct stat/*_mips*/ st_mips;
+   //assert(sizeof(st_mips) == sizeof(*st));   //ToDo: and offset of each field !
+   
+   int ret = fstat(fd, &st_mips);
+   __copy_mips_stat_to_portable((struct stat_portable *)st, &st_mips);
+   
+   return ret;
+#endif   
+}
+
+#endif // !__GDK__
+
 // =======================================================================
 // General routines
 // =======================================================================
@@ -220,9 +357,9 @@
 */
 int	Sys_FileTime (const char *path)
 {
-  struct	stat	buf;
+  struct stat_portable buf;
 
-  if (stat (path,&buf) == -1)
+  if (stat_portable(path,&buf) == -1)
     return -1;
 
   return buf.st_mtime;
@@ -237,15 +374,14 @@
 int Sys_FileOpenRead (const char *path, int *handle)
 {
   int	h;
-  struct stat	fileinfo;
+  struct stat_portable fileinfo;
 
-
-  h = open (path, O_RDONLY, 0666);
+  h = open(path, O_RDONLY, 0666);
   *handle = h;
   if (h == -1)
     return -1;
 
-  if (fstat (h,&fileinfo) == -1)
+  if (fstat_portable(h,&fileinfo) == -1)
     Sys_Error ("Error fstating %s", path);
 
   return fileinfo.st_size;
@@ -261,7 +397,7 @@
   , 0666);
 
   if (handle == -1)
-    Sys_Error ("Error opening %s: %s", path,strerror(errno));
+    Sys_Error ("Error opening %s: %s", path,strerror(errno_portable()));
 
   return handle;
 }
@@ -401,15 +537,19 @@
 
 qboolean direxists(const char* path)
 {
-  struct stat sb;
-  if(stat(path, &sb))
+  struct stat_portable sb;
+   
+  if(stat_portable(path, &sb))
   {
+LOGI("direxists 1: path=%s\n", path);
     return 0;	// error
   }
   if(sb.st_mode & S_IFDIR)
   {
+LOGI("direxists 2: path=%s\n", path);
      return 1;
   }
+LOGI("direxists 3: path=%s\n", path);
   return 0;
 }
 
@@ -488,7 +628,7 @@
       fwrite(&vernum, sizeof(vernum), 1, f);
       fclose(f);
     } else {
-        PMPLOG(("Could not write %s %d.\n", cachePath, errno));
+        PMPLOG(("Could not write %s %d.\n", cachePath, errno_portable()));
     }
   }
 }
@@ -505,7 +645,7 @@
 static qboolean gInitialized;
 void GL_ReInit();
 
-#if !defined(__clang__)
+#if !defined(__GDK__)
 bool AndroidInit()
 #else
 extern "C" bool AndroidInit_LLVM()
@@ -665,7 +805,7 @@
   return key_dest == key_game;
 }
 
-#if !defined(__clang__)
+#if !defined(__GDK__)
 int AndroidStep(int width, int height)
 #else
 extern "C" int AndroidStep_LLVM(int width, int height)
@@ -685,7 +825,7 @@
 extern void Host_Quit();
 
 
-#if !defined(__clang__)
+#if !defined(__GDK__)
 void AndroidQuit() {
 #else
 extern "C" void AndroidQuit_LLVM() {
diff --git a/samples/quake/src/com/android/quake/llvm/QuakeActivity.java b/samples/quake/src/com/android/quake/llvm/QuakeActivity.java
index 1a0bc99..a264902 100644
--- a/samples/quake/src/com/android/quake/llvm/QuakeActivity.java
+++ b/samples/quake/src/com/android/quake/llvm/QuakeActivity.java
@@ -51,40 +51,42 @@
         }
 
         if (foundQuakeData()) {
-          // HACK: create faked view in order to read bitcode in resource
+            byte[] pgm = null;
+            int pgmLength = 0;
+            if (QuakeLib.gdk())
+            {
+              // HACK: create faked view in order to read bitcode in resource
                 View view = new View(getApplication());
-                byte[] pgm;
-                int pgmLength;
 
-            // read bitcode in res
-            InputStream is = view.getResources().openRawResource(R.raw.libquake_portable);
-            try {
-               try {
-                  pgm = new byte[1024];
-                  pgmLength = 0;
+                // read bitcode in res
+                InputStream is = view.getResources().openRawResource(R.raw.libquake_portable);
+                try {
+                    try {
+                        pgm = new byte[1024];
+                        pgmLength = 0;
 
-                  while(true) {
-                     int bytesLeft = pgm.length - pgmLength;
-                     if (bytesLeft == 0) {
-                         byte[] buf2 = new byte[pgm.length * 2];
-                         System.arraycopy(pgm, 0, buf2, 0, pgm.length);
-                         pgm = buf2;
-                         bytesLeft = pgm.length - pgmLength;
+                        while(true) {
+                            int bytesLeft = pgm.length - pgmLength;
+                            if (bytesLeft == 0) {
+                                byte[] buf2 = new byte[pgm.length * 2];
+                                System.arraycopy(pgm, 0, buf2, 0, pgm.length);
+                                pgm = buf2;
+                                bytesLeft = pgm.length - pgmLength;
+                            }
+                            int bytesRead = is.read(pgm, pgmLength, bytesLeft);
+                            if (bytesRead <= 0) {
+                               break;
+                            }
+                            pgmLength += bytesRead;
+                        }
+                    } finally {
+                         is.close();
                      }
-                     int bytesRead = is.read(pgm, pgmLength, bytesLeft);
-                     if (bytesRead <= 0) {
-                        break;
-                     }
-                     pgmLength += bytesRead;
-    	          }
-               } finally {
-                  is.close();
-               }
-            } catch(IOException e) {
-               throw new Resources.NotFoundException();
-            }
-
-          //
+                } catch(IOException e) {
+                    throw new Resources.NotFoundException();
+                }
+	    }
+         //
             if (mQuakeLib == null) {
                 mQuakeLib = new QuakeLib(pgm, pgmLength);
                 if(! mQuakeLib.init()) {
diff --git a/samples/quake/src/com/android/quake/llvm/QuakeLib.java b/samples/quake/src/com/android/quake/llvm/QuakeLib.java
index 54bf7b0..664acf6 100644
--- a/samples/quake/src/com/android/quake/llvm/QuakeLib.java
+++ b/samples/quake/src/com/android/quake/llvm/QuakeLib.java
@@ -136,11 +136,13 @@
      static {
          System.loadLibrary("quake");
      }
-
+   
      public QuakeLib(byte[] pgm, int pgmLength) {
-         compile_bc(pgm, pgmLength);
+	 if (gdk())
+            compile_bc(pgm, pgmLength);
      }
 
+     public static native boolean gdk();
      private native boolean compile_bc(byte[] pgm, int pgmLength);
 
      public native boolean init();