4343
4444// SIMD256 can't be compiled on macOS ARM64, and performance of SIMD128 isn't
4545// great; but when compiling a universal2 binary, autoconf will set
46- // HACL_CAN_COMPILE_SIMD128 and HACL_CAN_COMPILE_SIMD256 because they *can* be
47- // compiled on x86_64. If we're on macOS ARM64, disable these preprocessor
48- // symbols.
46+ // _Py_HACL_CAN_COMPILE_VEC{128,256} because they *can* be compiled on x86_64.
47+ // If we're on macOS ARM64, we however disable these preprocessor symbols.
4948#if defined(__APPLE__ ) && defined(__arm64__ )
50- # undef HACL_CAN_COMPILE_SIMD128
51- # undef HACL_CAN_COMPILE_SIMD256
49+ # undef _Py_HACL_CAN_COMPILE_VEC128
50+ # undef _Py_HACL_CAN_COMPILE_VEC256
5251#endif
5352
5453// ECX
@@ -114,31 +113,32 @@ void detect_cpu_features(cpu_flags *flags) {
114113 }
115114}
116115
117- #ifdef HACL_CAN_COMPILE_SIMD128
116+ #if _Py_HACL_CAN_COMPILE_VEC128
118117static inline bool has_simd128 (cpu_flags * flags ) {
119- // For now this is Intel-only, could conceivably be #ifdef 'd to something
118+ // For now this is Intel-only, could conceivably be if 'd to something
120119 // else.
121120 return flags -> sse && flags -> sse2 && flags -> sse3 && flags -> sse41 && flags -> sse42 && flags -> cmov ;
122121}
123122#endif
124123
125- #ifdef HACL_CAN_COMPILE_SIMD256
124+ #if _Py_HACL_CAN_COMPILE_VEC256
126125static inline bool has_simd256 (cpu_flags * flags ) {
127126 return flags -> avx && flags -> avx2 ;
128127}
129128#endif
130129
131- // Small mismatch between the variable names Python defines as part of configure
132- // at the ones HACL* expects to be set in order to enable those headers.
133- #define HACL_CAN_COMPILE_VEC128 HACL_CAN_COMPILE_SIMD128
134- #define HACL_CAN_COMPILE_VEC256 HACL_CAN_COMPILE_SIMD256
130+ // HACL* expects HACL_CAN_COMPILE_VEC* macros to be set in order to enable
131+ // the corresponding SIMD instructions so we need to "forward" the values
132+ // we just deduced above.
133+ #define HACL_CAN_COMPILE_VEC128 _Py_HACL_CAN_COMPILE_VEC128
134+ #define HACL_CAN_COMPILE_VEC256 _Py_HACL_CAN_COMPILE_VEC256
135135
136136#include "_hacl/Hacl_Hash_Blake2b.h"
137137#include "_hacl/Hacl_Hash_Blake2s.h"
138- #if HACL_CAN_COMPILE_SIMD256
138+ #if _Py_HACL_CAN_COMPILE_VEC256
139139#include "_hacl/Hacl_Hash_Blake2b_Simd256.h"
140140#endif
141- #if HACL_CAN_COMPILE_SIMD128
141+ #if _Py_HACL_CAN_COMPILE_VEC128
142142#include "_hacl/Hacl_Hash_Blake2s_Simd128.h"
143143#endif
144144
@@ -165,7 +165,7 @@ blake2_get_state(PyObject *module)
165165 return (Blake2State * )state ;
166166}
167167
168- #if defined(HACL_CAN_COMPILE_SIMD128 ) || defined(HACL_CAN_COMPILE_SIMD256 )
168+ #if defined(_Py_HACL_CAN_COMPILE_VEC128 ) || defined(_Py_HACL_CAN_COMPILE_VEC256 )
169169static inline Blake2State *
170170blake2_get_state_from_type (PyTypeObject * module )
171171{
@@ -329,18 +329,18 @@ static inline bool is_blake2s(blake2_impl impl) {
329329}
330330
331331static inline blake2_impl type_to_impl (PyTypeObject * type ) {
332- #if defined(HACL_CAN_COMPILE_SIMD128 ) || defined(HACL_CAN_COMPILE_SIMD256 )
332+ #if defined(_Py_HACL_CAN_COMPILE_VEC128 ) || defined(_Py_HACL_CAN_COMPILE_VEC256 )
333333 Blake2State * st = blake2_get_state_from_type (type );
334334#endif
335335 if (!strcmp (type -> tp_name , blake2b_type_spec .name )) {
336- #ifdef HACL_CAN_COMPILE_SIMD256
336+ #if _Py_HACL_CAN_COMPILE_VEC256
337337 if (has_simd256 (& st -> flags ))
338338 return Blake2b_256 ;
339339 else
340340#endif
341341 return Blake2b ;
342342 } else if (!strcmp (type -> tp_name , blake2s_type_spec .name )) {
343- #ifdef HACL_CAN_COMPILE_SIMD128
343+ #if _Py_HACL_CAN_COMPILE_VEC128
344344 if (has_simd128 (& st -> flags ))
345345 return Blake2s_128 ;
346346 else
@@ -356,10 +356,10 @@ typedef struct {
356356 union {
357357 Hacl_Hash_Blake2s_state_t * blake2s_state ;
358358 Hacl_Hash_Blake2b_state_t * blake2b_state ;
359- #ifdef HACL_CAN_COMPILE_SIMD128
359+ #if _Py_HACL_CAN_COMPILE_VEC128
360360 Hacl_Hash_Blake2s_Simd128_state_t * blake2s_128_state ;
361361#endif
362- #ifdef HACL_CAN_COMPILE_SIMD256
362+ #if _Py_HACL_CAN_COMPILE_VEC256
363363 Hacl_Hash_Blake2b_Simd256_state_t * blake2b_256_state ;
364364#endif
365365 } ;
@@ -425,14 +425,14 @@ static void
425425update (Blake2Object * self , uint8_t * buf , Py_ssize_t len )
426426{
427427 switch (self -> impl ) {
428- // These need to be ifdef 'd out otherwise it's an unresolved symbol at
429- // link-time.
430- #ifdef HACL_CAN_COMPILE_SIMD256
428+ // blake2b_256_state and blake2s_128_state must be if 'd since
429+ // otherwise this results in an unresolved symbol at link-time.
430+ #if _Py_HACL_CAN_COMPILE_VEC256
431431 case Blake2b_256 :
432432 HACL_UPDATE (Hacl_Hash_Blake2b_Simd256_update ,self -> blake2b_256_state , buf , len );
433433 return ;
434434#endif
435- #ifdef HACL_CAN_COMPILE_SIMD128
435+ #if _Py_HACL_CAN_COMPILE_VEC128
436436 case Blake2s_128 :
437437 HACL_UPDATE (Hacl_Hash_Blake2s_Simd128_update ,self -> blake2s_128_state , buf , len );
438438 return ;
@@ -468,12 +468,12 @@ py_blake2b_or_s_new(PyTypeObject *type, PyObject *data, int digest_size,
468468 // Ensure that the states are NULL-initialized in case of an error.
469469 // See: py_blake2_clear() for more details.
470470 switch (self -> impl ) {
471- #if HACL_CAN_COMPILE_SIMD256
471+ #if _Py_HACL_CAN_COMPILE_VEC256
472472 case Blake2b_256 :
473473 self -> blake2b_256_state = NULL ;
474474 break ;
475475#endif
476- #if HACL_CAN_COMPILE_SIMD128
476+ #if _Py_HACL_CAN_COMPILE_VEC128
477477 case Blake2s_128 :
478478 self -> blake2s_128_state = NULL ;
479479 break ;
@@ -591,7 +591,7 @@ py_blake2b_or_s_new(PyTypeObject *type, PyObject *data, int digest_size,
591591 };
592592
593593 switch (self -> impl ) {
594- #if HACL_CAN_COMPILE_SIMD256
594+ #if _Py_HACL_CAN_COMPILE_VEC256
595595 case Blake2b_256 : {
596596 self -> blake2b_256_state = Hacl_Hash_Blake2b_Simd256_malloc_with_params_and_key (& params , last_node , key -> buf );
597597 if (self -> blake2b_256_state == NULL ) {
@@ -601,7 +601,7 @@ py_blake2b_or_s_new(PyTypeObject *type, PyObject *data, int digest_size,
601601 break ;
602602 }
603603#endif
604- #if HACL_CAN_COMPILE_SIMD128
604+ #if _Py_HACL_CAN_COMPILE_VEC128
605605 case Blake2s_128 : {
606606 self -> blake2s_128_state = Hacl_Hash_Blake2s_Simd128_malloc_with_params_and_key (& params , last_node , key -> buf );
607607 if (self -> blake2s_128_state == NULL ) {
@@ -733,7 +733,7 @@ blake2_blake2b_copy_locked(Blake2Object *self, Blake2Object *cpy)
733733{
734734 assert (cpy != NULL );
735735 switch (self -> impl ) {
736- #if HACL_CAN_COMPILE_SIMD256
736+ #if _Py_HACL_CAN_COMPILE_VEC256
737737 case Blake2b_256 : {
738738 cpy -> blake2b_256_state = Hacl_Hash_Blake2b_Simd256_copy (self -> blake2b_256_state );
739739 if (cpy -> blake2b_256_state == NULL ) {
@@ -742,7 +742,7 @@ blake2_blake2b_copy_locked(Blake2Object *self, Blake2Object *cpy)
742742 break ;
743743 }
744744#endif
745- #if HACL_CAN_COMPILE_SIMD128
745+ #if _Py_HACL_CAN_COMPILE_VEC128
746746 case Blake2s_128 : {
747747 cpy -> blake2s_128_state = Hacl_Hash_Blake2s_Simd128_copy (self -> blake2s_128_state );
748748 if (cpy -> blake2s_128_state == NULL ) {
@@ -853,12 +853,12 @@ _blake2_blake2b_digest_impl(Blake2Object *self)
853853 ENTER_HASHLIB (self );
854854 uint8_t digest_length = 0 ;
855855 switch (self -> impl ) {
856- #if HACL_CAN_COMPILE_SIMD256
856+ #if _Py_HACL_CAN_COMPILE_VEC256
857857 case Blake2b_256 :
858858 digest_length = Hacl_Hash_Blake2b_Simd256_digest (self -> blake2b_256_state , digest );
859859 break ;
860860#endif
861- #if HACL_CAN_COMPILE_SIMD128
861+ #if _Py_HACL_CAN_COMPILE_VEC128
862862 case Blake2s_128 :
863863 digest_length = Hacl_Hash_Blake2s_Simd128_digest (self -> blake2s_128_state , digest );
864864 break ;
@@ -891,12 +891,12 @@ _blake2_blake2b_hexdigest_impl(Blake2Object *self)
891891 ENTER_HASHLIB (self );
892892 uint8_t digest_length = 0 ;
893893 switch (self -> impl ) {
894- #if HACL_CAN_COMPILE_SIMD256
894+ #if _Py_HACL_CAN_COMPILE_VEC256
895895 case Blake2b_256 :
896896 digest_length = Hacl_Hash_Blake2b_Simd256_digest (self -> blake2b_256_state , digest );
897897 break ;
898898#endif
899- #if HACL_CAN_COMPILE_SIMD128
899+ #if _Py_HACL_CAN_COMPILE_VEC128
900900 case Blake2s_128 :
901901 digest_length = Hacl_Hash_Blake2s_Simd128_digest (self -> blake2s_128_state , digest );
902902 break ;
@@ -947,11 +947,11 @@ py_blake2b_get_digest_size(PyObject *op, void *Py_UNUSED(closure))
947947{
948948 Blake2Object * self = _Blake2Object_CAST (op );
949949 switch (self -> impl ) {
950- #if HACL_CAN_COMPILE_SIMD256
950+ #if _Py_HACL_CAN_COMPILE_VEC256
951951 case Blake2b_256 :
952952 return PyLong_FromLong (Hacl_Hash_Blake2b_Simd256_info (self -> blake2b_256_state ).digest_length );
953953#endif
954- #if HACL_CAN_COMPILE_SIMD128
954+ #if _Py_HACL_CAN_COMPILE_VEC128
955955 case Blake2s_128 :
956956 return PyLong_FromLong (Hacl_Hash_Blake2s_Simd128_info (self -> blake2s_128_state ).digest_length );
957957#endif
@@ -982,15 +982,15 @@ py_blake2_clear(PyObject *op)
982982 // it. If an error occurs in the constructor, we should only free
983983 // states that were allocated (i.e. that are not NULL).
984984 switch (self -> impl ) {
985- #if HACL_CAN_COMPILE_SIMD256
985+ #if _Py_HACL_CAN_COMPILE_VEC256
986986 case Blake2b_256 :
987987 if (self -> blake2b_256_state != NULL ) {
988988 Hacl_Hash_Blake2b_Simd256_free (self -> blake2b_256_state );
989989 self -> blake2b_256_state = NULL ;
990990 }
991991 break ;
992992#endif
993- #if HACL_CAN_COMPILE_SIMD128
993+ #if _Py_HACL_CAN_COMPILE_VEC128
994994 case Blake2s_128 :
995995 if (self -> blake2s_128_state != NULL ) {
996996 Hacl_Hash_Blake2s_Simd128_free (self -> blake2s_128_state );
0 commit comments