Repository: tesseract-ocr/tesseract
File: src/arch/intsimdmatrixrvv.cpp (C++)

Code before:
// 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.
///////////////////////////////////////////////////////////////////////

#ifdef HAVE_CONFIG_H
#  include "config_auto.h" // for HAVE_RVV, ...
#endif

#if HAVE_RVV
#  include "intsimdmatrix.h"
#  include "tesstypes.h"

namespace tesseract {

// Number of outputs held in each register. 8 x 32 bit ints.
constexpr int kNumOutputsPerRegister = 1;
// Maximum number of registers that we will use.
constexpr int kMaxOutputRegisters = 1;
// Number of inputs in the inputs register.
constexpr int kNumInputsPerRegister = 1;
// Number of inputs in each weight group.
constexpr int kNumInputsPerGroup = 1;

static int DotProduct(const int8_t *u, const int8_t *v, int num) {
  int total = 0;

  asm __volatile__ (
    "  .option       arch, +v                   \n\t"
    "  vsetvli t0,zero,e32,m8,ta,ma             \n\t"
    "  vmv.v.i v0,0                             \n\t"
    "1:                                         \n\t"
    "  vsetvli t0,%[num],e8,m2,ta,ma            \n\t"
    "  vle8.v v16,0(%[u])                       \n\t"
    "  vle8.v v24,0(%[v])                       \n\t"
    "  sub %[num],%[num],t0                     \n\t"
    "  vwmul.vv v8,v24,v16                      \n\t"
    "  add %[u],%[u],t0                         \n\t"
    "  add %[v],%[v],t0                         \n\t"
    "  vsetvli zero,zero,e16,m4,tu,ma           \n\t"
    "  vwadd.wv v0,v0,v8                        \n\t"
    "  bnez %[num],1b                           \n\t"
    "  vsetvli t0,zero,e32,m8,ta,ma             \n\t"
    "  vmv.s.x v8,zero                          \n\t"
    "  vredsum.vs v0,v0,v8                      \n\t"
    "  vmv.x.s %[total],v0                      \n\t"
    :  [u] "+r" (u),
       [v] "+r" (v),

Review comment (refactor):
I know that some other files use similar code, but for this new implementation I think we should simplify the code: remove these constants with the comments und use the numeric values below.

Code after:
// 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.
///////////////////////////////////////////////////////////////////////

#ifdef HAVE_CONFIG_H
#  include "config_auto.h" // for HAVE_RVV, ...
#endif

#if HAVE_RVV
#  include "intsimdmatrix.h"
#  include "tesstypes.h"

namespace tesseract {

static int DotProduct(const int8_t *u, const int8_t *v, int num) {
  int total = 0;

  asm __volatile__ (
    "  .option       arch, +v                   \n\t"
    "  vsetvli t0,zero,e32,m8,ta,ma             \n\t"
    "  vmv.v.i v0,0                             \n\t"
    "1:                                         \n\t"
    "  vsetvli t0,%[num],e8,m2,ta,ma            \n\t"
    "  vle8.v v16,0(%[u])                       \n\t"
    "  vle8.v v24,0(%[v])                       \n\t"
    "  sub %[num],%[num],t0                     \n\t"
    "  vwmul.vv v8,v24,v16                      \n\t"
    "  add %[u],%[u],t0                         \n\t"
    "  add %[v],%[v],t0                         \n\t"
    "  vsetvli zero,zero,e16,m4,tu,ma           \n\t"
    "  vwadd.wv v0,v0,v8                        \n\t"
    "  bnez %[num],1b                           \n\t"
    "  vsetvli t0,zero,e32,m8,ta,ma             \n\t"
    "  vmv.s.x v8,zero                          \n\t"
    "  vredsum.vs v0,v0,v8                      \n\t"
    "  vmv.x.s %[total],v0                      \n\t"
    :  [u] "+r" (u),
       [v] "+r" (v),
       [num] "+r" (num),
       [total] "+r" (total)
    :
    :  "cc", "memory"
  );

  return total;
}