cpu_fp16.cpp 825 B

123456789101112131415161718192021222324252627282930313233
  1. #include <stdio.h>
  2. #if defined __F16C__ || (defined _MSC_VER && _MSC_VER >= 1700 && defined __AVX__) || (defined __INTEL_COMPILER && defined __AVX__)
  3. #include <immintrin.h>
  4. int test()
  5. {
  6. const float src[] = { 0.0f, 0.0f, 0.0f, 0.0f };
  7. short dst[8];
  8. __m128 v_src = _mm_load_ps(src);
  9. __m128i v_dst = _mm_cvtps_ph(v_src, 0);
  10. _mm_storel_epi64((__m128i*)dst, v_dst);
  11. return (int)dst[0];
  12. }
  13. #elif defined __GNUC__ && (defined __arm__ || defined __aarch64__)
  14. #include "arm_neon.h"
  15. int test()
  16. {
  17. const float src[] = { 0.0f, 0.0f, 0.0f, 0.0f };
  18. short dst[8];
  19. float32x4_t v_src = *(float32x4_t*)src;
  20. float16x4_t v_dst = vcvt_f16_f32(v_src);
  21. *(float16x4_t*)dst = v_dst;
  22. return (int)dst[0];
  23. }
  24. #else
  25. #error "FP16 is not supported"
  26. #endif
  27. int main()
  28. {
  29. printf("%d\n", test());
  30. return 0;
  31. }