codec_choose.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include <stdbool.h>
  2. #include <stdint.h>
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. #include "libbase64.h"
  6. #include "codecs.h"
  7. #if (__x86_64__ || __i386__ || _M_X86 || _M_X64)
  8. #define BASE64_X86
  9. #endif
  10. #ifdef BASE64_X86
  11. #ifdef _MSC_VER
  12. #include <intrin.h>
  13. #define __cpuid_count(__level, __count, __eax, __ebx, __ecx, __edx) \
  14. { \
  15. int info[4]; \
  16. __cpuidex(info, __level, __count); \
  17. __eax = info[0]; \
  18. __ebx = info[1]; \
  19. __ecx = info[2]; \
  20. __edx = info[3]; \
  21. }
  22. #define __cpuid(__level, __eax, __ebx, __ecx, __edx) \
  23. __cpuid_count(__level, 0, __eax, __ebx, __ecx, __edx)
  24. #else
  25. #include <cpuid.h>
  26. #endif
  27. #ifndef bit_AVX2
  28. #define bit_AVX2 (1 << 5)
  29. #endif
  30. #ifndef bit_SSSE3
  31. #define bit_SSSE3 (1 << 9)
  32. #endif
  33. #ifndef bit_SSE41
  34. #define bit_SSE41 (1 << 19)
  35. #endif
  36. #ifndef bit_SSE42
  37. #define bit_SSE42 (1 << 20)
  38. #endif
  39. #ifndef bit_AVX
  40. #define bit_AVX (1 << 28)
  41. #endif
  42. #define bit_XSAVE_XRSTORE (1 << 27)
  43. #ifndef _XCR_XFEATURE_ENABLED_MASK
  44. #define _XCR_XFEATURE_ENABLED_MASK 0
  45. #endif
  46. #define _XCR_XMM_AND_YMM_STATE_ENABLED_BY_OS 0x6
  47. #endif
  48. // Function declarations:
  49. #define BASE64_CODEC_FUNCS(arch) \
  50. BASE64_ENC_FUNCTION(arch); \
  51. BASE64_DEC_FUNCTION(arch); \
  52. BASE64_CODEC_FUNCS(plain)
  53. static bool
  54. codec_choose_forced (struct codec *codec, int flags)
  55. {
  56. // If the user wants to use a certain codec,
  57. // always allow it, even if the codec is a no-op.
  58. // For testing purposes.
  59. if (!(flags & 0xFF)) {
  60. return false;
  61. }
  62. codec->enc = base64_stream_encode_plain;
  63. codec->dec = base64_stream_decode_plain;
  64. return true;
  65. }
  66. static bool
  67. codec_choose_arm (struct codec *codec)
  68. {
  69. (void)codec;
  70. return false;
  71. }
  72. static bool
  73. codec_choose_x86 (struct codec *codec)
  74. {
  75. (void)codec;
  76. return false;
  77. }
  78. void
  79. codec_choose (struct codec *codec, int flags)
  80. {
  81. // User forced a codec:
  82. if (codec_choose_forced(codec, flags)) {
  83. return;
  84. }
  85. // Runtime feature detection:
  86. if (codec_choose_arm(codec)) {
  87. return;
  88. }
  89. if (codec_choose_x86(codec)) {
  90. return;
  91. }
  92. codec->enc = base64_stream_encode_plain;
  93. codec->dec = base64_stream_decode_plain;
  94. }