Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CPU architecture guards for x86 and x64 on MSVC #8

Merged
merged 1 commit into from
Apr 25, 2024

Conversation

mfrischknecht
Copy link
Contributor

I noticed that rust-aegis runs very slow when I build it with the MSVC toolchain on Windows. From what my online searches turned up, it looks like MSVC uses _M_IX86 and _M_AMD64 for CPU architecture guards instead of __i386__ and __x86_64__ 1.

This causes libaegis never to select any hardware accelerated implementations when built this way. I have created a simple test program that demonstrates the effect:

$ git clone --recursive https://github.com/mfrischknecht/rust-aegis-msvc-aesni-test.git
$ cd ./rust-aegis-msvc-aesni-test
$ git checkout without-fix
$ cargo run --release --quiet
CPU Features:

get_cpu_features(): 0
has_neon():         0
has_armcrypto():    0
has_avx():          0
has_avx2():         0
has_avx512f():      0
has_aesni():        0
has_vaes():         0

Encrypted: 100 MiB
Elapsed: 5.4909146s
Throughput: 18.211902257594755 MiB/s

$ git checkout with-fix
$ cargo run --release --quiet
CPU Features:

get_cpu_features(): 0
has_neon():         0
has_armcrypto():    0
has_avx():          1
has_avx2():         1
has_avx512f():      0
has_aesni():        1
has_vaes():         0

Encrypted: 100 MiB
Elapsed: 0.0278724s
Throughput: 3.5036900302808514 GiB/s

Footnotes

  1. See:
    https://sourceforge.net/p/predef/wiki/Architectures/
    http://web.archive.org/web/20240211072249/https://sourceforge.net/p/predef/wiki/Architectures/
    https://archive.is/jRq9r

MSVC uses `_M_IX86` and `_M_AMD64` for CPU architecture
guards instead of `__i386__` and `__x86_64__` [^1].
Because of that, libaegis didn't upgrade from the
software implementation to those powered by AESNI
or AVX when compiled with Microsoft's Visual Studio
build tools.

[^1]: As per the table here:

https://sourceforge.net/p/predef/wiki/Architectures/
http://web.archive.org/web/20240211072249/https://sourceforge.net/p/predef/wiki/Architectures/
https://archive.is/jRq9r
@jedisct1
Copy link
Collaborator

Thank you!

The MSVC-specific macros were recognized for aarch64, but I didn't know that they were also needed for i386 and x86_64.

Thanks again!

@jedisct1 jedisct1 merged commit 3dac3ad into aegis-aead:main Apr 25, 2024
2 checks passed
@jedisct1
Copy link
Collaborator

A new version of libaegis was tagged, and I'm going to update the Rust crate as well.

@jedisct1
Copy link
Collaborator

Done!

@mfrischknecht
Copy link
Contributor Author

Great, thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants