simddetect.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. ///////////////////////////////////////////////////////////////////////
  2. // File: simddetect.h
  3. // Description: Architecture detector.
  4. // Author: Stefan Weil (based on code from Ray Smith)
  5. //
  6. // (C) Copyright 2014, Google Inc.
  7. // Licensed under the Apache License, Version 2.0 (the "License");
  8. // you may not use this file except in compliance with the License.
  9. // You may obtain a copy of the License at
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. ///////////////////////////////////////////////////////////////////////
  17. #ifndef TESSERACT_ARCH_SIMDDETECT_H_
  18. #define TESSERACT_ARCH_SIMDDETECT_H_
  19. #include "platform.h"
  20. namespace tesseract {
  21. // Function pointer for best calculation of dot product.
  22. using DotProductFunction = double (*)(const double*, const double*, int);
  23. extern DotProductFunction DotProduct;
  24. // Architecture detector. Add code here to detect any other architectures for
  25. // SIMD-based faster dot product functions. Intended to be a single static
  26. // object, but it does no real harm to have more than one.
  27. class SIMDDetect {
  28. public:
  29. // Returns true if AVX is available on this system.
  30. static inline bool IsAVXAvailable() {
  31. return detector.avx_available_;
  32. }
  33. // Returns true if AVX2 (integer support) is available on this system.
  34. static inline bool IsAVX2Available() {
  35. return detector.avx2_available_;
  36. }
  37. // Returns true if AVX512 Foundation (float) is available on this system.
  38. static inline bool IsAVX512FAvailable() {
  39. return detector.avx512F_available_;
  40. }
  41. // Returns true if AVX512 integer is available on this system.
  42. static inline bool IsAVX512BWAvailable() {
  43. return detector.avx512BW_available_;
  44. }
  45. // Returns true if SSE4.1 is available on this system.
  46. static inline bool IsSSEAvailable() {
  47. return detector.sse_available_;
  48. }
  49. // Update settings after config variable was set.
  50. static TESS_API void Update();
  51. private:
  52. // Constructor, must set all static member variables.
  53. SIMDDetect();
  54. private:
  55. // Singleton.
  56. static SIMDDetect detector;
  57. // If true, then AVX has been detected.
  58. static TESS_API bool avx_available_;
  59. static TESS_API bool avx2_available_;
  60. static TESS_API bool avx512F_available_;
  61. static TESS_API bool avx512BW_available_;
  62. // If true, then SSe4.1 has been detected.
  63. static TESS_API bool sse_available_;
  64. };
  65. } // namespace tesseract
  66. #endif // TESSERACT_ARCH_SIMDDETECT_H_