Skip to content

Commit ce00766

Browse files
nmoinvazDead2
authored andcommitted
Changes to support compilation with MSVC ARM & ARM64 (madler#386)
* Merge aarch64 and arm cmake sections. * Updated MSVC compiler support for ARM and ARM64. * Moved detection for -mfpu=neon to where the flag is set to simplify add_intrinsics_option. * Only add ${ACLEFLAG} on aarch64 if not WITH_NEON. * Rename arch/x86/ctzl.h to fallback_builtins.h.
1 parent 743def4 commit ce00766

File tree

11 files changed

+57
-62
lines changed

11 files changed

+57
-62
lines changed

CMakeLists.txt

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,16 @@ if(CMAKE_OSX_ARCHITECTURES)
7272
# If multiple architectures are requested (universal build), pick only the first
7373
list(GET CMAKE_OSX_ARCHITECTURES 0 ARCH)
7474
else()
75-
set(ARCH ${CMAKE_SYSTEM_PROCESSOR})
75+
if (MSVC)
76+
if("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "ARM" OR "${MSVC_C_ARCHITECTURE_ID}" STREQUAL "ARMV7")
77+
set(ARCH "arm")
78+
elseif ("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "ARM64")
79+
set(ARCH "aarch64")
80+
endif()
81+
endif()
82+
if(NOT ARCH)
83+
set(ARCH ${CMAKE_SYSTEM_PROCESSOR})
84+
endif()
7685
endif()
7786
message(STATUS "Architecture: ${ARCH}")
7887
if(CMAKE_TOOLCHAIN_FILE)
@@ -156,7 +165,7 @@ elseif(MSVC)
156165
set(SSE2FLAG "/arch:SSE2")
157166
endif()
158167
if("${ARCH}" MATCHES "arm")
159-
add_definitions("-D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE=1")
168+
add_definitions(-D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE)
160169
set(NEONFLAG "/arch:VFPv4")
161170
endif()
162171
if(WITH_NATIVE_INSTRUCTIONS)
@@ -174,13 +183,27 @@ else()
174183
message(STATUS "Ignoring WITH_NATIVE_INSTRUCTIONS; not implemented yet on this configuration")
175184
endif()
176185
endif()
177-
if(__GNUC__ AND "${ARCH}" MATCHES "arm")
178-
execute_process(COMMAND ${CMAKE_C_COMPILER} "-dumpmachine"
179-
OUTPUT_VARIABLE GCC_MACHINE)
180-
if ("${GCC_MACHINE}" MATCHES "eabihf")
181-
set(FLOATABI "-mfloat-abi=hard")
186+
# Check support for ARM floating point
187+
if("${ARCH}" MATCHES "arm")
188+
if (__GNUC__)
189+
execute_process(COMMAND ${CMAKE_C_COMPILER} "-dumpmachine"
190+
OUTPUT_VARIABLE GCC_MACHINE)
191+
if ("${GCC_MACHINE}" MATCHES "eabihf")
192+
set(FLOATABI "-mfloat-abi=hard")
193+
else()
194+
set(FLOATABI "-mfloat-abi=softfp")
195+
endif()
196+
endif()
197+
# Check whether -mfpu=neon is available
198+
set(CMAKE_REQUIRED_FLAGS "-mfpu=neon")
199+
check_c_source_compiles(
200+
"int main() { return 0; }"
201+
MFPU_NEON_AVAILABLE FAIL_REGEX "not supported")
202+
set(CMAKE_REQUIRED_FLAGS)
203+
if(MFPU_NEON_AVAILABLE)
204+
set(NEONFLAG "${FLOATABI} -mfpu=neon")
182205
else()
183-
set(FLOATABI "-mfloat-abi=softfp")
206+
set(NEONFLAG "${FLOATABI}")
184207
endif()
185208
endif()
186209
if(NOT NATIVEFLAG)
@@ -201,7 +224,6 @@ else()
201224
endif()
202225
if("${ARCH}" MATCHES "arm")
203226
set(ACLEFLAG "-march=armv8-a+crc")
204-
set(NEONFLAG "${FLOATABI} -mfpu=neon")
205227
elseif("${ARCH}" MATCHES "aarch64")
206228
set(ACLEFLAG "-march=armv8-a+crc")
207229
set(NEONFLAG "-march=armv8-a+crc+simd")
@@ -212,7 +234,6 @@ else()
212234
set(PCLMULFLAG ${NATIVEFLAG})
213235
if("${ARCH}" MATCHES "arm")
214236
set(ACLEFLAG "${NATIVEFLAG}")
215-
set(NEONFLAG "${FLOATABI} -mfpu=neon")
216237
elseif("${ARCH}" MATCHES "aarch64")
217238
set(ACLEFLAG "${NATIVEFLAG}")
218239
set(NEONFLAG "${NATIVEFLAG}")
@@ -483,16 +504,6 @@ else()
483504
endif()
484505
set(CMAKE_REQUIRED_FLAGS)
485506

486-
# Check whether -mfpu=neon is available
487-
set(CMAKE_REQUIRED_FLAGS "-mfpu=neon")
488-
check_c_source_compiles(
489-
"int main()
490-
{
491-
return 0;
492-
}"
493-
MFPU_NEON_AVAILABLE FAIL_REGEX "not supported")
494-
set(CMAKE_REQUIRED_FLAGS)
495-
496507
# FORCE_SSE2 option will only be shown if HAVE_SSE2_INTRIN is true
497508
if("${ARCH}" MATCHES "i[3-6]86")
498509
cmake_dependent_option(FORCE_SSE2 "Always assume CPU is SSE2 capable" OFF "HAVE_SSE2_INTRIN" OFF)
@@ -542,39 +553,25 @@ if("${ARCH}" MATCHES "arm" OR "${ARCH}" MATCHES "aarch64")
542553
set(ZLIB_ARCH_SRCS ${ZLIB_ARCH_SRCS} ${ARCHDIR}/armfeature.c ${ARCHDIR}/fill_window_arm.c)
543554
endif()
544555
if(WITH_OPTIM)
545-
if("${ARCH}" MATCHES "arm")
546-
if(WITH_ACLE)
547-
set(ZLIB_ARCH_SRCS ${ZLIB_ARCH_SRCS} ${ARCHDIR}/crc32_acle.c ${ARCHDIR}/insert_string_acle.c)
548-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${ACLEFLAG}")
549-
add_definitions("-DARM_ACLE_CRC_HASH")
550-
add_feature_info(ACLE_CRC 1 "Support CRC hash generation using the ACLE instruction set, using \"${ACLEFLAG}\"")
551-
endif()
556+
if("${ARCH}" MATCHES "arm" OR "${ARCH}" MATCHES "aarch64")
552557
if(WITH_NEON)
553-
if(MFPU_NEON_AVAILABLE)
554-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${NEONFLAG}")
555-
endif()
556-
add_definitions("-DARM_NEON_ADLER32")
557-
if(MSVC)
558-
add_definitions("-D__ARM_NEON__=1")
559-
endif(MSVC)
560558
set(ZLIB_ARCH_SRCS ${ZLIB_ARCH_SRCS} ${ARCHDIR}/adler32_neon.c)
559+
add_definitions(-DARM_NEON_ADLER32)
560+
add_intrinsics_option("${NEONFLAG}")
561+
if(MSVC)
562+
add_definitions(-D__ARM_NEON__)
563+
endif()
561564
add_feature_info(NEON_FILLWINDOW 1 "Support NEON instructions in fill_window_arm, using \"${NEONFLAG}\"")
562565
endif()
563-
elseif("${ARCH}" MATCHES "aarch64")
564566
if(WITH_ACLE)
565567
set(ZLIB_ARCH_SRCS ${ZLIB_ARCH_SRCS} ${ARCHDIR}/crc32_acle.c ${ARCHDIR}/insert_string_acle.c)
566-
add_definitions("-DARM_ACLE_CRC_HASH")
568+
add_definitions(-DARM_ACLE_CRC_HASH)
569+
# For ARM aarch64, we need to check WITH_NEON first
570+
if("${ARCH}" MATCHES "arm" OR NOT WITH_NEON)
571+
add_intrinsics_option("${ACLEFLAG}")
572+
endif()
567573
add_feature_info(ACLE_CRC 1 "Support CRC hash generation using the ACLE instruction set, using \"${ACLEFLAG}\"")
568574
endif()
569-
# We need to check WITH_NEON first
570-
if(WITH_NEON)
571-
add_definitions("-DARM_NEON_ADLER32")
572-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${NEONFLAG}")
573-
set(ZLIB_ARCH_SRCS ${ZLIB_ARCH_SRCS} ${ARCHDIR}/adler32_neon.c)
574-
add_feature_info(NEON_FILLWINDOW 1 "Support NEON instructions in fill_window_arm, using \"${NEONFLAG}\"")
575-
elseif(WITH_ACLE)
576-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${ACLEFLAG}")
577-
endif()
578575
elseif("${ARCHDIR}" MATCHES "arch/x86")
579576
add_definitions("-DX86_CPUID")
580577
set(ZLIB_ARCH_SRCS ${ZLIB_ARCH_SRCS} ${ARCHDIR}/x86.c)

arch/arm/adler32_neon.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ uint32_t adler32_neon(uint32_t adler, const unsigned char *buf, size_t len) {
109109

110110
for (i = 0; i < len; i += n) {
111111
if ((i + n) > len)
112-
n = len - i;
112+
n = (int)(len - i);
113113

114114
if (n < 16)
115115
break;

arch/arm/armfeature.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static int arm_has_crc32() {
1818
}
1919

2020
/* AArch64 has neon. */
21-
#if !defined(__aarch64__)
21+
#if !defined(__aarch64__) && !defined(_M_ARM64)
2222
static inline int arm_has_neon()
2323
{
2424
#if defined(__linux__) && defined(HWCAP_NEON)
@@ -41,7 +41,7 @@ ZLIB_INTERNAL int arm_cpu_has_neon;
4141
ZLIB_INTERNAL int arm_cpu_has_crc32;
4242

4343
void ZLIB_INTERNAL arm_check_features(void) {
44-
#if defined(__aarch64__)
44+
#if defined(__aarch64__) || defined(_M_ARM64)
4545
arm_cpu_has_neon = 1; /* always available */
4646
#else
4747
arm_cpu_has_neon = arm_has_neon();

deflate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ int ZEXPORT PREFIX(deflateInit2_)(PREFIX3(stream) *strm, int level, int method,
269269

270270
#ifdef X86_CPUID
271271
x86_check_features();
272-
#elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM)
272+
#elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
273273
arm_check_features();
274274
#endif
275275

arch/x86/ctzl.h renamed to fallback_builtins.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <intrin.h>
55
#ifdef X86_CPUID
6-
# include "x86.h"
6+
# include "arch/x86/x86.h"
77
#endif
88

99
#if defined(_MSC_VER) && !defined(__clang__)

functable.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extern Pos insert_string_acle(deflate_state *const s, const Pos str, unsigned in
1919
/* fill_window */
2020
#ifdef X86_SSE2
2121
extern void fill_window_sse(deflate_state *s);
22-
#elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM)
22+
#elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
2323
extern void fill_window_arm(deflate_state *s);
2424
#endif
2525

@@ -81,7 +81,7 @@ ZLIB_INTERNAL void fill_window_stub(deflate_state *s) {
8181
if (x86_cpu_has_sse2)
8282
# endif
8383
functable.fill_window=&fill_window_sse;
84-
#elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM)
84+
#elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
8585
functable.fill_window=&fill_window_arm;
8686
#endif
8787

inflate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ int ZEXPORT PREFIX(inflateInit2_)(PREFIX3(stream) *strm, int windowBits, const c
132132

133133
#ifdef X86_CPUID
134134
x86_check_features();
135-
#elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM)
135+
#elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
136136
arm_check_features();
137137
#endif
138138

match_p.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@
3333

3434

3535
#if defined(_MSC_VER) && !defined(__clang__)
36-
# if defined(_M_IX86) || defined(_M_AMD64) || defined(_M_IA64)
37-
# include "arch/x86/ctzl.h"
38-
# elif defined(_M_ARM)
39-
# include "arch/arm/ctzl.h"
36+
# if defined(_M_IX86) || defined(_M_AMD64) || defined(_M_IA64) || defined(_M_ARM) || defined(_M_ARM64)
37+
# include "fallback_builtins.h"
4038
# endif
4139
#endif
4240

memcopy.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static inline uint64_t load_64_bits(const unsigned char *in, unsigned bits) {
1818
#endif
1919
}
2020

21-
#if (defined(__GNUC__) || defined(__clang__)) && (defined(__ARM_NEON__) || defined(__ARM_NEON))
21+
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
2222
#include <arm_neon.h>
2323
typedef uint8x16_t inffast_chunk_t;
2424
#define INFFAST_CHUNKSIZE sizeof(inffast_chunk_t)
@@ -199,7 +199,7 @@ static inline unsigned char *chunkmemset_3(unsigned char *out, unsigned char *fr
199199
}
200200
#endif
201201

202-
#if defined(__aarch64__)
202+
#if defined(__aarch64__) || defined(_M_ARM64)
203203
static inline unsigned char *chunkmemset_6(unsigned char *out, unsigned char *from, unsigned dist, unsigned len) {
204204
uint16x8x3_t chunks;
205205
unsigned sz = sizeof(chunks);
@@ -265,7 +265,7 @@ static inline unsigned char *chunkmemset(unsigned char *out, unsigned dist, unsi
265265
chunk = chunkmemset_4(from);
266266
break;
267267
}
268-
#if defined(__aarch64__)
268+
#if defined(__aarch64__) || defined(_M_ARM64)
269269
case 6:
270270
return chunkmemset_6(out, from, dist, len);
271271
#endif

zendian.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#elif defined(WIN32) || defined(_WIN32)
2828
# define LITTLE_ENDIAN 1234
2929
# define BIG_ENDIAN 4321
30-
# if defined(_M_IX86) || defined(_M_AMD64) || defined(_M_IA64) || defined (_M_ARM)
30+
# if defined(_M_IX86) || defined(_M_AMD64) || defined(_M_IA64) || defined (_M_ARM) || defined (_M_ARM64)
3131
# define BYTE_ORDER LITTLE_ENDIAN
3232
# else
3333
# error Unknown endianness!

0 commit comments

Comments
 (0)