From ccfdc8cd62590a320089788448db85b39622d65c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zolt=C3=A1n=20Mizsei?= <zmizsei@extrowerk.com>
Date: Sat, 24 Aug 2019 09:32:55 +0200
Subject: Applied patches


diff --git a/common/math/math.h b/common/math/math.h
index 8403f4e..aa5d8e7 100644
--- a/common/math/math.h
+++ b/common/math/math.h
@@ -166,7 +166,10 @@ namespace embree
   __forceinline  int64_t min(int64_t  a, int64_t  b) { return a<b ? a:b; }
   __forceinline    float min(float    a, float    b) { return a<b ? a:b; }
   __forceinline   double min(double   a, double   b) { return a<b ? a:b; }
-#if defined(__X86_64__)
+#if !defined(__X86_64__) && defined (__HAIKU__)
+  __forceinline  ssize_t min(ssize_t  a, ssize_t  b) { return a<b ? a:b; }
+#endif
+#if defined(__X86_64__) || defined (__HAIKU__)
   __forceinline   size_t min(size_t   a, size_t   b) { return a<b ? a:b; }
 #endif
 
@@ -183,7 +186,10 @@ namespace embree
   __forceinline  int64_t max(int64_t  a, int64_t  b) { return a<b ? b:a; }
   __forceinline    float max(float    a, float    b) { return a<b ? b:a; }
   __forceinline   double max(double   a, double   b) { return a<b ? b:a; }
-#if defined(__X86_64__)
+#if !defined(__X86_64__) && defined (__HAIKU__)
+  __forceinline  ssize_t max(ssize_t  a, ssize_t  b) { return a<b ? b:a; }
+#endif
+#if defined(__X86_64__) || defined (__HAIKU__)
   __forceinline   size_t max(size_t   a, size_t   b) { return a<b ? b:a; }
 #endif
 
diff --git a/common/sys/alloc.cpp b/common/sys/alloc.cpp
index 1bc30fe..930f7d9 100644
--- a/common/sys/alloc.cpp
+++ b/common/sys/alloc.cpp
@@ -240,6 +240,7 @@ namespace embree
       return nullptr;
     }
 
+#ifndef __HAIKU__
     /* try direct huge page allocation first */
     if (isHugePageCandidate(bytes)) 
     {
@@ -257,6 +258,7 @@ namespace embree
       }
 #endif
     } 
+#endif
 
     /* fallback to 4k pages */
     void* ptr = (char*) mmap(0, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
diff --git a/common/sys/array.h b/common/sys/array.h
index 3da2a14..13565ea 100644
--- a/common/sys/array.h
+++ b/common/sys/array.h
@@ -139,7 +139,7 @@ namespace embree
     __forceinline       Ty& operator[](const unsigned i)       { assert(i<N); return data[i]; }
     __forceinline const Ty& operator[](const unsigned i) const { assert(i<N); return data[i]; }
 
-#if defined(__X86_64__)
+#if defined(__X86_64__) || defined(__HAIKU__)
     __forceinline       Ty& operator[](const size_t i)       { assert(i<N); return data[i]; }
     __forceinline const Ty& operator[](const size_t i) const { assert(i<N); return data[i]; }
 #endif
@@ -196,7 +196,7 @@ namespace embree
     __forceinline       Ty& operator[](const int i)      { assert(i>=0 && i<max_total_elements); resize(i+1); return data[i]; }
     __forceinline       Ty& operator[](const unsigned i) { assert(i<max_total_elements); resize(i+1); return data[i]; }
 
-#if defined(__X86_64__)
+#if defined(__X86_64__) || defined(__HAIKU__)
     __forceinline       Ty& operator[](const size_t i) { assert(i<max_total_elements); resize(i+1); return data[i]; }
 #endif
 
diff --git a/common/sys/intrinsics.h b/common/sys/intrinsics.h
index ce2983f..4acff1a 100644
--- a/common/sys/intrinsics.h
+++ b/common/sys/intrinsics.h
@@ -74,7 +74,7 @@ namespace embree
 #endif
   }
   
-#if defined(__X86_64__)
+#if defined(__X86_64__) || defined (__HAIKU__)
   __forceinline size_t bsf(size_t v) {
 #if defined(__AVX2__) 
     return _tzcnt_u64(v);
@@ -98,7 +98,7 @@ namespace embree
     return i;
   }
   
-#if defined(__X86_64__)
+#if defined(__X86_64__) || defined (__HAIKU__)
   __forceinline size_t bscf(size_t& v) 
   {
     size_t i = bsf(v);
@@ -394,6 +394,12 @@ namespace embree
     return _mm_popcnt_u32(in);
   }
   
+#if !defined(__X86_64__) && defined(__HAIKU__)
+  __forceinline size_t __popcnt(size_t in) {
+    return _mm_popcnt_u32(in);
+  }
+#endif
+  
 #if defined(__X86_64__)
   __forceinline size_t popcnt(size_t in) {
     return _mm_popcnt_u64(in);
diff --git a/common/sys/platform.h b/common/sys/platform.h
index fe52eb1..33aa1a2 100644
--- a/common/sys/platform.h
+++ b/common/sys/platform.h
@@ -73,7 +73,7 @@
 #endif
 
 /* try to detect other Unix systems */
-#if defined(__unix__) || defined (unix) || defined(__unix) || defined(_unix)
+#if defined(__unix__) || defined (unix) || defined(__unix) || defined(_unix) || defined(__HAIKU__)
 #  if !defined(__UNIX__)
 #     define __UNIX__
 #  endif
diff --git a/kernels/builders/bvh_builder_sah.h b/kernels/builders/bvh_builder_sah.h
index fff4bf2..137729b 100644
--- a/kernels/builders/bvh_builder_sah.h
+++ b/kernels/builders/bvh_builder_sah.h
@@ -43,7 +43,7 @@ namespace embree
         {
           if (RTC_BUILD_ARGUMENTS_HAS(settings,maxBranchingFactor)) branchingFactor = settings.maxBranchingFactor;
           if (RTC_BUILD_ARGUMENTS_HAS(settings,maxDepth          )) maxDepth        = settings.maxDepth;
-          if (RTC_BUILD_ARGUMENTS_HAS(settings,sahBlockSize      )) logBlockSize    = bsr(settings.sahBlockSize);
+          if (RTC_BUILD_ARGUMENTS_HAS(settings,sahBlockSize      )) logBlockSize    = bsr((size_t)settings.sahBlockSize);
           if (RTC_BUILD_ARGUMENTS_HAS(settings,minLeafSize       )) minLeafSize     = settings.minLeafSize;
           if (RTC_BUILD_ARGUMENTS_HAS(settings,maxLeafSize       )) maxLeafSize     = settings.maxLeafSize;
           if (RTC_BUILD_ARGUMENTS_HAS(settings,traversalCost     )) travCost        = settings.traversalCost;
-- 
2.30.2


From c4a0e9f41d9f5b98319eda8e498443c2b88287c6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zolt=C3=A1n=20Mizsei?= <zmizsei@extrowerk.com>
Date: Tue, 5 Jan 2021 20:45:01 +0100
Subject: CPU count


diff --git a/common/sys/sysinfo.cpp b/common/sys/sysinfo.cpp
index ef9cd77..ae2e632 100644
--- a/common/sys/sysinfo.cpp
+++ b/common/sys/sysinfo.cpp
@@ -10,6 +10,9 @@
 #include <pthread_np.h>
 typedef cpuset_t cpu_set_t;
 #endif
+#if defined(__HAIKU__)
+#include <kernel/OS.h>
+#endif
 
 ////////////////////////////////////////////////////////////////////////////////
 /// All Platforms
@@ -45,6 +48,10 @@ namespace embree
     return "Unix (32bit)";
 #elif defined(__UNIX__) && defined(__X86_64__)
     return "Unix (64bit)";
+#elif defined(__HAIKU__) && !defined(__X86_64__)
+    return "Haiku (32bit)";
+#elif defined(__HAIKU__) && defined(__X86_64__)
+    return "Haiku (64bit)";
 #else
     return "Unknown";
 #endif
@@ -478,7 +485,7 @@ namespace embree
 /// Linux Platform
 ////////////////////////////////////////////////////////////////////////////////
 
-#if defined(__LINUX__)
+#if defined(__LINUX__) || defined(__HAIKU__)
 
 #include <stdio.h>
 #include <unistd.h>
@@ -597,6 +604,10 @@ namespace embree
 #if defined(__MACOSX__)
     nThreads = sysconf(_SC_NPROCESSORS_ONLN); // does not work in Linux LXC container
     assert(nThreads);
+#elif defined(__HAIKU__)
+    system_info info;
+    get_system_info( &info );
+    nThreads = info.cpu_count;
 #else
     cpu_set_t set;
     if (pthread_getaffinity_np(pthread_self(), sizeof(set), &set) == 0)
-- 
2.30.2


From e3f86c3fb531e757357505c0da1f0144df18c5c9 Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Wed, 12 Jan 2022 16:40:24 +1000
Subject: Use workaround for Haiku


diff --git a/common/simd/vint4_sse2.h b/common/simd/vint4_sse2.h
index 9814d5c..2a50cca 100644
--- a/common/simd/vint4_sse2.h
+++ b/common/simd/vint4_sse2.h
@@ -452,7 +452,7 @@ namespace embree
   __forceinline int toScalar(const vint4& v) { return _mm_cvtsi128_si32(v); }
 
   __forceinline size_t toSizeT(const vint4& v) { 
-#if defined(__WIN32__) && !defined(__X86_64__) // win32 workaround
+#if (defined(__WIN32__) || defined(__HAIKU__)) && !defined(__X86_64__) // win32 workaround
     return toScalar(v);
 #else
     return _mm_cvtsi128_si64(v); 
-- 
2.30.2

