From bcc6015a3a94b5f2cd8decd9b7d2b0e41ec69467 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sun, 26 Feb 2023 16:46:49 +0100 Subject: [PATCH] QMessageAutenticationCode: add some Q_ASSUME()s to xored() ... telling the compiler all there is to know about block.size(), so it hopefully vectorizes the function well. Pick-to: 6.5 Change-Id: Icb3d5c983402e52a65c72af15f806f3a3fe6411f Reviewed-by: Thiago Macieira --- src/corelib/tools/qcryptographichash.cpp | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/corelib/tools/qcryptographichash.cpp b/src/corelib/tools/qcryptographichash.cpp index 09b53d4941e..86e1de70598 100644 --- a/src/corelib/tools/qcryptographichash.cpp +++ b/src/corelib/tools/qcryptographichash.cpp @@ -12,6 +12,8 @@ #include #include +#include +#include #include "../../3rdparty/sha1/sha1.cpp" @@ -1099,10 +1101,34 @@ constexpr int maxHashBlockSize() return result; } +[[maybe_unused]] +constexpr int minHashBlockSize() +{ + int result = INT_MAX; + using A = QCryptographicHash::Algorithm; + for (int i = 0; i < A::NumAlgorithms ; ++i) + result = std::min(result, qt_hash_block_size(A(i))); + return result; +} + +[[maybe_unused]] +constexpr int gcdHashBlockSize() +{ + int result = 0; + using A = QCryptographicHash::Algorithm; + for (int i = 0; i < A::NumAlgorithms ; ++i) + result = std::gcd(result, qt_hash_block_size(A(i))); + return result; +} + using HashBlock = QSmallByteArray; static HashBlock xored(const HashBlock &block, quint8 val) noexcept { + // some hints for the optimizer: + Q_ASSUME(block.size() >= minHashBlockSize()); + Q_ASSUME(block.size() <= maxHashBlockSize()); + Q_ASSUME(block.size() % gcdHashBlockSize() == 0); HashBlock result; result.resizeForOverwrite(block.size()); for (qsizetype i = 0; i < block.size(); ++i)