r/C_Programming • • Oct 22 '26

Question Is it possible to make compiler recognize AES iterations for hardware acceleration?

I'm using GCC and Clang exclusively for broad compatibility between systems and architectures. I have a conditional macros that targets widest vector AES size via compiler attributes for x86, arm, riscv and others. However I can't cover all of them and would like to know if there's a sort of "universal" way to make compiler detect AES iteration loop and replace it with hardware AES intrinsics, without attributes if possible

For example, you have a loop like:


#define N_BLOCKS 8

for (size_t i = 0; i < N_BLOCKS; ++i) {

// C code of the AES round that compilers can detect

}

If consider compiler extensions, GCC/Clang provide __builtin_ia32_vaesenc_v16qi/v32qi/v64qi syntax, but I wonder if there's a more universal "cross-compiler" way of doing it

6 Upvotes

13 comments sorted by

5

u/internet_safari_ Oct 22 '26

Not really an answer but on my phone I've had faster aead encryption/decryption times with far stronger parameters than with the old AES + acceleration, when using XChaCha20-Poly1305 instead, which doesn't use hardware acceleration at all. Unless there is some conpatibility need outside of your control, XChaCha20 is a better alternative to AES as far as I'm aware.

1

u/Salat_Leaf Oct 22 '26

Reasons:

  1. AES is the most reliable cipher of all time due to 3 decades of scrutiny, only lagging behind DES
  2. AEGIS

1

u/internet_safari_ Oct 22 '26 edited Oct 22 '26

And if you need to go more niche hardware/embedded, check out ascon or even my own thingaling here: https://github.com/Iain-Donald/ascon-lib

Edit: which is also software-only

2

u/WrStudio9017 Oct 22 '26

I don't think there is any way in C to express this in a way that is both universal and that will also map to very specific vectorized instructions like this. Your best bet is probably assembly for something this processor specific.

1

u/Salat_Leaf Oct 22 '26

I mean AES round is fixed, so is the implementation in hardware, I doubt there's a difference significant enough for compilers to account for diversity between architectures

6

u/Commercial-Job-9441 Oct 22 '26

no compiler does this. gcc and clang's auto-vectorizers only map plain arithmetic onto simd; there's no idiom recognition that spots an aes round written in c and swaps in aesni/vaes, so a "universal" attribute-free version doesn't exist. the usual pattern is what you're already doing: intrinsics per arch behind ifdefs, plus runtime dispatch with __builtin_cpu_supports("aes") so one binary picks the right path at startup. target_clones (gcc 6+, also in clang) can generate the multi-versioned variants for you and cuts the ifdef soup down a bit. and if this isn't for learning, openssl or libsodium already does per-arch runtime dispatch and is very hard to beat.

1

u/Professional-Crow904 Oct 22 '26

I've seen this discussion pop up every now and then for over 15-16 years since I've been on reddit. Lol. Nope. There is no standard cross-platform way to do it because SIMD is not even remotely common across CPUs.

Recently, ARM & RISC5 seem to be doing this magic thing called Vector Length Agnostic (VLA) SIMD instructions. But it's nowhere near what you're looking for. Meanwhile, you can arrange your array such that it's aligned to the maximum estimated register size, then pass aggressive -O3 -m<arch> flags to gcc and hope it properly converts them to some SIMD instructions.

1

u/Salat_Leaf Oct 22 '26

I see. Thanks for the recommendation

1

u/TheThiefMaster Oct 22 '26

I think you've missed that they're after the AES encryption instructions. There's no way auto-vectorisation uses those

4

u/wwabbbitt Oct 22 '26

Are you implementing AES yourself? That's usually not recommended. Most of us just use a library, with compile flags optimized for the architecture.

1

u/sciencekm Oct 22 '26

There isn't a single way to do AES in software. Some will use table lookups (different variations of table lookup) while some will perform straight computation only. Some will merge perform 8-bit substitutions while others will merge those into 16-bit substitutions.

Even hardware instructions (ARM vs Intel) do not break down the steps the same way.

1

u/FedUp233 Oct 25 '26

Do you really need to use macros or such? Seems encryption usually happens on relatively large chunks of data, so why not just call out to a library? And let the li tart you use take care of the hw dependencies.

You didn’t say what system you were running on, but if it’s something like windows or Linux then using a shared library makes sense and you can just connect to the proper library at run time.

I have a hard ti e seeing situations where the eliminator of a single call for each chunk of data being encrypted is going to make a significant performance difference - at least one worth the extra trouble.

1

u/TextZestyclose8854 15d ago

Hej kan någon hjälpa mig med detta

Your system does not support the AES instruction set (AES-NI), which is required to run Rainbow Six Siege. Check your BIOS/UEFI settings.