File: simd_wrappers.h

package info (click to toggle)
gemmlowp 0.0~git20211220.e844ffd-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 5,752 kB
  • sloc: cpp: 113,898; ansic: 9,221; python: 3,251; sh: 79; objc: 55; makefile: 16
file content (669 lines) | stat: -rw-r--r-- 25,588 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
// Copyright 2017 The Gemmlowp Authors. All Rights Reserved.
//
// 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.

// simd_wrappers.h: some inline functions wrapping SIMD intrinsics,
// extending the set of such functions from fixedpoint.h.

#ifndef GEMMLOWP_INTERNAL_SIMD_WRAPPERS_H_
#define GEMMLOWP_INTERNAL_SIMD_WRAPPERS_H_

#include <algorithm>
#include <type_traits>
#include "../fixedpoint/fixedpoint.h"

namespace gemmlowp {

template <typename ScalarType, int ScalarCount>
struct RegisterType {
  using Type = ScalarType;
};

inline std::int32_t Min(std::int32_t a, std::int32_t b) {
  return std::min(a, b);
}

inline std::int32_t Max(std::int32_t a, std::int32_t b) {
  return std::max(a, b);
}

inline void MulAdd(std::int32_t lhs, std::int32_t rhs, std::int32_t* acc) {
  *acc += lhs * rhs;
}

template <typename tScalarType, int tScalarCount>
struct RegisterBuffer {
  using ScalarType = tScalarType;
  static constexpr int kScalarCount = tScalarCount;
  using RegisterType = typename RegisterType<ScalarType, kScalarCount>::Type;
  static_assert((kScalarCount & (kScalarCount - 1)) == 0,
                "kScalarCount must be a power of two");
  static_assert(sizeof(RegisterType) % sizeof(ScalarType) == 0, "");
  static constexpr int kRegisterLanes =
      sizeof(RegisterType) / sizeof(ScalarType);
  static constexpr int kRegisterCount =
      (kScalarCount * sizeof(ScalarType) + sizeof(RegisterType) - 1) /
      sizeof(RegisterType);

  RegisterType reg[kRegisterCount];
};

template <typename tScalarType, int tRows, int tCols>
struct RegisterBlock {
  using ScalarType = tScalarType;
  static constexpr int kRows = tRows;
  static constexpr int kCols = tCols;
  static constexpr int kScalarCount = kRows * kCols;
  using BufferType = RegisterBuffer<ScalarType, kScalarCount>;
  using RegisterType = typename BufferType::RegisterType;
  static constexpr int kRegisterCount = BufferType::kRegisterCount;
  static constexpr int kRegisterLanes = BufferType::kRegisterLanes;

  BufferType buf;
};

template <typename RegisterBlockType>
struct RegisterBlockAddImpl {
  static RegisterBlockType Run(const RegisterBlockType& lhs,
                               const RegisterBlockType& rhs) {
    RegisterBlockType result;
    for (int i = 0; i < RegisterBlockType::kRegisterCount; i++) {
      result.buf.reg[i] = Add(lhs.buf.reg[i], rhs.buf.reg[i]);
    }
    return result;
  }
};

template <typename RegisterBlockType>
RegisterBlockType RegisterBlockAdd(const RegisterBlockType& lhs,
                                   const RegisterBlockType& rhs) {
  return RegisterBlockAddImpl<RegisterBlockType>::Run(lhs, rhs);
}

template <typename LhsType, typename RhsType>
struct ShouldFlipLhsRhs {
  static constexpr bool kValue =
      (LhsType::kScalarCount < RhsType::kScalarCount) ||
      (LhsType::kScalarCount == RhsType::kScalarCount &&
       (LhsType::kRows < RhsType::kRows));
};

template <typename LhsType, typename RhsType,
          bool Flip = ShouldFlipLhsRhs<LhsType, RhsType>::kValue>
struct FlipLhsRhs {
  using FlippedLhsType = LhsType;
  using FlippedRhsType = RhsType;
  static const FlippedLhsType& FlippedLhs(const LhsType& lhs,
                                          const RhsType& rhs) {
    (void)rhs;
    return lhs;
  }
  static const FlippedRhsType& FlippedRhs(const LhsType& lhs,
                                          const RhsType& rhs) {
    (void)lhs;
    return rhs;
  }
};

template <typename LhsType, typename RhsType>
struct FlipLhsRhs<LhsType, RhsType, true> {
  using FlippedLhsType = RhsType;
  using FlippedRhsType = LhsType;
  static const FlippedLhsType& FlippedLhs(const LhsType& lhs,
                                          const RhsType& rhs) {
    (void)lhs;
    return rhs;
  }
  static const FlippedRhsType& FlippedRhs(const LhsType& lhs,
                                          const RhsType& rhs) {
    (void)rhs;
    return lhs;
  }
};

template <typename Lhs, typename Rhs>
struct BroadcastBinaryOpShape {
  static constexpr int kRows =
      Lhs::kRows > Rhs::kRows ? Lhs::kRows : Rhs::kRows;
  static constexpr int kCols =
      Lhs::kCols > Rhs::kCols ? Lhs::kCols : Rhs::kCols;
};

template <typename Lhs, typename Rhs>
struct BroadcastBinaryOpRegisterBlock {
  using Shape = BroadcastBinaryOpShape<Lhs, Rhs>;
  using ScalarType = typename Lhs::ScalarType;
  using Type = RegisterBlock<ScalarType, Shape::kRows, Shape::kCols>;
};

template <typename Lhs, typename Rhs>
struct BroadcastAddImpl {
  using ResultBlockType =
      typename BroadcastBinaryOpRegisterBlock<Lhs, Rhs>::Type;
  static ResultBlockType Run(const Lhs& lhs, const Rhs& rhs) {
    ResultBlockType result;
    static constexpr int Rows = ResultBlockType::kRows;
    static constexpr int Cols = ResultBlockType::kCols;
    static constexpr int LhsRows = Lhs::kRows;
    static constexpr int LhsCols = Lhs::kCols;
    static constexpr int RhsRows = Rhs::kRows;
    static constexpr int RhsCols = Rhs::kCols;

    static_assert(LhsRows == Rows || LhsRows == 1, "");
    static_assert(RhsRows == Rows || RhsRows == 1, "");
    static_assert(LhsCols == Cols || LhsCols == 1, "");
    static_assert(RhsCols == Cols || RhsCols == 1, "");
    static_assert(ResultBlockType::kRegisterLanes == 1,
                  "This path is only for scalar values");
    static_assert(Lhs::kRegisterLanes == 1,
                  "This path is only for scalar values");
    static_assert(Rhs::kRegisterLanes == 1,
                  "This path is only for scalar values");

    for (int c = 0; c < Cols; c++) {
      const int lhs_c = LhsCols == Cols ? c : 0;
      const int rhs_c = RhsCols == Cols ? c : 0;
      for (int r = 0; r < Rows; r++) {
        const int lhs_r = LhsRows == Rows ? r : 0;
        const int rhs_r = RhsRows == Rows ? r : 0;
        result.buf.reg[r + c * Rows] =
            Add(lhs.buf.reg[lhs_r + lhs_c * LhsRows],
                rhs.buf.reg[rhs_r + rhs_c * RhsRows]);
      }
    }
    return result;
  }
};

template <typename Lhs, typename Rhs>
typename BroadcastBinaryOpRegisterBlock<Lhs, Rhs>::Type BroadcastAdd(
    const Lhs& lhs, const Rhs& rhs) {
  using Flip = FlipLhsRhs<Lhs, Rhs>;
  return BroadcastAddImpl<
      typename Flip::FlippedLhsType,
      typename Flip::FlippedRhsType>::Run(Flip::FlippedLhs(lhs, rhs),
                                          Flip::FlippedRhs(lhs, rhs));
}

template <typename Lhs, typename Rhs>
struct BroadcastShiftLeftImpl {
  using ResultBlockType =
      typename BroadcastBinaryOpRegisterBlock<Lhs, Rhs>::Type;
  static ResultBlockType Run(const Lhs& lhs, const Rhs& rhs) {
    ResultBlockType result;
    static constexpr int Rows = ResultBlockType::kRows;
    static constexpr int Cols = ResultBlockType::kCols;
    static constexpr int LhsRows = Lhs::kRows;
    static constexpr int LhsCols = Lhs::kCols;
    static constexpr int RhsRows = Rhs::kRows;
    static constexpr int RhsCols = Rhs::kCols;

    static_assert(LhsRows == Rows || LhsRows == 1, "");
    static_assert(RhsRows == Rows || RhsRows == 1, "");
    static_assert(LhsCols == Cols || LhsCols == 1, "");
    static_assert(RhsCols == Cols || RhsCols == 1, "");
    static_assert(ResultBlockType::kRegisterLanes == 1,
                  "This path is only for scalar values");
    static_assert(Lhs::kRegisterLanes == 1,
                  "This path is only for scalar values");
    static_assert(Rhs::kRegisterLanes == 1,
                  "This path is only for scalar values");

    for (int c = 0; c < Cols; c++) {
      const int lhs_c = LhsCols == Cols ? c : 0;
      const int rhs_c = RhsCols == Cols ? c : 0;
      for (int r = 0; r < Rows; r++) {
        const int lhs_r = LhsRows == Rows ? r : 0;
        const int rhs_r = RhsRows == Rows ? r : 0;
        result.buf.reg[r + c * Rows] =
            ShiftLeft(lhs.buf.reg[lhs_r + lhs_c * LhsRows],
                      rhs.buf.reg[rhs_r + rhs_c * RhsRows]);
      }
    }
    return result;
  }
};

template <typename Lhs, typename Rhs>
typename BroadcastBinaryOpRegisterBlock<Lhs, Rhs>::Type BroadcastShiftLeft(
    const Lhs& lhs, const Rhs& rhs) {
  using Flip = FlipLhsRhs<Lhs, Rhs>;
  return BroadcastShiftLeftImpl<
      typename Flip::FlippedLhsType,
      typename Flip::FlippedRhsType>::Run(Flip::FlippedLhs(lhs, rhs),
                                          Flip::FlippedRhs(lhs, rhs));
}

template <typename Lhs, typename Rhs>
struct BroadcastSaturatingRoundingDoublingHighMulImpl {
  using ResultBlockType =
      typename BroadcastBinaryOpRegisterBlock<Lhs, Rhs>::Type;
  static ResultBlockType Run(const Lhs& lhs, const Rhs& rhs) {
    ResultBlockType result;
    static constexpr int Rows = ResultBlockType::kRows;
    static constexpr int Cols = ResultBlockType::kCols;
    static constexpr int LhsRows = Lhs::kRows;
    static constexpr int LhsCols = Lhs::kCols;
    static constexpr int RhsRows = Rhs::kRows;
    static constexpr int RhsCols = Rhs::kCols;

    static_assert(LhsRows == Rows || LhsRows == 1, "");
    static_assert(RhsRows == Rows || RhsRows == 1, "");
    static_assert(LhsCols == Cols || LhsCols == 1, "");
    static_assert(RhsCols == Cols || RhsCols == 1, "");
    static_assert(ResultBlockType::kRegisterLanes == 1,
                  "This path is only for scalar values");
    static_assert(Lhs::kRegisterLanes == 1,
                  "This path is only for scalar values");
    static_assert(Rhs::kRegisterLanes == 1,
                  "This path is only for scalar values");

    for (int c = 0; c < Cols; c++) {
      const int lhs_c = LhsCols == Cols ? c : 0;
      const int rhs_c = RhsCols == Cols ? c : 0;
      for (int r = 0; r < Rows; r++) {
        const int lhs_r = LhsRows == Rows ? r : 0;
        const int rhs_r = RhsRows == Rows ? r : 0;
        result.buf.reg[r + c * Rows] = SaturatingRoundingDoublingHighMul(
            lhs.buf.reg[lhs_r + lhs_c * LhsRows],
            rhs.buf.reg[rhs_r + rhs_c * RhsRows]);
      }
    }
    return result;
  }
};

template <typename Lhs, typename Rhs>
typename BroadcastBinaryOpRegisterBlock<Lhs, Rhs>::Type
BroadcastSaturatingRoundingDoublingHighMul(const Lhs& lhs, const Rhs& rhs) {
  using Flip = FlipLhsRhs<Lhs, Rhs>;
  return BroadcastSaturatingRoundingDoublingHighMulImpl<
      typename Flip::FlippedLhsType,
      typename Flip::FlippedRhsType>::Run(Flip::FlippedLhs(lhs, rhs),
                                          Flip::FlippedRhs(lhs, rhs));
}

template <typename Lhs, typename Rhs>
struct BroadcastRoundingDivideByPOTImpl {
  using ResultBlockType =
      typename BroadcastBinaryOpRegisterBlock<Lhs, Rhs>::Type;
  static ResultBlockType Run(const Lhs& lhs, const Rhs& rhs) {
    ResultBlockType result;
    static constexpr int Rows = ResultBlockType::kRows;
    static constexpr int Cols = ResultBlockType::kCols;
    static constexpr int LhsRows = Lhs::kRows;
    static constexpr int LhsCols = Lhs::kCols;
    static constexpr int RhsRows = Rhs::kRows;
    static constexpr int RhsCols = Rhs::kCols;

    static_assert(LhsRows == Rows || LhsRows == 1, "");
    static_assert(RhsRows == Rows || RhsRows == 1, "");
    static_assert(LhsCols == Cols || LhsCols == 1, "");
    static_assert(RhsCols == Cols || RhsCols == 1, "");
    static_assert(ResultBlockType::kRegisterLanes == 1,
                  "This path is only for scalar values");
    static_assert(Lhs::kRegisterLanes == 1,
                  "This path is only for scalar values");
    static_assert(Rhs::kRegisterLanes == 1,
                  "This path is only for scalar values");

    for (int c = 0; c < Cols; c++) {
      const int lhs_c = LhsCols == Cols ? c : 0;
      const int rhs_c = RhsCols == Cols ? c : 0;
      for (int r = 0; r < Rows; r++) {
        const int lhs_r = LhsRows == Rows ? r : 0;
        const int rhs_r = RhsRows == Rows ? r : 0;
        result.buf.reg[r + c * Rows] =
            RoundingDivideByPOT(lhs.buf.reg[lhs_r + lhs_c * LhsRows],
                                rhs.buf.reg[rhs_r + rhs_c * RhsRows]);
      }
    }
    return result;
  }
};

template <typename Lhs, typename Rhs>
typename BroadcastBinaryOpRegisterBlock<Lhs, Rhs>::Type
BroadcastRoundingDivideByPOT(const Lhs& lhs, const Rhs& rhs) {
  using Flip = FlipLhsRhs<Lhs, Rhs>;
  return BroadcastRoundingDivideByPOTImpl<
      typename Flip::FlippedLhsType,
      typename Flip::FlippedRhsType>::Run(Flip::FlippedLhs(lhs, rhs),
                                          Flip::FlippedRhs(lhs, rhs));
}

template <typename Lhs, typename Rhs>
struct BroadcastMulImpl {
  using ResultBlockType =
      typename BroadcastBinaryOpRegisterBlock<Lhs, Rhs>::Type;
  static ResultBlockType Run(const Lhs& lhs, const Rhs& rhs) {
    ResultBlockType result;
    static constexpr int Rows = ResultBlockType::kRows;
    static constexpr int Cols = ResultBlockType::kCols;
    static constexpr int LhsRows = Lhs::kRows;
    static constexpr int LhsCols = Lhs::kCols;
    static constexpr int RhsRows = Rhs::kRows;
    static constexpr int RhsCols = Rhs::kCols;
    static_assert(ResultBlockType::kRegisterLanes == 1,
                  "This path is only for scalar values");
    static_assert(Lhs::kRegisterLanes == 1,
                  "This path is only for scalar values");
    static_assert(Rhs::kRegisterLanes == 1,
                  "This path is only for scalar values");

    static_assert(LhsRows == Rows || LhsRows == 1, "");
    static_assert(RhsRows == Rows || RhsRows == 1, "");
    static_assert(LhsCols == Cols || LhsCols == 1, "");
    static_assert(RhsCols == Cols || RhsCols == 1, "");
    for (int c = 0; c < Cols; c++) {
      const int lhs_c = LhsCols == Cols ? c : 0;
      const int rhs_c = RhsCols == Cols ? c : 0;
      for (int r = 0; r < Rows; r++) {
        const int lhs_r = LhsRows == Rows ? r : 0;
        const int rhs_r = RhsRows == Rows ? r : 0;
        result.buf.reg[r + c * Rows] =
            Mul(lhs.buf.reg[lhs_r + lhs_c * LhsRows],
                rhs.buf.reg[rhs_r + rhs_c * RhsRows]);
      }
    }
    return result;
  }
};

template <typename Lhs, typename Rhs>
typename BroadcastBinaryOpRegisterBlock<Lhs, Rhs>::Type BroadcastMul(
    const Lhs& lhs, const Rhs& rhs) {
  using Flip = FlipLhsRhs<Lhs, Rhs>;
  return BroadcastMulImpl<
      typename Flip::FlippedLhsType,
      typename Flip::FlippedRhsType>::Run(Flip::FlippedLhs(lhs, rhs),
                                          Flip::FlippedRhs(lhs, rhs));
}

template <typename Lhs, typename Rhs, typename Acc>
struct BroadcastMulAddImpl {
  static void Run(const Lhs& lhs, const Rhs& rhs, Acc* acc) {
    static constexpr int Rows = Acc::kRows;
    static constexpr int Cols = Acc::kCols;
    static constexpr int LhsRows = Lhs::kRows;
    static constexpr int LhsCols = Lhs::kCols;
    static constexpr int RhsRows = Rhs::kRows;
    static constexpr int RhsCols = Rhs::kCols;
    static_assert(Acc::kRegisterLanes == 1,
                  "This path is only for scalar values");
    static_assert(Lhs::kRegisterLanes == 1,
                  "This path is only for scalar values");
    static_assert(Rhs::kRegisterLanes == 1,
                  "This path is only for scalar values");

    static_assert(LhsRows == Rows || LhsRows == 1, "");
    static_assert(RhsRows == Rows || RhsRows == 1, "");
    static_assert(LhsCols == Cols || LhsCols == 1, "");
    static_assert(RhsCols == Cols || RhsCols == 1, "");
    for (int c = 0; c < Cols; c++) {
      const int lhs_c = LhsCols == Cols ? c : 0;
      const int rhs_c = RhsCols == Cols ? c : 0;
      for (int r = 0; r < Rows; r++) {
        const int lhs_r = LhsRows == Rows ? r : 0;
        const int rhs_r = RhsRows == Rows ? r : 0;
        MulAdd(lhs.buf.reg[lhs_r + lhs_c * LhsRows],
               rhs.buf.reg[rhs_r + rhs_c * RhsRows],
               &acc->buf.reg[r + c * Rows]);
      }
    }
  }
};

template <typename Lhs, typename Rhs, typename Acc>
void BroadcastMulAdd(const Lhs& lhs, const Rhs& rhs, Acc* acc) {
  using Flip = FlipLhsRhs<Lhs, Rhs>;
  BroadcastMulAddImpl<typename Flip::FlippedLhsType,
                      typename Flip::FlippedRhsType,
                      Acc>::Run(Flip::FlippedLhs(lhs, rhs),
                                Flip::FlippedRhs(lhs, rhs), acc);
}

template <typename RegisterBlockType, typename SrcObjectType>
struct LoadImpl {
  static_assert(std::is_same<SrcObjectType, void>::value,
                "This generic impl should never be hit");
};

template <typename ScalarType, int Rows, int Cols, typename SrcScalarType>
struct LoadImpl<RegisterBlock<ScalarType, Rows, Cols>,
                MatrixMap<SrcScalarType, MapOrder::ColMajor>> {
  using RegisterBlockType = RegisterBlock<ScalarType, Rows, Cols>;
  using SrcObjectType = MatrixMap<SrcScalarType, MapOrder::ColMajor>;
  static RegisterBlockType Run(const SrcObjectType& src, int row, int col) {
    RegisterBlockType result;
    int i = 0;
    for (int c = 0; c < Cols; c++) {
      const ScalarType* src_ptr = src.data(row, col + c);
      for (int r = 0; r < Rows; r++) {
        result.buf.reg[i++] = *src_ptr++;
      }
    }
    return result;
  }
};

template <typename ScalarType, int Rows, int Cols, typename SrcScalarType,
          VectorShape Shape>
struct LoadImpl<RegisterBlock<ScalarType, Rows, Cols>,
                VectorMap<SrcScalarType, Shape>> {
  using RegisterBlockType = RegisterBlock<ScalarType, Rows, Cols>;
  using SrcObjectType = VectorMap<SrcScalarType, Shape>;
  static RegisterBlockType Run(const SrcObjectType& src, int pos) {
    static_assert(Shape == VectorShape::Col || Rows == 1, "");
    static_assert(Shape == VectorShape::Row || Cols == 1, "");
    RegisterBlockType result;
    for (int i = 0; i < Rows * Cols; i++) {
      result.buf.reg[i] = src(pos + i);
    }
    return result;
  }
};

template <typename ScalarType, int Rows, int Cols, typename SrcScalarType,
          VectorShape Shape>
struct LoadImpl<RegisterBlock<ScalarType, Rows, Cols>,
                VectorDup<SrcScalarType, Shape>> {
  using RegisterBlockType = RegisterBlock<ScalarType, Rows, Cols>;
  using SrcObjectType = VectorDup<SrcScalarType, Shape>;
  static RegisterBlockType Run(const SrcObjectType& src, int) {
    static_assert(Shape == VectorShape::Col || Rows == 1, "");
    static_assert(Shape == VectorShape::Row || Cols == 1, "");
    RegisterBlockType result;
    for (int i = 0; i < Rows * Cols; i++) {
      result.buf.reg[i] = src(0);
    }
    return result;
  }
};

template <typename RegisterBlockType, typename SrcObjectType>
RegisterBlockType Load(const SrcObjectType& src, int row, int col) {
  return LoadImpl<RegisterBlockType, SrcObjectType>::Run(src, row, col);
}

template <typename RegisterBlockType, typename SrcObjectType>
RegisterBlockType Load(const SrcObjectType& src, int pos) {
  return LoadImpl<RegisterBlockType, SrcObjectType>::Run(src, pos);
}

template <typename RegisterBlockType>
struct LoadContiguousImpl {
  using ScalarType = typename RegisterBlockType::ScalarType;
  static_assert(RegisterBlockType::kRegisterLanes == 1,
                "This path is only for scalar values");
  static RegisterBlockType Run(const ScalarType* src) {
    RegisterBlockType result;
    for (int i = 0; i < RegisterBlockType::kScalarCount; i++) {
      result.buf.reg[i] = src[i];
    }
    return result;
  }
};

template <typename RegisterBlockType>
RegisterBlockType LoadContiguous(
    const typename RegisterBlockType::ScalarType* src) {
  return LoadContiguousImpl<RegisterBlockType>::Run(src);
}

template <int BroadcastRows, int BroadcastCols, typename SrcObjectType>
struct LoadForBroadcastingShape {};

template <int BroadcastRows, int BroadcastCols, typename ScalarType,
          VectorShape Shape>
struct LoadForBroadcastingShape<BroadcastRows, BroadcastCols,
                                VectorMap<ScalarType, Shape>> {
  static constexpr int kRows = Shape == VectorShape::Col ? BroadcastRows : 1;
  static constexpr int kCols = Shape == VectorShape::Row ? BroadcastCols : 1;
};

template <int BroadcastRows, int BroadcastCols, typename ScalarType,
          VectorShape Shape>
struct LoadForBroadcastingShape<BroadcastRows, BroadcastCols,
                                VectorDup<ScalarType, Shape>> {
  static constexpr int kRows = 1;
  static constexpr int kCols = 1;
};

template <typename RegisterBlockType, typename SrcObjectType>
struct LoadForBroadcastingRegisterBlock {
  using Shape =
      LoadForBroadcastingShape<RegisterBlockType::kRows,
                               RegisterBlockType::kCols, SrcObjectType>;
  using ScalarType = typename RegisterBlockType::ScalarType;
  using Type = RegisterBlock<ScalarType, Shape::kRows, Shape::kCols>;
};

template <typename RegisterBlockType, typename SrcObjectType>
struct LoadForBroadcastingImpl {
  static_assert(std::is_same<SrcObjectType, void>::value,
                "This generic impl should never be hit");
};

template <typename ScalarType, int Rows, int Cols, typename SrcScalarType,
          VectorShape Shape>
struct LoadForBroadcastingImpl<RegisterBlock<ScalarType, Rows, Cols>,
                               VectorMap<SrcScalarType, Shape>> {
  using RegisterBlockType = RegisterBlock<ScalarType, Rows, Cols>;
  using SrcObjectType = VectorMap<SrcScalarType, Shape>;
  using ResultBlockType =
      typename LoadForBroadcastingRegisterBlock<RegisterBlockType,
                                                SrcObjectType>::Type;
  static_assert(ResultBlockType::kRegisterLanes == 1,
                "This path is only for scalar values");
  static ResultBlockType Run(const SrcObjectType& src, int pos) {
    ResultBlockType result;
    for (int c = 0; c < ResultBlockType::kCols; c++) {
      for (int r = 0; r < ResultBlockType::kRows; r++) {
        const int i = Shape == VectorShape::Col ? r : c;
        result.buf.reg[r + c * ResultBlockType::kRows] = src(pos + i);
      }
    }
    return result;
  }
};

template <typename ScalarType, int Rows, int Cols, typename SrcScalarType,
          VectorShape Shape>
struct LoadForBroadcastingImpl<RegisterBlock<ScalarType, Rows, Cols>,
                               VectorDup<SrcScalarType, Shape>> {
  using RegisterBlockType = RegisterBlock<ScalarType, Rows, Cols>;
  using SrcObjectType = VectorDup<SrcScalarType, Shape>;
  using ResultBlockType =
      typename LoadForBroadcastingRegisterBlock<RegisterBlockType,
                                                SrcObjectType>::Type;
  static_assert(ResultBlockType::kRegisterLanes == 1,
                "This path is only for scalar values");
  static ResultBlockType Run(const SrcObjectType& src, int) {
    ResultBlockType result;
    for (int c = 0; c < ResultBlockType::kCols; c++) {
      for (int r = 0; r < ResultBlockType::kRows; r++) {
        result.buf.reg[r + c * ResultBlockType::kRows] = src(0);
      }
    }
    return result;
  }
};

template <typename RegisterBlockType, typename SrcObjectType>
typename LoadForBroadcastingRegisterBlock<RegisterBlockType,
                                          SrcObjectType>::Type
LoadForBroadcasting(const SrcObjectType& src, int row, int col) {
  return LoadForBroadcastingImpl<RegisterBlockType, SrcObjectType>::Run(
      src, row, col);
}

template <typename RegisterBlockType, typename SrcObjectType>
typename LoadForBroadcastingRegisterBlock<RegisterBlockType,
                                          SrcObjectType>::Type
LoadForBroadcasting(const SrcObjectType& src, int pos) {
  return LoadForBroadcastingImpl<RegisterBlockType, SrcObjectType>::Run(src,
                                                                        pos);
}

template <int ConstantValue, typename RegisterBlockType>
struct AddConstantImpl {
  static void Run(RegisterBlockType* block) {
    using RegisterType = typename RegisterBlockType::RegisterType;
    const RegisterType dup = Dup<RegisterType>(ConstantValue);
    for (int i = 0; i < RegisterBlockType::kRegisterCount; i++) {
      block->buf.reg[i] = Add(block->buf.reg[i], dup);
    }
  }
};

template <typename RegisterBlockType>
struct AddConstantImpl<0, RegisterBlockType> {
  static void Run(RegisterBlockType*) {
    // This is a no-op.
  }
};

template <int ConstantValue, typename RegisterBlockType>
void AddConstant(RegisterBlockType* block) {
  AddConstantImpl<ConstantValue, RegisterBlockType>::Run(block);
}

template <int N>
using RegBufferInt32 = RegisterBuffer<std::int32_t, N>;
template <int N>
using RegBufferInt16 = RegisterBuffer<std::int16_t, N>;
template <int N>
using RegBufferUint8 = RegisterBuffer<std::uint8_t, N>;
template <int N>
using RegBufferInt8 = RegisterBuffer<std::int8_t, N>;
template <int R, int C>
using RegBlockInt32 = RegisterBlock<std::int32_t, R, C>;
template <int R, int C>
using RegBlockInt16 = RegisterBlock<std::int16_t, R, C>;
template <int R, int C>
using RegBlockUint8 = RegisterBlock<std::uint8_t, R, C>;
template <int R, int C>
using RegBlockInt8 = RegisterBlock<std::int8_t, R, C>;

}  // end namespace gemmlowp

#if defined GEMMLOWP_NEON
#include "simd_wrappers_neon.h"
#elif defined GEMMLOWP_SSE4
#include "simd_wrappers_sse.h"
#elif defined GEMMLOWP_MSA
#include "simd_wrappers_msa.h"
#endif

#endif  // GEMMLOWP_INTERNAL_SIMD_WRAPPERS_H_