Commit 993de3d7 for tesseract

commit 993de3d7f26b9963e237ef8680621e8d5a1563d0
Author: Stefan Weil <sw@weilnetz.de>
Date:   Sat Nov 14 14:07:59 2026 +0100

    lstm: use NEON for tanh, the logistic function and the conversion to int8_t

    A profile of the recognition with tessdata_fast/eng on an Apple M1
    shows that the table lookups of Tanh and Logistic, which were done
    value by value with branches, take 15 % and 11 % of the time, and the
    conversion of the results to int8_t in NetworkIO::WriteTimeStepPart
    1 %.

    Add the functions for tanh, the logistic function and the conversion
    to int8_t for whole vectors with NEON and FAST_FLOAT for ARM64,
    src/arch/functionsneon.cpp, which handle four values per pass without
    branches. They load the table entries one by one, like the
    implementations for AVX2. The linear interpolation is written as the
    same expression in the NEON code as in the scalar code,
    t0 + (t1 - t0) * fraction, so that the compiler fuses it to a
    multiply-add in the same way in both and the results stay identical.
    They are used if the integer matrix for NEON is selected.

    The new functions do the same calculations in the same order, so the
    results are exactly the same as before, also when they are built with
    g++. Huge values are limited before they are scaled, so no floating
    point exception is raised for them.

    Recognition of 36 pages with tessdata_fast/eng is 1.23 times faster
    on an Apple M1 Pro (Apple clang 21: 17.1 s -> 13.8 s) and 1.27 times
    faster on the Cortex-X925 cores of a DGX Spark (g++ 13:
    14.7 s -> 11.6 s), recognition of 12 pages with tessdata_best/eng
    1.11 times (16.1 s -> 14.4 s) and 1.09 times (12.1 s -> 11.1 s), and
    training 1.03 times (15.83 s -> 15.38 s for 1000 lines on the M1,
    10.4 s -> 10.1 s on the X925). The text results and the results of
    the training are identical.

    Add the results for ARM64 to benchmark/lstm/README.md.

    Assisted-by: OpenCode / qwen3.8-27b-thinking (Alibaba Cloud)
    Signed-off-by: Stefan Weil <sw@weilnetz.de>

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7a25f639..3a9689de 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -685,10 +685,11 @@ if(HAVE_SSE4_1)
 endif(HAVE_SSE4_1)
 if(HAVE_NEON)
   list(APPEND arch_files_opt src/arch/dotproductneon.cpp
-       src/arch/intsimdmatrixneon.cpp)
+       src/arch/functionsneon.cpp src/arch/intsimdmatrixneon.cpp)
   if(NEON_COMPILE_FLAGS)
     set_source_files_properties(
-      src/arch/dotproductneon.cpp src/arch/intsimdmatrixneon.cpp
+      src/arch/dotproductneon.cpp src/arch/functionsneon.cpp
+      src/arch/intsimdmatrixneon.cpp
       PROPERTIES COMPILE_FLAGS ${NEON_COMPILE_FLAGS})
   endif()
 endif(HAVE_NEON)
@@ -766,6 +767,7 @@ if(ENABLE_PRECOMPILED_HEADERS)
     src/arch/dotproductsse.cpp
     src/arch/dotproductneon.cpp
     src/arch/functionsavx2.cpp
+    src/arch/functionsneon.cpp
     src/arch/intsimdmatrixavx2.cpp
     src/arch/intsimdmatrixsse.cpp
     src/arch/intsimdmatrixneon.cpp
diff --git a/Makefile.am b/Makefile.am
index 04c4f238..46d351ac 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -209,8 +209,11 @@ if OPENMP_SIMD
 libtesseract_neon_la_CXXFLAGS += -fopenmp-simd -DOPENMP_SIMD
 endif
 libtesseract_neon_la_CXXFLAGS += -I$(top_srcdir)/src/ccutil
+libtesseract_neon_la_CXXFLAGS += -I$(top_srcdir)/src/arch
+libtesseract_neon_la_CXXFLAGS += -I$(top_srcdir)/src/lstm
 libtesseract_neon_la_SOURCES = src/arch/intsimdmatrixneon.cpp
 libtesseract_neon_la_SOURCES += src/arch/dotproductneon.cpp
+libtesseract_neon_la_SOURCES += src/arch/functionsneon.cpp
 libtesseract_la_LIBADD += libtesseract_neon.la
 noinst_LTLIBRARIES += libtesseract_neon.la
 endif
diff --git a/benchmark/lstm/README.md b/benchmark/lstm/README.md
index fc164d8f..b48a4e25 100644
--- a/benchmark/lstm/README.md
+++ b/benchmark/lstm/README.md
@@ -172,6 +172,51 @@ and after the new NEON functions for the integer matrix product:
   X925 and 1.55 times faster on A725. The text results are identical.
 - The float models do not use the integer matrix product.

+### Whole-vector functions
+
+"Before" is the commit "lstm: calculate several dot products at once with
+NEON", "after" the commit which adds the NEON implementations of the
+functions for tanh, the logistic function and the conversion to `int8_t`
+for whole vectors (`src/arch/functionsneon.cpp`).
+
+Time for 36 pages (see above, `tessdata_fast`) and for the first 12 of them
+(`tessdata_best`) on M1 and on the Cortex-X925 cores of the DGX Spark:
+
+| Models                     | M1 before | M1 after | X925 before | X925 after |
+| -------------------------- | --------: | -------: | ----------: | ---------: |
+| `tessdata_fast` (36 pages) |    17.1 s |   13.8 s |     14.7 s  |    11.6 s  |
+| `tessdata_best` (12 pages) |    16.1 s |   14.4 s |     12.1 s  |    11.1 s  |
+
+- Recognition is 1.23 times faster with the integer models on M1 and 1.27
+  times on the X925, and 1.11 times faster with the float models on M1 and
+  1.09 times on the X925. The text results are identical.
+
+Training time (1000 training lines) on M1 and the X925:
+
+| Settings   | M1 before | M1 after | X925 before | X925 after |
+| ---------- | --------: | -------: | ----------: | ---------: |
+| Default    |   15.83 s |  15.38 s |     10.4 s  |    10.1 s  |
+
+- The training is 1.03 times faster on both machines. The before and the
+  after runs give exactly the same results (`mean rms=5.173%`,
+  `delta=35.870%` on M1 and `delta=35.879%` on the X925), so the NEON
+  functions give exactly the same results as the scalar functions, also
+  with g++ 13.
+
+Time for the whole-vector functions with 192 values (best of several runs,
+`-O3`, Apple clang 21) compared to the generic implementations, which use
+the scalar functions:
+
+| Function            | Generic | NEON  |
+| ------------------- | ------: | ----: |
+| Tanh                |  175 ns | 98 ns |
+| Logistic            |  162 ns | 138 ns |
+| Conversion to `int8_t` | 41 ns | 37 ns |
+
+- Tanh is about 1.8 times faster (the other layer widths give 1.7 to
+  1.8 times), the conversion to `int8_t` about 1.1 times faster and the
+  logistic function a bit faster.
+
 ## Results for x86_64

 Measured in November 2026 on an Intel Core i3-1115G4 (Tiger Lake, AVX512),
diff --git a/cmake/SourceLists.cmake b/cmake/SourceLists.cmake
index ba745924..c2bca5ac 100644
--- a/cmake/SourceLists.cmake
+++ b/cmake/SourceLists.cmake
@@ -46,6 +46,7 @@ set(TESSERACT_SRC_ARCH_SSE41

 set(TESSERACT_SRC_ARCH_NEON
     src/arch/dotproductneon.cpp
+    src/arch/functionsneon.cpp
     src/arch/intsimdmatrixneon.cpp
 )

diff --git a/src/arch/dotproduct.h b/src/arch/dotproduct.h
index 5de07e9f..cab2a9f0 100644
--- a/src/arch/dotproduct.h
+++ b/src/arch/dotproduct.h
@@ -50,6 +50,13 @@ void RowDotProductsNEON(const float *w, int w_stride, int num_rows,
 // the num_v rows of v, which all have n elements and are stored without gaps.
 void CrossDotProductsNEON(const float *u, int num_u, const float *v, int num_v,
                           int n, float *out, int out_stride);
+
+// Non-linearity functions and conversion to int8_t for n values with NEON
+// (src/arch/functionsneon.cpp), see TanhVector, LogisticVector and
+// FloatToInt8Vector in simddetect.h.
+void TanhVectorNEON(int n, const float *in, float *out);
+void LogisticVectorNEON(int n, const float *in, float *out);
+void FloatToInt8VectorNEON(int n, const float *in, int8_t *out);
 #endif

 #if defined(FAST_FLOAT)
diff --git a/src/arch/functionsneon.cpp b/src/arch/functionsneon.cpp
new file mode 100644
index 00000000..d2b83fed
--- /dev/null
+++ b/src/arch/functionsneon.cpp
@@ -0,0 +1,154 @@
+///////////////////////////////////////////////////////////////////////
+// File:        functionsneon.cpp
+// Description: Non-linearity functions and conversion of floats to
+//              int8_t for whole vectors with NEON.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+///////////////////////////////////////////////////////////////////////
+
+#if !defined(__ARM_NEON)
+#  if defined(__aarch64__) || defined(__arm__)
+#    error Implementation only for NEON capable architectures
+#  endif
+#else
+
+#  include <arm_neon.h>
+#  include <cstdint>
+#  include "dotproduct.h"
+#  include "functions.h" // for Tanh, Logistic, TanhTable, LogisticTable
+
+namespace tesseract {
+
+// The functions give exactly the same results as the scalar functions Tanh,
+// Logistic and IntCastRounded, because they do the same calculations in the
+// same order. The linear interpolation is written as the same expression in
+// the NEON code as in the scalar code, t0 + (t1 - t0) * fraction, so that the
+// compiler applies the same FMA contraction to both and the results stay
+// identical, whatever the compiler decides to do. This is only used for
+// ARM64.
+#  if defined(FAST_FLOAT) && defined(__ARM_ARCH_ISA_A64)
+
+// Returns the interpolated values of the table (TanhTable or LogisticTable)
+// for the 4 values of a, which must not be negative, like the scalar
+// functions do for x >= 0.
+static inline float32x4_t Lookup(const float *table, float32x4_t a) {
+  // Values which are too large for the table (the result is 1 for them) are
+  // limited first, so the indices are always valid and nothing overflows,
+  // also for huge values and for NaN (vminq_f32 returns the second value
+  // then).
+  const float32x4_t limit = vdupq_n_f32((kTableSize - 1) / kScaleFactor);
+  float32x4_t clamped = vminq_f32(a, limit);
+  float32x4_t scaled = vmulq_f32(clamped, vdupq_n_f32(kScaleFactor));
+  int32x4_t index =
+      vminq_s32(vcvtq_s32_f32(scaled), vdupq_n_s32(kTableSize - 2));
+  // Each value gives table[i] and table[i + 1]. Loading the table entries one
+  // by one is faster than the gather instruction.
+  int32_t i[4];
+  vst1q_s32(i, index);
+  float t0[4] = {table[i[0]], table[i[1]], table[i[2]], table[i[3]]};
+  float t1[4] = {table[i[0] + 1], table[i[1] + 1], table[i[2] + 1],
+                 table[i[3] + 1]};
+  float32x4_t t0_vec = vld1q_f32(t0);
+  float32x4_t t1_vec = vld1q_f32(t1);
+  float32x4_t fraction = vsubq_f32(scaled, vcvtq_f32_s32(index));
+  // Linear interpolation. This is written as the same expression as the
+  // scalar code, t0 + (t1 - t0) * fraction, instead of using a NEON
+  // intrinsic, so that the compiler applies the same FMA contraction to it as
+  // to the scalar code and the results stay identical.
+  float32x4_t result = t0_vec + (t1_vec - t0_vec) * fraction;
+  // The result is 1 for values which are too large for the table.
+  uint32x4_t too_large = reinterpret_cast<uint32x4_t>(vcgeq_f32(a, limit));
+  return vbslq_f32(too_large, vdupq_n_f32(1.0f), result);
+}
+
+// Computes out[i] = Tanh(in[i]) for n values. in and out can be the same.
+void TanhVectorNEON(int n, const float *in, float *out) {
+  const uint32x4_t abs_mask = vdupq_n_u32(0x7fffffff);
+  const float32x4_t zero = vdupq_n_f32(0.0f);
+  int i = 0;
+  for (; i + 4 <= n; i += 4) {
+    float32x4_t x = vld1q_f32(in + i);
+    // The sign bits are removed to use the table for the absolute value.
+    float32x4_t a =
+        vreinterpretq_f32_u32(vandq_u32(vreinterpretq_u32_f32(x), abs_mask));
+    float32x4_t result = Lookup(TanhTable, a);
+    // Tanh(x) = -Tanh(-x) for x < 0.
+    uint32x4_t negative = reinterpret_cast<uint32x4_t>(vcltq_f32(x, zero));
+    result = vbslq_f32(negative, vsubq_f32(zero, result), result);
+    vst1q_f32(out + i, result);
+  }
+  for (; i < n; i++) {
+    out[i] = Tanh(in[i]);
+  }
+}
+
+// Computes out[i] = Logistic(in[i]) for n values. in and out can be the same.
+void LogisticVectorNEON(int n, const float *in, float *out) {
+  const uint32x4_t abs_mask = vdupq_n_u32(0x7fffffff);
+  const float32x4_t one = vdupq_n_f32(1.0f);
+  const float32x4_t zero = vdupq_n_f32(0.0f);
+  int i = 0;
+  for (; i + 4 <= n; i += 4) {
+    float32x4_t x = vld1q_f32(in + i);
+    // The sign bits are removed to use the table for the absolute value.
+    float32x4_t a =
+        vreinterpretq_f32_u32(vandq_u32(vreinterpretq_u32_f32(x), abs_mask));
+    float32x4_t result = Lookup(LogisticTable, a);
+    // Logistic(x) = 1 - Logistic(-x) for x < 0.
+    uint32x4_t negative = reinterpret_cast<uint32x4_t>(vcltq_f32(x, zero));
+    result = vbslq_f32(negative, vsubq_f32(one, result), result);
+    vst1q_f32(out + i, result);
+  }
+  for (; i < n; i++) {
+    out[i] = Logistic(in[i]);
+  }
+}
+
+// Computes out[i] = IntCastRounded(ClipToRange(in[i] * INT8_MAX, -INT8_MAX,
+// INT8_MAX)) for n values.
+void FloatToInt8VectorNEON(int n, const float *in, int8_t *out) {
+  const float32x4_t scale = vdupq_n_f32(INT8_MAX);
+  const float32x4_t lower = vdupq_n_f32(-INT8_MAX);
+  const float32x4_t half = vdupq_n_f32(0.5f);
+  const float32x4_t zero = vdupq_n_f32(0.0f);
+  int i = 0;
+  for (; i + 8 <= n; i += 8) {
+    float32x4_t y0 = vmulq_f32(vld1q_f32(in + i), scale);
+    float32x4_t y1 = vmulq_f32(vld1q_f32(in + i + 4), scale);
+    // NaN gives INT8_MAX.
+    y0 = vmaxq_f32(vminq_f32(y0, scale), lower);
+    y1 = vmaxq_f32(vminq_f32(y1, scale), lower);
+    // Round half away from zero: truncate |y| + 0.5 and keep the sign.
+    // (vcvtq_s32_f32 truncates toward zero, like the cast to int in the
+    // scalar function.)
+    int32x4_t r0pos = vcvtq_s32_f32(vaddq_f32(y0, half));
+    int32x4_t r0neg = vcvtq_s32_f32(vaddq_f32(vnegq_f32(y0), half));
+    uint32x4_t m0 = reinterpret_cast<uint32x4_t>(vcgeq_f32(y0, zero));
+    int32x4_t s0 = vbslq_s32(m0, r0pos, vnegq_s32(r0neg));
+    int32x4_t r1pos = vcvtq_s32_f32(vaddq_f32(y1, half));
+    int32x4_t r1neg = vcvtq_s32_f32(vaddq_f32(vnegq_f32(y1), half));
+    uint32x4_t m1 = reinterpret_cast<uint32x4_t>(vcgeq_f32(y1, zero));
+    int32x4_t s1 = vbslq_s32(m1, r1pos, vnegq_s32(r1neg));
+    // Values are in [-127, 127], so the narrowing conversions do not
+    // saturate.
+    vst1_s8(out + i, vqmovn_s16(vcombine_s16(vqmovn_s32(s0), vqmovn_s32(s1))));
+  }
+  for (; i < n; i++) {
+    out[i] = IntCastRounded(
+        ClipToRange<float>(in[i] * INT8_MAX, -INT8_MAX, INT8_MAX));
+  }
+}
+
+#  endif // FAST_FLOAT && __ARM_ARCH_ISA_A64
+
+} // namespace tesseract.
+
+#endif
diff --git a/src/arch/simddetect.cpp b/src/arch/simddetect.cpp
index ad94010e..cf84cb39 100644
--- a/src/arch/simddetect.cpp
+++ b/src/arch/simddetect.cpp
@@ -244,6 +244,15 @@ static void SetDotProduct(DotProductFunction f, const IntSimdMatrix *m = nullptr
     LogisticVector = LogisticVectorAVX2;
     FloatToInt8Vector = FloatToInt8VectorAVX2;
   }
+#endif
+#if defined(FAST_FLOAT) && defined(__ARM_ARCH_ISA_A64)
+  // The integer matrices for NEON are only selected for a CPU with NEON.
+  if (m == &IntSimdMatrix::intSimdMatrixNEON ||
+      m == &IntSimdMatrix::intSimdMatrixNEONDotProd) {
+    TanhVector = TanhVectorNEON;
+    LogisticVector = LogisticVectorNEON;
+    FloatToInt8Vector = FloatToInt8VectorNEON;
+  }
 #endif
   RowDotProductsFunction rows = RowDotProductsGeneric;
   CrossDotProductsFunction cross = CrossDotProductsGeneric;