Skip to content

Commit 345b076

Browse files
kalenedraelserge-sans-paille
authored andcommitted
Add casts for scalar types
1 parent a3e38c4 commit 345b076

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

include/xsimd/arch/xsimd_scalar.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,19 @@ namespace xsimd
11981198
{
11991199
return cond ? true_br : false_br;
12001200
}
1201+
1202+
template <class T>
1203+
XSIMD_INLINE constexpr bool batch_bool_cast(bool b) noexcept
1204+
{
1205+
return b;
1206+
}
1207+
1208+
template <class T_out, class T_in>
1209+
XSIMD_INLINE constexpr T_out batch_cast(T_in const& val) noexcept
1210+
{
1211+
static_assert(!std::is_same<T_out, bool>::value, "cannot convert to bool, use !x or x != 0");
1212+
return static_cast<T_out>(val);
1213+
}
12011214
}
12021215

12031216
#endif

test/test_batch_cast.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ struct batch_cast_test
341341
T_out scalar_ref = static_cast<T_out>(in_test_value);
342342
T_out scalar_res = res.get(0);
343343
CHECK_SCALAR_EQ(scalar_ref, scalar_res);
344+
CHECK_SCALAR_EQ(scalar_ref, xsimd::batch_cast<T_out>(in_test_value));
344345
}
345346
}
346347

@@ -356,11 +357,13 @@ struct batch_cast_test
356357
B_common_out all_true_res = xsimd::batch_bool_cast<T_out>(all_true_in);
357358
INFO(name);
358359
CHECK_SCALAR_EQ(all_true_res.get(0), true);
360+
CHECK_SCALAR_EQ(xsimd::batch_bool_cast<B_out>(true), true);
359361

360362
B_common_in all_false_in(false);
361363
B_common_out all_false_res = xsimd::batch_bool_cast<T_out>(all_false_in);
362364
INFO(name);
363365
CHECK_SCALAR_EQ(all_false_res.get(0), false);
366+
CHECK_SCALAR_EQ(xsimd::batch_bool_cast<B_out>(false), false);
364367
}
365368
};
366369

0 commit comments

Comments
 (0)