From e7194f81a3d10bb49c3699742544bb83bf5cfd5b Mon Sep 17 00:00:00 2001
From: "n.savitchev" <n.savichev@gaijin.team>
Date: Thu, 11 Jan 2024 16:22:35 +0300
Subject: [PATCH] update JoltPhysics to compile for e2k (Elbrus2000) arch

applies https://github.com/jrouwe/JoltPhysics/pull/871
---
 prog/3rdPartyLibs/phys/joltPhysics/Jolt/Core/Core.h      | 9 +++++++++
 .../phys/joltPhysics/Jolt/Core/TickCounter.h             | 4 ++++
 prog/3rdPartyLibs/phys/joltPhysics/Jolt/Math/Math.h      | 4 ++++
 prog/3rdPartyLibs/phys/joltPhysics/setup.jam             | 1 +
 4 files changed, 18 insertions(+)

diff --git a/prog/3rdPartyLibs/phys/joltPhysics/Jolt/Core/Core.h b/prog/3rdPartyLibs/phys/joltPhysics/Jolt/Core/Core.h
index 9b8463403..b9e242ddb 100644
--- a/prog/3rdPartyLibs/phys/joltPhysics/Jolt/Core/Core.h
+++ b/prog/3rdPartyLibs/phys/joltPhysics/Jolt/Core/Core.h
@@ -180,6 +180,13 @@
 	#define JPH_VECTOR_ALIGNMENT 16
 	#define JPH_DVECTOR_ALIGNMENT 32
 	#define JPH_DISABLE_CUSTOM_ALLOCATOR
+#elif defined(__e2k__)
+	// Elbrus e2k architecture
+	#define JPH_CPU_E2K
+	#define JPH_CPU_ADDRESS_BITS 64
+	#define JPH_USE_SSE
+	#define JPH_VECTOR_ALIGNMENT 16
+	#define JPH_DVECTOR_ALIGNMENT 32
 #else
 	#error Unsupported CPU architecture
 #endif
@@ -334,6 +341,8 @@
 		#define JPH_BREAKPOINT	__asm volatile ("int $0x3")
 	#elif defined(JPH_CPU_ARM)
 		#define JPH_BREAKPOINT	__builtin_trap()
+	#elif defined(JPH_CPU_E2K)
+		#define JPH_BREAKPOINT	__builtin_trap()
 	#endif
 #elif defined(JPH_PLATFORM_WASM)
 	#define JPH_BREAKPOINT		do { } while (false) // Not supported
diff --git a/prog/3rdPartyLibs/phys/joltPhysics/Jolt/Core/TickCounter.h b/prog/3rdPartyLibs/phys/joltPhysics/Jolt/Core/TickCounter.h
index 5d42eb40d..2b5410e3d 100644
--- a/prog/3rdPartyLibs/phys/joltPhysics/Jolt/Core/TickCounter.h
+++ b/prog/3rdPartyLibs/phys/joltPhysics/Jolt/Core/TickCounter.h
@@ -9,6 +9,8 @@
 	#include <intrin.h>
 #elif defined(JPH_CPU_X86) && defined(JPH_COMPILER_GCC)
 	#include <x86intrin.h>
+#elif defined(JPH_CPU_E2K)
+	#include <x86intrin.h>
 #endif
 
 JPH_NAMESPACE_BEGIN
@@ -27,6 +29,8 @@ JPH_INLINE uint64 GetProcessorTickCount()
 	return JPH_PLATFORM_BLUE_GET_TICKS();
 #elif defined(JPH_CPU_X86)
 	return __rdtsc();
+#elif defined(JPH_CPU_E2K)
+	return __rdtsc();
 #elif defined(JPH_CPU_ARM) && defined(JPH_USE_NEON)
 	uint64 val;
 	asm volatile("mrs %0, cntvct_el0" : "=r" (val));
diff --git a/prog/3rdPartyLibs/phys/joltPhysics/Jolt/Math/Math.h b/prog/3rdPartyLibs/phys/joltPhysics/Jolt/Math/Math.h
index c37c04b1b..fe787f210 100644
--- a/prog/3rdPartyLibs/phys/joltPhysics/Jolt/Math/Math.h
+++ b/prog/3rdPartyLibs/phys/joltPhysics/Jolt/Math/Math.h
@@ -118,6 +118,8 @@ inline uint CountTrailingZeros(uint32 inValue)
 	#else
 		return __builtin_clz(__builtin_bitreverse32(inValue));
 	#endif
+#elif defined(JPH_CPU_E2K)
+		return inValue ? __builtin_ctz(inValue) : 32;
 #else
 	#error Undefined
 #endif
@@ -146,6 +148,8 @@ inline uint CountLeadingZeros(uint32 inValue)
 	#else
 		return __builtin_clz(inValue);
 	#endif
+#elif defined(JPH_CPU_E2K)
+		return inValue ? __builtin_clz(inValue) : 32;
 #else
 	#error Undefined
 #endif
diff --git a/prog/3rdPartyLibs/phys/joltPhysics/setup.jam b/prog/3rdPartyLibs/phys/joltPhysics/setup.jam
index c69ed8d6e..62d53bbec 100644
--- a/prog/3rdPartyLibs/phys/joltPhysics/setup.jam
+++ b/prog/3rdPartyLibs/phys/joltPhysics/setup.jam
@@ -10,3 +10,4 @@ if ! $(Platform) in iOS tvOS nswitch android && ( $(Platform) != macosx || $(Mac
 if $(Sanitize) = address { CPPopt += -DJPH_DISABLE_TEMP_ALLOCATOR -DJPH_DISABLE_CUSTOM_ALLOCATOR ; }
 if $(Platform) = nswitch { CPPopt += -DJPH_PLATFORM_LINUX ; }
 if $(Platform) in ps4 ps5 { CPPopt += -DJPH_PLATFORM_LINUX ; }
+if $(Platform) = linux64 && $(LinuxArch) = e2k { CPPopt += -w2810 ; }