| 1 |
From: "Michael R. Crusoe" <[email protected]> |
| 2 |
Date: Thu, 27 Feb 2025 10:06:00 +0100 |
| 3 |
Subject: Enable building on non-x86 |
| 4 |
|
| 5 |
Forwarded: https://github.com/bwa-mem2/bwa-mem2/pull/84 |
| 6 |
--- |
| 7 |
src/FMI_search.cpp | 4 ++++ |
| 8 |
src/FMI_search.h | 3 ++- |
| 9 |
src/bandedSWA.cpp | 7 ------- |
| 10 |
src/bandedSWA.h | 9 ++++++++- |
| 11 |
src/bwa.h | 7 +++++++ |
| 12 |
src/bwamem.cpp | 5 ++++- |
| 13 |
src/fastmap.cpp | 10 +++++++++- |
| 14 |
src/ksw.cpp | 1 - |
| 15 |
src/ksw.h | 3 ++- |
| 16 |
src/kswv.h | 3 ++- |
| 17 |
src/main.cpp | 5 +++++ |
| 18 |
src/runsimd.cpp | 2 +- |
| 19 |
src/utils.h | 38 ++++++++++++++++++++++++++++++++++++++ |
| 20 |
13 files changed, 82 insertions(+), 15 deletions(-) |
| 21 |
|
| 22 |
--- bwa-mem2.orig/src/FMI_search.cpp |
| 23 |
+++ bwa-mem2/src/FMI_search.cpp |
| 24 |
@@ -29,10 +29,14 @@ |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include "sais.h" |
| 28 |
+#if !(defined(__GNUC__) && __GNUC__ < 11 && !defined(__clang__)) |
| 29 |
+#include <immintrin.h> // For __rdtsc |
| 30 |
+#endif |
| 31 |
#include "FMI_search.h" |
| 32 |
#include "memcpy_bwamem.h" |
| 33 |
#include "profiling.h" |
| 34 |
|
| 35 |
+ |
| 36 |
#ifdef __cplusplus |
| 37 |
extern "C" { |
| 38 |
#endif |
| 39 |
--- bwa-mem2.orig/src/FMI_search.h |
| 40 |
+++ bwa-mem2/src/FMI_search.h |
| 41 |
@@ -34,7 +34,8 @@ |
| 42 |
#include <stdlib.h> |
| 43 |
#include <stdint.h> |
| 44 |
#include <string.h> |
| 45 |
-#include <immintrin.h> |
| 46 |
+#define SIMDE_ENABLE_NATIVE_ALIASES |
| 47 |
+#include <simde/x86/avx2.h> |
| 48 |
#include <limits.h> |
| 49 |
#include <fstream> |
| 50 |
|
| 51 |
--- bwa-mem2.orig/src/bandedSWA.cpp |
| 52 |
+++ bwa-mem2/src/bandedSWA.cpp |
| 53 |
@@ -4148,13 +4148,6 @@ |
| 54 |
|
| 55 |
/********************************************************************************/ |
| 56 |
/* SSE2 - 8 bit version */ |
| 57 |
-#ifndef __SSE4_1__ |
| 58 |
-static inline __m128i _mm_blendv_epi8 (__m128i x, __m128i y, __m128i mask) |
| 59 |
-{ |
| 60 |
- // Replace bit in x with bit in y when matching bit in mask is set: |
| 61 |
- return _mm_or_si128(_mm_andnot_si128(mask, x), _mm_and_si128(mask, y)); |
| 62 |
-} |
| 63 |
-#endif |
| 64 |
|
| 65 |
#define ZSCORE8(i4_128, y4_128) \ |
| 66 |
{ \ |
| 67 |
--- bwa-mem2.orig/src/bandedSWA.h |
| 68 |
+++ bwa-mem2/src/bandedSWA.h |
| 69 |
@@ -36,10 +36,17 @@ |
| 70 |
#include <assert.h> |
| 71 |
#include "macro.h" |
| 72 |
|
| 73 |
+#if !defined(__SSE__) |
| 74 |
+#define _mm_malloc(size, align) aligned_alloc(align, size) |
| 75 |
+#define _mm_free free |
| 76 |
+#define _MM_HINT_NTA 0 |
| 77 |
+#endif |
| 78 |
+ |
| 79 |
+#define SIMDE_ENABLE_NATIVE_ALIASES |
| 80 |
#if (__AVX512BW__ || __AVX2__) |
| 81 |
#include <immintrin.h> |
| 82 |
#else |
| 83 |
-#include <smmintrin.h> // for SSE4.1 |
| 84 |
+#include <simde/x86/sse4.1.h> // for SSE4.1 |
| 85 |
#define __mmask8 uint8_t |
| 86 |
#define __mmask16 uint16_t |
| 87 |
#endif |
| 88 |
--- bwa-mem2.orig/src/bwa.h |
| 89 |
+++ bwa-mem2/src/bwa.h |
| 90 |
@@ -37,6 +37,13 @@ |
| 91 |
#include "bwt.h" |
| 92 |
#include "macro.h" |
| 93 |
|
| 94 |
+#if !defined(__SSE__) |
| 95 |
+#define _mm_malloc(size, align) aligned_alloc(align, size) |
| 96 |
+#define _mm_free free |
| 97 |
+#define _MM_HINT_NTA 0 |
| 98 |
+#define _MM_HINT_T0 0 |
| 99 |
+#endif |
| 100 |
+ |
| 101 |
#define BWA_IDX_BWT 0x1 |
| 102 |
#define BWA_IDX_BNS 0x2 |
| 103 |
#define BWA_IDX_PAC 0x4 |
| 104 |
--- bwa-mem2.orig/src/bwamem.cpp |
| 105 |
+++ bwa-mem2/src/bwamem.cpp |
| 106 |
@@ -28,6 +28,9 @@ |
| 107 |
Heng Li <[email protected]> |
| 108 |
*****************************************************************************************/ |
| 109 |
|
| 110 |
+#if !(defined(__GNUC__) && __GNUC__ < 11 && !defined(__clang__)) |
| 111 |
+#include <immintrin.h> // For __rdtsc |
| 112 |
+#endif |
| 113 |
#include "bwamem.h" |
| 114 |
#include "FMI_search.h" |
| 115 |
#include "memcpy_bwamem.h" |
| 116 |
@@ -2026,7 +2029,7 @@ |
| 117 |
{ |
| 118 |
|
| 119 |
int32_t i; |
| 120 |
-#if ((!__AVX512BW__) & (__AVX2__ | __SSE2__)) |
| 121 |
+#if (!__AVX512BW__) |
| 122 |
for(i = 0; i <= MAX_SEQ_LEN16; i++) hist[i] = 0; |
| 123 |
#else |
| 124 |
__m512i zero512 = _mm512_setzero_si512(); |
| 125 |
--- bwa-mem2.orig/src/fastmap.cpp |
| 126 |
+++ bwa-mem2/src/fastmap.cpp |
| 127 |
@@ -36,9 +36,13 @@ |
| 128 |
#include <numa.h> |
| 129 |
#endif |
| 130 |
#include <sstream> |
| 131 |
+#if !(defined(__GNUC__) && __GNUC__ < 11 && !defined(__clang__)) |
| 132 |
+#include <immintrin.h> // For __rdtsc |
| 133 |
+#endif |
| 134 |
#include "fastmap.h" |
| 135 |
#include "FMI_search.h" |
| 136 |
|
| 137 |
+ |
| 138 |
#if AFF && (__linux__) |
| 139 |
#include <sys/sysinfo.h> |
| 140 |
int affy[256]; |
| 141 |
@@ -52,7 +56,7 @@ |
| 142 |
#ifdef _WIN32 |
| 143 |
__cpuid((int *) cpuid, (int)i); |
| 144 |
|
| 145 |
-#else |
| 146 |
+#elif defined(__x86_64__) || defined(__i386__) |
| 147 |
asm volatile |
| 148 |
("cpuid" : "=a" (cpuid[0]), "=b" (cpuid[1]), "=c" (cpuid[2]), "=d" (cpuid[3]) |
| 149 |
: "0" (i), "2" (0)); |
| 150 |
@@ -62,6 +66,7 @@ |
| 151 |
|
| 152 |
int HTStatus() |
| 153 |
{ |
| 154 |
+#if defined(__x86_64__) || defined(__i386__) |
| 155 |
unsigned int cpuid[4]; |
| 156 |
char platform_vendor[12]; |
| 157 |
__cpuid(0, cpuid); |
| 158 |
@@ -93,6 +98,9 @@ |
| 159 |
fprintf(stderr, "CPUs support hyperThreading !!\n"); |
| 160 |
|
| 161 |
return ht; |
| 162 |
+#else |
| 163 |
+ return 0; |
| 164 |
+#endif |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
--- bwa-mem2.orig/src/ksw.cpp |
| 169 |
+++ bwa-mem2/src/ksw.cpp |
| 170 |
@@ -30,7 +30,6 @@ |
| 171 |
#include <stdlib.h> |
| 172 |
#include <stdint.h> |
| 173 |
#include <assert.h> |
| 174 |
-#include <emmintrin.h> |
| 175 |
#include "ksw.h" |
| 176 |
#include "macro.h" |
| 177 |
|
| 178 |
--- bwa-mem2.orig/src/ksw.h |
| 179 |
+++ bwa-mem2/src/ksw.h |
| 180 |
@@ -26,7 +26,8 @@ |
| 181 |
#define __AC_KSW_H |
| 182 |
|
| 183 |
#include <stdint.h> |
| 184 |
-#include <emmintrin.h> |
| 185 |
+#define SIMDE_ENABLE_NATIVE_ALIASES |
| 186 |
+#include <simde/x86/sse2.h> |
| 187 |
|
| 188 |
#define KSW_XBYTE 0x10000 |
| 189 |
#define KSW_XSTOP 0x20000 |
| 190 |
--- bwa-mem2.orig/src/kswv.h |
| 191 |
+++ bwa-mem2/src/kswv.h |
| 192 |
@@ -39,7 +39,8 @@ |
| 193 |
#include "ksw.h" |
| 194 |
#include "bandedSWA.h" |
| 195 |
#else |
| 196 |
-#include <immintrin.h> |
| 197 |
+#define SIMDE_ENABLE_NATIVE_ALIASES |
| 198 |
+#include <simde/x86/avx2.h> |
| 199 |
#endif |
| 200 |
|
| 201 |
#ifdef __GNUC__ |
| 202 |
--- bwa-mem2.orig/src/main.cpp |
| 203 |
+++ bwa-mem2/src/main.cpp |
| 204 |
@@ -29,6 +29,9 @@ |
| 205 |
*****************************************************************************************/ |
| 206 |
|
| 207 |
// ---------------------------------- |
| 208 |
+#if !(defined(__GNUC__) && __GNUC__ < 11 && !defined(__clang__)) |
| 209 |
+#include <immintrin.h> // For __rdtsc |
| 210 |
+#endif |
| 211 |
#include "main.h" |
| 212 |
|
| 213 |
#ifndef PACKAGE_VERSION |
| 214 |
@@ -85,6 +88,8 @@ |
| 215 |
fprintf(stderr, "Executing in SSE4.2 mode!!\n"); |
| 216 |
#elif __SSE4_1__ |
| 217 |
fprintf(stderr, "Executing in SSE4.1 mode!!\n"); |
| 218 |
+#elif __SSE2__ |
| 219 |
+ fprintf(stderr, "Executing in SSE2!!\n"); |
| 220 |
#endif |
| 221 |
fprintf(stderr, "-----------------------------\n"); |
| 222 |
|
| 223 |
--- bwa-mem2.orig/src/runsimd.cpp |
| 224 |
+++ bwa-mem2/src/runsimd.cpp |
| 225 |
@@ -61,7 +61,7 @@ |
| 226 |
__asm__ volatile ("cpuid" |
| 227 |
: "=a" (cpuid[0]), "=b" (cpuid[1]), "=c" (cpuid[2]), "=d" (cpuid[3]) |
| 228 |
: "0" (func_id), "2" (subfunc_id)); |
| 229 |
-#else // on 32bit, ebx can NOT be used as PIC code |
| 230 |
+#elif defined(__i386__) // on 32bit, ebx can NOT be used as PIC code |
| 231 |
__asm__ volatile ("xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1" |
| 232 |
: "=a" (cpuid[0]), "=r" (cpuid[1]), "=c" (cpuid[2]), "=d" (cpuid[3]) |
| 233 |
: "0" (func_id), "2" (subfunc_id)); |
| 234 |
--- bwa-mem2.orig/src/utils.h |
| 235 |
+++ bwa-mem2/src/utils.h |
| 236 |
@@ -30,6 +30,7 @@ |
| 237 |
|
| 238 |
#include <stdint.h> |
| 239 |
#include <stdio.h> |
| 240 |
+#include <sys/select.h> |
| 241 |
#include <zlib.h> |
| 242 |
|
| 243 |
#ifdef __GNUC__ |
| 244 |
@@ -64,6 +65,43 @@ |
| 245 |
return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 ); |
| 246 |
} |
| 247 |
#endif |
| 248 |
+// From https://github.com/google/benchmark/blob/37177a84b7e8d33696ea1e1854513cb0de3b4dc3/src/cycleclock.h |
| 249 |
+// Apache 2.0 license |
| 250 |
+#elif defined(__aarch64__) |
| 251 |
+ // System timer of ARMv8 runs at a different frequency than the CPU's. |
| 252 |
+ // The frequency is fixed, typically in the range 1-50MHz. It can be |
| 253 |
+ // read at CNTFRQ special register. We assume the OS has set up |
| 254 |
+ // the virtual timer properly. |
| 255 |
+static inline unsigned long long __rdtsc(void) |
| 256 |
+{ |
| 257 |
+ int64_t virtual_timer_value; |
| 258 |
+ asm volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value)); |
| 259 |
+ return virtual_timer_value; |
| 260 |
+} |
| 261 |
+#elif !(defined(__GNUC__) && __GNUC__ >= 11) |
| 262 |
+static inline unsigned long long __rdtsc(void) |
| 263 |
+{ |
| 264 |
+#if defined(__ARM_ARCH) && (__ARM_ARCH >= 6) |
| 265 |
+ // V6 is the earliest arch that has a standard cyclecount |
| 266 |
+ // Native Client validator doesn't allow MRC instructions. |
| 267 |
+ uint32_t pmccntr; |
| 268 |
+ uint32_t pmuseren; |
| 269 |
+ uint32_t pmcntenset; |
| 270 |
+ // Read the user mode perf monitor counter access permissions. |
| 271 |
+ asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r"(pmuseren)); |
| 272 |
+ if (pmuseren & 1) { // Allows reading perfmon counters for user mode code. |
| 273 |
+ asm volatile("mrc p15, 0, %0, c9, c12, 1" : "=r"(pmcntenset)); |
| 274 |
+ if (pmcntenset & 0x80000000ul) { // Is it counting? |
| 275 |
+ asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(pmccntr)); |
| 276 |
+ // The counter is set up to count every 64th cycle |
| 277 |
+ return static_cast<int64_t>(pmccntr) * 64; // Should optimize to << 6 |
| 278 |
+ } |
| 279 |
+ } |
| 280 |
+#endif |
| 281 |
+ struct timeval tv; |
| 282 |
+ gettimeofday(&tv, nullptr); |
| 283 |
+ return static_cast<int64_t>(tv.tv_sec) * 1000000 + tv.tv_usec; |
| 284 |
+} |
| 285 |
#endif |
| 286 |
|
| 287 |
typedef struct { |