diff --git a/build.zig b/build.zig index 0033c88bc7..d2c4b0ba7f 100644 --- a/build.zig +++ b/build.zig @@ -208,129 +208,7 @@ pub fn build(b: *std.Build) !void { initLibConfig(target, lib); - const MFlags = enum { - sse2, - ssse3, - sse41, - avx, - avx2, - avx512f, - aes, - pclmul, - rdrnd, - crypto, - - fn f(flag: @This()) Target.Cpu.Feature.Set.Index { - return switch (flag) { - .sse2 => @intFromEnum(Target.x86.Feature.sse2), - .ssse3 => @intFromEnum(Target.x86.Feature.ssse3), - .sse41 => @intFromEnum(Target.x86.Feature.sse4_1), - .avx => @intFromEnum(Target.x86.Feature.avx), - .avx2 => @intFromEnum(Target.x86.Feature.avx2), - .avx512f => @intFromEnum(Target.x86.Feature.avx512f), - .aes => @intFromEnum(Target.x86.Feature.aes), - .pclmul => @intFromEnum(Target.x86.Feature.pclmul), - .rdrnd => @intFromEnum(Target.x86.Feature.rdrnd), - .crypto => @intFromEnum(Target.aarch64.Feature.crypto), - }; - } - }; - - const MLib = struct { - name: []const u8, - count: usize, - sources: []const []const u8, - flags: []const MFlags, - arches: []const std.Target.Cpu.Arch, - lib: *Compile = undefined, - }; - - var mlibs: [8]MLib = .{ - .{ - .name = "armcrypto", - .count = 3, - .sources = &.{ - "crypto_aead/aegis128l/aegis128l_armcrypto.c", - "crypto_aead/aegis256/aegis256_armcrypto.c", - "crypto_aead/aes256gcm/armcrypto/aead_aes256gcm_armcrypto.c", - }, - .flags = &.{.aes}, - .arches = &.{ .aarch64, .aarch64_be, .aarch64_32 }, - }, - - .{ - .name = "sse2", - .count = 1, - .sources = &.{ - "crypto_stream/salsa20/xmm6int/salsa20_xmm6int-sse2.c", - }, - .flags = &.{.sse2}, - .arches = &.{ .x86_64, .x86 }, - }, - .{ - .name = "ssse3", - .count = 3, - .sources = &.{ - "crypto_generichash/blake2b/ref/blake2b-compress-ssse3.c", - "crypto_pwhash/argon2/argon2-fill-block-ssse3.c", - "crypto_stream/chacha20/dolbeau/chacha20_dolbeau-ssse3.c", - }, - .flags = &.{ .sse2, .ssse3 }, - .arches = &.{ .x86_64, .x86 }, - }, - .{ - .name = "sse41", - .count = 1, - .sources = &.{ - "crypto_generichash/blake2b/ref/blake2b-compress-sse41.c", - }, - .flags = &.{ .sse2, .ssse3, .sse41 }, - .arches = &.{ .x86_64, .x86 }, - }, - .{ - .name = "avx2", - .count = 4, - .sources = &.{ - "crypto_generichash/blake2b/ref/blake2b-compress-avx2.c", - "crypto_pwhash/argon2/argon2-fill-block-avx2.c", - "crypto_stream/chacha20/dolbeau/chacha20_dolbeau-avx2.c", - "crypto_stream/salsa20/xmm6int/salsa20_xmm6int-avx2.c", - }, - .flags = &.{ .sse2, .ssse3, .sse41, .avx, .avx2 }, - .arches = &.{.x86_64}, - }, - .{ - .name = "avx512f", - .count = 1, - .sources = &.{ - "crypto_pwhash/argon2/argon2-fill-block-avx512f.c", - }, - .flags = &.{ .sse2, .ssse3, .sse41, .avx, .avx2, .avx512f }, - .arches = &.{.x86_64}, - }, - .{ - .name = "aesni", - .count = 3, - .sources = &.{ - "crypto_aead/aegis128l/aegis128l_aesni.c", - "crypto_aead/aegis256/aegis256_aesni.c", - "crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c", - }, - .flags = &.{ .avx, .aes, .pclmul }, - .arches = &.{.x86_64}, - }, - .{ - .name = "mrdrnd", - .count = 1, - .sources = &.{ - "randombytes/internal/randombytes_internal_random.c", - }, - .flags = &.{.rdrnd}, - .arches = &.{ .x86_64, .x86 }, - }, - }; - - const base_flags = &.{ + const flags = &.{ "-fvisibility=hidden", "-fno-strict-aliasing", "-fno-strict-overflow", @@ -340,61 +218,15 @@ pub fn build(b: *std.Build) !void { const allocator = heap.page_allocator; - // compile CPU-specific library code - for (&mlibs) |*mlib| { - var target2 = target; - for (mlib.arches) |arch| { - if (target.result.cpu.arch == arch) { - for (mlib.flags) |flag| { - target2.query.cpu_features_add.addFeature(flag.f()); - } - break; - } - } - - mlib.lib = b.addStaticLibrary(.{ - .name = mlib.name, - .target = target2, - .optimize = optimize, - }); - const elib = mlib.lib; - initLibConfig(target, elib); - - var flags = std.ArrayList([]const u8).init(allocator); - defer flags.deinit(); - try flags.appendSlice(base_flags); - - for (mlib.sources) |path| { - const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ src_path, path }); - elib.addCSourceFiles(.{ - .files = &.{full_path}, - .flags = flags.items, - }); - } - lib.linkLibrary(elib); - } - - // compile generic library code var walker = try src_dir.walk(allocator); - files: while (try walker.next()) |entry| { - var flags = std.ArrayList([]const u8).init(allocator); - defer flags.deinit(); - try flags.appendSlice(base_flags); - + while (try walker.next()) |entry| { const name = entry.basename; - if (mem.endsWith(u8, name, ".c")) { - for (mlibs) |mlib| { - for (mlib.sources) |path| { - if (mem.eql(u8, entry.path, path)) continue :files; - } - } - const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ src_path, entry.path }); lib.addCSourceFiles(.{ .files = &.{full_path}, - .flags = flags.items, + .flags = flags, }); } else if (mem.endsWith(u8, name, ".S")) { const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ src_path, entry.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 91e578df72..8dc9f840fa 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 ffebf220a0..0c3323a50b 100644 --- a/src/libsodium/crypto_verify/verify.c +++ b/src/libsodium/crypto_verify/verify.c @@ -26,9 +26,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 1176cd6677..d39063c5b0 100644 --- a/src/libsodium/randombytes/internal/randombytes_internal_random.c +++ b/src/libsodium/randombytes/internal/randombytes_internal_random.c @@ -45,7 +45,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 @@ -633,3 +637,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