Description: Add portable scalar fallback for non-x86 architectures
 The source unconditionally includes the x86-only SSE2/SSE3 headers
 (emmintrin.h/pmmintrin.h) and uses _mm_* intrinsics. These do not exist on
 arm64, so compilation fails with "emmintrin.h: No such file or directory".
 Guard the x86 SIMD code with __x86_64__/__i386__ and provide equivalent
 portable C++ scalar implementations (scalar SAD, separable convolutions and
 bilateral filter) plus POSIX-backed _mm_malloc/_mm_free replacements for
 other architectures.
Author: tangxiaofeng <tangxiaofeng@kylinos.cn>
Forwarded: not-needed

---

--- libelas-20150630.orig/src/descriptor.cpp
+++ libelas-20150630/src/descriptor.cpp
@@ -21,7 +21,11 @@ Street, Fifth Floor, Boston, MA 02110-13
 
 #include "descriptor.h"
 #include "filter.h"
+#if defined(__x86_64__) || defined(__i386__)
 #include <emmintrin.h>
+#else
+#include "mm_malloc.h"
+#endif
 
 using namespace std;
 using namespace Elas;
--- libelas-20150630.orig/src/elas.cpp
+++ libelas-20150630/src/elas.cpp
@@ -27,6 +27,23 @@ Street, Fifth Floor, Boston, MA 02110-13
 #include "triangle.h"
 #include "matrix.h"
 
+#if !defined(__x86_64__) && !defined(__i386__)
+#include "mm_malloc.h"
+
+namespace {
+// Scalar equivalent of _mm_sad_epu8 over a full 16-byte descriptor chunk:
+// returns the sum of absolute differences of all 16 lanes.
+static inline int32_t sad16(const uint8_t* a, const uint8_t* b) {
+  int32_t s = 0;
+  for (int32_t i = 0; i < 16; i++) {
+    int32_t d = (int32_t)a[i] - (int32_t)b[i];
+    s += (d < 0) ? -d : d;
+  }
+  return s;
+}
+} // namespace
+#endif
+
 using namespace std;
 
 void Elas::Elas::process (uint8_t* I1_,uint8_t* I2_,float* D1,float* D2,const int32_t* dims){
@@ -277,7 +294,9 @@ inline int16_t Elas::Elas::computeMatchi
   int32_t desc_offset_3 = -16*u_step+16*width*v_step;
   int32_t desc_offset_4 = +16*u_step+16*width*v_step;
   
+#if defined(__x86_64__) || defined(__i386__)
   __m128i xmm1,xmm2,xmm3,xmm4,xmm5,xmm6;
+#endif
 
   // check if we are inside the image region
   if (u>=window_size+u_step && u<=width-window_size-1-u_step && v>=window_size+v_step && v<=height-window_size-1-v_step) {
@@ -305,10 +324,17 @@ inline int16_t Elas::Elas::computeMatchi
       return -1;
     
     // load first blocks to xmm registers
+#if defined(__x86_64__) || defined(__i386__)
     xmm1 = _mm_load_si128((__m128i*)(I1_block_addr+desc_offset_1));
     xmm2 = _mm_load_si128((__m128i*)(I1_block_addr+desc_offset_2));
     xmm3 = _mm_load_si128((__m128i*)(I1_block_addr+desc_offset_3));
     xmm4 = _mm_load_si128((__m128i*)(I1_block_addr+desc_offset_4));
+#else
+    const uint8_t* b1 = I1_block_addr+desc_offset_1;
+    const uint8_t* b2 = I1_block_addr+desc_offset_2;
+    const uint8_t* b3 = I1_block_addr+desc_offset_3;
+    const uint8_t* b4 = I1_block_addr+desc_offset_4;
+#endif
     
     // declare match energy for each disparity
     int32_t u_warp;
@@ -340,6 +366,7 @@ inline int16_t Elas::Elas::computeMatchi
       I2_block_addr = I2_line_addr+16*u_warp;
 
       // compute match energy at this disparity
+#if defined(__x86_64__) || defined(__i386__)
       xmm6 = _mm_load_si128((__m128i*)(I2_block_addr+desc_offset_1));
       xmm6 = _mm_sad_epu8(xmm1,xmm6);
       xmm5 = _mm_load_si128((__m128i*)(I2_block_addr+desc_offset_2));
@@ -349,6 +376,12 @@ inline int16_t Elas::Elas::computeMatchi
       xmm5 = _mm_load_si128((__m128i*)(I2_block_addr+desc_offset_4));
       xmm6 = _mm_add_epi16(_mm_sad_epu8(xmm4,xmm5),xmm6);
       sum  = _mm_extract_epi16(xmm6,0)+_mm_extract_epi16(xmm6,4);
+#else
+      sum  = sad16(b1,I2_block_addr+desc_offset_1)
+           + sad16(b2,I2_block_addr+desc_offset_2)
+           + sad16(b3,I2_block_addr+desc_offset_3)
+           + sad16(b4,I2_block_addr+desc_offset_4);
+#endif
 
       // best + second best match
       if (sum<min_1_E) {
@@ -658,6 +691,7 @@ void Elas::Elas::createGrid(vector<suppo
   free(temp2);
 }
 
+#if defined(__x86_64__) || defined(__i386__)
 inline void Elas::Elas::updatePosteriorMinimum(__m128i* I2_block_addr,const int32_t &d,const int32_t &w,
                                          const __m128i &xmm1,__m128i &xmm2,int32_t &val,int32_t &min_val,int32_t &min_d) {
   xmm2 = _mm_load_si128(I2_block_addr);
@@ -679,6 +713,25 @@ inline void Elas::Elas::updatePosteriorM
     min_d   = d;
   }
 }
+#else
+inline void Elas::Elas::updatePosteriorMinimum(const uint8_t* I2_block_addr,const int32_t &d,const int32_t &w,
+                                         const uint8_t* I1_block_addr,int32_t &val,int32_t &min_val,int32_t &min_d) {
+  val = sad16(I1_block_addr,I2_block_addr)+w;
+  if (val<min_val) {
+    min_val = val;
+    min_d   = d;
+  }
+}
+
+inline void Elas::Elas::updatePosteriorMinimum(const uint8_t* I2_block_addr,const int32_t &d,
+                                         const uint8_t* I1_block_addr,int32_t &val,int32_t &min_val,int32_t &min_d) {
+  val = sad16(I1_block_addr,I2_block_addr);
+  if (val<min_val) {
+    min_val = val;
+    min_d   = d;
+  }
+}
+#endif
 
 inline void Elas::Elas::findMatch(int32_t &u,int32_t &v,float &plane_a,float &plane_b,float &plane_c,
                             int32_t* disparity_grid,int32_t *grid_dims,uint8_t* I1_desc,uint8_t* I2_desc,
@@ -734,8 +787,10 @@ inline void Elas::Elas::findMatch(int32_
   int32_t d_curr, u_warp, val;
   int32_t min_val = 10000;
   int32_t min_d   = -1;
+#if defined(__x86_64__) || defined(__i386__)
   __m128i xmm1    = _mm_load_si128((__m128i*)I1_block_addr);
   __m128i xmm2;
+#endif
 
   // left image
   if (!right_image) { 
@@ -745,14 +800,22 @@ inline void Elas::Elas::findMatch(int32_
         u_warp = u-d_curr;
         if (u_warp<window_size || u_warp>=width-window_size)
           continue;
+#if defined(__x86_64__) || defined(__i386__)
         updatePosteriorMinimum((__m128i*)(I2_line_addr+16*u_warp),d_curr,xmm1,xmm2,val,min_val,min_d);
+#else
+        updatePosteriorMinimum(I2_line_addr+16*u_warp,d_curr,I1_block_addr,val,min_val,min_d);
+#endif
       }
     }
     for (d_curr=d_plane_min; d_curr<=d_plane_max; d_curr++) {
       u_warp = u-d_curr;
       if (u_warp<window_size || u_warp>=width-window_size)
         continue;
+#if defined(__x86_64__) || defined(__i386__)
       updatePosteriorMinimum((__m128i*)(I2_line_addr+16*u_warp),d_curr,valid?*(P+abs(d_curr-d_plane)):0,xmm1,xmm2,val,min_val,min_d);
+#else
+      updatePosteriorMinimum(I2_line_addr+16*u_warp,d_curr,valid?*(P+abs(d_curr-d_plane)):0,I1_block_addr,val,min_val,min_d);
+#endif
     }
     
   // right image
@@ -763,14 +826,22 @@ inline void Elas::Elas::findMatch(int32_
         u_warp = u+d_curr;
         if (u_warp<window_size || u_warp>=width-window_size)
           continue;
+#if defined(__x86_64__) || defined(__i386__)
         updatePosteriorMinimum((__m128i*)(I2_line_addr+16*u_warp),d_curr,xmm1,xmm2,val,min_val,min_d);
+#else
+        updatePosteriorMinimum(I2_line_addr+16*u_warp,d_curr,I1_block_addr,val,min_val,min_d);
+#endif
       }
     }
     for (d_curr=d_plane_min; d_curr<=d_plane_max; d_curr++) {
       u_warp = u+d_curr;
       if (u_warp<window_size || u_warp>=width-window_size)
         continue;
+#if defined(__x86_64__) || defined(__i386__)
       updatePosteriorMinimum((__m128i*)(I2_line_addr+16*u_warp),d_curr,valid?*(P+abs(d_curr-d_plane)):0,xmm1,xmm2,val,min_val,min_d);
+#else
+      updatePosteriorMinimum(I2_line_addr+16*u_warp,d_curr,valid?*(P+abs(d_curr-d_plane)):0,I1_block_addr,val,min_val,min_d);
+#endif
     }
   }
 
@@ -1284,6 +1355,7 @@ void Elas::Elas::gapInterpolation(float*
 }
 
 // implements approximation to bilateral filtering
+#if defined(__x86_64__) || defined(__i386__)
 void Elas::Elas::adaptiveMean (float* D) {
   
   // get disparity image dimensions
@@ -1491,6 +1563,124 @@ void Elas::Elas::adaptiveMean (float* D)
   free(D_tmp);
 }
 
+#else
+void Elas::Elas::adaptiveMean (float* D) {
+  
+  // get disparity image dimensions
+  int32_t D_width          = width;
+  int32_t D_height         = height;
+  if (param.subsampling) {
+    D_width          = width/2;
+    D_height         = height/2;
+  }
+  
+  // allocate temporary memory
+  float* D_copy = (float*)malloc(D_width*D_height*sizeof(float));
+  float* D_tmp  = (float*)malloc(D_width*D_height*sizeof(float));
+  memcpy(D_copy,D,D_width*D_height*sizeof(float));
+  
+  // zero input disparity maps to -10 (this makes the bilateral
+  // weights of all valid disparities to 0 in this region)
+  for (int32_t i=0; i<D_width*D_height; i++) {
+    if (*(D+i)<0) {
+      *(D_copy+i) = -10;
+      *(D_tmp+i)  = -10;
+    }
+  }
+
+  if (param.subsampling) {
+    // 4 pixel bilateral filter width
+
+    // horizontal filter
+    for (int32_t v=3; v<D_height-3; v++) {
+      for (int32_t u=3; u<D_width; u++) {
+        float val_curr   = *(D_copy+v*D_width+(u-1));
+        float weight_sum = 0.0f;
+        float factor_sum = 0.0f;
+        for (int32_t k=0; k<4; k++) {
+          float xval = *(D_copy+v*D_width+(u-3+k));
+          float w    = 4.0f - fabsf(xval-val_curr);
+          if (w<0.0f) w = 0.0f;
+          weight_sum += w;
+          factor_sum += xval*w;
+        }
+        if (weight_sum>0) {
+          float d = factor_sum/weight_sum;
+          if (d>=0) *(D_tmp+v*D_width+(u-1)) = d;
+        }
+      }
+    }
+
+    // vertical filter
+    for (int32_t u=3; u<D_width-3; u++) {
+      for (int32_t v=3; v<D_height; v++) {
+        float val_curr   = *(D_tmp+(v-1)*D_width+u);
+        float weight_sum = 0.0f;
+        float factor_sum = 0.0f;
+        for (int32_t k=0; k<4; k++) {
+          float xval = *(D_tmp+(v-3+k)*D_width+u);
+          float w    = 4.0f - fabsf(xval-val_curr);
+          if (w<0.0f) w = 0.0f;
+          weight_sum += w;
+          factor_sum += xval*w;
+        }
+        if (weight_sum>0) {
+          float d = factor_sum/weight_sum;
+          if (d>=0) *(D+(v-1)*D_width+u) = d;
+        }
+      }
+    }
+    
+  } else {
+    // full resolution: 8 pixel bilateral filter width
+
+    // horizontal filter
+    for (int32_t v=3; v<D_height-3; v++) {
+      for (int32_t u=7; u<D_width; u++) {
+        float val_curr   = *(D_copy+v*D_width+(u-3));
+        float weight_sum = 0.0f;
+        float factor_sum = 0.0f;
+        for (int32_t k=0; k<8; k++) {
+          float xval = *(D_copy+v*D_width+(u-7+k));
+          float w    = 4.0f - fabsf(xval-val_curr);
+          if (w<0.0f) w = 0.0f;
+          weight_sum += w;
+          factor_sum += xval*w;
+        }
+        if (weight_sum>0) {
+          float d = factor_sum/weight_sum;
+          if (d>=0) *(D_tmp+v*D_width+(u-3)) = d;
+        }
+      }
+    }
+  
+    // vertical filter
+    for (int32_t u=3; u<D_width-3; u++) {
+      for (int32_t v=7; v<D_height; v++) {
+        float val_curr   = *(D_tmp+(v-3)*D_width+u);
+        float weight_sum = 0.0f;
+        float factor_sum = 0.0f;
+        for (int32_t k=0; k<8; k++) {
+          float xval = *(D_tmp+(v-7+k)*D_width+u);
+          float w    = 4.0f - fabsf(xval-val_curr);
+          if (w<0.0f) w = 0.0f;
+          weight_sum += w;
+          factor_sum += xval*w;
+        }
+        if (weight_sum>0) {
+          float d = factor_sum/weight_sum;
+          if (d>=0) *(D+(v-3)*D_width+u) = d;
+        }
+      }
+    }
+  }
+  
+  // free memory
+  free(D_copy);
+  free(D_tmp);
+}
+#endif
+
 void Elas::Elas::median (float* D) {
   
   // get disparity image dimensions
--- libelas-20150630.orig/src/elas.h
+++ libelas-20150630/src/elas.h
@@ -29,7 +29,9 @@ Street, Fifth Floor, Boston, MA 02110-13
 #include <string.h>
 #include <stdlib.h>
 #include <vector>
+#if defined(__x86_64__) || defined(__i386__)
 #include <emmintrin.h>
+#endif
 
 // define fixed-width datatypes for Visual Studio projects
 #ifndef _MSC_VER
@@ -200,10 +202,17 @@ private:
   void createGrid (std::vector<support_pt> p_support,int32_t* disparity_grid,int32_t* grid_dims,bool right_image);
 
   // matching
+#if defined(__x86_64__) || defined(__i386__)
   inline void updatePosteriorMinimum (__m128i* I2_block_addr,const int32_t &d,const int32_t &w,
                                       const __m128i &xmm1,__m128i &xmm2,int32_t &val,int32_t &min_val,int32_t &min_d);
   inline void updatePosteriorMinimum (__m128i* I2_block_addr,const int32_t &d,
                                       const __m128i &xmm1,__m128i &xmm2,int32_t &val,int32_t &min_val,int32_t &min_d);
+#else
+  inline void updatePosteriorMinimum (const uint8_t* I2_block_addr,const int32_t &d,const int32_t &w,
+                                      const uint8_t* I1_block_addr,int32_t &val,int32_t &min_val,int32_t &min_d);
+  inline void updatePosteriorMinimum (const uint8_t* I2_block_addr,const int32_t &d,
+                                      const uint8_t* I1_block_addr,int32_t &val,int32_t &min_val,int32_t &min_d);
+#endif
   inline void findMatch (int32_t &u,int32_t &v,float &plane_a,float &plane_b,float &plane_c,
                          int32_t* disparity_grid,int32_t *grid_dims,uint8_t* I1_desc,uint8_t* I2_desc,
                          int32_t *P,int32_t &plane_radius,bool &valid,bool &right_image,float* D);
--- libelas-20150630.orig/src/filter.cpp
+++ libelas-20150630/src/filter.cpp
@@ -41,6 +41,7 @@ Street, Fifth Floor, Boston, MA 02110-13
 
 // fast filters: implements 3x3 and 5x5 sobel filters and 
 //               5x5 blob and corner filters based on SSE2/3 instructions
+#if defined(__x86_64__) || defined(__i386__)
 namespace Elas::filter {
   
   // private namespace, public user functions at the bottom of this file
@@ -466,3 +467,195 @@ namespace Elas::filter {
     _mm_free( integral );
   }
 };
+
+#else // !(x86): portable scalar fallback for architectures without SSE2/3
+
+#include <stdlib.h>
+
+namespace Elas::filter {
+
+  static inline uint8_t clamp_u8(int32_t v) {
+    if (v < 0)   return 0;
+    if (v > 255) return 255;
+    return (uint8_t)v;
+  }
+
+  namespace detail {
+    void integral_image( const uint8_t* in, int32_t* out, int w, int h ) {
+      int32_t* out_top = out;
+      const uint8_t* line_end = in + w;
+      const uint8_t* in_end   = in + w*h;
+      int32_t line_sum = 0;
+      for( ; in != line_end; in++, out++ ) {
+        line_sum += *in;
+        *out = line_sum;
+      }
+      for( ; in != in_end; ) {
+        int32_t line_sum = 0;
+        const uint8_t* line_end = in + w;
+        for( ; in != line_end; in++, out++, out_top++ ) {
+          line_sum += *in;
+          *out = *out_top + line_sum;
+        }
+      }
+    }
+  }
+
+  void sobel3x3( const uint8_t* in, uint8_t* out_v, uint8_t* out_h, int w, int h ) {
+    int16_t* temp_h = (int16_t*)calloc((size_t)w * (size_t)h, sizeof(int16_t));
+    int16_t* temp_v = (int16_t*)calloc((size_t)w * (size_t)h, sizeof(int16_t));
+    if (!temp_h || !temp_v) {
+      free(temp_h);
+      free(temp_v);
+      return;
+    }
+
+    memset(out_v, 0, (size_t)w * (size_t)h);
+    memset(out_h, 0, (size_t)w * (size_t)h);
+
+    // vertical pass: [1,2,1] smoothing into temp_v, [1,0,-1] derivative into temp_h
+    for (int v = 1; v < h - 1; v++) {
+      const uint8_t* r0 = in + (v - 1) * w;
+      const uint8_t* r1 = in +  v      * w;
+      const uint8_t* r2 = in + (v + 1) * w;
+      int16_t* tv = temp_v + v * w;
+      int16_t* th = temp_h + v * w;
+      for (int u = 0; u < w; u++) {
+        tv[u] = (int16_t)((int32_t)r0[u] + 2 * (int32_t)r1[u] + (int32_t)r2[u]);
+        th[u] = (int16_t)((int32_t)r0[u] - (int32_t)r2[u]);
+      }
+    }
+
+    // horizontal passes: [1,0,-1] on temp_v, [1,2,1] on temp_h
+    for (int v = 0; v < h; v++) {
+      const int16_t* tv = temp_v + v * w;
+      const int16_t* th = temp_h + v * w;
+      uint8_t* ov = out_v + v * w;
+      uint8_t* oh = out_h + v * w;
+      for (int u = 0; u < w - 2; u++) {
+        int32_t vv = (((int32_t)tv[u] - (int32_t)tv[u + 2]) >> 2) + 128;
+        int32_t hh = (((int32_t)th[u] + 2 * (int32_t)th[u + 1] + (int32_t)th[u + 2]) >> 2) + 128;
+        ov[u + 1] = clamp_u8(vv);
+        oh[u + 1] = clamp_u8(hh);
+      }
+    }
+
+    free(temp_h);
+    free(temp_v);
+  }
+
+  void sobel5x5( const uint8_t* in, uint8_t* out_v, uint8_t* out_h, int w, int h ) {
+    int16_t* temp_h = (int16_t*)calloc((size_t)w * (size_t)h, sizeof(int16_t));
+    int16_t* temp_v = (int16_t*)calloc((size_t)w * (size_t)h, sizeof(int16_t));
+    if (!temp_h || !temp_v) {
+      free(temp_h);
+      free(temp_v);
+      return;
+    }
+
+    memset(out_v, 0, (size_t)w * (size_t)h);
+    memset(out_h, 0, (size_t)w * (size_t)h);
+
+    // vertical pass: [1,4,6,4,1] smoothing into temp_v, [1,2,0,-2,-1] derivative into temp_h
+    for (int v = 2; v < h - 2; v++) {
+      const uint8_t* r0 = in + (v - 2) * w;
+      const uint8_t* r1 = in + (v - 1) * w;
+      const uint8_t* r2 = in +  v      * w;
+      const uint8_t* r3 = in + (v + 1) * w;
+      const uint8_t* r4 = in + (v + 2) * w;
+      int16_t* tv = temp_v + v * w;
+      int16_t* th = temp_h + v * w;
+      for (int u = 0; u < w; u++) {
+        tv[u] = (int16_t)((int32_t)r0[u] + 4 * (int32_t)r1[u] + 6 * (int32_t)r2[u] + 4 * (int32_t)r3[u] + (int32_t)r4[u]);
+        th[u] = (int16_t)((int32_t)r0[u] + 2 * (int32_t)r1[u] - 2 * (int32_t)r3[u] - (int32_t)r4[u]);
+      }
+    }
+
+    // horizontal passes: [1,2,0,-2,-1] on temp_v, [1,4,6,4,1] on temp_h
+    for (int v = 0; v < h; v++) {
+      const int16_t* tv = temp_v + v * w;
+      const int16_t* th = temp_h + v * w;
+      uint8_t* ov = out_v + v * w;
+      uint8_t* oh = out_h + v * w;
+      for (int u = 0; u < w - 4; u++) {
+        int32_t vv = (((int32_t)tv[u] + 2 * (int32_t)tv[u + 1] - 2 * (int32_t)tv[u + 3] - (int32_t)tv[u + 4]) >> 7) + 128;
+        int32_t hh = (((int32_t)th[u] + 4 * (int32_t)th[u + 1] + 6 * (int32_t)th[u + 2] + 4 * (int32_t)th[u + 3] + (int32_t)th[u + 4]) >> 7) + 128;
+        ov[u + 2] = clamp_u8(vv);
+        oh[u + 2] = clamp_u8(hh);
+      }
+    }
+
+    free(temp_h);
+    free(temp_v);
+  }
+
+  // -1 -1  0  1  1
+  // -1 -1  0  1  1
+  //  0  0  0  0  0
+  //  1  1  0 -1 -1
+  //  1  1  0 -1 -1
+  void checkerboard5x5( const uint8_t* in, int16_t* out, int w, int h ) {
+    int16_t* temp = (int16_t*)calloc((size_t)w * (size_t)h, sizeof(int16_t));
+    if (!temp)
+      return;
+
+    memset(out, 0, (size_t)w * (size_t)h * sizeof(int16_t));
+
+    // vertical pass: [1,1,0,-1,-1]
+    for (int v = 2; v < h - 2; v++) {
+      const uint8_t* r0 = in + (v - 2) * w;
+      const uint8_t* r1 = in + (v - 1) * w;
+      const uint8_t* r3 = in + (v + 1) * w;
+      const uint8_t* r4 = in + (v + 2) * w;
+      int16_t* t = temp + v * w;
+      for (int u = 0; u < w; u++)
+        t[u] = (int16_t)((int32_t)r0[u] + (int32_t)r1[u] - (int32_t)r3[u] - (int32_t)r4[u]);
+    }
+
+    // horizontal pass: [1,1,0,-1,-1]
+    for (int v = 0; v < h; v++) {
+      const int16_t* t = temp + v * w;
+      int16_t* o = out + v * w;
+      for (int u = 0; u < w - 4; u++)
+        o[u + 2] = (int16_t)((int32_t)t[u] + (int32_t)t[u + 1] - (int32_t)t[u + 3] - (int32_t)t[u + 4]);
+    }
+
+    free(temp);
+  }
+
+  // -1 -1 -1 -1 -1
+  // -1  1  1  1 -1
+  // -1  1  8  1 -1
+  // -1  1  1  1 -1
+  // -1 -1 -1 -1 -1
+  void blob5x5( const uint8_t* in, int16_t* out, int w, int h ) {
+    int32_t* integral = (int32_t*)malloc((size_t)w * (size_t)h * sizeof(int32_t));
+    if (!integral)
+      return;
+
+    memset(out, 0, (size_t)w * (size_t)h * sizeof(int16_t));
+
+    detail::integral_image( in, integral, w, h );
+    int16_t* out_ptr   = out + 3 + 3*w;
+    int16_t* out_end   = out + w * h - 2 - 2*w;
+    const int32_t* i00 = integral;
+    const int32_t* i50 = integral + 5;
+    const int32_t* i05 = integral + 5*w;
+    const int32_t* i55 = integral + 5 + 5*w;
+    const int32_t* i11 = integral + 1 + 1*w;
+    const int32_t* i41 = integral + 4 + 1*w;
+    const int32_t* i14 = integral + 1 + 4*w;
+    const int32_t* i44 = integral + 4 + 4*w;
+    const uint8_t* im22 = in + 3 + 3*w;
+    for( ; out_ptr != out_end; out_ptr++, i00++, i50++, i05++, i55++, i11++, i41++, i14++, i44++, im22++ ) {
+      int32_t result = 0;
+      result = -( *i55 - *i50 - *i05 + *i00 );
+      result += 2*( *i44 - *i41 - *i14 + *i11 );
+      result += 7* *im22;
+      *out_ptr = (int16_t)result;
+    }
+    free( integral );
+  }
+};
+
+#endif
--- libelas-20150630.orig/src/filter.h
+++ libelas-20150630/src/filter.h
@@ -22,8 +22,10 @@ Street, Fifth Floor, Boston, MA 02110-13
 #ifndef __FILTER_H__
 #define __FILTER_H__
 
-#include <emmintrin.h>
-#include <pmmintrin.h>
+#if defined(__x86_64__) || defined(__i386__)
+  #include <emmintrin.h>
+  #include <pmmintrin.h>
+#endif
 
 // define fixed-width datatypes for Visual Studio projects
 #ifndef _MSC_VER
@@ -43,6 +45,7 @@ Street, Fifth Floor, Boston, MA 02110-13
 //               5x5 blob and corner filters based on SSE2/3 instructions
 namespace Elas::filter {
   
+#if defined(__x86_64__) || defined(__i386__)
   // private namespace, public user functions at the bottom of this file
   namespace detail {
     void integral_image( const uint8_t* in, int32_t* out, int w, int h );
@@ -76,6 +79,7 @@ namespace Elas::filter {
     
     void convolve_cols_3x3( const unsigned char* in, int16_t* out_v, int16_t* out_h, int w, int h );
   }
+#endif
   
   void sobel3x3( const uint8_t* in, uint8_t* out_v, uint8_t* out_h, int w, int h );
   
--- /dev/null
+++ libelas-20150630/src/mm_malloc.h
@@ -0,0 +1,33 @@
+/*
+ * Portable replacement for the x86-only _mm_malloc/_mm_free
+ * 16-byte aligned allocation helpers.
+ *
+ * On x86 these symbols are provided by <emmintrin.h>/<mm_malloc.h>;
+ * on other architectures (e.g. arm64/loong64/riscv64) that header is not
+ * available, so define minimal POSIX-backed equivalents with the same
+ * semantics.  Only the allocation helpers are provided here; the SIMD
+ * intrinsics themselves are handled by architecture-specific code paths.
+ */
+
+#ifndef LIBELAS_MM_MALLOC_H
+#define LIBELAS_MM_MALLOC_H
+
+#if !defined(__x86_64__) && !defined(__i386__)
+
+#include <stddef.h>
+#include <stdlib.h>
+
+static inline void* _mm_malloc(size_t size, size_t align) {
+  void* p = 0;
+  if (posix_memalign(&p, align, size) != 0)
+    return 0;
+  return p;
+}
+
+static inline void _mm_free(void* p) {
+  free(p);
+}
+
+#endif /* !x86 */
+
+#endif /* LIBELAS_MM_MALLOC_H */
\ No newline at end of file
