Autor Beitrag
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: So 24.10.04 13:20 
Ich habe zwei Funktionen geschrieben, die aus einem LongWord einen speziellen 28bit-Integer machen und vice versa. Dieser ist folgendermaßen aufgebaut: 0xxxxxxx 0xxxxxxx 0xxxxxxx 0xxxxxxxalso jeweils das höchstwertige Bit auf 0 gesetzt, wobei das Least Significant Byte rechts steht und nicht links. Vorzeichen gibt es nicht, dargestellt wird dieser Integertyp in einem array[0..3] of Byte.
Die Funktionen sind einige Takte schneller als das Ergebnis des kompilierten Delphi-Codes (steht in den Kommentaren) und benötigt keine Stackzugriffe (im Gegensatz zum Delphi-Kompilat).
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
function Int28ToLongWord(aValue: TInt28): LongWord;
asm
  // Int28:
  // The MSB of every Byte is set to 0 and does not influence the value.

  // Take the rightmost byte and let it there,
  // take the second rightmost byte and move it 7bit to left
  //                                    (in an 32bit-variable)
  // a.s.o.
  // Result := (aValue[3]) shl  0 or
  //           (aValue[2]) shl  7 or
  //           (aValue[1]) shl 14 or
  //           (aValue[0]) shl 21;

  MOV EDX, EAX;
  XOR EAX, EAX;
  OR   AL,  DL;
  SHL EAX, $07;
  OR   AL,  DH;
  SHL EAX, $07;
  SHR EDX, $10;
  OR   AL,  DL;
  SHL EAX, $07;
  OR   AL,  DH;
end;

function LongWordToInt28(aValue: LongWord): TInt28;
asm
  // move every byte in Value to the right, take the 7 LSBs
  // and assign them to the result
  // Result[3] := (aValue shr  0) and $7F;
  // Result[2] := (aValue shr  7) and $7F;
  // Result[1] := (aValue shr 14) and $7F;
  // Result[0] := (aValue shr 21) and $7F;

  MOV EDX, EAX;
  SHL  AX, $08;
  SHR EDX, $07;
  MOV  AL,  DL;
  SHL EAX, $10;
  SHR EDX, $07;
  MOV  AH,  DL;
  SHR EDX, $07;
  MOV  AL,  DL;
  AND EAX, $7F7F7F7F;
end;
Geht das Ganze vielleicht noch schneller oder habe ich sogar irgendwo eine Schwachstelle übersehen (Stichprobenartige Testläufe lieferten jedes Mal das richtige Ergebnis)?

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert