3

vaddv_u8 and some other similar new v-intrinsics from AArch64 (arm64) return uint8_t. How can I treat result of this intrinsic as a neon register instead of plain C type?

For example:

void paddClz(uint8_t* x)
{
    uint8x8_t ret = vdup_n_u8(0);
    for (int i = 0; i < 8; ++i, x += 8)
    {
        uint8x8_t x8 = vld1_u8(x);
        uint8_t sum = vaddv_u8(x8);
        uint8x8_t r = vdup_n_u8(sum); //or: r = vset_lane_u8(sum, r, 0);
        r = vclz_u8(r);
        ret = vext_u8(ret, r, 1);
    }
    vst1_u8(x, ret);
}

what clang generated:

paddClz(unsigned char*): // @paddClz(unsigned char*)
  mov x8, xzr
  movi d0, #0000000000000000
.LBB0_1: // =>This Inner Loop Header: Depth=1
  ldr d1, [x0, x8]
  add x8, x8, #8 // =8
  cmp w8, #64 // =64
  addv b1, v1.8b
  dup v1.8b, v1.b[0]   <<== useless! I only need/use/care about v1.b[0]
  clz v1.8b, v1.8b
  ext v0.8b, v0.8b, v1.8b, #1
  b.ne .LBB0_1
  str d0, [x0, #64]
  ret

As you can see there is a useless dup intrinsic required to get the uint8_t vaddv_u8 result converted to a type that will work as an argument for vclz_u8. I take only first lane from the subsequent vclz_u8 result, so actually duplicating it to all lanes would be wasted work.

How can I write it in intrinsics to get that sum in neon typed variable without making the compiler emit useless opcodes? (And preferably without this extra noise in the source code.) To make it clear and obvious if it wasn't: I'm not asking to optimize or improve that piece of code that I posted; I simply wrote it to show the issue.

5
  • 1
    Don't count on being able to get current compilers to emit non-terrible asm for ARM intrinsics. For some reason they're much worse with ARM SIMD than with x86 SSE/AVX intrinsics, or PowerPC Altivec intrinsics. Apr 25, 2018 at 23:12
  • @PeterCordes yes, totally agree, but in this case for some reason intrinsic itself is declared in a way that makes you go through some hops to get simplest and most straightforward code where you have to rely that compiler is smart enough, while there was no need for such requirement in the first place.
    – Pavel P
    Apr 25, 2018 at 23:17
  • @PeterCordes GCC lags way behind LLVM in this department. LLVM (at least the ARMv8 version shipped with XCode) does an excellent job of emitting ARM SIMD instructions from intrinsics.
    – BitBank
    Apr 27, 2018 at 0:03
  • @BitBank: Thanks, I'll have to try clang -target aarch64 on godbolt.org next time I'm looking at ARM intrinsics. (I only mess around with ARM code for SO answers.) Apr 27, 2018 at 0:06
  • Looks like arm64 isn't as well implemented in general (so far). I'd be interested to try on armcc (arm's compiler).
    – Pavel P
    Apr 27, 2018 at 1:35

3 Answers 3

2

You should really get a test device with an in-order SoC. Apple's A series chips are all out-of-order, by far the most powerful ones to be precise.

Your implementation might run adequately fast on your iPhone, but will be barely any faster than the simplest C versions on in-order cores, straight unusable.

Think twice before you rush into writing loops on NEON. You can avoid the so-called "horizontal" operations altogether most of the time by transposing the matrix, then do the "vertical" math instead.


#define vuzp8(a, b, c) ({ \
    c = vuzp_u8(a, b); \
    a = c.val[0]; \
    b = c.val[1]; \
})

void foo(uint8_t *pDst, uint8_t *pSrc)
{
    uint8x8x4_t top, bottom;
    uint8x8x2_t temp;

    top = vld4_u8(pSrc);
    pSrc += 32;
    bottom = vld4_u8(pSrc);

    vuzp8(top.val[0], bottom.val[0], temp);
    vuzp8(top.val[1], bottom.val[1], temp);
    vuzp8(top.val[2], bottom.val[2], temp);
    vuzp8(top.val[3], bottom.val[3], temp);

    top.val[0] += bottom.val[0];
    top.val[1] += bottom.val[1];
    top.val[2] += bottom.val[2];
    top.val[3] += bottom.val[3];

    top.val[0] += top.val[1];
    top.val[2] += top.val[3];

    top.val[0] += top.val[2];

    top.val[0] = vclz_u8(top.val[0]);

    vst1_u8(pDst, top.val[0]);
}

Another example where you ask yourself if intrinsux makes sense at all. Its clumsiness makes the code much more complex, and it isn't expressive enough to do three 128bit plus one 64bit adds instead of six 64bit ones.

In addition, you have to double check if the compiler didn't mess up anything, again, especially when you do permutations(vzip, vuzp, vtrn)

I think the machine code will be fine on aarch32, but I'm not so sure about aarch64 where the permutation instructions are vastly different.

I think that you understand by now why I hate intrinsux like the pest. It's more nuisance than any help.

PS: The Teclast P10 Android tablet is quite a good candidate as an aarch64 test device: All the eight cores are the same, Android 7.12 64bit is installed, and it only costs around $100.

4
  • @Bitbank says clang for AArch64 is non-terrible with intrinsics. Have you tried it recently? Also that there isn't a large penalty anymore for NEON<->integer on ARMv8. Is that just Apple's ARMv8 cores, or is that AArch64 in general? Apr 27, 2018 at 11:27
  • @PeterCordes I dunno, to be honest. I, too have no in-order ARMv8 device yet ;-) (but soon) As you can see above, transposing matrices is a trivial thing on NEON, and thus, I rarely do any horizontal math. And besides, the destination operand of horizontal instructions on aarch64 isn't an ARM register. When you write in assembly, the result lands in index zero of a vector. It cannot be expressed in intrinsux though which is outrageous. intrinsux badly needs a complete rehaul. Apr 27, 2018 at 11:33
  • Yeah, that seems like a ridiculous design. Intel intrinics have some similar warts, How to merge a scalar into a vector without the compiler wasting an instruction zeroing upper elements? Design limitation in Intel's intrinsics?. (Also mentions that it's hard to get pmovzx with a memory operand.) But that's just omission of useful stuff, not wrong types. Apr 27, 2018 at 11:38
  • @PeterCordes What really sucks is that intrinsux is fragmented between aarch32 and aarch64. You cannot use the aarch64 specific ones such as trn1/2, zip1/2, uzp1/2 if you target aarch32. What's the point of intrinsux if you have to write separate codes for each architecture anyway? Apr 27, 2018 at 11:48
1

Your workaround potentially makes the performance worse. Your problem is written as if you want a scalar result from your single vector of uint8_t's. There's nothing wrong with the vaddv_u8 instruction returning a scalar value. On ARMv8, the "NEON unit" is now fully integrated and doesn't have a large penalty for moving data between NEON and ARM registers. Just use the C intrinsic to count leading zeros of the result and you'll have what you need:

int paddClz(const uint8_t* x)
{
    uint8x8_t x8 = vld1_u8(x);
    uint8_t sum = vaddv_u8(x8);
    return __builtin_clz(sum) - 24;
}

The intrisic will get compiled into the single ARM instruction (CLZ).

If you're working with a larger data set, then write the C code to properly reflect that fact.

10
  • 1
    The OP actually wants to keep using further SIMD instructions on the result, but this is the right answer to the question as asked (except you're now counting the 24 leading zeros in an int before the low 8 bits, which is what vclz_u8 counts). The OP should fix the example code, like I commented on his answer. Apr 27, 2018 at 0:08
  • 1
    Thanks for catching the 32/8 issue. He keeps creating misleading questions which appear to focus on a single NEON register. If he's really working with large data sets, then he''ll keep getting incorrect solutions.
    – BitBank
    Apr 27, 2018 at 0:12
  • Exactly. Plus this question was a mess before I cleaned up the terminology to talk about intrinsics instead of opcodes! It's an interesting question, so I wish it was asked correctly. Apr 27, 2018 at 0:22
  • Please read the title of the question. That's the question,there were no other questions. The body of entire message just shows an example, not some code that I'm trying to optimize, in other words I don't care if somebody provides alternative piece of the code that does it 10 times faster. I simply want to know in general: "How to treat result of vaddv_u8 in arm64 as a neon register"
    – Pavel P
    Apr 27, 2018 at 1:27
  • 1
    ok, so it sounds like you're fighting the compiler. Clang/LLVM will normally produce better results than GCC. If all else fails, write assembly language in a separate .S file. It's the only way to guarantee that your instructions don't get changed.
    – BitBank
    Apr 27, 2018 at 2:56
1

It seems that I can do this in clang:

int paddClz(const uint8_t* x)
{
    uint8x8_t x8 = vld1_u8(x);
    uint8_t sum = vaddv_u8(x8);
    uint8x8_t r;
    r = vset_lane_u8(sum, r, 0);
    r = vclz_u8(r);
    return vget_lane_u8(r, 0);
}

This produces exactly what I want:

addv b0, v0.8b
clz v0.8b, v0.8b

However, gcc produces some mess from that code. The other issue is that it uses uninitialized r and depending on how you setup your build it might not be acceptable. More over, it doesn't seem to work in more complex scenarios. Is there a better/proper way to do it?

6
  • You do eventually want the result in a scalar ARM register, so you could maybe write the source so the compiler can addv / umov w0, v0.b[0] / clz w0, w0. But then you're counting the 24 leading zeros above the 8-bit value. If ARM throughtput is better than NEON, it might still be a win to have to subtract 24, especially if you can fold that into something else for free. Apr 26, 2018 at 21:18
  • In my case I need the result in neon and I keep processing/manipulating it there.
    – Pavel P
    Apr 26, 2018 at 23:29
  • Then a function that returns uint8x8_t would be a better example than int, right? Apr 26, 2018 at 23:49
  • Oh, yes, that's just to make some minimal example that compiles, not some actual code.
    – Pavel P
    Apr 26, 2018 at 23:51
  • I mean an example of what you need / are trying to achieve. Returning an int allows clz w0,w0 as part of a possible optimal solution, but returning a uint8x8_t doesn't. It's important to craft your compiler test-cases carefully if you want meaningful results. e.g. maybe some compiler only shoots itself in the foot when you later ask for the value as an int. Or maybe some compiler only gives good results for this when you later ask for an int, instead of using it with some further vector operations. This doesn't prove that you can get clang to generate good code for using r. Apr 26, 2018 at 23:53

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.