Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: PowerPC Numerics / Part 3 - Numerics in PowerPC Assembly Language
Chapter 13 - Assembly-Language Numeric Conversions


Conversions From Integer to Floating-Point Formats

No single instruction is available to convert an integer to floating-point format. However, you can perform this operation using the algorithm in Listing 13-1. First, define the following constant:

kmagic: word 0x43300000,0x80000000
This constant must have an exponent of 52 after subtracting the bias for the double format (1023), and the lower half of the constant (bit 33) must begin with a 1. In the constant kmagic above, the first word (eight hexadecimal digits) corresponds to the exponent part and the last word corresponds to the integer part.

When you have an integer you want to convert, invert its sign, append the exponent part of the constant to the integer to be converted, and then load it into a floating-point register with the new exponent appended. Finally, subtract the floating-point constant from the newly formed floating-point integer. The assembly code in Listing 13-1 shows how this is done. The code fragment assumes that general-purpose register GPR0 contains the value 0 and that register GPR3 contains the value to be converted.

Listing 13-1 Converting a number from integer format to floating-point format

   addis r1,r0,0x4330   # r1 contains 0x4330000
   stw   r1,20000(r0)   # store exponent part for integer
   xoris r3,r3,0x8000   # invert sign of integer
   stw   r3,20004(r0)   # store fraction part for integer
                        # now all parts are in memory
   lfd   f0,20000(r0)   # load integer in double format into f0
   lfd   f1,kmagic(r0)  # load constant into f1
   fsub  f0,f0,f1       # f0 contains converted integer

Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996