diff --git a/gcc/config/i386/avx512fintrin.h b/gcc/config/i386/avx512fintrin.h
index bc10c823c76..29511fd2831 100644
--- a/gcc/config/i386/avx512fintrin.h
+++ b/gcc/config/i386/avx512fintrin.h
@@ -1639,16 +1639,27 @@ _mm_maskz_sub_round_ss (__mmask8 __U, __m128 __A, __m128 __B,
 
 #endif
 
+/* Constant helper to represent the ternary logic operations among
+   vector A, B and C.  */
+typedef enum
+{
+  _MM_TERNLOG_A = 0xF0,
+  _MM_TERNLOG_B = 0xCC,
+  _MM_TERNLOG_C = 0xAA
+} _MM_TERNLOG_ENUM;
+
 #ifdef __OPTIMIZE__
 extern __inline __m512i
 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
 _mm512_ternarylogic_epi64 (__m512i __A, __m512i __B, __m512i __C,
 			   const int __imm)
 {
-  return (__m512i) __builtin_ia32_pternlogq512_mask ((__v8di) __A,
-						     (__v8di) __B,
-						     (__v8di) __C, __imm,
-						     (__mmask8) -1);
+  return (__m512i)
+    __builtin_ia32_pternlogq512_mask ((__v8di) __A,
+				      (__v8di) __B,
+				      (__v8di) __C,
+				      (unsigned char) __imm,
+				      (__mmask8) -1);
 }
 
 extern __inline __m512i
@@ -1656,10 +1667,12 @@ __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
 _mm512_mask_ternarylogic_epi64 (__m512i __A, __mmask8 __U, __m512i __B,
 				__m512i __C, const int __imm)
 {
-  return (__m512i) __builtin_ia32_pternlogq512_mask ((__v8di) __A,
-						     (__v8di) __B,
-						     (__v8di) __C, __imm,
-						     (__mmask8) __U);
+  return (__m512i)
+    __builtin_ia32_pternlogq512_mask ((__v8di) __A,
+				      (__v8di) __B,
+				      (__v8di) __C,
+				      (unsigned char) __imm,
+				      (__mmask8) __U);
 }
 
 extern __inline __m512i
@@ -1667,10 +1680,12 @@ __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
 _mm512_maskz_ternarylogic_epi64 (__mmask8 __U, __m512i __A, __m512i __B,
 				 __m512i __C, const int __imm)
 {
-  return (__m512i) __builtin_ia32_pternlogq512_maskz ((__v8di) __A,
-						      (__v8di) __B,
-						      (__v8di) __C,
-						      __imm, (__mmask8) __U);
+  return (__m512i)
+    __builtin_ia32_pternlogq512_maskz ((__v8di) __A,
+				       (__v8di) __B,
+				       (__v8di) __C,
+				       (unsigned char) __imm,
+				       (__mmask8) __U);
 }
 
 extern __inline __m512i
@@ -1678,10 +1693,12 @@ __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
 _mm512_ternarylogic_epi32 (__m512i __A, __m512i __B, __m512i __C,
 			   const int __imm)
 {
-  return (__m512i) __builtin_ia32_pternlogd512_mask ((__v16si) __A,
-						     (__v16si) __B,
-						     (__v16si) __C,
-						     __imm, (__mmask16) -1);
+  return (__m512i)
+    __builtin_ia32_pternlogd512_mask ((__v16si) __A,
+				      (__v16si) __B,
+				      (__v16si) __C,
+				      (unsigned char) __imm,
+				      (__mmask16) -1);
 }
 
 extern __inline __m512i
@@ -1689,10 +1706,12 @@ __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
 _mm512_mask_ternarylogic_epi32 (__m512i __A, __mmask16 __U, __m512i __B,
 				__m512i __C, const int __imm)
 {
-  return (__m512i) __builtin_ia32_pternlogd512_mask ((__v16si) __A,
-						     (__v16si) __B,
-						     (__v16si) __C,
-						     __imm, (__mmask16) __U);
+  return (__m512i)
+    __builtin_ia32_pternlogd512_mask ((__v16si) __A,
+				      (__v16si) __B,
+				      (__v16si) __C,
+				      (unsigned char) __imm,
+				      (__mmask16) __U);
 }
 
 extern __inline __m512i
@@ -1700,33 +1719,56 @@ __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
 _mm512_maskz_ternarylogic_epi32 (__mmask16 __U, __m512i __A, __m512i __B,
 				 __m512i __C, const int __imm)
 {
-  return (__m512i) __builtin_ia32_pternlogd512_maskz ((__v16si) __A,
-						      (__v16si) __B,
-						      (__v16si) __C,
-						      __imm, (__mmask16) __U);
+  return (__m512i)
+    __builtin_ia32_pternlogd512_maskz ((__v16si) __A,
+				       (__v16si) __B,
+				       (__v16si) __C,
+				       (unsigned char) __imm,
+				       (__mmask16) __U);
 }
 #else
-#define _mm512_ternarylogic_epi64(A, B, C, I)				\
-  ((__m512i) __builtin_ia32_pternlogq512_mask ((__v8di)(__m512i)(A),	\
-    (__v8di)(__m512i)(B), (__v8di)(__m512i)(C), (int)(I), (__mmask8)-1))
-#define _mm512_mask_ternarylogic_epi64(A, U, B, C, I)			\
-  ((__m512i) __builtin_ia32_pternlogq512_mask ((__v8di)(__m512i)(A),	\
-    (__v8di)(__m512i)(B), (__v8di)(__m512i)(C), (int)(I), (__mmask8)(U)))
-#define _mm512_maskz_ternarylogic_epi64(U, A, B, C, I)			\
-  ((__m512i) __builtin_ia32_pternlogq512_maskz ((__v8di)(__m512i)(A),	\
-    (__v8di)(__m512i)(B), (__v8di)(__m512i)(C), (int)(I), (__mmask8)(U)))
-#define _mm512_ternarylogic_epi32(A, B, C, I)				\
-  ((__m512i) __builtin_ia32_pternlogd512_mask ((__v16si)(__m512i)(A),	\
-    (__v16si)(__m512i)(B), (__v16si)(__m512i)(C), (int)(I),		\
-    (__mmask16)-1))
-#define _mm512_mask_ternarylogic_epi32(A, U, B, C, I)			\
-  ((__m512i) __builtin_ia32_pternlogd512_mask ((__v16si)(__m512i)(A),	\
-    (__v16si)(__m512i)(B), (__v16si)(__m512i)(C), (int)(I),		\
-    (__mmask16)(U)))
-#define _mm512_maskz_ternarylogic_epi32(U, A, B, C, I)			\
-  ((__m512i) __builtin_ia32_pternlogd512_maskz ((__v16si)(__m512i)(A),	\
-    (__v16si)(__m512i)(B), (__v16si)(__m512i)(C), (int)(I),		\
-    (__mmask16)(U)))
+#define _mm512_ternarylogic_epi64(A, B, C, I)			\
+  ((__m512i)							\
+   __builtin_ia32_pternlogq512_mask ((__v8di) (__m512i) (A),	\
+				     (__v8di) (__m512i) (B),	\
+				     (__v8di) (__m512i) (C),	\
+				     (unsigned char) (I),	\
+				     (__mmask8) -1))
+#define _mm512_mask_ternarylogic_epi64(A, U, B, C, I)		\
+  ((__m512i)							\
+   __builtin_ia32_pternlogq512_mask ((__v8di) (__m512i) (A),	\
+				     (__v8di) (__m512i) (B),	\
+				     (__v8di) (__m512i) (C),	\
+				     (unsigned char)(I),	\
+				     (__mmask8) (U)))
+#define _mm512_maskz_ternarylogic_epi64(U, A, B, C, I)		\
+  ((__m512i)							\
+   __builtin_ia32_pternlogq512_maskz ((__v8di) (__m512i) (A),	\
+				      (__v8di) (__m512i) (B),	\
+				      (__v8di) (__m512i) (C),	\
+				      (unsigned char) (I),	\
+				      (__mmask8) (U)))
+#define _mm512_ternarylogic_epi32(A, B, C, I)			\
+  ((__m512i)							\
+   __builtin_ia32_pternlogd512_mask ((__v16si) (__m512i) (A),	\
+				     (__v16si) (__m512i) (B),	\
+				     (__v16si) (__m512i) (C),	\
+				     (unsigned char) (I),	\
+				     (__mmask16) -1))
+#define _mm512_mask_ternarylogic_epi32(A, U, B, C, I)		\
+  ((__m512i)							\
+   __builtin_ia32_pternlogd512_mask ((__v16si) (__m512i) (A),	\
+				     (__v16si) (__m512i) (B),	\
+				     (__v16si) (__m512i) (C),	\
+				     (unsigned char) (I),	\
+				     (__mmask16) (U)))
+#define _mm512_maskz_ternarylogic_epi32(U, A, B, C, I)		\
+  ((__m512i)							\
+   __builtin_ia32_pternlogd512_maskz ((__v16si) (__m512i) (A),	\
+				      (__v16si) (__m512i) (B),	\
+				      (__v16si) (__m512i) (C),	\
+				      (unsigned char) (I),	\
+				      (__mmask16) (U)))
 #endif
 
 extern __inline __m512d
diff --git a/gcc/config/i386/avx512vlintrin.h b/gcc/config/i386/avx512vlintrin.h
index bbced242506..26b286eae6b 100644
--- a/gcc/config/i386/avx512vlintrin.h
+++ b/gcc/config/i386/avx512vlintrin.h
@@ -10575,10 +10575,12 @@ __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
 _mm256_ternarylogic_epi64 (__m256i __A, __m256i __B, __m256i __C,
 			   const int __imm)
 {
-  return (__m256i) __builtin_ia32_pternlogq256_mask ((__v4di) __A,
-						     (__v4di) __B,
-						     (__v4di) __C, __imm,
-						     (__mmask8) -1);
+  return (__m256i)
+    __builtin_ia32_pternlogq256_mask ((__v4di) __A,
+				      (__v4di) __B,
+				      (__v4di) __C,
+				      (unsigned char) __imm,
+				      (__mmask8) -1);
 }
 
 extern __inline __m256i
@@ -10587,10 +10589,12 @@ _mm256_mask_ternarylogic_epi64 (__m256i __A, __mmask8 __U,
 				__m256i __B, __m256i __C,
 				const int __imm)
 {
-  return (__m256i) __builtin_ia32_pternlogq256_mask ((__v4di) __A,
-						     (__v4di) __B,
-						     (__v4di) __C, __imm,
-						     (__mmask8) __U);
+  return (__m256i)
+    __builtin_ia32_pternlogq256_mask ((__v4di) __A,
+				      (__v4di) __B,
+				      (__v4di) __C,
+				      (unsigned char) __imm,
+				      (__mmask8) __U);
 }
 
 extern __inline __m256i
@@ -10599,11 +10603,12 @@ _mm256_maskz_ternarylogic_epi64 (__mmask8 __U, __m256i __A,
 				 __m256i __B, __m256i __C,
 				 const int __imm)
 {
-  return (__m256i) __builtin_ia32_pternlogq256_maskz ((__v4di) __A,
-						      (__v4di) __B,
-						      (__v4di) __C,
-						      __imm,
-						      (__mmask8) __U);
+  return (__m256i)
+    __builtin_ia32_pternlogq256_maskz ((__v4di) __A,
+				       (__v4di) __B,
+				       (__v4di) __C,
+				       (unsigned char) __imm,
+				       (__mmask8) __U);
 }
 
 extern __inline __m256i
@@ -10611,10 +10616,12 @@ __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
 _mm256_ternarylogic_epi32 (__m256i __A, __m256i __B, __m256i __C,
 			   const int __imm)
 {
-  return (__m256i) __builtin_ia32_pternlogd256_mask ((__v8si) __A,
-						     (__v8si) __B,
-						     (__v8si) __C, __imm,
-						     (__mmask8) -1);
+  return (__m256i)
+    __builtin_ia32_pternlogd256_mask ((__v8si) __A,
+				      (__v8si) __B,
+				      (__v8si) __C,
+				      (unsigned char) __imm,
+				      (__mmask8) -1);
 }
 
 extern __inline __m256i
@@ -10623,10 +10630,12 @@ _mm256_mask_ternarylogic_epi32 (__m256i __A, __mmask8 __U,
 				__m256i __B, __m256i __C,
 				const int __imm)
 {
-  return (__m256i) __builtin_ia32_pternlogd256_mask ((__v8si) __A,
-						     (__v8si) __B,
-						     (__v8si) __C, __imm,
-						     (__mmask8) __U);
+  return (__m256i)
+    __builtin_ia32_pternlogd256_mask ((__v8si) __A,
+				      (__v8si) __B,
+				      (__v8si) __C,
+				      (unsigned char) __imm,
+				      (__mmask8) __U);
 }
 
 extern __inline __m256i
@@ -10635,11 +10644,12 @@ _mm256_maskz_ternarylogic_epi32 (__mmask8 __U, __m256i __A,
 				 __m256i __B, __m256i __C,
 				 const int __imm)
 {
-  return (__m256i) __builtin_ia32_pternlogd256_maskz ((__v8si) __A,
-						      (__v8si) __B,
-						      (__v8si) __C,
-						      __imm,
-						      (__mmask8) __U);
+  return (__m256i)
+    __builtin_ia32_pternlogd256_maskz ((__v8si) __A,
+				       (__v8si) __B,
+				       (__v8si) __C,
+				       (unsigned char) __imm,
+				       (__mmask8) __U);
 }
 
 extern __inline __m128i
@@ -10647,33 +10657,40 @@ __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
 _mm_ternarylogic_epi64 (__m128i __A, __m128i __B, __m128i __C,
 			const int __imm)
 {
-  return (__m128i) __builtin_ia32_pternlogq128_mask ((__v2di) __A,
-						     (__v2di) __B,
-						     (__v2di) __C, __imm,
-						     (__mmask8) -1);
+  return (__m128i)
+    __builtin_ia32_pternlogq128_mask ((__v2di) __A,
+				      (__v2di) __B,
+				      (__v2di) __C,
+				      (unsigned char) __imm,
+				      (__mmask8) -1);
 }
 
 extern __inline __m128i
 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
 _mm_mask_ternarylogic_epi64 (__m128i __A, __mmask8 __U,
-			     __m128i __B, __m128i __C, const int __imm)
+			     __m128i __B, __m128i __C,
+			     const int __imm)
 {
-  return (__m128i) __builtin_ia32_pternlogq128_mask ((__v2di) __A,
-						     (__v2di) __B,
-						     (__v2di) __C, __imm,
-						     (__mmask8) __U);
+  return (__m128i)
+    __builtin_ia32_pternlogq128_mask ((__v2di) __A,
+				      (__v2di) __B,
+				      (__v2di) __C,
+				      (unsigned char) __imm,
+				      (__mmask8) __U);
 }
 
 extern __inline __m128i
 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
 _mm_maskz_ternarylogic_epi64 (__mmask8 __U, __m128i __A,
-			      __m128i __B, __m128i __C, const int __imm)
+			      __m128i __B, __m128i __C,
+			      const int __imm)
 {
-  return (__m128i) __builtin_ia32_pternlogq128_maskz ((__v2di) __A,
-						      (__v2di) __B,
-						      (__v2di) __C,
-						      __imm,
-						      (__mmask8) __U);
+  return (__m128i)
+    __builtin_ia32_pternlogq128_maskz ((__v2di) __A,
+				       (__v2di) __B,
+				       (__v2di) __C,
+				       (unsigned char) __imm,
+				       (__mmask8) __U);
 }
 
 extern __inline __m128i
@@ -10681,33 +10698,40 @@ __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
 _mm_ternarylogic_epi32 (__m128i __A, __m128i __B, __m128i __C,
 			const int __imm)
 {
-  return (__m128i) __builtin_ia32_pternlogd128_mask ((__v4si) __A,
-						     (__v4si) __B,
-						     (__v4si) __C, __imm,
-						     (__mmask8) -1);
+  return (__m128i)
+    __builtin_ia32_pternlogd128_mask ((__v4si) __A,
+				      (__v4si) __B,
+				      (__v4si) __C,
+				      (unsigned char) __imm,
+				      (__mmask8) -1);
 }
 
 extern __inline __m128i
 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
 _mm_mask_ternarylogic_epi32 (__m128i __A, __mmask8 __U,
-			     __m128i __B, __m128i __C, const int __imm)
+			     __m128i __B, __m128i __C,
+			     const int __imm)
 {
-  return (__m128i) __builtin_ia32_pternlogd128_mask ((__v4si) __A,
-						     (__v4si) __B,
-						     (__v4si) __C, __imm,
-						     (__mmask8) __U);
+  return (__m128i)
+    __builtin_ia32_pternlogd128_mask ((__v4si) __A,
+				      (__v4si) __B,
+				      (__v4si) __C,
+				      (unsigned char) __imm,
+				      (__mmask8) __U);
 }
 
 extern __inline __m128i
 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
 _mm_maskz_ternarylogic_epi32 (__mmask8 __U, __m128i __A,
-			      __m128i __B, __m128i __C, const int __imm)
+			      __m128i __B, __m128i __C,
+			      const int __imm)
 {
-  return (__m128i) __builtin_ia32_pternlogd128_maskz ((__v4si) __A,
-						      (__v4si) __B,
-						      (__v4si) __C,
-						      __imm,
-						      (__mmask8) __U);
+  return (__m128i)
+    __builtin_ia32_pternlogd128_maskz ((__v4si) __A,
+				       (__v4si) __B,
+				       (__v4si) __C,
+				       (unsigned char) __imm,
+				       (__mmask8) __U);
 }
 
 extern __inline __m256
@@ -12910,53 +12934,101 @@ _mm256_permutex_pd (__m256d __X, const int __M)
     (__v2di)(__m128i)_mm_setzero_si128 (),\
     (__mmask8)(U)))
 
-#define _mm256_ternarylogic_epi64(A, B, C, I)                           \
-  ((__m256i) __builtin_ia32_pternlogq256_mask ((__v4di)(__m256i)(A),	\
-    (__v4di)(__m256i)(B), (__v4di)(__m256i)(C), (int)(I), (__mmask8)-1))
-
-#define _mm256_mask_ternarylogic_epi64(A, U, B, C, I)			\
-  ((__m256i) __builtin_ia32_pternlogq256_mask ((__v4di)(__m256i)(A),	\
-    (__v4di)(__m256i)(B), (__v4di)(__m256i)(C), (int)(I), (__mmask8)(U)))
-
-#define _mm256_maskz_ternarylogic_epi64(U, A, B, C, I)			\
-  ((__m256i) __builtin_ia32_pternlogq256_maskz ((__v4di)(__m256i)(A),	\
-    (__v4di)(__m256i)(B), (__v4di)(__m256i)(C), (int)(I), (__mmask8)(U)))
-
-#define _mm256_ternarylogic_epi32(A, B, C, I)                           \
-  ((__m256i) __builtin_ia32_pternlogd256_mask ((__v8si)(__m256i)(A),	\
-    (__v8si)(__m256i)(B), (__v8si)(__m256i)(C), (int)(I), (__mmask8)-1))
-
-#define _mm256_mask_ternarylogic_epi32(A, U, B, C, I)                   \
-  ((__m256i) __builtin_ia32_pternlogd256_mask ((__v8si)(__m256i)(A),	\
-    (__v8si)(__m256i)(B), (__v8si)(__m256i)(C), (int)(I), (__mmask8)(U)))
-
-#define _mm256_maskz_ternarylogic_epi32(U, A, B, C, I)			\
-  ((__m256i) __builtin_ia32_pternlogd256_maskz ((__v8si)(__m256i)(A),	\
-    (__v8si)(__m256i)(B), (__v8si)(__m256i)(C), (int)(I), (__mmask8)(U)))
-
-#define _mm_ternarylogic_epi64(A, B, C, I)                              \
-  ((__m128i) __builtin_ia32_pternlogq128_mask ((__v2di)(__m128i)(A),	\
-    (__v2di)(__m128i)(B), (__v2di)(__m128i)(C), (int)(I), (__mmask8)-1))
-
-#define _mm_mask_ternarylogic_epi64(A, U, B, C, I)			\
-  ((__m128i) __builtin_ia32_pternlogq128_mask ((__v2di)(__m128i)(A),	\
-    (__v2di)(__m128i)(B), (__v2di)(__m128i)(C), (int)(I), (__mmask8)(U)))
-
-#define _mm_maskz_ternarylogic_epi64(U, A, B, C, I)			\
-  ((__m128i) __builtin_ia32_pternlogq128_maskz ((__v2di)(__m128i)(A),	\
-    (__v2di)(__m128i)(B), (__v2di)(__m128i)(C), (int)(I), (__mmask8)(U)))
-
-#define _mm_ternarylogic_epi32(A, B, C, I)                              \
-  ((__m128i) __builtin_ia32_pternlogd128_mask ((__v4si)(__m128i)(A),	\
-    (__v4si)(__m128i)(B), (__v4si)(__m128i)(C), (int)(I), (__mmask8)-1))
-
-#define _mm_mask_ternarylogic_epi32(A, U, B, C, I)			\
-  ((__m128i) __builtin_ia32_pternlogd128_mask ((__v4si)(__m128i)(A),	\
-    (__v4si)(__m128i)(B), (__v4si)(__m128i)(C), (int)(I), (__mmask8)(U)))
-
-#define _mm_maskz_ternarylogic_epi32(U, A, B, C, I)			\
-  ((__m128i) __builtin_ia32_pternlogd128_maskz ((__v4si)(__m128i)(A),	\
-    (__v4si)(__m128i)(B), (__v4si)(__m128i)(C), (int)(I), (__mmask8)(U)))
+#define _mm256_ternarylogic_epi64(A, B, C, I)			\
+  ((__m256i)							\
+   __builtin_ia32_pternlogq256_mask ((__v4di) (__m256i) (A),	\
+				     (__v4di) (__m256i) (B),	\
+				     (__v4di) (__m256i) (C),	\
+				     (unsigned char) (I),	\
+				     (__mmask8) -1))
+
+#define _mm256_mask_ternarylogic_epi64(A, U, B, C, I)		\
+  ((__m256i)							\
+   __builtin_ia32_pternlogq256_mask ((__v4di) (__m256i) (A),	\
+				     (__v4di) (__m256i) (B),	\
+				     (__v4di) (__m256i) (C),	\
+				     (unsigned char) (I),	\
+				     (__mmask8) (U)))
+
+#define _mm256_maskz_ternarylogic_epi64(U, A, B, C, I)		\
+  ((__m256i)							\
+   __builtin_ia32_pternlogq256_maskz ((__v4di) (__m256i) (A),	\
+				      (__v4di) (__m256i) (B),	\
+				      (__v4di) (__m256i) (C),	\
+				      (unsigned char) (I),	\
+				      (__mmask8) (U)))
+
+#define _mm256_ternarylogic_epi32(A, B, C, I)			\
+  ((__m256i)							\
+   __builtin_ia32_pternlogd256_mask ((__v8si) (__m256i) (A),	\
+				     (__v8si) (__m256i) (B),	\
+				     (__v8si) (__m256i) (C),	\
+				     (unsigned char) (I),	\
+				     (__mmask8) -1))
+
+#define _mm256_mask_ternarylogic_epi32(A, U, B, C, I)		\
+  ((__m256i)							\
+   __builtin_ia32_pternlogd256_mask ((__v8si) (__m256i) (A),	\
+				     (__v8si) (__m256i) (B),	\
+				     (__v8si) (__m256i) (C),	\
+				     (unsigned char) (I),	\
+				     (__mmask8) (U)))
+
+#define _mm256_maskz_ternarylogic_epi32(U, A, B, C, I)		\
+  ((__m256i)							\
+   __builtin_ia32_pternlogd256_maskz ((__v8si) (__m256i) (A),	\
+				      (__v8si) (__m256i) (B),	\
+				      (__v8si) (__m256i) (C),	\
+				      (unsigned char) (I),	\
+				      (__mmask8) (U)))
+
+#define _mm_ternarylogic_epi64(A, B, C, I)			\
+  ((__m128i)							\
+   __builtin_ia32_pternlogq128_mask ((__v2di) (__m128i) (A),	\
+				     (__v2di) (__m128i) (B),	\
+				     (__v2di) (__m128i) (C),	\
+				     (unsigned char) (I),	\
+				     (__mmask8) -1))
+
+#define _mm_mask_ternarylogic_epi64(A, U, B, C, I)		\
+  ((__m128i)							\
+   __builtin_ia32_pternlogq128_mask ((__v2di) (__m128i) (A),	\
+				     (__v2di) (__m128i) (B),	\
+				     (__v2di) (__m128i) (C),	\
+				     (unsigned char) (I),	\
+				     (__mmask8) (U)))
+
+#define _mm_maskz_ternarylogic_epi64(U, A, B, C, I)		\
+  ((__m128i)							\
+   __builtin_ia32_pternlogq128_maskz ((__v2di) (__m128i) (A),	\
+				      (__v2di) (__m128i) (B),	\
+				      (__v2di) (__m128i) (C),	\
+				      (unsigned char) (I),	\
+				      (__mmask8) (U)))
+
+#define _mm_ternarylogic_epi32(A, B, C, I)			\
+  ((__m128i)							\
+   __builtin_ia32_pternlogd128_mask ((__v4si) (__m128i) (A),	\
+				     (__v4si) (__m128i) (B),	\
+				     (__v4si) (__m128i) (C),	\
+				     (unsigned char) (I),	\
+				     (__mmask8) -1))
+
+#define _mm_mask_ternarylogic_epi32(A, U, B, C, I)		\
+  ((__m128i)							\
+   __builtin_ia32_pternlogd128_mask ((__v4si) (__m128i) (A),	\
+				     (__v4si) (__m128i) (B),	\
+				     (__v4si) (__m128i) (C),	\
+				     (unsigned char) (I),	\
+				     (__mmask8) (U)))
+
+#define _mm_maskz_ternarylogic_epi32(U, A, B, C, I)		\
+  ((__m128i)							\
+   __builtin_ia32_pternlogd128_maskz ((__v4si) (__m128i) (A),	\
+				      (__v4si) (__m128i) (B),	\
+				      (__v4si) (__m128i) (C),	\
+				      (unsigned char) (I),	\
+				      (__mmask8) (U)))
 
 #define _mm256_roundscale_ps(A, B)				        \
   ((__m256) __builtin_ia32_rndscaleps_256_mask ((__v8sf)(__m256)(A),    \
diff --git a/gcc/testsuite/gcc.target/i386/avx512f-vpternlogd-1.c b/gcc/testsuite/gcc.target/i386/avx512f-vpternlogd-1.c
index b14af235ea3..a88153a85db 100644
--- a/gcc/testsuite/gcc.target/i386/avx512f-vpternlogd-1.c
+++ b/gcc/testsuite/gcc.target/i386/avx512f-vpternlogd-1.c
@@ -12,7 +12,8 @@ volatile __mmask16 m;
 void extern
 avx512f_test (void)
 {
-  x = _mm512_ternarylogic_epi32 (x, y, z, 0xF0);
-  x = _mm512_mask_ternarylogic_epi32 (x, m, y, z, 0xF0);
-  x = _mm512_maskz_ternarylogic_epi32 (m, x, y, z, 0xF0);
+  x = _mm512_ternarylogic_epi32 (x, y, z, _MM_TERNLOG_A);
+  x = _mm512_mask_ternarylogic_epi32 (x, m, y, z, _MM_TERNLOG_B);
+  x = _mm512_maskz_ternarylogic_epi32 (m, x, y, z,
+				       _MM_TERNLOG_A | _MM_TERNLOG_C);
 }
diff --git a/gcc/testsuite/gcc.target/i386/avx512f-vpternlogq-1.c b/gcc/testsuite/gcc.target/i386/avx512f-vpternlogq-1.c
index e7aa7a7d3ed..ef302464765 100644
--- a/gcc/testsuite/gcc.target/i386/avx512f-vpternlogq-1.c
+++ b/gcc/testsuite/gcc.target/i386/avx512f-vpternlogq-1.c
@@ -12,7 +12,8 @@ volatile __mmask8 m;
 void extern
 avx512f_test (void)
 {
-  x = _mm512_ternarylogic_epi64 (x, y, z, 0xF0);
-  x = _mm512_mask_ternarylogic_epi64 (x, m, y, z, 0xF0);
-  x = _mm512_maskz_ternarylogic_epi64 (m, x, y, z, 0xF0);
+  x = _mm512_ternarylogic_epi64 (x, y, z, _MM_TERNLOG_A);
+  x = _mm512_mask_ternarylogic_epi64 (x, m, y, z, _MM_TERNLOG_B);
+  x = _mm512_maskz_ternarylogic_epi64 (m, x, y, z,
+				       _MM_TERNLOG_A | _MM_TERNLOG_C);
 }
diff --git a/gcc/testsuite/gcc.target/i386/avx512vl-vpternlogd-1.c b/gcc/testsuite/gcc.target/i386/avx512vl-vpternlogd-1.c
index 590e1ef3213..045a266664c 100644
--- a/gcc/testsuite/gcc.target/i386/avx512vl-vpternlogd-1.c
+++ b/gcc/testsuite/gcc.target/i386/avx512vl-vpternlogd-1.c
@@ -16,11 +16,12 @@ volatile __mmask8 m;
 void extern
 avx512vl_test (void)
 {
-  y = _mm256_ternarylogic_epi32 (y, y2, y3, 0xF0);
-  y = _mm256_mask_ternarylogic_epi32 (y, m, y2, y3, 0xF0);
-  y = _mm256_maskz_ternarylogic_epi32 (m, y, y2, y3, 0xF0);
+  y = _mm256_ternarylogic_epi32 (y, y2, y3, _MM_TERNLOG_A);
+  y = _mm256_mask_ternarylogic_epi32 (y, m, y2, y3, _MM_TERNLOG_B);
+  y = _mm256_maskz_ternarylogic_epi32 (m, y, y2, y3, _MM_TERNLOG_C);
 
-  x = _mm_ternarylogic_epi32 (x, x2, x3, 0xF0);
-  x = _mm_mask_ternarylogic_epi32 (x, m, x2, x3, 0xF0);
-  x = _mm_maskz_ternarylogic_epi32 (m, x, x2, x3, 0xF0);
+  x = _mm_ternarylogic_epi32 (x, x2, x3, _MM_TERNLOG_A);
+  x = _mm_mask_ternarylogic_epi32 (x, m, x2, x3, ~_MM_TERNLOG_B);
+  x = _mm_maskz_ternarylogic_epi32 (m, x, x2, x3,
+				    _MM_TERNLOG_A | _MM_TERNLOG_C);
 }
diff --git a/gcc/testsuite/gcc.target/i386/avx512vl-vpternlogq-1.c b/gcc/testsuite/gcc.target/i386/avx512vl-vpternlogq-1.c
index 3e08a71e703..3a6707c8f65 100644
--- a/gcc/testsuite/gcc.target/i386/avx512vl-vpternlogq-1.c
+++ b/gcc/testsuite/gcc.target/i386/avx512vl-vpternlogq-1.c
@@ -16,11 +16,13 @@ volatile __mmask8 m;
 void extern
 avx512vl_test (void)
 {
-  y = _mm256_ternarylogic_epi64 (y, y2, y3, 0xF0);
-  y = _mm256_mask_ternarylogic_epi64 (y, m, y2, y3, 0xF0);
-  y = _mm256_maskz_ternarylogic_epi64 (m, y, y2, y3, 0xF0);
+  y = _mm256_ternarylogic_epi64 (y, y2, y3, _MM_TERNLOG_A);
+  y = _mm256_mask_ternarylogic_epi64 (y, m, y2, y3, ~_MM_TERNLOG_B);
+  y = _mm256_maskz_ternarylogic_epi64 (m, y, y2, y3,
+				       _MM_TERNLOG_A | _MM_TERNLOG_C);
 
-  x = _mm_ternarylogic_epi64 (x, x2, x3, 0xF0);
-  x = _mm_mask_ternarylogic_epi64 (x, m, x2, x3, 0xF0);
-  x = _mm_maskz_ternarylogic_epi64 (m, x, x2, x3, 0xF0);
+  x = _mm_ternarylogic_epi64 (x, x2, x3, _MM_TERNLOG_A);
+  x = _mm_mask_ternarylogic_epi64 (x, m, x2, x3, ~_MM_TERNLOG_B);
+  x = _mm_maskz_ternarylogic_epi64 (m, x, x2, x3,
+				    _MM_TERNLOG_A | _MM_TERNLOG_C);
 }
diff --git a/gcc/testsuite/gcc.target/i386/testimm-10.c b/gcc/testsuite/gcc.target/i386/testimm-10.c
index d0e9b42f2fe..426295a439e 100644
--- a/gcc/testsuite/gcc.target/i386/testimm-10.c
+++ b/gcc/testsuite/gcc.target/i386/testimm-10.c
@@ -22,13 +22,6 @@ test8bit (void)
   m512i = _mm512_mask_permutex_epi64 (m512i, mmask8, m512i, 256); /* { dg-error "the last argument must be an 8-bit immediate" } */
   m512i = _mm512_maskz_permutex_epi64 (mmask8, m512i, 256); /* { dg-error "the last argument must be an 8-bit immediate" } */
 
-  m512i = _mm512_ternarylogic_epi64 (m512i, m512i, m512i, 256); /* { dg-error "the last argument must be an 8-bit immediate" } */
-  m512i = _mm512_mask_ternarylogic_epi64 (m512i, mmask8, m512i, m512i, 256); /* { dg-error "the last argument must be an 8-bit immediate" } */
-  m512i = _mm512_maskz_ternarylogic_epi64 (mmask8, m512i, m512i, m512i, 256); /* { dg-error "the last argument must be an 8-bit immediate" } */
-  m512i = _mm512_ternarylogic_epi32 (m512i, m512i, m512i, 256); /* { dg-error "the last argument must be an 8-bit immediate" } */
-  m512i = _mm512_mask_ternarylogic_epi32 (m512i, mmask16, m512i, m512i, 256); /* { dg-error "the last argument must be an 8-bit immediate" } */
-  m512i = _mm512_maskz_ternarylogic_epi32 (mmask16, m512i, m512i, m512i, 256); /* { dg-error "the last argument must be an 8-bit immediate" } */
-
   m512i = _mm512_shuffle_epi32 (m512i, 256); /* { dg-error "the last argument must be an 8-bit immediate" } */
   m512i = _mm512_mask_shuffle_epi32 (m512i, mmask16, m512i, 256); /* { dg-error "the last argument must be an 8-bit immediate" } */
   m512i = _mm512_maskz_shuffle_epi32 (mmask16, m512i, 256); /* { dg-error "the last argument must be an 8-bit immediate" } */
