Skip to content

Commit 38d69fe

Browse files
committed
include __builtin_popcount replacement function
Some systems/compilers lack __builtin_popcount(), so replace it as necessary. Reported by Dennis Clarke; ok dtucker@
1 parent c94138d commit 38d69fe

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

configure.ac

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,6 +2084,19 @@ AC_CHECK_FUNCS([ \
20842084
warn \
20852085
])
20862086

2087+
AC_MSG_CHECKING([whether compiler supports __builtin_popcount])
2088+
AC_LINK_IFELSE([AC_LANG_PROGRAM([[
2089+
#include <stdlib.h>
2090+
]],
2091+
[[ int x = 123, y;
2092+
y = __builtin_popcount(123);
2093+
exit(y == 6 ? 0 : -1); ]])],
2094+
[ AC_MSG_RESULT([yes]) ], [
2095+
AC_MSG_RESULT([no])
2096+
AC_DEFINE([MISSING_BUILTIN_POPCOUNT], [1], [Define if your compiler lacks __builtin_popcount])
2097+
]
2098+
)
2099+
20872100
AC_CHECK_DECLS([bzero, memmem])
20882101

20892102
dnl Wide character support.

libcrux_mlkem768_sha3.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,14 @@ static inline uint32_t core_num__u32_8__from_le_bytes(uint8_t buf[4]) {
177177
}
178178

179179
static inline uint32_t core_num__u8_6__count_ones(uint8_t x0) {
180-
#ifdef _MSC_VER
180+
#if defined(_MSC_VER)
181181
return __popcnt(x0);
182-
#else
182+
#elif !defined(MISSING_BUILTIN_POPCOUNT)
183183
return __builtin_popcount(x0);
184+
#else
185+
const uint8_t v[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
186+
return v[x0 & 0xf] + v[(x0 >> 4) & 0xf];
187+
184188
#endif
185189
}
186190

mlkem768.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ echo '#define KRML_HOST_EPRINTF(...)'
4949
echo '#define KRML_HOST_EXIT(x) fatal_f("internal error")'
5050
echo
5151

52+
__builtin_popcount_replacement='
53+
const uint8_t v[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
54+
return v[x0 & 0xf] + v[(x0 >> 4) & 0xf];
55+
'
56+
5257
for i in $FILES; do
5358
echo "/* from $i */"
5459
# Changes to all files:
@@ -62,7 +67,10 @@ for i in $FILES; do
6267
# Replace endian functions with versions that work.
6368
perl -0777 -pe 's/(static inline void core_num__u64_9__to_le_bytes.*\n)([^}]*\n)/\1 v = htole64(v);\n\2/' |
6469
perl -0777 -pe 's/(static inline uint64_t core_num__u64_9__from_le_bytes.*?)return v;/\1return le64toh(v);/s' |
65-
perl -0777 -pe 's/(static inline uint32_t core_num__u32_8__from_le_bytes.*?)return v;/\1return le32toh(v);/s'
70+
perl -0777 -pe 's/(static inline uint32_t core_num__u32_8__from_le_bytes.*?)return v;/\1return le32toh(v);/s' |
71+
# Compat for popcount.
72+
perl -0777 -pe 's/\#ifdef (_MSC_VER)(.*?return __popcnt\(x0\);)/\#if defined(\1)\2/s' |
73+
perl -0777 -pe "s/\\#else(\\n\\s+return __builtin_popcount\\(x0\\);)/\\#elif !defined(MISSING_BUILTIN_POPCOUNT)\\1\\n#else$__builtin_popcount_replacement/s"
6674
;;
6775
# Default: pass through.
6876
*)

0 commit comments

Comments
 (0)