This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch tymux-10-quiet-head
in repository terminology.

View the commit online.

commit d8c9703b0509113af457f05796d3afb916cad1a6
Author: [email protected] <[email protected]>
AuthorDate: Sun Oct 25 13:41:18 2026 -0600

    simd: add optional AVX2 intake kernels
    
    AVX2 versions of the five intake kernels sit behind a new simd-avx2
    Meson option, off by default, because the gain is modest.
    
    AVX2 is not part of the x86-64 baseline, so the kernels are compiled
    with a per-function target attribute and the rest of the binary stays
    baseline. They are dispatched only when eina_cpu_features_get()
    reports EINA_CPU_AVX2, which Eina sets only when the CPU has AVX2 and
    the OS saves the YMM state (OSXSAVE and XCR0). TERMINOLOGY_SIMD_DISABLE
    still forces the scalar path.
    
    Dispatch is a runtime flag checked ahead of the SSE2 branch, compiled
    only in a simd-avx2 build, so the dispatchers of a default build are
    unchanged. Callgrind on tybench counts the same instructions for a
    default build as before this change, to within about 160 out of
    60-113 million. NEON is untouched.
    
    EINA_CPU_AVX2 is new in EFL after 1.28.1. Configure probes for the
    symbol rather than the version, because EFL git builds of that period
    all report 1.28.99. With the option on and Eina lacking the symbol,
    configure fails with a clear message. The option needs an x86_64 host.
    A default build does not use the symbol.
    
    Eina fills in the feature mask in eina_init(), so simd_init() must run
    after it. tytest called simd_init() before eina_init(); it now
    initializes Eina first, as Terminology, tybench and tyfuzz already do.
    
    The parity tests were written once against VEC(). To test SSE2 and
    AVX2 in the same binary, they now walk a table of kernel sets, each
    tagged with the CPU features it needs, and run only the sets the CPU
    supports. A simd-avx2 build on a CPU without AVX2 therefore does not
    die with SIGILL in meson test. The tests ignore
    TERMINOLOGY_SIMD_DISABLE, so the unit-scalar run still checks the
    vector kernels against the scalar reference.
    
    Termcell is 16 bytes, so the AVX2 records-OR kernel vectorises the
    16-byte stride: two records per 256-bit register, with an odd last
    record left to the scalar loop. Unlike SSE2 and NEON there is no
    12-byte path, because no caller uses that stride any more. The
    _Static_assert message in termpty.c was reworded to stay true for
    every kernel set.
    
    Benchmark: tybench, release build of the same simd-avx2 binary, with
    AVX2 and SSE2 selected by a temporary toggle that is not part of this
    commit. Run with taskset -c 4, -i 10 -w 3, corpora from
    tests/bench/gen_corpus.py, 7 interleaved runs per mode on an idle
    machine. Median throughput in MB/s:
    
      corpus        SSE2    AVX2
      plain-ascii   114.5   118.5   (+3.6%)
      scroll         68.3    73.8   (+8.0%)
      sgr           106.0   108.4   (+2.2%)
      unicode        92.8    95.2   (+2.6%)
    
    Callgrind on the same binary, 512 KiB corpora: AVX2 executes 10% fewer
    instructions on plain-ascii, 15% fewer on scroll, 5% fewer on sgr and
    3% fewer on unicode than SSE2. No corpus got slower. The scroll gain
    has not been profiled, so it is not attributed to a specific kernel.
    
    Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
    Claude-Session: https://claude.ai/code/session_013EAviPfxPDDoPwP4mjK41P
---
 meson.build              |  17 ++++
 meson_options.txt        |   4 +
 src/bin/meson.build      |   1 +
 src/bin/simd/simd.c      | 137 ++++++++++++++++++++++------
 src/bin/simd/simd.h      |  25 +++++-
 src/bin/simd/simd_avx2.c | 230 +++++++++++++++++++++++++++++++++++++++++++++++
 src/bin/termpty.c        |   6 +-
 src/bin/tytest.c         |   5 +-
 8 files changed, 393 insertions(+), 32 deletions(-)

diff --git a/meson.build b/meson.build
index aeb42df5..c1be0cd3 100644
--- a/meson.build
+++ b/meson.build
@@ -81,6 +81,9 @@ foreach efl_dep: efl_deps
   if efl_dep == 'edje'
     edje_cc_path = dep.get_variable(pkgconfig: 'prefix') + '/bin/edje_cc'
   endif
+  if efl_dep == 'eina'
+    eina_dep = dep
+  endif
   if efl_dep == 'eet'
     eet_path = dep.get_variable(pkgconfig: 'prefix') + '/bin/eet'
   endif
@@ -93,6 +96,20 @@ if cc.has_function('evas_object_textgrid_ligatures_set', dependencies: evas_dep)
   config_data.set('HAVE_TEXTGRID_LIGATURES', 1)
 endif
 
+if get_option('simd-avx2')
+  if host_machine.cpu_family() != 'x86_64'
+    error('simd-avx2 is only supported on x86_64')
+  endif
+  # Eina gained EINA_CPU_AVX2 after 1.28.1, and git builds of that period
+  # report the same version, so the symbol is probed rather than the version.
+  if not cc.has_header_symbol('Eina.h', 'EINA_CPU_AVX2',
+                              dependencies: eina_dep)
+    error('simd-avx2 needs an Eina that provides EINA_CPU_AVX2 ' +
+          '(EFL newer than 1.28.1)')
+  endif
+  add_project_arguments('-DTERMINOLOGY_ENABLE_AVX2', language: 'c')
+endif
+
 if cc.has_function('mkstemps')
   config_data.set('HAVE_MKSTEMPS', 1)
 endif
diff --git a/meson_options.txt b/meson_options.txt
index 57917b70..a7559f2e 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -16,6 +16,10 @@ option('benchmarks',
        type: 'boolean',
        value: false,
        description: 'Enable generating tybench, used to benchmark the pty intake path. (default=false)')
+option('simd-avx2',
+       type: 'boolean',
+       value: false,
+       description: 'Build the AVX2 intake kernels, used when the CPU supports them. Requires an Eina providing EINA_CPU_AVX2 (newer than EFL 1.28.1). (default=false)')
 option('nls',
        type: 'boolean',
        value: true,
diff --git a/src/bin/meson.build b/src/bin/meson.build
index f2cf7cd6..cc0c7b1c 100644
--- a/src/bin/meson.build
+++ b/src/bin/meson.build
@@ -2,6 +2,7 @@ simd_sources = ['simd/simd.c',
                 'simd/simd_scalar.c',
                 'simd/simd_neon.c',
                 'simd/simd_sse2.c',
+                'simd/simd_avx2.c',
                 'simd/simd.h']
 
 # X11 colour names for the OSC 10/11/12 parsers
diff --git a/src/bin/simd/simd.c b/src/bin/simd/simd.c
index ed4c0fe9..c79916c8 100644
--- a/src/bin/simd/simd.c
+++ b/src/bin/simd/simd.c
@@ -9,6 +9,9 @@ static Eina_Bool _use_simd = EINA_TRUE;
 #else
 static Eina_Bool _use_simd = EINA_FALSE;
 #endif
+#if defined(TERMINOLOGY_HAVE_AVX2)
+static Eina_Bool _use_avx2 = EINA_FALSE;
+#endif
 
 void
 simd_init(void)
@@ -20,6 +23,10 @@ simd_init(void)
    if (s && s[0] && strcmp(s, "0") != 0)
      _use_simd = EINA_FALSE;
 #endif
+#if defined(TERMINOLOGY_HAVE_AVX2)
+   _use_avx2 = (_use_simd && (eina_cpu_features_get() & EINA_CPU_AVX2))
+      ? EINA_TRUE : EINA_FALSE;
+#endif
 }
 
 Eina_Bool
@@ -43,6 +50,10 @@ simd_scan_plain_ascii(const unsigned char *buf, size_t len)
    if (EINA_LIKELY(_use_simd))
      return VEC(scan_plain_ascii)(buf, len);
 #endif
+#if defined(TERMINOLOGY_HAVE_AVX2)
+   if (EINA_LIKELY(_use_avx2))
+     return simd_scan_plain_ascii_avx2(buf, len);
+#endif
 #if defined(TERMINOLOGY_HAVE_SSE2)
    if (EINA_LIKELY(_use_simd))
      return simd_scan_plain_ascii_sse2(buf, len);
@@ -57,6 +68,10 @@ simd_scan_plain_ascii_u32(const Eina_Unicode *buf, size_t len)
    if (EINA_LIKELY(_use_simd))
      return VEC(scan_plain_ascii_u32)(buf, len);
 #endif
+#if defined(TERMINOLOGY_HAVE_AVX2)
+   if (EINA_LIKELY(_use_avx2))
+     return simd_scan_plain_ascii_u32_avx2(buf, len);
+#endif
 #if defined(TERMINOLOGY_HAVE_SSE2)
    if (EINA_LIKELY(_use_simd))
      return simd_scan_plain_ascii_u32_sse2(buf, len);
@@ -71,6 +86,10 @@ simd_rscan_nonzero(const unsigned char *buf, size_t len)
    if (EINA_LIKELY(_use_simd))
      return simd_rscan_nonzero_neon(buf, len);
 #endif
+#if defined(TERMINOLOGY_HAVE_AVX2)
+   if (EINA_LIKELY(_use_avx2))
+     return simd_rscan_nonzero_avx2(buf, len);
+#endif
 #if defined(TERMINOLOGY_HAVE_SSE2)
    if (EINA_LIKELY(_use_simd))
      return simd_rscan_nonzero_sse2(buf, len);
@@ -89,6 +108,13 @@ simd_records_or_byte(void *buf, size_t n, size_t rec, size_t off,
         return;
      }
 #endif
+#if defined(TERMINOLOGY_HAVE_AVX2)
+   if (EINA_LIKELY(_use_avx2))
+     {
+        simd_records_or_byte_avx2(buf, n, rec, off, bit);
+        return;
+     }
+#endif
 #if defined(TERMINOLOGY_HAVE_SSE2)
    if (EINA_LIKELY(_use_simd))
      {
@@ -109,6 +135,13 @@ simd_widen_ascii(const unsigned char *buf, size_t len, Eina_Unicode *out)
         return;
      }
 #endif
+#if defined(TERMINOLOGY_HAVE_AVX2)
+   if (EINA_LIKELY(_use_avx2))
+     {
+        simd_widen_ascii_avx2(buf, len, out);
+        return;
+     }
+#endif
 #if defined(TERMINOLOGY_HAVE_SSE2)
    if (EINA_LIKELY(_use_simd))
      {
@@ -455,13 +488,51 @@ _test_ref_widen(void)
      }
 }
 
-/* From here on, the vector kernels against the scalar ones. Both targets
- * implement the same set, so the tests are written once, against the
- * kernel defined by VEC above. */
+/* From here on, the vector kernels against the scalar ones. Every kernel set
+ * implements the same functions, so the tests are written once and run for
+ * each set in the table below. */
 #if defined(TERMINOLOGY_HAVE_NEON) || defined(TERMINOLOGY_HAVE_SSE2)
 
+typedef struct tag_SimdVector
+{
+   Eina_Cpu_Features required;
+   size_t (*scan)(const unsigned char *buf, size_t len);
+   size_t (*scan_u32)(const Eina_Unicode *buf, size_t len);
+   size_t (*rscan)(const unsigned char *buf, size_t len);
+   void (*records_or)(void *buf, size_t n, size_t rec, size_t off,
+                      unsigned char bit);
+   void (*widen)(const unsigned char *buf, size_t len, Eina_Unicode *out);
+} SimdVector;
+
+static const SimdVector _vectors[] = {
+#if defined(TERMINOLOGY_HAVE_NEON)
+   { 0,
+     simd_scan_plain_ascii_neon,
+     simd_scan_plain_ascii_u32_neon,
+     simd_rscan_nonzero_neon,
+     simd_records_or_byte_neon,
+     simd_widen_ascii_neon },
+#endif
+#if defined(TERMINOLOGY_HAVE_SSE2)
+   { 0,
+     simd_scan_plain_ascii_sse2,
+     simd_scan_plain_ascii_u32_sse2,
+     simd_rscan_nonzero_sse2,
+     simd_records_or_byte_sse2,
+     simd_widen_ascii_sse2 },
+#endif
+#if defined(TERMINOLOGY_HAVE_AVX2)
+   { EINA_CPU_AVX2,
+     simd_scan_plain_ascii_avx2,
+     simd_scan_plain_ascii_u32_avx2,
+     simd_rscan_nonzero_avx2,
+     simd_records_or_byte_avx2,
+     simd_widen_ascii_avx2 },
+#endif
+};
+
 static void
-_test_scan(void)
+_test_scan(const SimdVector *set)
 {
    size_t len, off;
    int density;
@@ -477,7 +548,7 @@ _test_scan(void)
 
                   _fill(p, len, density);
                   assert(simd_scan_plain_ascii_scalar(p, len) ==
-                         VEC(scan_plain_ascii)(p, len));
+                         set->scan(p, len));
                   assert(_guards_intact(base, off + len));
                   free(base);
                }
@@ -486,7 +557,7 @@ _test_scan(void)
 }
 
 static void
-_test_scan_u32(void)
+_test_scan_u32(const SimdVector *set)
 {
    size_t len, off;
    int density;
@@ -505,7 +576,7 @@ _test_scan_u32(void)
 
                   _fill_u32(p, len, density);
                   assert(simd_scan_plain_ascii_u32_scalar(p, len) ==
-                         VEC(scan_plain_ascii_u32)(p, len));
+                         set->scan_u32(p, len));
                   assert(_guards_intact(base,
                                         (off + len) * sizeof(Eina_Unicode)));
                   free(base);
@@ -515,7 +586,7 @@ _test_scan_u32(void)
 }
 
 static void
-_test_rscan(void)
+_test_rscan(const SimdVector *set)
 {
    size_t len, off, pos;
    int density;
@@ -537,7 +608,7 @@ _test_rscan(void)
                     }
 
                   assert(simd_rscan_nonzero_scalar(p, len) ==
-                         VEC(rscan_nonzero)(p, len));
+                         set->rscan(p, len));
                   assert(_guards_intact(base, off + len));
                   free(base);
                }
@@ -555,17 +626,18 @@ _test_rscan(void)
              memset(buf, 0, sizeof(buf));
              buf[pos] = 0x01;
              assert(simd_rscan_nonzero_scalar(buf, len) == pos + 1);
-             assert(VEC(rscan_nonzero)(buf, len) == pos + 1);
+             assert(set->rscan(buf, len) == pos + 1);
           }
      }
 }
 
 static void
-_test_records_or(void)
+_test_records_or(const SimdVector *set)
 {
    size_t n, off, rec, i;
 
-   /* Both vectorised strides (12 and 16 bytes) and ones that fall back. */
+   /* The 16-byte stride every kernel set vectorises, the 12-byte one only
+    * some do, and the others fall back to scalar. */
    for (rec = 11; rec <= 16; rec++)
      {
         for (n = 0; n <= 40; n++)
@@ -584,7 +656,7 @@ _test_records_or(void)
                     }
 
                   simd_records_or_byte_scalar(a + GUARD, n, rec, off, 0x40);
-                  VEC(records_or_byte)(b + GUARD, n, rec, off, 0x40);
+                  set->records_or(b + GUARD, n, rec, off, 0x40);
 
                   assert(memcmp(a + GUARD, b + GUARD, n * rec) == 0);
                   assert(_guards_intact(a, n * rec));
@@ -597,7 +669,7 @@ _test_records_or(void)
 }
 
 static void
-_test_widen(void)
+_test_widen(const SimdVector *set)
 {
    size_t len, off, i;
 
@@ -620,7 +692,7 @@ _test_widen(void)
                }
 
              simd_widen_ascii_scalar(p, len, o1);
-             VEC(widen_ascii)(p, len, o2);
+             set->widen(p, len, o2);
 
              assert(memcmp(o1, o2, (len + 8) * sizeof(Eina_Unicode)) == 0);
              for (i = 0; i < 8; i++)
@@ -636,7 +708,7 @@ _test_widen(void)
 
 /* Every byte value, at every position, exhaustively. */
 static void
-_test_every_byte(void)
+_test_every_byte(const SimdVector *set)
 {
    unsigned int v;
    size_t len, pos;
@@ -652,14 +724,14 @@ _test_every_byte(void)
                   memset(buf, 'x', sizeof(buf));
                   buf[pos] = (unsigned char)v;
                   assert(simd_scan_plain_ascii_scalar(buf, len) ==
-                         VEC(scan_plain_ascii)(buf, len));
+                         set->scan(buf, len));
                }
           }
      }
 }
 
 static void
-_test_every_u32_boundary(void)
+_test_every_u32_boundary(const SimdVector *set)
 {
    static const Eina_Unicode vals[] = {
         0x00, 0x01, 0x1f, 0x20, 0x21, 0x7d, 0x7e, 0x7f, 0x80, 0xa0,
@@ -679,7 +751,7 @@ _test_every_u32_boundary(void)
                   for (i = 0; i < len; i++) buf[i] = 'x';
                   buf[pos] = vals[v];
                   assert(simd_scan_plain_ascii_u32_scalar(buf, len) ==
-                         VEC(scan_plain_ascii_u32)(buf, len));
+                         set->scan_u32(buf, len));
                }
           }
      }
@@ -696,13 +768,26 @@ tytest_simd_parity(void)
    _test_ref_records_or();
    _test_ref_widen();
 #if defined(TERMINOLOGY_HAVE_NEON) || defined(TERMINOLOGY_HAVE_SSE2)
-   _test_scan();
-   _test_scan_u32();
-   _test_rscan();
-   _test_records_or();
-   _test_widen();
-   _test_every_byte();
-   _test_every_u32_boundary();
+   {
+      size_t k;
+
+      for (k = 0; k < sizeof(_vectors) / sizeof(_vectors[0]); k++)
+        {
+           const SimdVector *set = &_vectors[k];
+
+           /* Calls the kernels directly, so a kernel set the CPU lacks must
+            * be skipped here whatever TERMINOLOGY_SIMD_DISABLE says. */
+           if ((eina_cpu_features_get() & set->required) != set->required)
+             continue;
+           _test_scan(set);
+           _test_scan_u32(set);
+           _test_rscan(set);
+           _test_records_or(set);
+           _test_widen(set);
+           _test_every_byte(set);
+           _test_every_u32_boundary(set);
+        }
+   }
 #endif
    return 0;
 }
diff --git a/src/bin/simd/simd.h b/src/bin/simd/simd.h
index 5c65dfa4..19190bf3 100644
--- a/src/bin/simd/simd.h
+++ b/src/bin/simd/simd.h
@@ -15,6 +15,10 @@
 #endif
 #if defined(__x86_64__)
 # define TERMINOLOGY_HAVE_SSE2 1
+/* AVX2 is optional (-Dsimd-avx2=true) and probed at runtime by simd_init(). */
+# if defined(TERMINOLOGY_ENABLE_AVX2)
+#  define TERMINOLOGY_HAVE_AVX2 1
+# endif
 #endif
 
 /* Index of the first byte that is not plain printable ASCII (0x20..0x7e),
@@ -27,6 +31,9 @@ size_t simd_scan_plain_ascii_neon(const unsigned char *buf, size_t len);
 #if defined(TERMINOLOGY_HAVE_SSE2)
 size_t simd_scan_plain_ascii_sse2(const unsigned char *buf, size_t len);
 #endif
+#if defined(TERMINOLOGY_HAVE_AVX2)
+size_t simd_scan_plain_ascii_avx2(const unsigned char *buf, size_t len);
+#endif
 
 /* Same, over decoded codepoints. */
 size_t simd_scan_plain_ascii_u32(const Eina_Unicode *buf, size_t len);
@@ -37,6 +44,9 @@ size_t simd_scan_plain_ascii_u32_neon(const Eina_Unicode *buf, size_t len);
 #if defined(TERMINOLOGY_HAVE_SSE2)
 size_t simd_scan_plain_ascii_u32_sse2(const Eina_Unicode *buf, size_t len);
 #endif
+#if defined(TERMINOLOGY_HAVE_AVX2)
+size_t simd_scan_plain_ascii_u32_avx2(const Eina_Unicode *buf, size_t len);
+#endif
 
 /* Index one past the last non-zero byte, or 0 if every byte is zero. */
 size_t simd_rscan_nonzero(const unsigned char *buf, size_t len);
@@ -47,6 +57,9 @@ size_t simd_rscan_nonzero_neon(const unsigned char *buf, size_t len);
 #if defined(TERMINOLOGY_HAVE_SSE2)
 size_t simd_rscan_nonzero_sse2(const unsigned char *buf, size_t len);
 #endif
+#if defined(TERMINOLOGY_HAVE_AVX2)
+size_t simd_rscan_nonzero_avx2(const unsigned char *buf, size_t len);
+#endif
 
 /* OR 'bit' into byte 'off' of each of 'n' records of 'rec' bytes. 'off' must
  * be less than 'rec'. */
@@ -62,6 +75,10 @@ void simd_records_or_byte_neon(void *buf, size_t n, size_t rec, size_t off,
 void simd_records_or_byte_sse2(void *buf, size_t n, size_t rec, size_t off,
                                unsigned char bit);
 #endif
+#if defined(TERMINOLOGY_HAVE_AVX2)
+void simd_records_or_byte_avx2(void *buf, size_t n, size_t rec, size_t off,
+                               unsigned char bit);
+#endif
 
 /* Widen bytes already known to be plain printable ASCII into codepoints. */
 void simd_widen_ascii(const unsigned char *buf, size_t len, Eina_Unicode *out);
@@ -75,9 +92,15 @@ void simd_widen_ascii_neon(const unsigned char *buf, size_t len,
 void simd_widen_ascii_sse2(const unsigned char *buf, size_t len,
                            Eina_Unicode *out);
 #endif
+#if defined(TERMINOLOGY_HAVE_AVX2)
+void simd_widen_ascii_avx2(const unsigned char *buf, size_t len,
+                           Eina_Unicode *out);
+#endif
 
 /* Read TERMINOLOGY_SIMD_DISABLE, which switches the vector kernels off without
- * a rebuild. Modelled on EFL's EVAS_NEON_DISABLE. */
+ * a rebuild. Modelled on EFL's EVAS_NEON_DISABLE. When AVX2 is compiled in,
+ * also enable it if Eina reports the CPU supports it. Eina reports no feature
+ * before eina_init(), so call this after it or the AVX2 kernels stay unused. */
 void simd_init(void);
 
 Eina_Bool simd_enabled(void);
diff --git a/src/bin/simd/simd_avx2.c b/src/bin/simd/simd_avx2.c
new file mode 100644
index 00000000..5364322b
--- /dev/null
+++ b/src/bin/simd/simd_avx2.c
@@ -0,0 +1,230 @@
+/* AVX2 intake kernels. The target attribute keeps the rest of the binary on
+ * baseline x86-64; the dispatcher only reaches these after a runtime probe. */
+#include "private.h"
+#include "simd.h"
+#include <string.h>
+
+#if defined(TERMINOLOGY_HAVE_AVX2)
+
+#include <immintrin.h>
+
+#if defined(__GNUC__)
+# define AVX2_TARGET __attribute__((target("avx2")))
+#else
+# error AVX2 kernels need GCC or clang target attributes
+#endif
+
+AVX2_TARGET
+size_t
+simd_scan_plain_ascii_avx2(const unsigned char *buf, size_t len)
+{
+   const __m256i lo = _mm256_set1_epi8(0x20);
+   const __m256i hi = _mm256_set1_epi8(0x7e);
+   size_t i = 0;
+
+   for (; i + 32 <= len; i += 32)
+     {
+        __m256i v = _mm256_loadu_si256((const __m256i *)(buf + i));
+        __m256i bad;
+        unsigned int m;
+
+        /* c < 0x20 || c >= 0x7f. Signed compares cover both: bytes with the
+         * high bit set are negative, so they fail the first test, and 0x7f
+         * alone fails "signed > 0x7e". */
+        bad = _mm256_or_si256(_mm256_cmpgt_epi8(lo, v),
+                              _mm256_cmpgt_epi8(v, hi));
+        m = (unsigned int)_mm256_movemask_epi8(bad);
+        if (m) return i + (size_t)__builtin_ctz(m);
+     }
+
+   /* Runs are short in terminal output: do not fall from 32 bytes to scalar. */
+   if (i + 16 <= len)
+     {
+        __m128i v = _mm_loadu_si128((const __m128i *)(buf + i));
+        __m128i bad;
+        unsigned int m;
+
+        bad = _mm_or_si128(_mm_cmplt_epi8(v, _mm_set1_epi8(0x20)),
+                           _mm_cmpgt_epi8(v, _mm_set1_epi8(0x7e)));
+        m = (unsigned int)_mm_movemask_epi8(bad);
+        if (m) return i + (size_t)__builtin_ctz(m);
+        i += 16;
+     }
+
+   for (; i < len; i++)
+     {
+        unsigned char c = buf[i];
+
+        if ((c < 0x20) || (c >= 0x7f)) return i;
+     }
+   return len;
+}
+
+AVX2_TARGET
+size_t
+simd_scan_plain_ascii_u32_avx2(const Eina_Unicode *buf, size_t len)
+{
+   /* AVX2 has no unsigned 32-bit compare either, so g < 0x20 || g >= 0x7f is
+    * split across a wrapping subtraction and a signed compare, as in the SSE2
+    * kernel. */
+   const __m256i bias = _mm256_set1_epi32(0x20);
+   const __m256i span = _mm256_set1_epi32(0x7e);
+   size_t i = 0;
+
+   for (; i + 8 <= len; i += 8)
+     {
+        __m256i v = _mm256_loadu_si256((const __m256i *)(buf + i));
+        __m256i under = _mm256_sub_epi32(v, bias);
+        __m256i over = _mm256_cmpgt_epi32(v, span);
+        /* movemask keeps the top bit of every byte, so lane k owns bits
+         * 4k..4k+3 and the lowest set bit gives the first offending lane
+         * through ctz >> 2. 'over' sets all four bits of a lane for
+         * 0x7f..0x7fffffff. 'under' catches the rest: g - 0x20 has a byte
+         * with its top bit set for every g below 0x20 and every g from
+         * 0x80000000 up (the low byte for 0x80000000..0x8000001f, the high
+         * byte beyond), while it is below 0x80 in every byte for 0x20..0x7e. */
+        unsigned int m = (unsigned int)(_mm256_movemask_epi8(under) |
+                                        _mm256_movemask_epi8(over));
+
+        if (m) return i + (size_t)(__builtin_ctz(m) >> 2);
+     }
+
+   if (i + 4 <= len)
+     {
+        __m128i v = _mm_loadu_si128((const __m128i *)(buf + i));
+        __m128i under = _mm_sub_epi32(v, _mm_set1_epi32(0x20));
+        __m128i over = _mm_cmpgt_epi32(v, _mm_set1_epi32(0x7e));
+        unsigned int m = (unsigned int)(_mm_movemask_epi8(under) |
+                                        _mm_movemask_epi8(over));
+
+        if (m) return i + (size_t)(__builtin_ctz(m) >> 2);
+        i += 4;
+     }
+
+   for (; i < len; i++)
+     {
+        Eina_Unicode g = buf[i];
+
+        if ((g < 0x20) || (g >= 0x7f)) return i;
+     }
+   return len;
+}
+
+AVX2_TARGET
+size_t
+simd_rscan_nonzero_avx2(const unsigned char *buf, size_t len)
+{
+   const __m256i zero = _mm256_setzero_si256();
+   size_t i = len;
+
+   while (i >= 32)
+     {
+        __m256i v = _mm256_loadu_si256((const __m256i *)(buf + i - 32));
+        unsigned int m;
+
+        /* 0xff in every lane whose byte is zero. */
+        m = (unsigned int)_mm256_movemask_epi8(_mm256_cmpeq_epi8(v, zero));
+        if (m != 0xffffffffu)
+          {
+             /* Highest clear bit is the last non-zero byte of this block. */
+             size_t lane = 31 - (size_t)__builtin_clz(~m);
+
+             return i - 32 + lane + 1;
+          }
+        i -= 32;
+     }
+
+   if (i >= 16)
+     {
+        __m128i v = _mm_loadu_si128((const __m128i *)(buf + i - 16));
+        unsigned int m;
+
+        m = (unsigned int)_mm_movemask_epi8(_mm_cmpeq_epi8(v,
+                                                           _mm_setzero_si128()));
+        if (m != 0xffffu)
+          {
+             size_t lane = 31 - (size_t)__builtin_clz(~m & 0xffffu);
+
+             return i - 16 + lane + 1;
+          }
+        i -= 16;
+     }
+
+   while (i > 0)
+     {
+        if (buf[i - 1]) return i;
+        i--;
+     }
+   return 0;
+}
+
+AVX2_TARGET
+void
+simd_records_or_byte_avx2(void *buf, size_t n, size_t rec, size_t off,
+                          unsigned char bit)
+{
+   unsigned char *p = (unsigned char *)buf;
+   size_t i = 0;
+
+   /* Only the 16-byte stride, which is Termcell's: a 256-bit register covers
+    * two records and an odd last record goes to the scalar loop. The 12-byte
+    * stride the SSE2 and NEON kernels also handle predates the current
+    * Termcell and is left to the scalar form. */
+   if ((rec == 16) && (off < rec))
+     {
+        unsigned char pat[32];
+        __m256i m;
+
+        memset(pat, 0, sizeof(pat));
+        pat[off] = bit;
+        pat[rec + off] = bit;
+        m = _mm256_loadu_si256((const __m256i *)pat);
+
+        for (; i + 2 <= n; i += 2, p += 32)
+          {
+             __m256i v = _mm256_loadu_si256((const __m256i *)p);
+
+             _mm256_storeu_si256((__m256i *)p, _mm256_or_si256(v, m));
+          }
+     }
+
+   for (; i < n; i++, p += rec)
+     p[off] |= bit;
+}
+
+AVX2_TARGET
+void
+simd_widen_ascii_avx2(const unsigned char *buf, size_t len, Eina_Unicode *out)
+{
+   size_t i = 0;
+
+   /* vpmovzxbd widens the low eight bytes of a 128-bit register, so four of
+    * them cover 32 input bytes. */
+   for (; i + 32 <= len; i += 32)
+     {
+        const unsigned char *in = buf + i;
+        Eina_Unicode *o = out + i;
+        __m256i a = _mm256_cvtepu8_epi32(_mm_loadl_epi64((const __m128i *)in));
+        __m256i b = _mm256_cvtepu8_epi32(
+           _mm_loadl_epi64((const __m128i *)(in + 8)));
+        __m256i c = _mm256_cvtepu8_epi32(
+           _mm_loadl_epi64((const __m128i *)(in + 16)));
+        __m256i d = _mm256_cvtepu8_epi32(
+           _mm_loadl_epi64((const __m128i *)(in + 24)));
+
+        _mm256_storeu_si256((__m256i *)o, a);
+        _mm256_storeu_si256((__m256i *)(o + 8), b);
+        _mm256_storeu_si256((__m256i *)(o + 16), c);
+        _mm256_storeu_si256((__m256i *)(o + 24), d);
+     }
+
+   for (; i + 8 <= len; i += 8)
+     _mm256_storeu_si256((__m256i *)(out + i),
+                         _mm256_cvtepu8_epi32(
+                            _mm_loadl_epi64((const __m128i *)(buf + i))));
+
+   for (; i < len; i++)
+     out[i] = buf[i];
+}
+
+#endif
diff --git a/src/bin/termpty.c b/src/bin/termpty.c
index 2c01110a..1d36dda4 100644
--- a/src/bin/termpty.c
+++ b/src/bin/termpty.c
@@ -1051,9 +1051,9 @@ _mark_autowrapped(Termcell *cells, ssize_t n)
      }
 
    _Static_assert(sizeof(Termcell) == 16,
-                  "Termcell size changed: simd_records_or_byte() vectorises "
-                  "12- and 16-byte strides and silently falls back to scalar "
-                  "otherwise");
+                  "Termcell size changed: simd_records_or_byte() only "
+                  "vectorises a 16-byte stride on every target and silently "
+                  "falls back to scalar otherwise");
    simd_records_or_byte(cells, (size_t)n, sizeof(Termcell), off, bit);
 #undef NOT_PROBED
 }
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 206fb17f..7a84f24b 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -725,7 +725,9 @@ main(int argc, char **argv)
    /* Before the argument loop: the unit-test path returns out of it without
     * ever reaching tytest_common_init(), and several of those tests drive the
     * real parser. Leaving the kernels un-dispatched there would make
-    * TERMINOLOGY_SIMD_DISABLE silently ineffective for 'tytest all'. */
+    * TERMINOLOGY_SIMD_DISABLE silently ineffective for 'tytest all'. Eina
+    * comes first so that simd_init() sees the CPU features. */
+   eina_init();
    simd_init();
 
    for (i = 1; i < argc; i++)
@@ -741,7 +743,6 @@ main(int argc, char **argv)
         else return _run_tytests(argc, argv);
      }
 
-   eina_init();
    emile_init();
    ecore_init();
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to