Utils.Mos 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. MODULE Utils;
  2. IMPORT SYSTEM;
  3. (* note that the biased exponent (exponent + 127) of an IEEE 32bit float starts at bit 23 *)
  4. (* add y to the binary exponent of y. PACK(x, y) is equivalent to x := x * 2^y. *)
  5. PROCEDURE PACK*(VAR x: REAL; y: LONGINT);
  6. CODE
  7. LDR R2, [FP, #x] ; R2 := address of x
  8. LDR R3, [FP, #y] ; R3 := y
  9. ADD SP, SP, #8
  10. LDR R4, [R2, #+0]; R4 := value of x
  11. ADD R4, R4, R3, LSL #23 ; increase the (biased) exponent of x by y
  12. STR R4, [R2, #+0] ; store new value of x
  13. END PACK;
  14. (* remove the binary exponent on x and put it into y. UNPK is the reverse operation of PACK. The resulting x is normalized, i.e. 1.0 <= x < 2.0. *)
  15. PROCEDURE UNPK*(VAR x: REAL; VAR y: LONGINT);
  16. CODE
  17. LDR R2, [FP, #x] ; R2 := address of x
  18. LDR R3, [FP, #y] ; R3 := address of y
  19. ADD SP, SP, #8
  20. LDR R4, [R2, #+0] ; R4 := value of x
  21. MOV R5, R4, LSR #23 ; R5 := biased exponent (and sign) of x
  22. SUB R5, R5, #127 ; R5 := exponent of x (biased exponent - 127)
  23. STR R5, [R3, #+0] ; store exponent of x as value for y
  24. SUB R4, R4, R5, LSL #23; reduce the biased exponent of x by the value of y
  25. STR R4, [R2, #+0] ; store new value of x
  26. END UNPK;
  27. END Utils.