43
43
44
44
// SIMD256 can't be compiled on macOS ARM64, and performance of SIMD128 isn't
45
45
// 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.
49
48
#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
52
51
#endif
53
52
54
53
// ECX
@@ -114,31 +113,32 @@ void detect_cpu_features(cpu_flags *flags) {
114
113
}
115
114
}
116
115
117
- #ifdef HACL_CAN_COMPILE_SIMD128
116
+ #if _Py_HACL_CAN_COMPILE_VEC128
118
117
static 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
120
119
// else.
121
120
return flags -> sse && flags -> sse2 && flags -> sse3 && flags -> sse41 && flags -> sse42 && flags -> cmov ;
122
121
}
123
122
#endif
124
123
125
- #ifdef HACL_CAN_COMPILE_SIMD256
124
+ #if _Py_HACL_CAN_COMPILE_VEC256
126
125
static inline bool has_simd256 (cpu_flags * flags ) {
127
126
return flags -> avx && flags -> avx2 ;
128
127
}
129
128
#endif
130
129
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
135
135
136
136
#include "_hacl/Hacl_Hash_Blake2b.h"
137
137
#include "_hacl/Hacl_Hash_Blake2s.h"
138
- #if HACL_CAN_COMPILE_SIMD256
138
+ #if _Py_HACL_CAN_COMPILE_VEC256
139
139
#include "_hacl/Hacl_Hash_Blake2b_Simd256.h"
140
140
#endif
141
- #if HACL_CAN_COMPILE_SIMD128
141
+ #if _Py_HACL_CAN_COMPILE_VEC128
142
142
#include "_hacl/Hacl_Hash_Blake2s_Simd128.h"
143
143
#endif
144
144
@@ -165,7 +165,7 @@ blake2_get_state(PyObject *module)
165
165
return (Blake2State * )state ;
166
166
}
167
167
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 )
169
169
static inline Blake2State *
170
170
blake2_get_state_from_type (PyTypeObject * module )
171
171
{
@@ -329,18 +329,18 @@ static inline bool is_blake2s(blake2_impl impl) {
329
329
}
330
330
331
331
static 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 )
333
333
Blake2State * st = blake2_get_state_from_type (type );
334
334
#endif
335
335
if (!strcmp (type -> tp_name , blake2b_type_spec .name )) {
336
- #ifdef HACL_CAN_COMPILE_SIMD256
336
+ #if _Py_HACL_CAN_COMPILE_VEC256
337
337
if (has_simd256 (& st -> flags ))
338
338
return Blake2b_256 ;
339
339
else
340
340
#endif
341
341
return Blake2b ;
342
342
} else if (!strcmp (type -> tp_name , blake2s_type_spec .name )) {
343
- #ifdef HACL_CAN_COMPILE_SIMD128
343
+ #if _Py_HACL_CAN_COMPILE_VEC128
344
344
if (has_simd128 (& st -> flags ))
345
345
return Blake2s_128 ;
346
346
else
@@ -356,10 +356,10 @@ typedef struct {
356
356
union {
357
357
Hacl_Hash_Blake2s_state_t * blake2s_state ;
358
358
Hacl_Hash_Blake2b_state_t * blake2b_state ;
359
- #ifdef HACL_CAN_COMPILE_SIMD128
359
+ #if _Py_HACL_CAN_COMPILE_VEC128
360
360
Hacl_Hash_Blake2s_Simd128_state_t * blake2s_128_state ;
361
361
#endif
362
- #ifdef HACL_CAN_COMPILE_SIMD256
362
+ #if _Py_HACL_CAN_COMPILE_VEC256
363
363
Hacl_Hash_Blake2b_Simd256_state_t * blake2b_256_state ;
364
364
#endif
365
365
} ;
@@ -425,14 +425,14 @@ static void
425
425
update (Blake2Object * self , uint8_t * buf , Py_ssize_t len )
426
426
{
427
427
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
431
431
case Blake2b_256 :
432
432
HACL_UPDATE (Hacl_Hash_Blake2b_Simd256_update ,self -> blake2b_256_state , buf , len );
433
433
return ;
434
434
#endif
435
- #ifdef HACL_CAN_COMPILE_SIMD128
435
+ #if _Py_HACL_CAN_COMPILE_VEC128
436
436
case Blake2s_128 :
437
437
HACL_UPDATE (Hacl_Hash_Blake2s_Simd128_update ,self -> blake2s_128_state , buf , len );
438
438
return ;
@@ -468,12 +468,12 @@ py_blake2b_or_s_new(PyTypeObject *type, PyObject *data, int digest_size,
468
468
// Ensure that the states are NULL-initialized in case of an error.
469
469
// See: py_blake2_clear() for more details.
470
470
switch (self -> impl ) {
471
- #if HACL_CAN_COMPILE_SIMD256
471
+ #if _Py_HACL_CAN_COMPILE_VEC256
472
472
case Blake2b_256 :
473
473
self -> blake2b_256_state = NULL ;
474
474
break ;
475
475
#endif
476
- #if HACL_CAN_COMPILE_SIMD128
476
+ #if _Py_HACL_CAN_COMPILE_VEC128
477
477
case Blake2s_128 :
478
478
self -> blake2s_128_state = NULL ;
479
479
break ;
@@ -591,7 +591,7 @@ py_blake2b_or_s_new(PyTypeObject *type, PyObject *data, int digest_size,
591
591
};
592
592
593
593
switch (self -> impl ) {
594
- #if HACL_CAN_COMPILE_SIMD256
594
+ #if _Py_HACL_CAN_COMPILE_VEC256
595
595
case Blake2b_256 : {
596
596
self -> blake2b_256_state = Hacl_Hash_Blake2b_Simd256_malloc_with_params_and_key (& params , last_node , key -> buf );
597
597
if (self -> blake2b_256_state == NULL ) {
@@ -601,7 +601,7 @@ py_blake2b_or_s_new(PyTypeObject *type, PyObject *data, int digest_size,
601
601
break ;
602
602
}
603
603
#endif
604
- #if HACL_CAN_COMPILE_SIMD128
604
+ #if _Py_HACL_CAN_COMPILE_VEC128
605
605
case Blake2s_128 : {
606
606
self -> blake2s_128_state = Hacl_Hash_Blake2s_Simd128_malloc_with_params_and_key (& params , last_node , key -> buf );
607
607
if (self -> blake2s_128_state == NULL ) {
@@ -733,7 +733,7 @@ blake2_blake2b_copy_locked(Blake2Object *self, Blake2Object *cpy)
733
733
{
734
734
assert (cpy != NULL );
735
735
switch (self -> impl ) {
736
- #if HACL_CAN_COMPILE_SIMD256
736
+ #if _Py_HACL_CAN_COMPILE_VEC256
737
737
case Blake2b_256 : {
738
738
cpy -> blake2b_256_state = Hacl_Hash_Blake2b_Simd256_copy (self -> blake2b_256_state );
739
739
if (cpy -> blake2b_256_state == NULL ) {
@@ -742,7 +742,7 @@ blake2_blake2b_copy_locked(Blake2Object *self, Blake2Object *cpy)
742
742
break ;
743
743
}
744
744
#endif
745
- #if HACL_CAN_COMPILE_SIMD128
745
+ #if _Py_HACL_CAN_COMPILE_VEC128
746
746
case Blake2s_128 : {
747
747
cpy -> blake2s_128_state = Hacl_Hash_Blake2s_Simd128_copy (self -> blake2s_128_state );
748
748
if (cpy -> blake2s_128_state == NULL ) {
@@ -853,12 +853,12 @@ _blake2_blake2b_digest_impl(Blake2Object *self)
853
853
ENTER_HASHLIB (self );
854
854
uint8_t digest_length = 0 ;
855
855
switch (self -> impl ) {
856
- #if HACL_CAN_COMPILE_SIMD256
856
+ #if _Py_HACL_CAN_COMPILE_VEC256
857
857
case Blake2b_256 :
858
858
digest_length = Hacl_Hash_Blake2b_Simd256_digest (self -> blake2b_256_state , digest );
859
859
break ;
860
860
#endif
861
- #if HACL_CAN_COMPILE_SIMD128
861
+ #if _Py_HACL_CAN_COMPILE_VEC128
862
862
case Blake2s_128 :
863
863
digest_length = Hacl_Hash_Blake2s_Simd128_digest (self -> blake2s_128_state , digest );
864
864
break ;
@@ -891,12 +891,12 @@ _blake2_blake2b_hexdigest_impl(Blake2Object *self)
891
891
ENTER_HASHLIB (self );
892
892
uint8_t digest_length = 0 ;
893
893
switch (self -> impl ) {
894
- #if HACL_CAN_COMPILE_SIMD256
894
+ #if _Py_HACL_CAN_COMPILE_VEC256
895
895
case Blake2b_256 :
896
896
digest_length = Hacl_Hash_Blake2b_Simd256_digest (self -> blake2b_256_state , digest );
897
897
break ;
898
898
#endif
899
- #if HACL_CAN_COMPILE_SIMD128
899
+ #if _Py_HACL_CAN_COMPILE_VEC128
900
900
case Blake2s_128 :
901
901
digest_length = Hacl_Hash_Blake2s_Simd128_digest (self -> blake2s_128_state , digest );
902
902
break ;
@@ -947,11 +947,11 @@ py_blake2b_get_digest_size(PyObject *op, void *Py_UNUSED(closure))
947
947
{
948
948
Blake2Object * self = _Blake2Object_CAST (op );
949
949
switch (self -> impl ) {
950
- #if HACL_CAN_COMPILE_SIMD256
950
+ #if _Py_HACL_CAN_COMPILE_VEC256
951
951
case Blake2b_256 :
952
952
return PyLong_FromLong (Hacl_Hash_Blake2b_Simd256_info (self -> blake2b_256_state ).digest_length );
953
953
#endif
954
- #if HACL_CAN_COMPILE_SIMD128
954
+ #if _Py_HACL_CAN_COMPILE_VEC128
955
955
case Blake2s_128 :
956
956
return PyLong_FromLong (Hacl_Hash_Blake2s_Simd128_info (self -> blake2s_128_state ).digest_length );
957
957
#endif
@@ -982,15 +982,15 @@ py_blake2_clear(PyObject *op)
982
982
// it. If an error occurs in the constructor, we should only free
983
983
// states that were allocated (i.e. that are not NULL).
984
984
switch (self -> impl ) {
985
- #if HACL_CAN_COMPILE_SIMD256
985
+ #if _Py_HACL_CAN_COMPILE_VEC256
986
986
case Blake2b_256 :
987
987
if (self -> blake2b_256_state != NULL ) {
988
988
Hacl_Hash_Blake2b_Simd256_free (self -> blake2b_256_state );
989
989
self -> blake2b_256_state = NULL ;
990
990
}
991
991
break ;
992
992
#endif
993
- #if HACL_CAN_COMPILE_SIMD128
993
+ #if _Py_HACL_CAN_COMPILE_VEC128
994
994
case Blake2s_128 :
995
995
if (self -> blake2s_128_state != NULL ) {
996
996
Hacl_Hash_Blake2s_Simd128_free (self -> blake2s_128_state );
0 commit comments