]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Convert arch detection from preprocessor errors to runtime with header
authorNathan Moinvaziri <nathan@nathanm.com>
Sun, 18 Jan 2026 03:12:44 +0000 (19:12 -0800)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Sat, 24 Jan 2026 20:48:57 +0000 (21:48 +0100)
arch/arm/adler32_neon.c
arch/arm/crc32_armv8.c
arch/arm/slide_hash_armv6.c
arch/arm/slide_hash_neon.c
cmake/detect-arch.c
cmake/detect-arch.cmake
zarch.h [new file with mode: 0644]

index b09594736f8581e7bd9a270f6fb83af99af41628..2ef02398e632d81bb4ce3a28bc3afddb42cee326 100644 (file)
@@ -9,8 +9,8 @@
 #ifdef ARM_NEON
 
 #include "zbuild.h"
-#include "adler32_p.h"
 #include "neon_intrins.h"
+#include "adler32_p.h"
 
 static const uint16_t ALIGNED_(64) taps[64] = {
     64, 63, 62, 61, 60, 59, 58, 57,
index 7d5d1b939a9ca7c9cfca82be65385bce16c9def3..d42f5cc13039a5a329846a9badca2fb710a5cbdd 100644 (file)
@@ -7,8 +7,8 @@
 #ifdef ARM_CRC32
 
 #include "zbuild.h"
-#include "crc32.h"
 #include "acle_intrins.h"
+#include "crc32.h"
 
 Z_INTERNAL Z_TARGET_CRC uint32_t crc32_armv8(uint32_t crc, const uint8_t *buf, size_t len) {
     uint32_t c = ~crc;
index da22a993e9f962adabd44352b8f516cdeba8283c..b241e6c5e64a5eecae9e6aacf29c07ca093772e0 100644 (file)
@@ -6,8 +6,8 @@
 #ifdef ARM_SIMD
 
 #include "zbuild.h"
-#include "deflate.h"
 #include "acle_intrins.h"
+#include "deflate.h"
 
 /* SIMD version of hash_chain rebase */
 static inline void slide_hash_chain(Pos *table, uint32_t entries, uint16_t wsize) {
index 3b954d17eea782f00ba2b9f9c9c5e04db97c662e..e5a636baef5fcfc4bcc3116350bc1a31dfb6e9b0 100644 (file)
@@ -11,8 +11,8 @@
 #ifdef ARM_NEON
 
 #include "zbuild.h"
-#include "deflate.h"
 #include "neon_intrins.h"
+#include "deflate.h"
 
 /* SIMD version of hash_chain rebase */
 static inline void slide_hash_chain(Pos *table, uint32_t entries, uint16_t wsize) {
index 6a33f8bb9d69eb9ace32dbf38e71e6372f893f04..d9bc7c1ec4d9242de2db1e5c3f23995ba506a4fe 100644 (file)
-// archdetect.c -- Detect compiler architecture and raise preprocessor error
-//                 containing a simple arch identifier.
-// Copyright (C) 2019 Hans Kristian Rosbach
-// Licensed under the Zlib license, see LICENSE.md for details
+/* detect-arch.c -- Detect compiler architecture and print to stderr
+ *                  containing a simple arch identifier.
+ * Copyright (C) 2019 Hans Kristian Rosbach
+ * Licensed under the Zlib license, see LICENSE.md for details
+ */
 
-// x86_64
-#if defined(__x86_64__) || defined(_M_X64)
-    #error archfound x86_64
+#include <stdio.h>
 
-// x86
-#elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
-      defined(__i386) || defined(_M_IX86)
-    #error archfound i686
+#include "../zarch.h"
 
-// ARM
-#elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
-    #error archfound aarch64
-#elif defined(__arm__) || defined(__arm) || defined(_M_ARM) || defined(__TARGET_ARCH_ARM)
-    #if defined(__ARM64_ARCH_8__) || defined(__ARMv8__) || defined(__ARMv8_A__)
-        #error archfound armv8
-    #elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__)
-        #error archfound armv7
-    #elif defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6T2__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6M__)
-        #error archfound armv6
-    #elif defined(__ARM_ARCH_5T__) || defined(__ARM_ARCH_5TE__) || defined(__ARM_ARCH_5TEJ__)
-        #error archfound armv5
-    #elif defined(__ARM_ARCH_4T__) || defined(__TARGET_ARCH_5E__)
-        #error archfound armv4
-    #elif defined(__ARM_ARCH_3__) || defined(__TARGET_ARCH_3M__)
-        #error archfound armv3
-    #elif defined(__ARM_ARCH_2__)
-        #error archfound armv2
-    #endif
-
-// PowerPC
-#elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__)
-    #if defined(__64BIT__) || defined(__powerpc64__) || defined(__ppc64__)
-        #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
-            #error archfound powerpc64le
-        #else
-            #error archfound powerpc64
-        #endif
-    #else
-        #error archfound powerpc
-    #endif
-
-// --------------- Less common architectures alphabetically below ---------------
-
-// ALPHA
-#elif defined(__alpha__) || defined(__alpha)
-    #error archfound alpha
-
-// Blackfin
-#elif defined(__BFIN__)
-    #error archfound blackfin
-
-// Itanium
-#elif defined(__ia64) || defined(_M_IA64)
-    #error archfound ia64
-
-// MIPS
-#elif defined(__mips__) || defined(__mips)
-    #error archfound mips
-
-// Motorola 68000-series
-#elif defined(__m68k__)
-    #error archfound m68k
-
-// SuperH
-#elif defined(__sh__)
-    #error archfound sh
-
-// SPARC
-#elif defined(__sparc__) || defined(__sparc)
-    #if defined(__sparcv9) || defined(__sparc_v9__)
-        #error archfound sparc9
-    #elif defined(__sparcv8) || defined(__sparc_v8__)
-        #error archfound sparc8
-    #endif
-
-// SystemZ
-#elif defined(__370__)
-    #error archfound s370
-#elif defined(__s390__)
-    #error archfound s390
-#elif defined(__s390x) || defined(__zarch__)
-    #error archfound s390x
-
-// PARISC
-#elif defined(__hppa__)
-    #error archfound parisc
-
-// RS-6000
-#elif defined(__THW_RS6000)
-    #error archfound rs6000
-
-// RISC-V
-#elif defined(__riscv)
-    #if __riscv_xlen == 64
-        #error archfound riscv64
-    #elif __riscv_xlen == 32
-        #error archfound riscv32
-    #endif
-
-// LOONGARCH
-#elif defined(__loongarch_lp64)
-    #error archfound loongarch64
-
-// Emscripten (WebAssembly)
-#elif defined(__EMSCRIPTEN__)
-    #error archfound wasm32
-
-// return 'unrecognized' if we do not know what architecture this is
-#else
-    #error archfound unrecognized
-#endif
+int main(void) {
+    fprintf(stderr, "archfound " ARCH_NAME "\n");
+    return 0;
+}
index f7ca6f1b81f0571795088bec9d3d3d6ccb7d7393..bebb26f586af32c8b2ff9f769ef867188ae5cdf4 100644 (file)
@@ -11,17 +11,17 @@ elseif(EMSCRIPTEN)
 elseif(CMAKE_CROSSCOMPILING)
     set(ARCH ${CMAKE_C_COMPILER_TARGET})
 else()
-    # Let preprocessor parse archdetect.c and raise an error containing the arch identifier
+    # Compile and run detect-arch.c which prints architecture name to stderr
     try_run(
         run_result_unused
         compile_result_unused
         ${CMAKE_CURRENT_BINARY_DIR}
         ${CMAKE_CURRENT_LIST_DIR}/detect-arch.c
-        COMPILE_OUTPUT_VARIABLE RAWOUTPUT
         CMAKE_FLAGS CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}
+        RUN_OUTPUT_VARIABLE RAWOUTPUT
     )
 
-    # Find basearch tag, and extract the arch word into BASEARCH variable
+    # Find archfound tag, and extract the arch word into ARCH variable
     string(REGEX REPLACE ".*archfound ([a-zA-Z0-9_]+).*" "\\1" ARCH "${RAWOUTPUT}")
     if(NOT ARCH)
         set(ARCH unknown)
diff --git a/zarch.h b/zarch.h
new file mode 100644 (file)
index 0000000..f53b26b
--- /dev/null
+++ b/zarch.h
@@ -0,0 +1,195 @@
+/* zarch.h -- Detect compiler architecture and define ARCH_* macros
+ * Copyright (C) 2019 Hans Kristian Rosbach
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#ifndef ZARCH_H
+#define ZARCH_H
+
+/* x86_64 */
+#if defined(__x86_64__) || defined(_M_X64)
+#  define ARCH_X86
+#  define ARCH_64BIT
+#  define ARCH_NAME "x86_64"
+
+/* x86 (32-bit) */
+#elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
+      defined(__i386) || defined(_M_IX86)
+#  define ARCH_X86
+#  define ARCH_32BIT
+#  define ARCH_NAME "i686"
+
+/* ARM 64-bit */
+#elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
+#  define ARCH_ARM
+#  define ARCH_64BIT
+#  define ARCH_NAME "aarch64"
+
+/* ARM 32-bit */
+#elif defined(__arm__) || defined(__arm) || defined(_M_ARM) || defined(__TARGET_ARCH_ARM)
+#  define ARCH_ARM
+#  define ARCH_32BIT
+#  if defined(__ARM64_ARCH_8__) || defined(__ARM_ARCH_8A__) || defined(__ARMv8__) || defined(__ARMv8_A__)
+#    define ARCH_NAME "armv8"
+#  elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__)
+#    define ARCH_NAME "armv7"
+#  elif defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6T2__) || \
+        defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6ZK__) || \
+        defined(__ARM_ARCH_6M__)
+#    define ARCH_NAME "armv6"
+#  elif defined(__ARM_ARCH_5T__) || defined(__ARM_ARCH_5TE__) || defined(__ARM_ARCH_5TEJ__)
+#    define ARCH_NAME "armv5"
+#  elif defined(__ARM_ARCH_4T__)
+#    define ARCH_NAME "armv4"
+#  elif defined(__ARM_ARCH_3__)
+#    define ARCH_NAME "armv3"
+#  elif defined(__ARM_ARCH_2__)
+#    define ARCH_NAME "armv2"
+#  else
+#    define ARCH_NAME "arm"
+#  endif
+
+/* PowerPC */
+#elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__)
+#  define ARCH_POWER
+#  if defined(__64BIT__) || defined(__powerpc64__) || defined(__ppc64__)
+#    define ARCH_64BIT
+#    if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#      define ARCH_NAME "powerpc64le"
+#    else
+#      define ARCH_NAME "powerpc64"
+#    endif
+#  else
+#    define ARCH_32BIT
+#    define ARCH_NAME "powerpc"
+#  endif
+
+/* --------------- Less common architectures alphabetically below --------------- */
+
+/* ALPHA */
+#elif defined(__alpha__) || defined(__alpha)
+#  define ARCH_ALPHA
+#  define ARCH_64BIT
+#  define ARCH_NAME "alpha"
+
+/* Blackfin */
+#elif defined(__BFIN__)
+#  define ARCH_BLACKFIN
+#  define ARCH_32BIT
+#  define ARCH_NAME "blackfin"
+
+/* Itanium */
+#elif defined(__ia64) || defined(_M_IA64)
+#  define ARCH_IA64
+#  define ARCH_64BIT
+#  define ARCH_NAME "ia64"
+
+/* MIPS */
+#elif defined(__mips__) || defined(__mips)
+#  define ARCH_MIPS
+#  if defined(__mips64) || defined(__mips64__)
+#    define ARCH_64BIT
+#    define ARCH_NAME "mips64"
+#  else
+#    define ARCH_32BIT
+#    define ARCH_NAME "mips"
+#  endif
+
+/* Motorola 68000-series */
+#elif defined(__m68k__)
+#  define ARCH_M68K
+#  define ARCH_32BIT
+#  define ARCH_NAME "m68k"
+
+/* SuperH */
+#elif defined(__sh__)
+#  define ARCH_SH
+#  define ARCH_32BIT
+#  define ARCH_NAME "sh"
+
+/* SPARC */
+#elif defined(__sparc__) || defined(__sparc)
+#  define ARCH_SPARC
+#  if defined(__sparcv9) || defined(__sparc_v9__) || defined(__sparc64__)
+#    define ARCH_64BIT
+#    define ARCH_NAME "sparc9"
+#  elif defined(__sparcv8) || defined(__sparc_v8__)
+#    define ARCH_32BIT
+#    define ARCH_NAME "sparc8"
+#  else
+#    define ARCH_32BIT
+#    define ARCH_NAME "sparc"
+#  endif
+
+/* SystemZ / S390 */
+#elif defined(__s390x) || defined(__zarch__)
+#  define ARCH_S390
+#  define ARCH_64BIT
+#  define ARCH_NAME "s390x"
+#elif defined(__s390__)
+#  define ARCH_S390
+#  define ARCH_32BIT
+#  define ARCH_NAME "s390"
+#elif defined(__370__)
+#  define ARCH_S390
+#  define ARCH_32BIT
+#  define ARCH_NAME "s370"
+
+/* PARISC */
+#elif defined(__hppa__)
+#  define ARCH_PARISC
+#  if defined(__hppa64__) || defined(__LP64__)
+#    define ARCH_64BIT
+#    define ARCH_NAME "parisc64"
+#  else
+#    define ARCH_32BIT
+#    define ARCH_NAME "parisc"
+#  endif
+
+/* RS-6000 */
+#elif defined(__THW_RS6000)
+#  define ARCH_RS6000
+#  define ARCH_32BIT
+#  define ARCH_NAME "rs6000"
+
+/* RISC-V */
+#elif defined(__riscv)
+#  define ARCH_RISCV
+#  if __riscv_xlen == 64
+#    define ARCH_64BIT
+#    define ARCH_NAME "riscv64"
+#  elif __riscv_xlen == 32
+#    define ARCH_32BIT
+#    define ARCH_NAME "riscv32"
+#  else
+#    define ARCH_NAME "riscv"
+#  endif
+
+/* LOONGARCH */
+#elif defined(__loongarch_lp64)
+#  define ARCH_LOONGARCH
+#  define ARCH_64BIT
+#  define ARCH_NAME "loongarch64"
+
+/* Emscripten (WebAssembly) */
+#elif defined(__EMSCRIPTEN__)
+#  define ARCH_WASM
+#  if defined(__wasm64__)
+#    define ARCH_64BIT
+#    define ARCH_NAME "wasm64"
+#  else
+#    define ARCH_32BIT
+#    define ARCH_NAME "wasm32"
+#  endif
+
+/* Unrecognized architecture */
+#else
+#  if defined(__LP64__) || defined(_LP64) || defined(_WIN64)
+#    define ARCH_64BIT
+#  else
+#    define ARCH_32BIT
+#  endif
+#  define ARCH_NAME "unrecognized"
+#endif
+
+#endif /* ZARCH_H */