BLAKE2 / libb2

C library providing BLAKE2b, BLAKE2s, BLAKE2bp, BLAKE2sp
Creative Commons Zero v1.0 Universal
132 stars 47 forks source link

Add GCC constructor for get_cpu_features #34

Closed noloader closed 4 years ago

noloader commented 4 years ago

This commit adds a constructor to prime cpu features.

The functions looks safe, except for the write to the static cpu_feature_t feature. I think that could have atomicity problems on 32-bit systems if it is a 64-bit value in multi-threaded environments. That is, only half the value gets written and then a thread gets preempted. The partial write could momentarily leave the feature in an indeterminate state.

If cpu_feature_t is not a 64-bit value, then this commit is probably not needed.

sneves commented 4 years ago

According to the C standard, an enumeration is the implementation-defined smallest type that can represent all values. Hence, I'm doubtful that any 32-bit architecture will turn cpu_feature_t into a multi-word value. The default is int.

You can make sure of this by doing the following after the declaration of enum cpu_feature_t:

enum { BLAKE2_STATIC_ASSERT_1 = 1 / (sizeof(cpu_feature_t) <= sizeof(int)) };