Enable constant folding for half(int) via float_to_half_rtne<true>

libeigen/eigen!3364

Co-authored-by: Antonio Sanchez <cantonios@google.com>
diff --git a/Eigen/src/Core/arch/Default/Half.h b/Eigen/src/Core/arch/Default/Half.h
index 9ac7184..6983b9c 100644
--- a/Eigen/src/Core/arch/Default/Half.h
+++ b/Eigen/src/Core/arch/Default/Half.h
@@ -166,7 +166,12 @@
 
 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC _EIGEN_MAYBE_CONSTEXPR __half_raw raw_uint16_to_half(numext::uint16_t x);
 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC numext::uint16_t raw_half_as_uint16(const __half_raw& h);
+template <bool AssumeArgumentIsNormalOrInfinityOrZero = false>
 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half_raw float_to_half_rtne(float ff);
+template <>
+EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half_raw float_to_half_rtne<false>(float ff);
+template <>
+EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half_raw float_to_half_rtne<true>(float ff);
 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC float half_to_float(__half_raw h);
 
 // Narrowing a wider floating-point value or an integer of more than 24 bits through float rounds twice. Rounding to
@@ -290,18 +295,18 @@
       : half_impl::half_base(half_impl::raw_uint16_to_half(b ? 0x3c00 : 0)) {}
   template <class T, std::enable_if_t<!std::is_integral<T>::value, int> = 0>
   explicit EIGEN_DEVICE_FUNC half(T val)
-      : half_impl::half_base(half_impl::float_to_half_rtne(half_impl::narrow_to_float(val))) {}
+      : half_impl::half_base(half_impl::float_to_half_rtne<false>(half_impl::narrow_to_float(val))) {}
   // Float rounds only integers beyond 2^24, which half overflows either way.
   template <class T, std::enable_if_t<std::is_integral<T>::value, int> = 0>
   explicit EIGEN_DEVICE_FUNC half(T val)
-      : half_impl::half_base(half_impl::float_to_half_rtne(static_cast<float>(val))) {}
-  explicit EIGEN_DEVICE_FUNC half(float f) : half_impl::half_base(half_impl::float_to_half_rtne(f)) {}
+      : half_impl::half_base(half_impl::float_to_half_rtne<true>(static_cast<float>(val))) {}
+  explicit EIGEN_DEVICE_FUNC half(float f) : half_impl::half_base(half_impl::float_to_half_rtne<false>(f)) {}
 
   // Following the convention of numpy, converting between complex and
   // float will lead to loss of imag value.
   template <typename RealScalar>
   explicit EIGEN_DEVICE_FUNC half(std::complex<RealScalar> c)
-      : half_impl::half_base(half_impl::float_to_half_rtne(half_impl::narrow_to_float(c.real()))) {}
+      : half_impl::half_base(half_impl::float_to_half_rtne<false>(half_impl::narrow_to_float(c.real()))) {}
 
   EIGEN_DEVICE_FUNC operator float() const {  // NOLINT: Allow implicit conversion to float, because it is lossless.
     return half_impl::half_to_float(*this);
@@ -757,7 +762,8 @@
 #endif
 }
 
-EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half_raw float_to_half_rtne(float ff) {
+template <>
+EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half_raw float_to_half_rtne<false>(float ff) {
 #if defined(EIGEN_GPU_COMPILE_PHASE)
   __half tmp_ff = __float2half(ff);
   return *(__half_raw*)&tmp_ff;
@@ -826,6 +832,44 @@
 #endif
 }
 
+// float_to_half_rtne template specialization that assumes that its function
+// argument (ff) is either a normal floating-point number that does not
+// underflow to a half subnormal, +/-infinity, or zero. Used to improve
+// compile-time constant folding and conversion from an integer type to half.
+template <>
+EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half_raw float_to_half_rtne<true>(float ff) {
+#if defined(EIGEN_GPU_COMPILE_PHASE)
+  __half tmp_ff = __float2half(ff);
+  return *(__half_raw*)&tmp_ff;
+
+#elif EIGEN_HAS_ARM64_FP16
+  __half_raw h;
+  h.x = static_cast<__fp16>(ff);
+  return h;
+
+#elif defined(EIGEN_HAS_BUILTIN_FLOAT16)
+  __half_raw h;
+  h.x = static_cast<_Float16>(ff);
+  return h;
+
+#else
+  uint32_t f_bits = Eigen::numext::bit_cast<uint32_t>(ff);
+  const uint32_t f16max_bits = {(127 + 16) << 23};
+  const uint32_t sign = f_bits & 0x80000000u;
+  f_bits ^= sign;
+
+  numext::uint16_t raw = 0x0000u;
+  if (f_bits >= f16max_bits) {
+    raw = 0x7c00u;
+  } else if (f_bits != 0) {
+    const uint32_t mant_odd = (f_bits >> 13) & 1u;
+    f_bits += 0xc8000fffU + mant_odd;
+    raw = static_cast<numext::uint16_t>(f_bits >> 13);
+  }
+  return raw_uint16_to_half(raw | static_cast<numext::uint16_t>(sign >> 16));
+#endif
+}
+
 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC float half_to_float(__half_raw h) {
 #if defined(EIGEN_GPU_COMPILE_PHASE)
   return __half2float(h);
diff --git a/Eigen/src/Core/functors/BinaryFunctors.h b/Eigen/src/Core/functors/BinaryFunctors.h
index 2bbed74..f184fa9 100644
--- a/Eigen/src/Core/functors/BinaryFunctors.h
+++ b/Eigen/src/Core/functors/BinaryFunctors.h
@@ -262,11 +262,11 @@
 struct scalar_cmp_op<LhsScalar, RhsScalar, cmp_EQ, UseTypedComparators> : binary_op_base<LhsScalar, RhsScalar> {
   using result_type = std::conditional_t<UseTypedComparators, LhsScalar, bool>;
   EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type operator()(const LhsScalar& a, const RhsScalar& b) const {
-    return a == b ? result_type(1) : result_type(0);
+    return result_type(a == b);
   }
   template <typename Packet>
   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
-    const Packet cst_one = pset1<Packet>(result_type(1));
+    const Packet cst_one = pset1<Packet>(result_type(true));
     return pand(pcmp_eq(a, b), cst_one);
   }
 };
@@ -275,11 +275,11 @@
 struct scalar_cmp_op<LhsScalar, RhsScalar, cmp_LT, UseTypedComparators> : binary_op_base<LhsScalar, RhsScalar> {
   using result_type = std::conditional_t<UseTypedComparators, LhsScalar, bool>;
   EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type operator()(const LhsScalar& a, const RhsScalar& b) const {
-    return a < b ? result_type(1) : result_type(0);
+    return result_type(a < b);
   }
   template <typename Packet>
   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
-    const Packet cst_one = pset1<Packet>(result_type(1));
+    const Packet cst_one = pset1<Packet>(result_type(true));
     return pand(pcmp_lt(a, b), cst_one);
   }
 };
@@ -288,11 +288,11 @@
 struct scalar_cmp_op<LhsScalar, RhsScalar, cmp_LE, UseTypedComparators> : binary_op_base<LhsScalar, RhsScalar> {
   using result_type = std::conditional_t<UseTypedComparators, LhsScalar, bool>;
   EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type operator()(const LhsScalar& a, const RhsScalar& b) const {
-    return a <= b ? result_type(1) : result_type(0);
+    return result_type(a <= b);
   }
   template <typename Packet>
   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
-    const Packet cst_one = pset1<Packet>(result_type(1));
+    const Packet cst_one = pset1<Packet>(result_type(true));
     return pand(cst_one, pcmp_le(a, b));
   }
 };
@@ -301,11 +301,11 @@
 struct scalar_cmp_op<LhsScalar, RhsScalar, cmp_GT, UseTypedComparators> : binary_op_base<LhsScalar, RhsScalar> {
   using result_type = std::conditional_t<UseTypedComparators, LhsScalar, bool>;
   EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type operator()(const LhsScalar& a, const RhsScalar& b) const {
-    return a > b ? result_type(1) : result_type(0);
+    return result_type(a > b);
   }
   template <typename Packet>
   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
-    const Packet cst_one = pset1<Packet>(result_type(1));
+    const Packet cst_one = pset1<Packet>(result_type(true));
     return pand(cst_one, pcmp_lt(b, a));
   }
 };
@@ -314,11 +314,11 @@
 struct scalar_cmp_op<LhsScalar, RhsScalar, cmp_GE, UseTypedComparators> : binary_op_base<LhsScalar, RhsScalar> {
   using result_type = std::conditional_t<UseTypedComparators, LhsScalar, bool>;
   EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type operator()(const LhsScalar& a, const RhsScalar& b) const {
-    return a >= b ? result_type(1) : result_type(0);
+    return result_type(a >= b);
   }
   template <typename Packet>
   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
-    const Packet cst_one = pset1<Packet>(result_type(1));
+    const Packet cst_one = pset1<Packet>(result_type(true));
     return pand(cst_one, pcmp_le(b, a));
   }
 };
@@ -327,11 +327,11 @@
 struct scalar_cmp_op<LhsScalar, RhsScalar, cmp_UNORD, UseTypedComparators> : binary_op_base<LhsScalar, RhsScalar> {
   using result_type = std::conditional_t<UseTypedComparators, LhsScalar, bool>;
   EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type operator()(const LhsScalar& a, const RhsScalar& b) const {
-    return !(a <= b || b <= a) ? result_type(1) : result_type(0);
+    return result_type(!(a <= b || b <= a));
   }
   template <typename Packet>
   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
-    const Packet cst_one = pset1<Packet>(result_type(1));
+    const Packet cst_one = pset1<Packet>(result_type(true));
     return pandnot(cst_one, por(pcmp_le(a, b), pcmp_le(b, a)));
   }
 };
@@ -340,11 +340,11 @@
 struct scalar_cmp_op<LhsScalar, RhsScalar, cmp_NEQ, UseTypedComparators> : binary_op_base<LhsScalar, RhsScalar> {
   using result_type = std::conditional_t<UseTypedComparators, LhsScalar, bool>;
   EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type operator()(const LhsScalar& a, const RhsScalar& b) const {
-    return a != b ? result_type(1) : result_type(0);
+    return result_type(a != b);
   }
   template <typename Packet>
   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
-    const Packet cst_one = pset1<Packet>(result_type(1));
+    const Packet cst_one = pset1<Packet>(result_type(true));
     return pandnot(cst_one, pcmp_eq(a, b));
   }
 };
@@ -529,14 +529,14 @@
   // `false` any value `a` that satisfies `a == Scalar(0)`
   // `true` is the complement of `false`
   EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()(const Scalar& a, const Scalar& b) const {
-    return (a != Scalar(0)) && (b != Scalar(0)) ? Scalar(1) : Scalar(0);
+    return Scalar((a != Scalar(0)) && (b != Scalar(0)));
   }
   template <typename Packet>
   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
     EIGEN_IF_CONSTEXPR ((std::is_same<Scalar, bool>::value)) {
       return pand(a, b);
     } else {
-      const Packet cst_one = pset1<Packet>(Scalar(1));
+      const Packet cst_one = pset1<Packet>(Scalar(true));
       // and(a,b) == !or(!a,!b)
       Packet not_a = pcmp_eq(a, pzero(a));
       Packet not_b = pcmp_eq(b, pzero(b));
@@ -567,14 +567,14 @@
   // `false` any value `a` that satisfies `a == Scalar(0)`
   // `true` is the complement of `false`
   EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()(const Scalar& a, const Scalar& b) const {
-    return (a != Scalar(0)) || (b != Scalar(0)) ? Scalar(1) : Scalar(0);
+    return Scalar((a != Scalar(0)) || (b != Scalar(0)));
   }
   template <typename Packet>
   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
     EIGEN_IF_CONSTEXPR ((std::is_same<Scalar, bool>::value)) {
       return por(a, b);
     } else {
-      const Packet cst_one = pset1<Packet>(Scalar(1));
+      const Packet cst_one = pset1<Packet>(Scalar(true));
       // if or(a,b) == 0, then a == 0 and b == 0
       // or(a,b) == !nor(a,b)
       Packet a_nor_b = pcmp_eq(por(a, b), pzero(a));
@@ -603,14 +603,14 @@
   // `false` any value `a` that satisfies `a == Scalar(0)`
   // `true` is the complement of `false`
   EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()(const Scalar& a, const Scalar& b) const {
-    return (a != Scalar(0)) != (b != Scalar(0)) ? Scalar(1) : Scalar(0);
+    return Scalar((a != Scalar(0)) != (b != Scalar(0)));
   }
   template <typename Packet>
   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
     EIGEN_IF_CONSTEXPR ((std::is_same<Scalar, bool>::value)) {
       return pxor(a, b);
     } else {
-      const Packet cst_one = pset1<Packet>(Scalar(1));
+      const Packet cst_one = pset1<Packet>(Scalar(true));
       // xor(a,b) == xor(!a,!b)
       Packet not_a = pcmp_eq(a, pzero(a));
       Packet not_b = pcmp_eq(b, pzero(b));
diff --git a/Eigen/src/Core/functors/TernaryFunctors.h b/Eigen/src/Core/functors/TernaryFunctors.h
index a53eec8..140621f 100644
--- a/Eigen/src/Core/functors/TernaryFunctors.h
+++ b/Eigen/src/Core/functors/TernaryFunctors.h
@@ -29,7 +29,7 @@
   using result_type = Scalar;
   EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()(const ThenScalar& a, const ElseScalar& b,
                                                                     const ConditionScalar& cond) const {
-    return cond == ConditionScalar(0) ? b : a;
+    return cond == ConditionScalar(false) ? b : a;
   }
   template <typename Packet>
   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b, const Packet& cond) const {
diff --git a/Eigen/src/Core/functors/UnaryFunctors.h b/Eigen/src/Core/functors/UnaryFunctors.h
index 822cf9c..c25cef0 100644
--- a/Eigen/src/Core/functors/UnaryFunctors.h
+++ b/Eigen/src/Core/functors/UnaryFunctors.h
@@ -1197,11 +1197,11 @@
   // `false` any value `a` that satisfies `a == Scalar(0)`
   // `true` is the complement of `false`
   EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()(const Scalar& a) const {
-    return a == Scalar(0) ? Scalar(1) : Scalar(0);
+    return Scalar(a == Scalar(0));
   }
   template <typename Packet>
   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a) const {
-    const Packet cst_one = pset1<Packet>(Scalar(1));
+    const Packet cst_one = pset1<Packet>(Scalar(true));
     EIGEN_IF_CONSTEXPR ((std::is_same<Scalar, bool>::value)) {
       // Boolean packet lanes are canonical, so logical NOT is 1 & ~a.
       return pandnot(cst_one, a);
diff --git a/contrib/benchmarks/Tensor/bench_coefficient_wise.cpp b/contrib/benchmarks/Tensor/bench_coefficient_wise.cpp
index 945027e..9f4203a 100644
--- a/contrib/benchmarks/Tensor/bench_coefficient_wise.cpp
+++ b/contrib/benchmarks/Tensor/bench_coefficient_wise.cpp
@@ -297,6 +297,59 @@
   state.counters["threads"] = threads;
 }
 
+// --- Element-wise type casting: a.cast<Tgt>() ---
+template <typename Src, typename Tgt>
+static void BM_Cast(benchmark::State& state) {
+  const int M = state.range(0);
+  const int N = state.range(1);
+
+  Tensor<Src, 2> a(M, N);
+  Tensor<Tgt, 2> b(M, N);
+  a.setRandom();
+
+  for (auto _ : state) {
+    b = a.template cast<Tgt>();
+    benchmark::DoNotOptimize(b.data());
+    benchmark::ClobberMemory();
+  }
+  state.SetBytesProcessed(state.iterations() * M * N * (sizeof(Src) + sizeof(Tgt)));
+}
+
+template <typename T>
+static void BM_Sigmoid_Typed(benchmark::State& state) {
+  const int M = state.range(0);
+  const int N = state.range(1);
+
+  Tensor<T, 2> a(M, N);
+  Tensor<T, 2> b(M, N);
+  a.setRandom();
+
+  for (auto _ : state) {
+    b = a.sigmoid();
+    benchmark::DoNotOptimize(b.data());
+    benchmark::ClobberMemory();
+  }
+  state.SetBytesProcessed(state.iterations() * M * N * sizeof(T) * 2);
+}
+
+template <typename T>
+static void BM_Inverse_Typed(benchmark::State& state) {
+  const int M = state.range(0);
+  const int N = state.range(1);
+
+  Tensor<T, 2> a(M, N);
+  Tensor<T, 2> b(M, N);
+  a.setRandom();
+  a = a.abs() + a.constant(T(1));
+
+  for (auto _ : state) {
+    b = a.inverse();
+    benchmark::DoNotOptimize(b.data());
+    benchmark::ClobberMemory();
+  }
+  state.SetBytesProcessed(state.iterations() * M * N * sizeof(T) * 2);
+}
+
 // clang-format off
 #define CWISE_SIZES \
   ->Args({256, 256})->Args({1024, 1024})
@@ -326,6 +379,12 @@
 BENCHMARK_TEMPLATE(BM_Select, int32_t) CWISE_SIZES;
 BENCHMARK_TEMPLATE(BM_Select, Eigen::half) CWISE_SIZES;
 BENCHMARK_TEMPLATE(BM_Select, Eigen::bfloat16) CWISE_SIZES;
+BENCHMARK_TEMPLATE(BM_Cast, Eigen::half, bool) CWISE_SIZES;
+BENCHMARK_TEMPLATE(BM_Cast, Eigen::bfloat16, bool) CWISE_SIZES;
+BENCHMARK_TEMPLATE(BM_Sigmoid_Typed, Eigen::half) CWISE_SIZES;
+BENCHMARK_TEMPLATE(BM_Sigmoid_Typed, Eigen::bfloat16) CWISE_SIZES;
+BENCHMARK_TEMPLATE(BM_Inverse_Typed, Eigen::half) CWISE_SIZES;
+BENCHMARK_TEMPLATE(BM_Inverse_Typed, Eigen::bfloat16) CWISE_SIZES;
 BENCHMARK(BM_Add_ThreadPool) CWISE_THREADPOOL_SIZES->UseRealTime();
 BENCHMARK(BM_Mul_ThreadPool) CWISE_THREADPOOL_SIZES->UseRealTime();
 BENCHMARK(BM_FMA_ThreadPool) CWISE_THREADPOOL_SIZES->UseRealTime();
diff --git a/contrib/benchmarks/Tensor/bench_reduction.cpp b/contrib/benchmarks/Tensor/bench_reduction.cpp
index 17d6c2f..a2d2272 100644
--- a/contrib/benchmarks/Tensor/bench_reduction.cpp
+++ b/contrib/benchmarks/Tensor/bench_reduction.cpp
@@ -35,41 +35,47 @@
 }
 
 // --- Partial reduction along dim 0 (inner dim, ColMajor) ---
-static void BM_ReduceInner(benchmark::State& state) {
+template <typename T>
+static void BM_ReduceInner_Typed(benchmark::State& state) {
   const int M = state.range(0);
   const int N = state.range(1);
 
-  Tensor<Scalar, 2> A(M, N);
+  Tensor<T, 2> A(M, N);
   A.setRandom();
 
   Eigen::array<int, 1> reduce_dims = {0};
 
   for (auto _ : state) {
-    Tensor<Scalar, 1> result = A.sum(reduce_dims);
+    Tensor<T, 1> result = A.sum(reduce_dims);
     benchmark::DoNotOptimize(result.data());
     benchmark::ClobberMemory();
   }
-  state.SetBytesProcessed(state.iterations() * M * N * sizeof(Scalar));
+  state.SetBytesProcessed(state.iterations() * M * N * sizeof(T));
 }
 
+static void BM_ReduceInner(benchmark::State& state) { BM_ReduceInner_Typed<Scalar>(state); }
+
 // --- Partial reduction along dim 1 (outer dim, ColMajor) ---
-static void BM_ReduceOuter(benchmark::State& state) {
+template <typename T>
+static void BM_ReduceOuter_Typed(benchmark::State& state) {
   const int M = state.range(0);
   const int N = state.range(1);
 
-  Tensor<Scalar, 2> A(M, N);
+  Tensor<T, 2> A(M, N);
   A.setRandom();
 
   Eigen::array<int, 1> reduce_dims = {1};
 
   for (auto _ : state) {
-    Tensor<Scalar, 1> result = A.sum(reduce_dims);
+    Tensor<T, 1> result = A.sum(reduce_dims);
     benchmark::DoNotOptimize(result.data());
     benchmark::ClobberMemory();
   }
-  state.SetBytesProcessed(state.iterations() * M * N * sizeof(Scalar));
+  state.SetBytesProcessed(state.iterations() * M * N * sizeof(T));
 }
 
+static void BM_ReduceOuter(benchmark::State& state) { BM_ReduceOuter_Typed<Scalar>(state); }
+
 // --- Rank-4 partial reduction (batch x channels x H x W), reduce along spatial dims ---
 static void BM_ReduceSpatial(benchmark::State& state) {
   const int batch = state.range(0);
@@ -127,5 +133,9 @@
 BENCHMARK(BM_FullReduction<internal::MaxReducer<Scalar>>) REDUCTION_SIZES->Name("MaxReduction_Full");
 BENCHMARK(BM_ReduceInner) REDUCTION_SIZES;
 BENCHMARK(BM_ReduceOuter) REDUCTION_SIZES;
+BENCHMARK_TEMPLATE(BM_ReduceInner_Typed, Eigen::half) REDUCTION_SIZES;
+BENCHMARK_TEMPLATE(BM_ReduceOuter_Typed, Eigen::half) REDUCTION_SIZES;
+BENCHMARK_TEMPLATE(BM_ReduceInner_Typed, Eigen::bfloat16) REDUCTION_SIZES;
+BENCHMARK_TEMPLATE(BM_ReduceOuter_Typed, Eigen::bfloat16) REDUCTION_SIZES;
 BENCHMARK(BM_ReduceSpatial) SPATIAL_SIZES;
 BENCHMARK(BM_FullReduction_ThreadPool) THREADPOOL_REDUCTION_SIZES;
diff --git a/test/array_cwise.cpp b/test/array_cwise.cpp
index eba0db4..18f5069 100644
--- a/test/array_cwise.cpp
+++ b/test/array_cwise.cpp
@@ -808,7 +808,7 @@
   ArrayType m1 = random_for_arithmetic<ArrayType>(rows, cols), m2 = random_for_arithmetic<ArrayType>(rows, cols),
             m3(rows, cols), m4 = m1;
 
-  m4 = (m4.abs() == Scalar(0)).select(1, m4);
+  m4 = (m4.abs() == Scalar(0)).select(Scalar(1), m4);
 
   // use operator overloads with default return type
 
@@ -816,7 +816,7 @@
   VERIFY(((m1 - Scalar(1)) < m1).all());
   if (rows * cols > 1) {
     m3 = m1;
-    m3(r, c) += 1;
+    m3(r, c) += Scalar(1);
     VERIFY(!(m1 < m3).all());
     VERIFY(!(m1 > m3).all());
   }
@@ -824,15 +824,15 @@
   VERIFY((m1 <= m2 || m1 >= m2).all());
 
   // comparisons array to scalar
-  VERIFY((m1 != (m1(r, c) + 1)).any());
-  VERIFY((m1 > (m1(r, c) - 1)).any());
-  VERIFY((m1 < (m1(r, c) + 1)).any());
+  VERIFY((m1 != (m1(r, c) + Scalar(1))).any());
+  VERIFY((m1 > (m1(r, c) - Scalar(1))).any());
+  VERIFY((m1 < (m1(r, c) + Scalar(1))).any());
   VERIFY((m1 == m1(r, c)).any());
 
   // comparisons scalar to array
-  VERIFY(((m1(r, c) + 1) != m1).any());
-  VERIFY(((m1(r, c) - 1) < m1).any());
-  VERIFY(((m1(r, c) + 1) > m1).any());
+  VERIFY(((m1(r, c) + Scalar(1)) != m1).any());
+  VERIFY(((m1(r, c) - Scalar(1)) < m1).any());
+  VERIFY(((m1(r, c) + Scalar(1)) > m1).any());
   VERIFY((m1(r, c) == m1).any());
 
   // currently, any() / all() are not vectorized, so use VERIFY_IS_CWISE_EQUAL to test vectorized path
@@ -884,16 +884,16 @@
   VERIFY_IS_APPROX((m1 > m2).select(m1, m2), m1.cwiseMax(m2));
   Scalar mid = (m1.cwiseAbs().minCoeff() + m1.cwiseAbs().maxCoeff()) / Scalar(2);
   for (int j = 0; j < cols; ++j)
-    for (int i = 0; i < rows; ++i) m3(i, j) = abs(m1(i, j)) < mid ? 0 : m1(i, j);
+    for (int i = 0; i < rows; ++i) m3(i, j) = abs(m1(i, j)) < mid ? Scalar(0) : m1(i, j);
   VERIFY_IS_APPROX((m1.abs() < ArrayType::Constant(rows, cols, mid)).select(ArrayType::Zero(rows, cols), m1), m3);
   // shorter versions:
-  VERIFY_IS_APPROX((m1.abs() < ArrayType::Constant(rows, cols, mid)).select(0, m1), m3);
-  VERIFY_IS_APPROX((m1.abs() >= ArrayType::Constant(rows, cols, mid)).select(m1, 0), m3);
+  VERIFY_IS_APPROX((m1.abs() < ArrayType::Constant(rows, cols, mid)).select(Scalar(0), m1), m3);
+  VERIFY_IS_APPROX((m1.abs() >= ArrayType::Constant(rows, cols, mid)).select(m1, Scalar(0)), m3);
   // even shorter version:
-  VERIFY_IS_APPROX((m1.abs() < mid).select(0, m1), m3);
+  VERIFY_IS_APPROX((m1.abs() < mid).select(Scalar(0), m1), m3);
 
   // count
-  VERIFY(((m1.abs() + 1) > RealScalar(0.1)).count() == rows * cols);
+  VERIFY(((m1.abs() + Scalar(1)) > RealScalar(0.1)).count() == rows * cols);
 
   // and/or
   VERIFY((m1 < RealScalar(0) && m1 > RealScalar(0)).count() == 0);
@@ -904,9 +904,9 @@
   typedef Array<Index, Dynamic, 1> ArrayOfIndices;
 
   // TODO allows colwise/rowwise for array
-  VERIFY_IS_APPROX(((m1.abs() + 1) > RealScalar(0.1)).colwise().count(),
+  VERIFY_IS_APPROX(((m1.abs() + Scalar(1)) > RealScalar(0.1)).colwise().count(),
                    ArrayOfIndices::Constant(cols, rows).transpose());
-  VERIFY_IS_APPROX(((m1.abs() + 1) > RealScalar(0.1)).rowwise().count(), ArrayOfIndices::Constant(rows, cols));
+  VERIFY_IS_APPROX(((m1.abs() + Scalar(1)) > RealScalar(0.1)).rowwise().count(), ArrayOfIndices::Constant(rows, cols));
 
   // simple data type that does not permit implicit conversions
   struct scalar_wrapper {
@@ -918,8 +918,8 @@
 
   // test bug2966: select did not support some scalar types that forbade implicit conversions from bool
   ArrayX<scalar_wrapper> m5(10);
-  m5.setConstant(scalar_wrapper(0));
-  m5 = (m5 == scalar_wrapper(0)).select(m5, m5);
+  m5.setConstant(scalar_wrapper(Scalar(0)));
+  m5 = (m5 == scalar_wrapper(Scalar(0))).select(m5, m5);
 }
 
 template <typename ArrayType>
@@ -1763,6 +1763,8 @@
         ArrayXXf(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
     CALL_SUBTEST_8(comparisons(
         ArrayXXi(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
+    CALL_SUBTEST_8(comparisons(Array<Eigen::half, 32, 32>()));
+    CALL_SUBTEST_8(comparisons(Array<Eigen::bfloat16, 32, 32>()));
   }
   for (int i = 0; i < g_repeat; i++) {
     CALL_SUBTEST_6(min_max(Array<float, 1, 1>()));
@@ -1811,7 +1813,9 @@
                                                                    internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
     CALL_SUBTEST_23(integer_typed_predicates_test(ArrayX<uint32_t>(internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
     CALL_SUBTEST_24(typed_logicals_test(ArrayX<float>(internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
+    CALL_SUBTEST_24(typed_logicals_test(ArrayX<Eigen::half>(internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
     CALL_SUBTEST_25(typed_logicals_test(ArrayX<double>(internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
+    CALL_SUBTEST_25(typed_logicals_test(ArrayX<Eigen::bfloat16>(internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
     CALL_SUBTEST_26(typed_logicals_test(ArrayX<std::complex<float>>(internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
     CALL_SUBTEST_27(typed_logicals_test(ArrayX<std::complex<double>>(internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
     CALL_SUBTEST_42(bool_logical_ops<>());