diff --git a/build.zig b/build.zig index 217852a009..4b06f31640 100644 --- a/build.zig +++ b/build.zig @@ -6,6 +6,135 @@ const mem = std.mem; const Compile = std.Build.Step.Compile; const Target = std.Target; +fn initLibConfig(target: std.Build.ResolvedTarget, lib: *Compile) void { + lib.linkLibC(); + lib.addIncludePath(.{ .path = "src/libsodium/include/sodium" }); + lib.defineCMacro("_GNU_SOURCE", "1"); + lib.defineCMacro("CONFIGURED", "1"); + lib.defineCMacro("DEV_MODE", "1"); + lib.defineCMacro("HAVE_ATOMIC_OPS", "1"); + lib.defineCMacro("HAVE_C11_MEMORY_FENCES", "1"); + lib.defineCMacro("HAVE_CET_H", "1"); + lib.defineCMacro("HAVE_GCC_MEMORY_FENCES", "1"); + lib.defineCMacro("HAVE_INLINE_ASM", "1"); + lib.defineCMacro("HAVE_INTTYPES_H", "1"); + lib.defineCMacro("HAVE_STDINT_H", "1"); + lib.defineCMacro("HAVE_TI_MODE", "1"); + lib.want_lto = false; + + const endian = target.result.cpu.arch.endian(); + switch (endian) { + .big => lib.defineCMacro("NATIVE_BIG_ENDIAN", "1"), + .little => lib.defineCMacro("NATIVE_LITTLE_ENDIAN", "1"), + } + + switch (target.result.os.tag) { + .linux => { + lib.defineCMacro("ASM_HIDE_SYMBOL", ".hidden"); + lib.defineCMacro("TLS", "_Thread_local"); + + lib.defineCMacro("HAVE_CATCHABLE_ABRT", "1"); + lib.defineCMacro("HAVE_CATCHABLE_SEGV", "1"); + lib.defineCMacro("HAVE_CLOCK_GETTIME", "1"); + lib.defineCMacro("HAVE_GETPID", "1"); + lib.defineCMacro("HAVE_INLINE_ASM", "1"); + lib.defineCMacro("HAVE_MADVISE", "1"); + lib.defineCMacro("HAVE_MLOCK", "1"); + lib.defineCMacro("HAVE_MMAP", "1"); + lib.defineCMacro("HAVE_MPROTECT", "1"); + lib.defineCMacro("HAVE_NANOSLEEP", "1"); + lib.defineCMacro("HAVE_POSIX_MEMALIGN", "1"); + lib.defineCMacro("HAVE_PTHREAD_PRIO_INHERIT", "1"); + lib.defineCMacro("HAVE_PTHREAD", "1"); + lib.defineCMacro("HAVE_RAISE", "1"); + lib.defineCMacro("HAVE_SYSCONF", "1"); + lib.defineCMacro("HAVE_SYS_AUXV_H", "1"); + lib.defineCMacro("HAVE_SYS_MMAN_H", "1"); + lib.defineCMacro("HAVE_SYS_PARAM_H", "1"); + lib.defineCMacro("HAVE_SYS_RANDOM_H", "1"); + lib.defineCMacro("HAVE_WEAK_SYMBOLS", "1"); + }, + .windows => { + lib.defineCMacro("HAVE_RAISE", "1"); + lib.defineCMacro("HAVE_SYS_PARAM_H", "1"); + if (lib.isStaticLibrary()) { + lib.defineCMacro("SODIUM_STATIC", "1"); + } + }, + .macos => { + lib.defineCMacro("ASM_HIDE_SYMBOL", ".private_extern"); + lib.defineCMacro("TLS", "_Thread_local"); + + lib.defineCMacro("HAVE_ARC4RANDOM", "1"); + lib.defineCMacro("HAVE_ARC4RANDOM_BUF", "1"); + lib.defineCMacro("HAVE_CATCHABLE_ABRT", "1"); + lib.defineCMacro("HAVE_CATCHABLE_SEGV", "1"); + lib.defineCMacro("HAVE_CLOCK_GETTIME", "1"); + lib.defineCMacro("HAVE_GETENTROPY", "1"); + lib.defineCMacro("HAVE_GETPID", "1"); + lib.defineCMacro("HAVE_MADVISE", "1"); + lib.defineCMacro("HAVE_MEMSET_S", "1"); + lib.defineCMacro("HAVE_MLOCK", "1"); + lib.defineCMacro("HAVE_MMAP", "1"); + lib.defineCMacro("HAVE_MPROTECT", "1"); + lib.defineCMacro("HAVE_NANOSLEEP", "1"); + lib.defineCMacro("HAVE_POSIX_MEMALIGN", "1"); + lib.defineCMacro("HAVE_PTHREAD", "1"); + lib.defineCMacro("HAVE_PTHREAD_PRIO_INHERIT", "1"); + lib.defineCMacro("HAVE_RAISE", "1"); + lib.defineCMacro("HAVE_SYSCONF", "1"); + lib.defineCMacro("HAVE_SYS_MMAN_H", "1"); + lib.defineCMacro("HAVE_SYS_PARAM_H", "1"); + lib.defineCMacro("HAVE_SYS_RANDOM_H", "1"); + lib.defineCMacro("HAVE_WEAK_SYMBOLS", "1"); + }, + .wasi => { + lib.defineCMacro("HAVE_ARC4RANDOM", "1"); + lib.defineCMacro("HAVE_ARC4RANDOM_BUF", "1"); + lib.defineCMacro("HAVE_CLOCK_GETTIME", "1"); + lib.defineCMacro("HAVE_GETENTROPY", "1"); + lib.defineCMacro("HAVE_NANOSLEEP", "1"); + lib.defineCMacro("HAVE_POSIX_MEMALIGN", "1"); + lib.defineCMacro("HAVE_SYS_AUXV_H", "1"); + lib.defineCMacro("HAVE_SYS_PARAM_H", "1"); + lib.defineCMacro("HAVE_SYS_RANDOM_H", "1"); + }, + else => {}, + } + + switch (target.result.cpu.arch) { + .x86_64 => { + lib.defineCMacro("HAVE_AMD64_ASM", "1"); + lib.defineCMacro("HAVE_AVX_ASM", "1"); + lib.defineCMacro("HAVE_CPUID", "1"); + lib.defineCMacro("HAVE_MMINTRIN_H", "1"); + lib.defineCMacro("HAVE_EMMINTRIN_H", "1"); + lib.defineCMacro("HAVE_PMMINTRIN_H", "1"); + lib.defineCMacro("HAVE_TMMINTRIN_H", "1"); + lib.defineCMacro("HAVE_SMMINTRIN_H", "1"); + lib.defineCMacro("HAVE_AVXINTRIN_H", "1"); + lib.defineCMacro("HAVE_AVX2INTRIN_H", "1"); + lib.defineCMacro("HAVE_AVX512FINTRIN_H", "1"); + lib.defineCMacro("HAVE_WMMINTRIN_H", "1"); + lib.defineCMacro("HAVE_RDRAND", "1"); + }, + .aarch64, .aarch64_be => { + lib.defineCMacro("HAVE_ARMCRYPTO", "1"); + }, + .wasm32, .wasm64 => { + lib.defineCMacro("__wasm__", "1"); + }, + else => {}, + } + + switch (target.result.os.tag) { + .wasi => { + lib.defineCMacro("__wasi__", "1"); + }, + else => {}, + } +} + pub fn build(b: *std.Build) !void { const root_path = b.pathFromRoot("."); var cwd = try fs.openDirAbsolute(root_path, .{}); @@ -29,17 +158,8 @@ pub fn build(b: *std.Build) !void { } switch (target.result.cpu.arch) { - // Features we assume are always available because they won't affect - // code generation in files that don't use them. - .x86_64 => { - target.query.cpu_features_add.addFeature(@intFromEnum(Target.x86.Feature.sse4_1)); - target.query.cpu_features_add.addFeature(@intFromEnum(Target.x86.Feature.aes)); - target.query.cpu_features_add.addFeature(@intFromEnum(Target.x86.Feature.pclmul)); - target.query.cpu_features_add.addFeature(@intFromEnum(Target.x86.Feature.rdrnd)); - }, - .aarch64, .aarch64_be => { - target.query.cpu_features_add.addFeature(@intFromEnum(Target.aarch64.Feature.crypto)); - // ARM CPUs supported by Windows also support NEON. + .aarch64, .aarch64_be, .aarch64_32 => { + // ARM CPUs supported by Windows are assumed to have NEON support if (target.result.isMinGW()) { target.query.cpu_features_add.addFeature(@intFromEnum(Target.aarch64.Feature.neon)); } @@ -48,12 +168,12 @@ pub fn build(b: *std.Build) !void { } const static_lib = b.addStaticLibrary(.{ - .name = "sodium", + .name = if (target.result.isMinGW()) "libsodium-static" else "sodium", .target = target, .optimize = optimize, }); const shared_lib = b.addSharedLibrary(.{ - .name = if (target.result.isMinGW()) "sodium_shared" else "sodium", + .name = if (target.result.isMinGW()) "libsodium" else "sodium", .target = target, .optimize = optimize, .strip = optimize != .Debug and !target.result.isMinGW(), @@ -85,164 +205,29 @@ pub fn build(b: *std.Build) !void { b.installArtifact(lib); lib.installHeader(.{ .path = src_path ++ "/include/sodium.h" }, "sodium.h"); lib.installHeadersDirectory(.{ .path = src_path ++ "/include/sodium" }, "sodium", .{}); - lib.linkLibC(); - - lib.addIncludePath(.{ .path = "src/libsodium/include/sodium" }); - lib.defineCMacro("_GNU_SOURCE", "1"); - lib.defineCMacro("CONFIGURED", "1"); - lib.defineCMacro("DEV_MODE", "1"); - lib.defineCMacro("HAVE_ATOMIC_OPS", "1"); - lib.defineCMacro("HAVE_C11_MEMORY_FENCES", "1"); - lib.defineCMacro("HAVE_CET_H", "1"); - lib.defineCMacro("HAVE_GCC_MEMORY_FENCES", "1"); - lib.defineCMacro("HAVE_INLINE_ASM", "1"); - lib.defineCMacro("HAVE_INTTYPES_H", "1"); - lib.defineCMacro("HAVE_STDINT_H", "1"); - lib.defineCMacro("HAVE_TI_MODE", "1"); - - const endian = target.result.cpu.arch.endian(); - switch (endian) { - .big => lib.defineCMacro("NATIVE_BIG_ENDIAN", "1"), - .little => lib.defineCMacro("NATIVE_LITTLE_ENDIAN", "1"), - } - switch (target.result.os.tag) { - .linux => { - lib.defineCMacro("ASM_HIDE_SYMBOL", ".hidden"); - lib.defineCMacro("TLS", "_Thread_local"); + initLibConfig(target, lib); - lib.defineCMacro("HAVE_CATCHABLE_ABRT", "1"); - lib.defineCMacro("HAVE_CATCHABLE_SEGV", "1"); - lib.defineCMacro("HAVE_CLOCK_GETTIME", "1"); - lib.defineCMacro("HAVE_GETPID", "1"); - lib.defineCMacro("HAVE_INLINE_ASM", "1"); - lib.defineCMacro("HAVE_MADVISE", "1"); - lib.defineCMacro("HAVE_MLOCK", "1"); - lib.defineCMacro("HAVE_MMAP", "1"); - lib.defineCMacro("HAVE_MPROTECT", "1"); - lib.defineCMacro("HAVE_NANOSLEEP", "1"); - lib.defineCMacro("HAVE_POSIX_MEMALIGN", "1"); - lib.defineCMacro("HAVE_PTHREAD_PRIO_INHERIT", "1"); - lib.defineCMacro("HAVE_PTHREAD", "1"); - lib.defineCMacro("HAVE_RAISE", "1"); - lib.defineCMacro("HAVE_SYSCONF", "1"); - lib.defineCMacro("HAVE_SYS_AUXV_H", "1"); - lib.defineCMacro("HAVE_SYS_MMAN_H", "1"); - lib.defineCMacro("HAVE_SYS_PARAM_H", "1"); - lib.defineCMacro("HAVE_SYS_RANDOM_H", "1"); - lib.defineCMacro("HAVE_WEAK_SYMBOLS", "1"); - }, - .windows => { - lib.defineCMacro("HAVE_RAISE", "1"); - lib.defineCMacro("HAVE_SYS_PARAM_H", "1"); - if (lib.isStaticLibrary()) { - lib.defineCMacro("SODIUM_STATIC", "1"); - } - }, - .macos => { - lib.defineCMacro("ASM_HIDE_SYMBOL", ".private_extern"); - lib.defineCMacro("TLS", "_Thread_local"); - - lib.defineCMacro("HAVE_ARC4RANDOM", "1"); - lib.defineCMacro("HAVE_ARC4RANDOM_BUF", "1"); - lib.defineCMacro("HAVE_CATCHABLE_ABRT", "1"); - lib.defineCMacro("HAVE_CATCHABLE_SEGV", "1"); - lib.defineCMacro("HAVE_CLOCK_GETTIME", "1"); - lib.defineCMacro("HAVE_GETENTROPY", "1"); - lib.defineCMacro("HAVE_GETPID", "1"); - lib.defineCMacro("HAVE_MADVISE", "1"); - lib.defineCMacro("HAVE_MEMSET_S", "1"); - lib.defineCMacro("HAVE_MLOCK", "1"); - lib.defineCMacro("HAVE_MMAP", "1"); - lib.defineCMacro("HAVE_MPROTECT", "1"); - lib.defineCMacro("HAVE_NANOSLEEP", "1"); - lib.defineCMacro("HAVE_POSIX_MEMALIGN", "1"); - lib.defineCMacro("HAVE_PTHREAD", "1"); - lib.defineCMacro("HAVE_PTHREAD_PRIO_INHERIT", "1"); - lib.defineCMacro("HAVE_RAISE", "1"); - lib.defineCMacro("HAVE_SYSCONF", "1"); - lib.defineCMacro("HAVE_SYS_MMAN_H", "1"); - lib.defineCMacro("HAVE_SYS_PARAM_H", "1"); - lib.defineCMacro("HAVE_SYS_RANDOM_H", "1"); - lib.defineCMacro("HAVE_WEAK_SYMBOLS", "1"); - }, - .wasi => { - lib.defineCMacro("HAVE_ARC4RANDOM", "1"); - lib.defineCMacro("HAVE_ARC4RANDOM_BUF", "1"); - lib.defineCMacro("HAVE_CLOCK_GETTIME", "1"); - lib.defineCMacro("HAVE_GETENTROPY", "1"); - lib.defineCMacro("HAVE_NANOSLEEP", "1"); - lib.defineCMacro("HAVE_POSIX_MEMALIGN", "1"); - lib.defineCMacro("HAVE_SYS_AUXV_H", "1"); - lib.defineCMacro("HAVE_SYS_PARAM_H", "1"); - lib.defineCMacro("HAVE_SYS_RANDOM_H", "1"); - }, - else => {}, - } - - switch (target.result.cpu.arch) { - .x86_64 => { - lib.defineCMacro("HAVE_AMD64_ASM", "1"); - lib.defineCMacro("HAVE_AVX_ASM", "1"); - lib.defineCMacro("HAVE_CPUID", "1"); - lib.defineCMacro("HAVE_MMINTRIN_H", "1"); - lib.defineCMacro("HAVE_EMMINTRIN_H", "1"); - - const cpu_features = target.result.cpu.features; - const has_sse3 = cpu_features.isEnabled(@intFromEnum(Target.x86.Feature.sse3)); - const has_ssse3 = cpu_features.isEnabled(@intFromEnum(Target.x86.Feature.ssse3)); - const has_sse4_1 = cpu_features.isEnabled(@intFromEnum(Target.x86.Feature.sse4_1)); - const has_avx = cpu_features.isEnabled(@intFromEnum(Target.x86.Feature.avx)); - const has_avx2 = cpu_features.isEnabled(@intFromEnum(Target.x86.Feature.avx2)); - const has_avx512f = cpu_features.isEnabled(@intFromEnum(Target.x86.Feature.avx512f)); - const has_aes = cpu_features.isEnabled(@intFromEnum(Target.x86.Feature.aes)); - const has_pclmul = cpu_features.isEnabled(@intFromEnum(Target.x86.Feature.pclmul)); - const has_rdrnd = cpu_features.isEnabled(@intFromEnum(Target.x86.Feature.rdrnd)); - - if (has_sse3) lib.defineCMacro("HAVE_PMMINTRIN_H", "1"); - if (has_ssse3) lib.defineCMacro("HAVE_TMMINTRIN_H", "1"); - if (has_sse4_1) lib.defineCMacro("HAVE_SMMINTRIN_H", "1"); - if (has_avx) lib.defineCMacro("HAVE_AVXINTRIN_H", "1"); - if (has_avx2) lib.defineCMacro("HAVE_AVX2INTRIN_H", "1"); - if (has_avx512f) lib.defineCMacro("HAVE_AVX512FINTRIN_H", "1"); - if (has_aes and has_pclmul) lib.defineCMacro("HAVE_WMMINTRIN_H", "1"); - if (has_rdrnd) lib.defineCMacro("HAVE_RDRAND", "1"); - }, - .aarch64, .aarch64_be => { - const cpu_features = target.result.cpu.features; - const has_neon = cpu_features.isEnabled(@intFromEnum(Target.aarch64.Feature.neon)); - const has_crypto = cpu_features.isEnabled(@intFromEnum(Target.aarch64.Feature.crypto)); - if (has_neon and has_crypto) { - lib.defineCMacro("HAVE_ARMCRYPTO", "1"); - } - }, - .wasm32, .wasm64 => { - lib.defineCMacro("__wasm__", "1"); - }, - else => {}, - } - - switch (target.result.os.tag) { - .wasi => { - lib.defineCMacro("__wasi__", "1"); - }, - else => {}, - } + const flags = &.{ + "-fvisibility=hidden", + "-fno-strict-aliasing", + "-fno-strict-overflow", + "-fwrapv", + "-flax-vector-conversions", + }; const allocator = heap.page_allocator; + var walker = try src_dir.walk(allocator); while (try walker.next()) |entry| { const name = entry.basename; if (mem.endsWith(u8, name, ".c")) { const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ src_path, entry.path }); - const flags = &.{ - "-fvisibility=hidden", - "-fno-strict-aliasing", - "-fno-strict-overflow", - "-fwrapv", - "-flax-vector-conversions", - }; - lib.addCSourceFiles(.{ .files = &.{full_path}, .flags = flags }); + + lib.addCSourceFiles(.{ + .files = &.{full_path}, + .flags = flags, + }); } else if (mem.endsWith(u8, name, ".S")) { const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ src_path, entry.path }); lib.addAssemblyFile(.{ .path = full_path }); diff --git a/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c b/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c index fe755a5ac3..b2ac748d86 100644 --- a/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c +++ b/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c @@ -16,9 +16,11 @@ #if defined(HAVE_TMMINTRIN_H) && defined(HAVE_WMMINTRIN_H) -#ifdef __GNUC__ -#pragma GCC target("avx,aes,pclmul") -#endif +# ifdef __clang__ +# pragma clang attribute push(__attribute__((target("aes,avx,pclmul"))), apply_to = function) +# elif defined(__GNUC__) +# pragma GCC target("aes,avx,pclmul") +# endif #if !defined(_MSC_VER) || _MSC_VER < 1800 #define __vectorcall @@ -1006,4 +1008,8 @@ crypto_aead_aes256gcm_is_available(void) return sodium_runtime_has_pclmul() & sodium_runtime_has_aesni() & sodium_runtime_has_avx(); } +#ifdef __clang__ +# pragma clang attribute pop +#endif + #endif diff --git a/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-avx2.c b/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-avx2.c index 4945462d4f..3152e286a1 100644 --- a/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-avx2.c +++ b/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-avx2.c @@ -12,11 +12,10 @@ #if defined(HAVE_AVX2INTRIN_H) && defined(HAVE_EMMINTRIN_H) && \ defined(HAVE_TMMINTRIN_H) && defined(HAVE_SMMINTRIN_H) -# ifdef __GNUC__ -# pragma GCC target("sse2") -# pragma GCC target("ssse3") -# pragma GCC target("sse4.1") -# pragma GCC target("avx2") +# ifdef __clang__ +# pragma clang attribute push(__attribute__((target("sse2,ssse3,sse4.1,avx2"))), apply_to = function) +# elif defined(__GNUC__) +# pragma GCC target("sse2,ssse3,sse4.1,avx2") # endif # include @@ -46,4 +45,8 @@ blake2b_compress_avx2(blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES]) return 0; } +# ifdef __clang__ +# pragma clang attribute pop +# endif + #endif diff --git a/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-sse41.c b/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-sse41.c index f085c6153e..ecab71644b 100644 --- a/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-sse41.c +++ b/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-sse41.c @@ -11,10 +11,10 @@ #if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) && \ defined(HAVE_SMMINTRIN_H) -# ifdef __GNUC__ -# pragma GCC target("sse2") -# pragma GCC target("ssse3") -# pragma GCC target("sse4.1") +# ifdef __clang__ +# pragma clang attribute push(__attribute__((target("sse2,ssse3,sse4.1"))), apply_to = function) +# elif defined(__GNUC__) +# pragma GCC target("sse2,ssse3,sse4.1") # endif # include @@ -84,4 +84,8 @@ blake2b_compress_sse41(blake2b_state *S, return 0; } +# ifdef __clang__ +# pragma clang attribute pop +# endif + #endif diff --git a/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-ssse3.c b/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-ssse3.c index 6372da03e6..8d0e3e9daf 100644 --- a/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-ssse3.c +++ b/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-ssse3.c @@ -7,9 +7,10 @@ #if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) -# ifdef __GNUC__ -# pragma GCC target("sse2") -# pragma GCC target("ssse3") +# ifdef __clang__ +# pragma clang attribute push(__attribute__((target("sse2,ssse3"))), apply_to = function) +# elif defined(__GNUC__) +# pragma GCC target("sse2,ssse3") # endif # include @@ -87,4 +88,8 @@ blake2b_compress_ssse3(blake2b_state *S, return 0; } +# ifdef __clang__ +# pragma clang attribute pop +# endif + #endif diff --git a/src/libsodium/crypto_onetimeauth/poly1305/sse2/poly1305_sse2.c b/src/libsodium/crypto_onetimeauth/poly1305/sse2/poly1305_sse2.c index 24859284ed..0576c86ef1 100644 --- a/src/libsodium/crypto_onetimeauth/poly1305/sse2/poly1305_sse2.c +++ b/src/libsodium/crypto_onetimeauth/poly1305/sse2/poly1305_sse2.c @@ -10,7 +10,9 @@ #if defined(HAVE_TI_MODE) && defined(HAVE_EMMINTRIN_H) -# ifdef __GNUC__ +# ifdef __clang__ +# pragma clang attribute push(__attribute__((target("sse2"))), apply_to = function) +# elif defined(__GNUC__) # pragma GCC target("sse2") # endif @@ -946,4 +948,8 @@ struct crypto_onetimeauth_poly1305_implementation SODIUM_C99(.onetimeauth_final =) crypto_onetimeauth_poly1305_sse2_final }; +#ifdef __clang__ +# pragma clang attribute pop +#endif + #endif diff --git a/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-avx2.c b/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-avx2.c index a35e1f9107..5c7956632b 100644 --- a/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-avx2.c +++ b/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-avx2.c @@ -22,11 +22,10 @@ #if defined(HAVE_AVX2INTRIN_H) && defined(HAVE_EMMINTRIN_H) && \ defined(HAVE_TMMINTRIN_H) && defined(HAVE_SMMINTRIN_H) -# ifdef __GNUC__ -# pragma GCC target("sse2") -# pragma GCC target("ssse3") -# pragma GCC target("sse4.1") -# pragma GCC target("avx2") +# ifdef __clang__ +# pragma clang attribute push(__attribute__((target("sse2,ssse3,sse4.1,avx2"))), apply_to = function) +# elif defined(__GNUC__) +# pragma GCC target("sse2,ssse3,sse4.1,avx2") # endif # ifdef _MSC_VER @@ -236,4 +235,9 @@ argon2_fill_segment_avx2(const argon2_instance_t *instance, } } } + +#ifdef __clang__ +# pragma clang attribute pop +#endif + #endif diff --git a/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-avx512f.c b/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-avx512f.c index 6566804ece..033f7c21cd 100644 --- a/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-avx512f.c +++ b/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-avx512f.c @@ -22,12 +22,10 @@ #if defined(HAVE_AVX512FINTRIN_H) && defined(HAVE_AVX2INTRIN_H) && \ defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) && defined(HAVE_SMMINTRIN_H) -# ifdef __GNUC__ -# pragma GCC target("sse2") -# pragma GCC target("ssse3") -# pragma GCC target("sse4.1") -# pragma GCC target("avx2") -# pragma GCC target("avx512f") +# ifdef __clang__ +# pragma clang attribute push(__attribute__((target("sse2,ssse3,sse4.1,avx2,avx512f"))), apply_to = function) +# elif defined(__GNUC__) +# pragma GCC target("sse2,ssse3,sse4.1,avx2,avx512f") # endif # ifdef _MSC_VER @@ -241,4 +239,9 @@ argon2_fill_segment_avx512f(const argon2_instance_t *instance, } } } + +#ifdef __clang__ +# pragma clang attribute pop +#endif + #endif diff --git a/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-ssse3.c b/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-ssse3.c index fc85a4787f..1930bc35f3 100644 --- a/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-ssse3.c +++ b/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-ssse3.c @@ -21,9 +21,10 @@ #if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) -# ifdef __GNUC__ -# pragma GCC target("sse2") -# pragma GCC target("ssse3") +# ifdef __clang__ +# pragma clang attribute push(__attribute__((target("sse2,ssse3"))), apply_to = function) +# elif defined(__GNUC__) +# pragma GCC target("sse2,ssse3") # endif # ifdef _MSC_VER @@ -235,4 +236,9 @@ argon2_fill_segment_ssse3(const argon2_instance_t *instance, } } } + +#ifdef __clang__ +# pragma clang attribute pop +#endif + #endif diff --git a/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c b/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c index c3ea0a3baa..89fe8aec87 100644 --- a/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c +++ b/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c @@ -38,13 +38,14 @@ #ifdef HAVE_EMMINTRIN_H -# ifdef __GNUC__ +# ifdef __clang__ +# pragma clang attribute push(__attribute__((target("sse2"))), apply_to = function) +# elif defined(__GNUC__) # pragma GCC target("sse2") # endif + # include -# if defined(__XOP__) && defined(DISABLED) -# include -# endif + # include "private/sse2_64_32.h" # include "../crypto_scrypt.h" @@ -397,4 +398,9 @@ escrypt_kdf_sse(escrypt_local_t *local, const uint8_t *passwd, size_t passwdlen, /* Success! */ return 0; } + +# ifdef __clang__ +# pragma clang attribute pop +# endif + #endif diff --git a/src/libsodium/crypto_stream/chacha20/dolbeau/chacha20_dolbeau-avx2.c b/src/libsodium/crypto_stream/chacha20/dolbeau/chacha20_dolbeau-avx2.c index 610005004d..2bf2250b59 100644 --- a/src/libsodium/crypto_stream/chacha20/dolbeau/chacha20_dolbeau-avx2.c +++ b/src/libsodium/crypto_stream/chacha20/dolbeau/chacha20_dolbeau-avx2.c @@ -11,11 +11,10 @@ #if defined(HAVE_AVX2INTRIN_H) && defined(HAVE_EMMINTRIN_H) && \ defined(HAVE_TMMINTRIN_H) && defined(HAVE_SMMINTRIN_H) -# ifdef __GNUC__ -# pragma GCC target("sse2") -# pragma GCC target("ssse3") -# pragma GCC target("sse4.1") -# pragma GCC target("avx2") +# ifdef __clang__ +# pragma clang attribute push(__attribute__((target("sse2,ssse3,sse4.1,avx2"))), apply_to = function) +# elif defined(__GNUC__) +# pragma GCC target("sse2,ssse3,sse4.1,avx2") # endif # include @@ -174,4 +173,8 @@ struct crypto_stream_chacha20_implementation SODIUM_C99(.stream_ietf_ext_xor_ic =) stream_ietf_ext_ref_xor_ic }; +# ifdef __clang__ +# pragma clang attribute pop +# endif + #endif diff --git a/src/libsodium/crypto_stream/chacha20/dolbeau/chacha20_dolbeau-ssse3.c b/src/libsodium/crypto_stream/chacha20/dolbeau/chacha20_dolbeau-ssse3.c index ad13c3af11..eb52bda62c 100644 --- a/src/libsodium/crypto_stream/chacha20/dolbeau/chacha20_dolbeau-ssse3.c +++ b/src/libsodium/crypto_stream/chacha20/dolbeau/chacha20_dolbeau-ssse3.c @@ -10,9 +10,10 @@ #if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) -# ifdef __GNUC__ -# pragma GCC target("sse2") -# pragma GCC target("ssse3") +# ifdef __clang__ +# pragma clang attribute push(__attribute__((target("sse2,ssse3"))), apply_to = function) +# elif defined(__GNUC__) +# pragma GCC target("sse2,ssse3") # endif # include @@ -168,4 +169,8 @@ struct crypto_stream_chacha20_implementation SODIUM_C99(.stream_ietf_ext_xor_ic =) stream_ietf_ext_ref_xor_ic }; +# ifdef __clang__ +# pragma clang attribute pop +# endif + #endif diff --git a/src/libsodium/crypto_stream/salsa20/xmm6int/salsa20_xmm6int-avx2.c b/src/libsodium/crypto_stream/salsa20/xmm6int/salsa20_xmm6int-avx2.c index 507d7fed5b..87789bb49a 100644 --- a/src/libsodium/crypto_stream/salsa20/xmm6int/salsa20_xmm6int-avx2.c +++ b/src/libsodium/crypto_stream/salsa20/xmm6int/salsa20_xmm6int-avx2.c @@ -10,18 +10,17 @@ #if defined(HAVE_AVX2INTRIN_H) && defined(HAVE_EMMINTRIN_H) && \ defined(HAVE_TMMINTRIN_H) && defined(HAVE_SMMINTRIN_H) -# ifdef __GNUC__ -# pragma GCC target("sse2") -# pragma GCC target("ssse3") -# pragma GCC target("sse4.1") -# pragma GCC target("avx2") +# ifdef __clang__ +# pragma clang attribute push(__attribute__((target("sse2,ssse3,sse4.1,avx2"))), apply_to = function) +# elif defined(__GNUC__) +# pragma GCC target("sse2,ssse3,sse4.1,avx2") # endif -#include -#include -#include -#include -#include "private/sse2_64_32.h" +# include +# include +# include +# include +# include "private/sse2_64_32.h" # include "../stream_salsa20.h" # include "salsa20_xmm6int-avx2.h" @@ -128,4 +127,8 @@ struct crypto_stream_salsa20_implementation SODIUM_C99(.stream_xor_ic =) stream_avx2_xor_ic }; +#ifdef __clang__ +# pragma clang attribute pop +#endif + #endif diff --git a/src/libsodium/crypto_stream/salsa20/xmm6int/salsa20_xmm6int-sse2.c b/src/libsodium/crypto_stream/salsa20/xmm6int/salsa20_xmm6int-sse2.c index 16cca0dafe..32ef354de4 100644 --- a/src/libsodium/crypto_stream/salsa20/xmm6int/salsa20_xmm6int-sse2.c +++ b/src/libsodium/crypto_stream/salsa20/xmm6int/salsa20_xmm6int-sse2.c @@ -9,7 +9,9 @@ #ifdef HAVE_EMMINTRIN_H -# ifdef __GNUC__ +# ifdef __clang__ +# pragma clang attribute push(__attribute__((target("sse2"))), apply_to = function) +# elif defined(__GNUC__) # pragma GCC target("sse2") # endif # include @@ -119,4 +121,8 @@ struct crypto_stream_salsa20_implementation SODIUM_C99(.stream_xor_ic =) stream_sse2_xor_ic }; +#ifdef __clang__ +# pragma clang attribute pop +#endif + #endif diff --git a/src/libsodium/crypto_verify/verify.c b/src/libsodium/crypto_verify/verify.c index 2dfa7121bd..c79d4aaf22 100644 --- a/src/libsodium/crypto_verify/verify.c +++ b/src/libsodium/crypto_verify/verify.c @@ -27,9 +27,6 @@ crypto_verify_64_bytes(void) #if defined(HAVE_EMMINTRIN_H) && defined(__SSE2__) -# ifdef __GNUC__ -# pragma GCC target("sse2") -# endif # include static inline int diff --git a/src/libsodium/randombytes/internal/randombytes_internal_random.c b/src/libsodium/randombytes/internal/randombytes_internal_random.c index ecb7c08a3e..00bb703380 100644 --- a/src/libsodium/randombytes/internal/randombytes_internal_random.c +++ b/src/libsodium/randombytes/internal/randombytes_internal_random.c @@ -50,7 +50,11 @@ # include #endif #ifdef HAVE_RDRAND -# pragma GCC target("rdrnd") +# ifdef __clang__ +# pragma clang attribute push(__attribute__((target("rdrnd"))), apply_to = function) +# elif defined(__GNUC__) +# pragma GCC target("rdrnd") +# endif # include #endif @@ -655,3 +659,9 @@ struct randombytes_implementation randombytes_internal_implementation = { SODIUM_C99(.buf =) randombytes_internal_random_buf, SODIUM_C99(.close =) randombytes_internal_random_close }; + +#ifdef HAVE_RDRAND +# ifdef __clang__ +# pragma clang attribute pop +# endif +#endif