[libcxx-commits] [libcxx] [libc++] Optimize num_get integral functions (PR #121795)

Florian Hahn via libcxx-commits libcxx-commits at lists.llvm.org
Thu Aug 14 04:17:58 PDT 2025


================
@@ -83,11 +85,22 @@ struct __num_get : protected __num_get_base {
       unsigned*& __g_end,
       _CharT* __atoms);
 
-  _LIBCPP_HIDE_FROM_ABI static string __stage2_int_prep(ios_base& __iob, _CharT& __thousands_sep) {
-    locale __loc                 = __iob.getloc();
-    const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
-    __thousands_sep              = __np.thousands_sep();
-    return __np.grouping();
+  _LIBCPP_HIDE_FROM_ABI static ptrdiff_t __atoms_offset(const _CharT* __atoms, _CharT __val) {
+#    if _LIBCPP_HAS_ALGORITHM_VECTOR_UTILS
+    if constexpr (is_same<_CharT, char>::value) {
+      _LIBCPP_DIAGNOSTIC_PUSH
+      _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wpsabi") // This should be removed once https://llvm.org/PR128361 is fixed
+      using __vec   = __simd_vector<char, 32>;
+      __vec __chars = std::__broadcast<__vec>(__val);
+      __vec __cmp   = std::__partial_load<__vec, __int_chr_cnt>(__atoms);
+      auto __res    = __chars == __cmp;
+      if (std::__none_of(__res))
+        return __int_chr_cnt;
+      return std::min(__int_chr_cnt, std::__find_first_set(__res));
+      _LIBCPP_DIAGNOSTIC_POP
+    }
+#    endif
+    return std::find(__atoms, __atoms + __int_chr_cnt, __val) - __atoms;
----------------
fhahn wrote:

So with https://github.com/llvm/llvm-project/pull/153566, we will be able to vectorize something like

```
auto test(std::vector<short>::iterator first, short s) {
  auto ptr = __builtin_assume_aligned(&*first, 2);
   __builtin_assume_dereferenceable(ptr, 128 * sizeof(short));
   return std::find(first, first + 128, s);
}
```

https://godbolt.org/z/Wrovddv37

https://github.com/llvm/llvm-project/pull/121795


More information about the libcxx-commits mailing list