Bin ich zu Blöd dafür

Ich dachte du willst mich zum Gold-Mitglied machen *G*

Dann werde ich mal ein Gegenbeispiel auftreiben:

Code:
// Reverse the 32-bits in EAX, leaving the result in EBX:
                     mov( 32, cl );
RvsLoop:    shr( 1, eax );    // Move current bit in EAX to the carry flag.
                     rcl( 1, ebx );    // Shift the bit back into EBX, backwards.
                     dec( cl );
                     jnz RvsLoop

Dies ist der einfache - triviale und übersichtliche Code. Dummerweise muss die Schleife 32 mal durchlaufen werden um bei einem 32bit Register die Bits umzudrehen.

Hier der Optimierte Code:

Code:
//optimierte Version
     mov( eax, edx );         // Make a copy of the odd bits in the data.
     shr( 1, eax );           // Move the even bits to the odd positions.
     and( $5555_5555, edx );  // Isolate the odd bits by clearing even bits.
     and( $5555_5555, eax );  // Isolate the even bits (in odd posn now).
     shl( 1, edx );           // Move the odd bits to the even positions.
     or( edx, eax );          // Merge the bits and complete the swap.
  
     mov( eax, edx );         // Make a copy of the odd numbered bit pairs.
     shr( 2, eax );           // Move the even bit pairs to the odd posn.
     and( $3333_3333, edx );  // Isolate the odd pairs by clearing even pairs.
     and( $3333_3333, eax );  // Isolate the even pairs (in odd posn now).
     shl( 2, edx );           // Move the odd pairs to the even positions.
     or( edx, eax );          // Merge the bits and complete the swap.
 
     mov( eax, edx );         // Make a copy of the odd numbered nibbles.
     shr( 4, eax );           // Move the even nibbles to the odd position.
     and( $0f0f_0f0f, edx );  // Isolate the odd nibbles.
     and( $0f0f_0f0f, eax );  // Isolate the even nibbles (in odd posn now).
     shl( 4, edx );           // Move the odd pairs to the even positions.
     or( edx, eax );          // Merge the bits and complete the swap.
 
     bswap( eax );            // Swap the bytes and words.

ich fürchte ohne Kommentare wäre er a) schlecht wartbar und b) unverständlich. Allerdings ist die Optimierung eine substantielle Geschwindigkeitsverbesserung die nicht ungenutzt bleiben darf wenn diese Operation häufig gebraucht wird.

Du bist dran ;)
 
Zuletzt bearbeitet:
es ist immerhin schon High Level Assembler (ein Präprozessor für verschiedenen Assembler der Hight Level Konstrukte in den jeweiligen Assemblercode übersetzt und eine Bib bereit stellt - ist für asm lernen gedacht).

Aber der Algorithmus wäre auch in Java nicht lesbarer. Er arbeitet mit logischen Tricks auf Bitebene.
 

Neue Beiträge

Zurück