[GH-ISSUE #1720] Optimize intrinsic functions with pre-processor-fu #1137

Open
opened 2026-10-14 03:36:19 +07:00 by bsakaguchi · 8 comments
Owner

Originally created by @magnumripper on GitHub (Aug 29, 2015).
Original GitHub issue: https://github.com/openwall/john/issues/1720

I'm pretty sure we can drop most branches in the SIMD functions using some clever rewrites and macros. The OpenCL functions does that. Well, that's not really a fair comparison since everything is always inlined but anyway.

Originally created by @magnumripper on GitHub (Aug 29, 2015). Original GitHub issue: https://github.com/openwall/john/issues/1720 I'm pretty sure we can drop most branches in the SIMD functions using some clever rewrites and macros. The OpenCL functions does that. Well, that's not really a fair comparison since everything is always inlined but anyway.
Author
Owner

@magnumripper commented on GitHub (Aug 29, 2015):

Example: The actual MD4 function would be SIMD_MD4(w, a, b, c, d). Everything else would be preprocessor stuff and at compile time most of it would disappear and there would be NO branches. I'm pretty sure the boost would be noticable.

<!-- gh-comment-id:136047462 --> @magnumripper commented on GitHub (Aug 29, 2015): Example: The actual MD4 _function_ would be `SIMD_MD4(w, a, b, c, d)`. Everything else would be preprocessor stuff and at compile time most of it would disappear and there would be NO branches. I'm pretty sure the boost would be noticable.
Author
Owner

@magnumripper commented on GitHub (Aug 29, 2015):

One thing that complicates things is that macros can't contain macros. So we can't put pseudo-intrinsics in macros...

<!-- gh-comment-id:136048243 --> @magnumripper commented on GitHub (Aug 29, 2015): One thing that complicates things is that macros can't contain macros. So we can't put pseudo-intrinsics in macros...
Author
Owner

@jfoug commented on GitHub (Aug 29, 2015):

Debuggging is also very hard with macro usages like this. I am seeing this in parts of dyna. You want to be DAMN sure things are correct before macro-izing things, and have a fall back to allow debugging.

<!-- gh-comment-id:136053741 --> @jfoug commented on GitHub (Aug 29, 2015): Debuggging is also very hard with macro usages like this. I am seeing this in parts of dyna. You want to be DAMN sure things are correct before macro-izing things, and have a fall back to allow debugging.
Author
Owner

@magnumripper commented on GitHub (Aug 31, 2015):

BTW this is very interesting:

Using our CPU intrinsics format, and AVX2 (all this is on well):

$ ../run/john -test -form=wpapsk
Benchmarking: wpapsk, WPA/WPA2 PSK [PBKDF2-SHA1 256/256 AVX2 8x]... (8xOMP) DONE
Raw:    13116 c/s real, 1649 c/s virtual

Same hardware, but using our OpenCL code (and an AVX2-aware CPU driver):

$ ../run/john -test -form=wpapsk-opencl -dev=0
Device 0: Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz
Benchmarking: wpapsk-opencl, WPA/WPA2 PSK [PBKDF2-SHA1 OpenCL]... DONE
Raw:    13540 c/s real, 1699 c/s virtual

Above, the device asked for scalar code so we served it that, then it was auto-vectorized and actually faster than our intrinsics.

Here's forcing 8x vector source code:

$ ../run/john -test -form=wpapsk-opencl -dev=0 -force-vector=8
Device 0: Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz
Benchmarking: wpapsk-opencl, WPA/WPA2 PSK [PBKDF2-SHA1 OpenCL 8x]... DONE
Raw:    13320 c/s real, 1668 c/s virtual

Slightly slower than auto-vectorized in this case, but still faster than our CPU format.

We don't get this good results for OpenCL with all formats, yet. But some day, we may. Scalar OpenCL is very easy to write, much much easier than writing plain CPU code using intrinsics.

<!-- gh-comment-id:136434756 --> @magnumripper commented on GitHub (Aug 31, 2015): BTW this is very interesting: Using our CPU intrinsics format, and AVX2 (all this is on well): ``` $ ../run/john -test -form=wpapsk Benchmarking: wpapsk, WPA/WPA2 PSK [PBKDF2-SHA1 256/256 AVX2 8x]... (8xOMP) DONE Raw: 13116 c/s real, 1649 c/s virtual ``` Same hardware, but using our OpenCL code (and an AVX2-aware CPU driver): ``` $ ../run/john -test -form=wpapsk-opencl -dev=0 Device 0: Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz Benchmarking: wpapsk-opencl, WPA/WPA2 PSK [PBKDF2-SHA1 OpenCL]... DONE Raw: 13540 c/s real, 1699 c/s virtual ``` Above, the device asked for scalar code so we served it that, then it was auto-vectorized and actually faster than our intrinsics. Here's forcing 8x vector source code: ``` $ ../run/john -test -form=wpapsk-opencl -dev=0 -force-vector=8 Device 0: Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz Benchmarking: wpapsk-opencl, WPA/WPA2 PSK [PBKDF2-SHA1 OpenCL 8x]... DONE Raw: 13320 c/s real, 1668 c/s virtual ``` Slightly slower than auto-vectorized in this case, but still faster than our CPU format. We don't get this good results for OpenCL with all formats, yet. But some day, we may. Scalar OpenCL is very easy to write, much much easier than writing plain CPU code using intrinsics.
Author
Owner

@magnumripper commented on GitHub (Sep 5, 2015):

One thing that complicates things is that macros can't contain macros. So we can't put pseudo-intrinsics in macros...

This is not correct. They can, but you'll need to be careful.

<!-- gh-comment-id:137982808 --> @magnumripper commented on GitHub (Sep 5, 2015): > One thing that complicates things is that macros can't contain macros. So we can't put pseudo-intrinsics in macros... This is not correct. They can, but you'll need to be careful.
Author
Owner

@jfoug commented on GitHub (Sep 5, 2015):

How do you define a macro in a macro? This shortfall was why I did the dynamic-big-hash.c the way I did (using an external script to do my macro expansions).

<!-- gh-comment-id:137989625 --> @jfoug commented on GitHub (Sep 5, 2015): How do you define a macro in a macro? This shortfall was why I did the dynamic-big-hash.c the way I did (using an external script to do my macro expansions).
Author
Owner

@magnumripper commented on GitHub (Sep 5, 2015):

You can not define a macro in a macro, but you can call a macro from a macro. Actually in as many levels as you want.

http://stackoverflow.com/questions/7972785/can-a-c-macro-definition-refer-to-other-macros

<!-- gh-comment-id:138002723 --> @magnumripper commented on GitHub (Sep 5, 2015): You can not define a macro in a macro, but you can _call_ a macro from a macro. Actually in as many levels as you want. http://stackoverflow.com/questions/7972785/can-a-c-macro-definition-refer-to-other-macros
Author
Owner

@magnumripper commented on GitHub (Sep 7, 2015):

A little experiment is now in the cpp-intrinsics topic branch. Specifically 51f3fe6 for now.

Currently only MD4 & MD5 are done, and not completely. What is done, is there are now (behind the curtain) two different functions - one for a single/first block and another for "reload". Also, the "flat to interleaved" is moved to a separate function but that is also hidden by PP macros (mosty optimized away since SSEi_flags are constants).

Boost is 5-10% depending on format. Still, I'm not quite sure we want to walk this path.

<!-- gh-comment-id:138344840 --> @magnumripper commented on GitHub (Sep 7, 2015): A little experiment is now in the [cpp-intrinsics](https://github.com/magnumripper/JohnTheRipper/commits/cpp-intrinsics) topic branch. Specifically 51f3fe6 for now. Currently only MD4 & MD5 are done, and not completely. What is done, is there are now (behind the curtain) two different functions - one for a single/first block and another for "reload". Also, the "flat to interleaved" is moved to a separate function but that is also hidden by PP macros (mosty optimized away since SSEi_flags are constants). Boost is 5-10% depending on format. Still, I'm not quite sure we want to walk this path.
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#1137
No description provided.