Skip to content

Commit

Permalink
Merge pull request #9 from mfrischknecht/fix-rust-cross-mingw-compila…
Browse files Browse the repository at this point in the history
…tion

Add/expand compile guards for VAES (fixing MinGW cross compilation)
  • Loading branch information
jedisct1 authored Apr 29, 2024
2 parents a71b0ba + 3ce51b1 commit c88c73c
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 60 deletions.
2 changes: 2 additions & 0 deletions src/aegis128x2/aegis128x2.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,12 @@ aegis128x2_pick_best_implementation(void)
#endif

#if defined(__x86_64__) || defined(_M_AMD64) || defined(__i386__) || defined(_M_IX86)
# ifdef HAVE_VAESINTRIN_H
if (aegis_runtime_has_vaes() && aegis_runtime_has_avx2()) {
implementation = &aegis128x2_avx2_implementation;
return 0;
}
# endif
if (aegis_runtime_has_aesni() && aegis_runtime_has_avx()) {
implementation = &aegis128x2_aesni_implementation;
return 0;
Expand Down
40 changes: 22 additions & 18 deletions src/aegis128x2/aegis128x2_avx2.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,28 @@
# include "aegis128x2.h"
# include "aegis128x2_avx2.h"

# ifdef __clang__
# pragma clang attribute push(__attribute__((target("vaes,avx2"))), apply_to = function)
# elif defined(__GNUC__)
# pragma GCC target("vaes,avx2")
# endif
# ifdef HAVE_VAESINTRIN_H

# ifdef __clang__
# pragma clang attribute push(__attribute__((target("vaes,avx2"))), apply_to = function)
# elif defined(__GNUC__)
# pragma GCC target("vaes,avx2")
# endif

# include <immintrin.h>
# include <immintrin.h>

# define AES_BLOCK_LENGTH 32
# define AES_BLOCK_LENGTH 32

typedef __m256i aes_block_t;

# define AES_BLOCK_XOR(A, B) _mm256_xor_si256((A), (B))
# define AES_BLOCK_AND(A, B) _mm256_and_si256((A), (B))
# define AES_BLOCK_LOAD128_BROADCAST(A) \
_mm256_broadcastsi128_si256(_mm_loadu_si128((const void *) (A)))
# define AES_BLOCK_LOAD(A) _mm256_loadu_si256((const aes_block_t *) (const void *) (A))
# define AES_BLOCK_LOAD_64x2(A, B) _mm256_broadcastsi128_si256(_mm_set_epi64x((A), (B)))
# define AES_BLOCK_STORE(A, B) _mm256_storeu_si256((aes_block_t *) (void *) (A), (B))
# define AES_ENC(A, B) _mm256_aesenc_epi128((A), (B))
# define AES_BLOCK_XOR(A, B) _mm256_xor_si256((A), (B))
# define AES_BLOCK_AND(A, B) _mm256_and_si256((A), (B))
# define AES_BLOCK_LOAD128_BROADCAST(A) \
_mm256_broadcastsi128_si256(_mm_loadu_si128((const void *) (A)))
# define AES_BLOCK_LOAD(A) _mm256_loadu_si256((const aes_block_t *) (const void *) (A))
# define AES_BLOCK_LOAD_64x2(A, B) _mm256_broadcastsi128_si256(_mm_set_epi64x((A), (B)))
# define AES_BLOCK_STORE(A, B) _mm256_storeu_si256((aes_block_t *) (void *) (A), (B))
# define AES_ENC(A, B) _mm256_aesenc_epi128((A), (B))

static inline void
aegis128x2_update(aes_block_t *const state, const aes_block_t d1, const aes_block_t d2)
Expand All @@ -50,7 +52,7 @@ aegis128x2_update(aes_block_t *const state, const aes_block_t d1, const aes_bloc
state[4] = AES_BLOCK_XOR(state[4], d2);
}

# include "aegis128x2_common.h"
# include "aegis128x2_common.h"

struct aegis128x2_implementation aegis128x2_avx2_implementation = {
.encrypt_detached = encrypt_detached,
Expand All @@ -66,8 +68,10 @@ struct aegis128x2_implementation aegis128x2_avx2_implementation = {
.state_decrypt_detached_final = state_decrypt_detached_final,
};

# ifdef __clang__
# pragma clang attribute pop
# ifdef __clang__
# pragma clang attribute pop
# endif

# endif

#endif
2 changes: 2 additions & 0 deletions src/aegis128x2/aegis128x2_avx2.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "../common/common.h"
#include "implementations.h"

#ifdef HAVE_VAESINTRIN_H
extern struct aegis128x2_implementation aegis128x2_avx2_implementation;
#endif

#endif
2 changes: 1 addition & 1 deletion src/aegis128x4/aegis128x4.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@ aegis128x4_pick_best_implementation(void)
implementation = &aegis128x4_avx512_implementation;
return 0;
}
# endif
if (aegis_runtime_has_vaes() && aegis_runtime_has_avx2()) {
implementation = &aegis128x4_avx2_implementation;
return 0;
}
# endif
if (aegis_runtime_has_aesni() && aegis_runtime_has_avx()) {
implementation = &aegis128x4_aesni_implementation;
return 0;
Expand Down
26 changes: 15 additions & 11 deletions src/aegis128x4/aegis128x4_avx2.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
# include "aegis128x4.h"
# include "aegis128x4_avx2.h"

# ifdef __clang__
# pragma clang attribute push(__attribute__((target("vaes,avx2"))), apply_to = function)
# elif defined(__GNUC__)
# pragma GCC target("vaes,avx2")
# endif
# ifdef HAVE_VAESINTRIN_H

# ifdef __clang__
# pragma clang attribute push(__attribute__((target("vaes,avx2"))), apply_to = function)
# elif defined(__GNUC__)
# pragma GCC target("vaes,avx2")
# endif

# include <immintrin.h>
# include <immintrin.h>

# define AES_BLOCK_LENGTH 64
# define AES_BLOCK_LENGTH 64

typedef struct {
__m256i b0;
Expand Down Expand Up @@ -83,7 +85,7 @@ aegis128x4_update(aes_block_t *const state, const aes_block_t d1, const aes_bloc
state[4] = AES_BLOCK_XOR(state[4], d2);
}

# include "aegis128x4_common.h"
# include "aegis128x4_common.h"

struct aegis128x4_implementation aegis128x4_avx2_implementation = {
.encrypt_detached = encrypt_detached,
Expand All @@ -99,8 +101,10 @@ struct aegis128x4_implementation aegis128x4_avx2_implementation = {
.state_decrypt_detached_final = state_decrypt_detached_final,
};

# ifdef __clang__
# pragma clang attribute pop
# ifdef __clang__
# pragma clang attribute pop
# endif

# endif

#endif
#endif
2 changes: 2 additions & 0 deletions src/aegis128x4/aegis128x4_avx2.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "../common/common.h"
#include "implementations.h"

#ifdef HAVE_VAESINTRIN_H
extern struct aegis128x4_implementation aegis128x4_avx2_implementation;
#endif

#endif
2 changes: 2 additions & 0 deletions src/aegis256x2/aegis256x2.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,12 @@ aegis256x2_pick_best_implementation(void)
#endif

#if defined(__x86_64__) || defined(_M_AMD64) || defined(__i386__) || defined(_M_IX86)
# ifdef HAVE_VAESINTRIN_H
if (aegis_runtime_has_vaes() && aegis_runtime_has_avx2()) {
implementation = &aegis256x2_avx2_implementation;
return 0;
}
# endif
if (aegis_runtime_has_aesni() && aegis_runtime_has_avx()) {
implementation = &aegis256x2_aesni_implementation;
return 0;
Expand Down
42 changes: 23 additions & 19 deletions src/aegis256x2/aegis256x2_avx2.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,28 @@
# include "aegis256x2.h"
# include "aegis256x2_avx2.h"

# ifdef __clang__
# pragma clang attribute push(__attribute__((target("vaes,avx2"))), apply_to = function)
# elif defined(__GNUC__)
# pragma GCC target("vaes,avx2")
# endif
# ifdef HAVE_VAESINTRIN_H

# ifdef __clang__
# pragma clang attribute push(__attribute__((target("vaes,avx2"))), apply_to = function)
# elif defined(__GNUC__)
# pragma GCC target("vaes,avx2")
# endif

# include <immintrin.h>
# include <immintrin.h>

# define AES_BLOCK_LENGTH 32
# define AES_BLOCK_LENGTH 32

typedef __m256i aes_block_t;

# define AES_BLOCK_XOR(A, B) _mm256_xor_si256((A), (B))
# define AES_BLOCK_AND(A, B) _mm256_and_si256((A), (B))
# define AES_BLOCK_LOAD128_BROADCAST(A) \
_mm256_broadcastsi128_si256(_mm_loadu_si128((const void *) (A)))
# define AES_BLOCK_LOAD(A) _mm256_loadu_si256((const aes_block_t *) (const void *) (A))
# define AES_BLOCK_LOAD_64x2(A, B) _mm256_broadcastsi128_si256(_mm_set_epi64x((A), (B)))
# define AES_BLOCK_STORE(A, B) _mm256_storeu_si256((aes_block_t *) (void *) (A), (B))
# define AES_ENC(A, B) _mm256_aesenc_epi128((A), (B))
# define AES_BLOCK_XOR(A, B) _mm256_xor_si256((A), (B))
# define AES_BLOCK_AND(A, B) _mm256_and_si256((A), (B))
# define AES_BLOCK_LOAD128_BROADCAST(A) \
_mm256_broadcastsi128_si256(_mm_loadu_si128((const void *) (A)))
# define AES_BLOCK_LOAD(A) _mm256_loadu_si256((const aes_block_t *) (const void *) (A))
# define AES_BLOCK_LOAD_64x2(A, B) _mm256_broadcastsi128_si256(_mm_set_epi64x((A), (B)))
# define AES_BLOCK_STORE(A, B) _mm256_storeu_si256((aes_block_t *) (void *) (A), (B))
# define AES_ENC(A, B) _mm256_aesenc_epi128((A), (B))

static inline void
aegis256x2_update(aes_block_t *const state, const aes_block_t d)
Expand All @@ -45,7 +47,7 @@ aegis256x2_update(aes_block_t *const state, const aes_block_t d)
state[0] = AES_BLOCK_XOR(AES_ENC(tmp, state[0]), d);
}

# include "aegis256x2_common.h"
# include "aegis256x2_common.h"

struct aegis256x2_implementation aegis256x2_avx2_implementation = {
.encrypt_detached = encrypt_detached,
Expand All @@ -61,8 +63,10 @@ struct aegis256x2_implementation aegis256x2_avx2_implementation = {
.state_decrypt_detached_final = state_decrypt_detached_final,
};

# ifdef __clang__
# pragma clang attribute pop
# ifdef __clang__
# pragma clang attribute pop
# endif

# endif

#endif
#endif
2 changes: 2 additions & 0 deletions src/aegis256x2/aegis256x2_avx2.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "../common/common.h"
#include "implementations.h"

#ifdef HAVE_VAESINTRIN_H
extern struct aegis256x2_implementation aegis256x2_avx2_implementation;
#endif

#endif
2 changes: 1 addition & 1 deletion src/aegis256x4/aegis256x4.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@ aegis256x4_pick_best_implementation(void)
implementation = &aegis256x4_avx512_implementation;
return 0;
}
# endif
if (aegis_runtime_has_vaes() && aegis_runtime_has_avx2()) {
implementation = &aegis256x4_avx2_implementation;
return 0;
}
# endif
if (aegis_runtime_has_aesni() && aegis_runtime_has_avx()) {
implementation = &aegis256x4_aesni_implementation;
return 0;
Expand Down
24 changes: 14 additions & 10 deletions src/aegis256x4/aegis256x4_avx2.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
# include "aegis256x4.h"
# include "aegis256x4_avx2.h"

# ifdef __clang__
# pragma clang attribute push(__attribute__((target("vaes,avx2"))), apply_to = function)
# elif defined(__GNUC__)
# pragma GCC target("vaes,avx2")
# endif
# ifdef HAVE_VAESINTRIN_H

# ifdef __clang__
# pragma clang attribute push(__attribute__((target("vaes,avx2"))), apply_to = function)
# elif defined(__GNUC__)
# pragma GCC target("vaes,avx2")
# endif

# include <immintrin.h>
# include <immintrin.h>

# define AES_BLOCK_LENGTH 64
# define AES_BLOCK_LENGTH 64

typedef struct {
__m256i b0;
Expand Down Expand Up @@ -78,7 +80,7 @@ aegis256x4_update(aes_block_t *const state, const aes_block_t d)
state[0] = AES_BLOCK_XOR(AES_ENC(tmp, state[0]), d);
}

# include "aegis256x4_common.h"
# include "aegis256x4_common.h"

struct aegis256x4_implementation aegis256x4_avx2_implementation = {
.encrypt_detached = encrypt_detached,
Expand All @@ -94,8 +96,10 @@ struct aegis256x4_implementation aegis256x4_avx2_implementation = {
.state_decrypt_detached_final = state_decrypt_detached_final,
};

# ifdef __clang__
# pragma clang attribute pop
# ifdef __clang__
# pragma clang attribute pop
# endif

# endif

#endif
2 changes: 2 additions & 0 deletions src/aegis256x4/aegis256x4_avx2.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "../common/common.h"
#include "implementations.h"

#ifdef HAVE_VAESINTRIN_H
extern struct aegis256x4_implementation aegis256x4_avx2_implementation;
#endif

#endif

0 comments on commit c88c73c

Please sign in to comment.