[GH-ISSUE #1201] Abandon vtranspose_epi32 in pseudo-intrinsics.h #753

Closed
opened 2026-10-14 03:22:31 +07:00 by bsakaguchi · 9 comments
Owner

Originally created by @lei-april on GitHub (Apr 12, 2015).
Original GitHub issue: https://github.com/openwall/john/issues/1201

Originally assigned to: @lei-april on GitHub.

vtranspose_epi32 is used to transpose a matrix held by an array of SIMD registers. The implementation gets more complex as the SIMD width doubles. I didn't figure out how to implement it for AVX512/MIC, and a guy on stackoverflow gave me an answer, which looks quite complex.

It will be even harder to implement this for AVX1024 or higher. Since currently this function is only used by one format (raw-sha1-ng), I think we might replace it with scalar code for better scalability.

Originally created by @lei-april on GitHub (Apr 12, 2015). Original GitHub issue: https://github.com/openwall/john/issues/1201 Originally assigned to: @lei-april on GitHub. vtranspose_epi32 is used to transpose a matrix held by an array of SIMD registers. The implementation gets more complex as the SIMD width doubles. I didn't figure out how to implement it for AVX512/MIC, and a guy on stackoverflow gave me an [answer](http://stackoverflow.com/a/29587984/2927352), which looks quite complex. It will be even harder to implement this for AVX1024 or higher. Since currently this function is only used by one format (raw-sha1-ng), I think we might replace it with scalar code for better scalability.
Author
Owner

@magnumripper commented on GitHub (Apr 12, 2015):

Sure, we can use scalar code for widths > 8 (at least for now). That SE table show some significant speedup though, especially for larger widths.

n            4(SSE)          8(AVX)    16(AVX512)    32(AVX1024)  
SIMD ops          8              24           64            160
SIMD +r/w ops    16              40           96            224     
Scalar r/w ops   24             112          480           1984

Perhaps there's a way to write the intrinsics version using arrays and loops, making the code shorter yet in the end end up with more or less the same resulting code?

I suspect this vtranspose_epi32() function (we should probably give it a better/more specific name) might have lots of future use - isn't this the same functionality that is done some other way when sse-intrinsics.c functions are called with SSEi_FLAT_IN? We should compare benchmarks on AVX2 using vector vs. scalar code and see real-world difference. But this will be influenced by unrelated bottlenecks.

<!-- gh-comment-id:92148225 --> @magnumripper commented on GitHub (Apr 12, 2015): Sure, we can use scalar code for widths > 8 (at least for now). That SE table show some significant speedup though, _especially_ for larger widths. ``` n 4(SSE) 8(AVX) 16(AVX512) 32(AVX1024) SIMD ops 8 24 64 160 SIMD +r/w ops 16 40 96 224 Scalar r/w ops 24 112 480 1984 ``` Perhaps there's a way to write the intrinsics version using arrays and loops, making the code shorter yet in the end end up with more or less the same resulting code? I suspect this vtranspose_epi32() function (we should probably give it a better/more specific name) might have lots of future use - isn't this the same functionality that is done some other way when sse-intrinsics.c functions are called with SSEi_FLAT_IN? We should compare benchmarks on AVX2 using vector vs. scalar code and see real-world difference. But this will be influenced by unrelated bottlenecks.
Author
Owner

@lei-april commented on GitHub (Apr 13, 2015):

If the matrix is transposed immediately after loaded from memory, which is the case in raw-sha1-ng, we may use the gather/scatter in AVX512/MIC to do the transposition with even fewer instructions, like this:

__m512i index = _mm512_set_epi32(0, 1, 2, ..., 15);
for (i = 0; i < 16; ++i)
    W[i] = _mm512_gather_epi32(index, &M[0][i], 64);

In this case, the 2-D array M is supposed to be stored continuously in memory.

The gather/scatter instructions should also be available in AVX1024 or higher, so we can use this scheme for all widths > 8. But if we have to do in-register transposition in future cases, we'd need another way.

<!-- gh-comment-id:92159252 --> @lei-april commented on GitHub (Apr 13, 2015): If the matrix is transposed immediately after loaded from memory, which is the case in raw-sha1-ng, we may use the gather/scatter in AVX512/MIC to do the transposition with even fewer instructions, like this: ``` c __m512i index = _mm512_set_epi32(0, 1, 2, ..., 15); for (i = 0; i < 16; ++i) W[i] = _mm512_gather_epi32(index, &M[0][i], 64); ``` In this case, the 2-D array M is supposed to be stored continuously in memory. The gather/scatter instructions should also be available in AVX1024 or higher, so we can use this scheme for all widths > 8. But if we have to do in-register transposition in future cases, we'd need another way.
Author
Owner

@magnumripper commented on GitHub (Apr 13, 2015):

That's nice. So it only works on AVX512? I don't even get a google hit from "_mm512_gather_epi32"

<!-- gh-comment-id:92240270 --> @magnumripper commented on GitHub (Apr 13, 2015): That's nice. So it only works on AVX512? I don't even get a google hit from "_mm512_gather_epi32"
Author
Owner

@lei-april commented on GitHub (Apr 13, 2015):

Sorry, I made a typo. It's _mm512_i32gather_epi32. MIC supports it too. You can find it in the reference manual.

<!-- gh-comment-id:92246243 --> @lei-april commented on GitHub (Apr 13, 2015): Sorry, I made a typo. It's _mm512_i32gather_epi32. MIC supports it too. You can find it in the reference manual.
Author
Owner

@magnumripper commented on GitHub (Apr 13, 2015):

OK that instruction is also in AVX2 but not lower.

<!-- gh-comment-id:92254757 --> @magnumripper commented on GitHub (Apr 13, 2015): OK that instruction is also in AVX2 but not lower.
Author
Owner

@magnumripper commented on GitHub (Apr 13, 2015):

We have this in several places in sse-intrinsics.c

#if __SSE4_1__
        unsigned k;
        vtype *W = w;
        ARCH_WORD_32 *saved_key = (ARCH_WORD_32*)_data;
        MD5_PARA_DO(k)
        {
            if (SSEi_flags & SSEi_4BUF_INPUT) {
                for (i=0; i < 16; ++i) { GATHER_4x(W[i], saved_key, i); }
                saved_key += (VS32<<6);
            } else if (SSEi_flags & SSEi_2BUF_INPUT) {
                for (i=0; i < 16; ++i) { GATHER_2x(W[i], saved_key, i); }
                saved_key += (VS32<<5);
            } else {
                for (i=0; i < 16; ++i) { GATHER(W[i], saved_key, i); }
                saved_key += (VS32<<4);
            }
            W += 16;
        }
#else
    (scalar code)

Perhaps you should try implementing #elif __AVX512__ || __MIC__ and #elif __AVX2__ clauses for them? That would speed up some formats a lot (I think).

<!-- gh-comment-id:92264411 --> @magnumripper commented on GitHub (Apr 13, 2015): We have this in several places in sse-intrinsics.c ``` C #if __SSE4_1__ unsigned k; vtype *W = w; ARCH_WORD_32 *saved_key = (ARCH_WORD_32*)_data; MD5_PARA_DO(k) { if (SSEi_flags & SSEi_4BUF_INPUT) { for (i=0; i < 16; ++i) { GATHER_4x(W[i], saved_key, i); } saved_key += (VS32<<6); } else if (SSEi_flags & SSEi_2BUF_INPUT) { for (i=0; i < 16; ++i) { GATHER_2x(W[i], saved_key, i); } saved_key += (VS32<<5); } else { for (i=0; i < 16; ++i) { GATHER(W[i], saved_key, i); } saved_key += (VS32<<4); } W += 16; } #else (scalar code) ``` Perhaps you should try implementing `#elif __AVX512__ || __MIC__` and `#elif __AVX2__` clauses for them? That would speed up some formats a lot (I think).
Author
Owner

@magnumripper commented on GitHub (Apr 13, 2015):

Perhaps you should try implementing #elif __AVX512__ || __MIC__ and #elif __AVX2__ clauses for them?

Btw obviously it would need to be

#if __AVX512__ || __MIC__
#elif __AVX2__
#elif __SSE4_1__
#else
#endif
<!-- gh-comment-id:92265251 --> @magnumripper commented on GitHub (Apr 13, 2015): > Perhaps you should try implementing `#elif __AVX512__ || __MIC__` and `#elif __AVX2__` clauses for them? Btw obviously it would need to be ``` C #if __AVX512__ || __MIC__ #elif __AVX2__ #elif __SSE4_1__ #else #endif ```
Author
Owner

@lei-april commented on GitHub (Apr 13, 2015):

OK, I'll do it.

<!-- gh-comment-id:92314313 --> @lei-april commented on GitHub (Apr 13, 2015): OK, I'll do it.
Author
Owner

@magnumripper commented on GitHub (Apr 14, 2015):

Removed from pseudo_intrinsics.h in cec121c1

<!-- gh-comment-id:93007776 --> @magnumripper commented on GitHub (Apr 14, 2015): Removed from pseudo_intrinsics.h in cec121c1
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openwall/john#753
No description provided.