1  #include <immintrin.h>
       2  #include "m256-check.h"
       3  
       4  typedef union
       5  {
       6    __m512i x;
       7    char a[64];
       8  } union512i_b;
       9  
      10  typedef union
      11  {
      12    __m512i x;
      13    short a[32];
      14  } union512i_w;
      15  
      16  typedef union
      17  {
      18    __m512i x;
      19    int a[16];
      20  } union512i_d;
      21  
      22  typedef union
      23  {
      24    __m512i x;
      25    long long a[8];
      26  } union512i_q;
      27  
      28  typedef union
      29  {
      30    __m512 x;
      31    float a[16];
      32  } union512;
      33  
      34  typedef union
      35  {
      36    __m512d x;
      37    double a[8];
      38  } union512d;
      39  
      40  typedef union
      41  {
      42    __m512i x;
      43    unsigned char a[64];
      44  } union512i_ub;
      45            
      46  typedef union
      47  {
      48   __m512i x;
      49   unsigned short a[32];
      50   } union512i_uw;
      51                        
      52  typedef union
      53  {
      54   __m512i x;
      55   unsigned int a[16];
      56  } union512i_ud;
      57                    
      58  typedef union
      59  {
      60   __m512i x;
      61   unsigned long long a[8];
      62  } union512i_uq;
      63  
      64  typedef union
      65  {
      66    __m128h x;
      67    _Float16 a[8];
      68  } union128h;
      69  
      70  typedef union
      71  {
      72    __m256h x;
      73    _Float16 a[16];
      74  } union256h;
      75  
      76  typedef union
      77  {
      78    __m512h x;
      79    _Float16 a[32];
      80  } union512h;
      81  
      82  CHECK_EXP (union512i_b, char, "%d")
      83  CHECK_EXP (union512i_w, short, "%d")
      84  CHECK_EXP (union512i_d, int, "0x%x")
      85  CHECK_EXP (union512i_q, long long, "0x%llx")
      86  CHECK_EXP (union512, float, "%f")
      87  CHECK_EXP (union512d, double, "%f")
      88  CHECK_EXP (union512i_ub, unsigned char, "%d")
      89  CHECK_EXP (union512i_uw, unsigned short, "%d")
      90  CHECK_EXP (union512i_ud, unsigned int, "0x%x")
      91  CHECK_EXP (union512i_uq, unsigned long long, "0x%llx")
      92       
      93  
      94  CHECK_FP_EXP (union512, float, ESP_FLOAT, "%f")
      95  CHECK_FP_EXP (union512d, double, ESP_DOUBLE, "%f")
      96  
      97  #define CHECK_ROUGH_EXP(UNION_TYPE, VALUE_TYPE, FMT)		\
      98  static int							\
      99  __attribute__((noinline, unused))				\
     100  check_rough_##UNION_TYPE (UNION_TYPE u, const VALUE_TYPE *v,	\
     101  			  VALUE_TYPE eps)			\
     102  {								\
     103    int i;							\
     104    int err = 0;							\
     105  								\
     106    for (i = 0; i < ARRAY_SIZE (u.a); i++)			\
     107      {								\
     108        /* We can have have v[i] == 0 == u.a[i]  for some i,	\
     109           when we test zero-masking.  */				\
     110        if (v[i] == 0.0 && u.a[i] == 0.0)				\
     111  	continue;						\
     112        if (v[i] == 0.0 && u.a[i] != 0.0)				\
     113  	{							\
     114  	  err++;						\
     115  	  PRINTF ("%i: " FMT " != " FMT "\n",			\
     116  		  i, v[i], u.a[i]);				\
     117  	}							\
     118        VALUE_TYPE rel_err = (u.a[i] - v[i]) / v[i];		\
     119        if (((rel_err < 0) ? -rel_err : rel_err) > eps)		\
     120  	{							\
     121  	  err++;						\
     122  	  PRINTF ("%i: " FMT " != " FMT "\n",			\
     123  		  i, v[i], u.a[i]);				\
     124  	}							\
     125      }								\
     126    return err;							\
     127  }
     128  
     129  CHECK_ROUGH_EXP (union512, float, "%f")
     130  CHECK_ROUGH_EXP (union512d, double, "%f")
     131  CHECK_ROUGH_EXP (union256, float, "%f")
     132  CHECK_ROUGH_EXP (union256d, double, "%f")
     133  CHECK_ROUGH_EXP (union128, float, "%f")
     134  CHECK_ROUGH_EXP (union128d, double, "%f")
     135  
     136  #ifdef AVX512FP16
     137  
     138  CHECK_EXP (union128h, _Float16, "%f")
     139  CHECK_EXP (union256h, _Float16, "%f")
     140  CHECK_EXP (union512h, _Float16, "%f")
     141  
     142  #ifndef ESP_FLOAT16
     143  #define ESP_FLOAT16 0.27
     144  #endif
     145  
     146  CHECK_FP_EXP (union128h, _Float16, ESP_FLOAT16, "%f")
     147  CHECK_FP_EXP (union256h, _Float16, ESP_FLOAT16, "%f")
     148  CHECK_FP_EXP (union512h, _Float16, ESP_FLOAT16, "%f")
     149  
     150  CHECK_ROUGH_EXP (union128h, _Float16, "%f")
     151  CHECK_ROUGH_EXP (union256h, _Float16, "%f")
     152  CHECK_ROUGH_EXP (union512h, _Float16, "%f")
     153  #endif