TweetFollow Us on Twitter

June 96 - 64-Bit Integer Math on 680x0 Machines

64-Bit Integer Math on 680x0 Machines


When an application has to perform integer arithmetic with numbers larger than 32 bits on both the PowerPC and 680x0 platforms, you could use the floating-point types of the SANE and PowerPC Numerics libraries. But if all you really need is a larger integer, a better choice is to use the existing 64-bit math routines available on the PowerPC platform and write an equivalent library for the 680x0 Macintosh. This article presents just such a library.

Developers of PowerPC applications that need 64-bit math can simply call the various "wide" Toolbox routines. These routines perform addition, subtraction, multiplication, division, square root, and a few other operations. On the 680x0-based Macintosh, some of these same routines are available in QuickDraw GX. But if you can't assume your customers have QuickDraw GX installed, you need a library that supports 64-bit math.

The Wide library presented in this article works on both platforms and has exactly the same interface and types as the wide routines in the Toolbox on PowerPC machines. The library also provides some new routines such as 32-bit to 64-bit add and subtract and a 64-bit-to-string conversion function. The library is included on this issue's CD, along with its source code.

All the routines use the 64-bit data type defined in the header file Types.h, which is the standard type used for signed 64-bit integers on both the PowerPC and 680x0 Macintosh:

struct wide {
   Sint32   hi;   /* upper 32 bits (signed) */
   Uint32   lo;   /* lower 32 bits (unsigned) */
typedef struct wide wide, *WidePtr;


Before plunging into the Wide library, let's see what 64-bit math routines I'll be talking about. First, I'll introduce those that are available on PowerPC machines, then those you'll find on a 680x0 Macintosh with QuickDraw GX, and finally the routines in the Wide library.


In the header file FixMath.h, the routines listed in Table 1 are defined for 64-bit math on the PowerPC platform.


On 680x0 machines that have QuickDraw GX installed, all the wide routines for the PowerPC platform listed in Table 1 are available, with the exception of WideBitShift. The QuickDraw GX header file GXTypes.h defines the wide routine types and function prototypes in exactly the same way that the header file FixMath.h does for PowerPC machines.

In addition, QuickDraw GX on 680x0 machines has a routine that the PowerPC platform doesn't have: WideScale. This function returns the bit number of the highest-order nonzero bit in a 64-bit number. The Wide library implements this function on the PowerPC platform.


The Wide 64-bit integer math library on this issue's CD provides all the wide routines that are available on PowerPC machines and on 680x0 machines with QuickDraw GX, plus a few extras. The extra routines, which are available on both the PowerPC and 680x0 platforms, are listed in Table 2.

WideAssign32, WideAdd32, WideSubtract32. These routines are self-explanatory.

WideToDecStr. This routine converts a signed 64-bit integer to the SANE string type decimal, which is also defined by the PowerPC Numerics library. This string structure is a good intermediate format for final conversion to a string format of your choosing.

Since WideToDecStr calls the SANE library to generate the string, SANE must be linked with your 680x0 application. The SANE library is included with all the major development systems.

To convert the string returned by WideToDecStr to a Pascal string, call the SANE routine dec2str.

    If you want to generate a localized number, take a look at the article "International Number Formatting" in develop Issue 16. You could call the LocalizeNumberString function from that article after converting the output of WideToDecStr to a Pascal string, or you could modify LocalizeNumberString to accept the output of WideToDecStr.*
p> WideInit. The library is self-initializing; the first time you call any wide routine, WideInit is also called. If the execution speed of your first runtime call to a wide routine is important, you have the option of calling WideInit during your application's startup to avoid that overhead.

The purpose of WideInit is to determine what processor is being used, or emulated; it calls Gestalt to make this determination. If your Macintosh has a 68020-68040 CPU (68020, 68030, or 68040), the library will use the 64-bit multiply and divide instructions available on that processor; otherwise, the library will have to call software subroutines for those operations. On 68000 machines, such as the Macintosh Plus and SE, the processor's multiply instruction is limited to 32 bits and the library has no choice but to use the slower algorithmic approach for multiplication and division.


The library can be compiled on the 680x0 and PowerPC platforms using either the Metrowerks CodeWarrior or Symantec C development system. The library tests which development system is compiling it and, if it's not CodeWarrior or Symantec, the preprocessor displays an error message saying the library needs to be ported to your environment. This is necessary because there's some inline assembly language in the source file, as discussed later in this section, and different C compilers handle assembly language differently.

While the interface routines to our 64-bit library are the same on the PowerPC and 680x0 machines, when you compile the library a different subset of routines is linked in, depending on your environment:

  • If you build the library for a 680x0 machine without QuickDraw GX headers, all the Wide library routines are defined.

  • If you build the library for a 680x0 machine and include the QuickDraw GX header file GXTypes.h or GXMath.h before the Wide library's Wide.h header file, the extra routines and the WideBitShift routine are defined. The other wide routines are already available via the QuickDraw GX traps.

  • When you compile for the PowerPC platform, only the five extra routines (WideAssign32, WideAdd32, WideSubtract32, WideToDecStr, and WideInit) are defined in the library. All the other wide routines already exist in the PowerPC Toolbox. Additionally, if GXTypes.h or GXMath.h isn't included, WideScale is defined.
Table 3 summarizes where the wide routines can be found on the different platforms.

Note that the Wide library decides at compile time which routines to use. When QuickDraw GX header files are not included, the Wide library routines are called. If your application needs to make a runtime decision about whether to use QuickDraw GX, you'll need to make some changes to the library. One solution is to rename the Wide library routines and remove the conditional compilation tests for QuickDraw GX from the source. Then at run time you can decide which version to call -- the QuickDraw GX routines if they're available, or the internal Wide library routinesif not.


The Wide library was compiled with version 2.1 of Apple's universal headers. The latest headers are available on this issue's CD. You should make sure you have a recent version of these headers, because the library uses the constant GENERATING68K. If the header file ConditionalMacros.h doesn't contain this constant, your version of the universal headers is too old.


Some of the routines in the library are written in assembly language to take advantage of the 64-bit multiply and divide instructions on 68020-68040 machines, because on these machines the C language will use only 32-bit multiply and divide instructions. On PowerPC machines, the Wide library doesn't need assembly language because the 64-bit multiply and divide routines are provided by the Toolbox.

The library's source file Wide.c contains both C and assembly language. It has been successfully compiled by Symantec C 7.0.4 and CodeWarrior 7. If you want to compile the library on any other development system, you may have to do a little work porting it. Most of the changes will be confined to the conditional compilation statements at the beginning of Wide.c where the differences in SANE types and inline assembly language are handled.


Now let's look at a couple of the more interesting routines in the Wide library to see how they work. See the source code on the CD for full implementations of all the routines.


WideMultiply (Listing 1) performs a 32-by-32-bit multiply and produces a 64-bit result. The first and second parameters are the two signed 32-bit integers to be multiplied together. The return value is a pointer to the 64-bit result that's also returned via the third parameter.

Listing 1. The multiply routine

wide *WideMultiply (
   long multiplicand,   /* in: first value to multiply */
   long multiplier,      /* in: second value to multiply */
   wide *target_ptr)      /* out: 64 bits to be assigned */
   /* Initialize Wide library if not already done. */
   if (!gWide_Initialized) WideInit();

   /* If the 64-bit multiply instruction is available... */
   if (gWide_64instr) {
      /* Execute the assembly-language instruction MULS.L */
      Wide_MulS64(multiplicand, multiplier, target_ptr);
   else {
      /* Call the Toolbox to perform the multiply. */
      LongMul(multiplicand, multiplier, (Int64Bit *) target_ptr);

   return (target_ptr);
WideMultiply first tests whether the library has been initialized yet; if not, it calls WideInit. Next the routine tests whether the 64-bit multiply instruction is available on the current CPU by examining the global variable gWide_64instr (which was set by the initialization routine WideInit). If the instruction is available, WideMultiply calls the assembly-language function Wide_MulS64 to take advantage of it (as described later); otherwise, WideMultiply calls the Toolbox routine LongMul to perform the multiplication, as would be the case on 68000 machines.


The WideSquareRoot function (Listing 2) takes a 64-bit unsigned number as input and returns a 32-bit unsigned result. All possible results can be expressed in 32 bits, so overflow isn't possible.

Listing 2. The square root routine

unsigned long WideSquareRoot (
   const wide *source_ptr) /* in: value to take the square root of */
   wide            work_integer;
   Extended_80      extended_80_number;

   /* Initialize Wide library if not already done. */
   if (!gWide_Initialized) WideInit();

   /* Convert "wide" number to "extended" format. */
   Wide_ToExtended(&extended_80_number, source_ptr);

   /* If compiling with CodeWarrior, the parameter to sqrt is a
      pointer instead of a value, as defined in PowerPC Numerics. */
#ifdef __MWERKS__
   extended_80_number = sqrt(extended_80_number);

   /* Convert "extended" format to "wide" number. */
   Wide_FromExtended(&work_integer, &extended_80_number);
   /* OK to ignore work_integer.hi as it's always 0. */
   return (work_integer.lo);
For this routine I decided to let the SANE library do the work of generating the square root. The routine converts the 64-bit input number to an 80-bit floating-point number and then calls the SANE library function sqrt to calculate the square root. Finally, WideSquareRoot converts the resulting 80-bit floating-point number back to a 64-bit integer and returns the low-order half of the result.

When a 64-bit integer is converted to an 80-bit floating-point number, no loss in precision occurs. An 80-bit floating-point number is made up of three parts -- the sign (1 bit), the exponent (15 bits), and the fractional part (64 bits). As you can see, a 64-bit integer exactly fits in the fractional part.

Two differences between the CodeWarrior and Symantec development systems that show up in the Wide library's WideSquareRoot function are the 80-bit floating-point types and the parameters of the SANE library's square root function. Under CodeWarrior, the Wide library internal type Extended_80 is defined as the type extended80, and Sqrt returns the result to the same location as the input number. Under Symantec C, Extended_80 is defined as the type extended, and sqrt returns the result as a function return value.


The Wide library uses internal assembly-language routines to execute 64-bit multiply and divide instructions on machines that support those instructions. In case you're interested, here are the details.

Symantec and CodeWarrior handle the asm keyword differently, so I used some preprocessor commands (#defines) to handle the differences between the two development systems. Near the beginning of the Wide.c source file there are four #defines that differ depending on which development system you're using, as shown in Table 4.


Wide_MulS64 (Listing 3) is an internal assembly-language routine that WideMultiply calls to execute the 64-bit multiply instruction on the 68020-68040 CPUs. It starts with ASM_FUNC_HEAD, as mentioned in Table 4. The three definitions at the start of the function (MULTIPLICAND, MULTIPLIER, and OUT_PTR) are the byte offsets to the parameters. Although in Symantec C it's possible to refer to function parameters by name via A6, this isn't possible in CodeWarrior. I had to give up accessing the parameters by name and use #defines instead.

Listing 3. 64-bit multiply instruction

ASM_FUNC_HEAD static void Wide_MulS64 (
   long multiplicand,   /* in: first value to multiply */
   long multiplier,     /* in: second value to multiply */
   wide *out_ptr)       /* out: 64 bits to be assigned */
#define MULTIPLICAND     8
#define MULTIPLIER      12
#define OUT_PTR         16

      MOVE.L   MULTIPLICAND(A6),D0     //
      DC.W      0x4C2E,0x0C01,0x000C   // MULS.L multiplier(A6),D1-D0
      MOVE.L   OUT_PTR(A6),A0          //
      MOVE.L    D0,WIDE_LO(A0)         //
      MOVE.L   D1,WIDE_HI(A0)          //
To execute the 64-bit multiply instruction I had to define it with a DC.W directive that generates the desired object code. This was necessary because the Symantec C inline assembler supports only the 32-bit multiply instruction and won't recognize the 64-bit assembly opcode.


If the 64-bit divide instruction isn't available, the library calls the internal assembly-language routine Wide_DivideU (Listing 4) to perform the division using an algorithm. The algorithm is basically a binary version of the paper and pencil method of doing long division that all of us learned in school. It's a loop that executes once for each bit in the size of the divisor, which is 32 in our case. The Wide_DivideU subroutine actually handles only unsigned division, but the library function that calls it will take care of converting the input parameters to positive values and, if required, converting the result to a negative value.

Listing 4. 64-bit unsigned division algorithm

ASM_FUNC_HEAD static void Wide_DivideU (
   wide *dividend_ptr,      /* in/out: 64 bits to be divided */
   long divisor,            /* in: value to divide by */
   long *remainder_ptr)     /* out: the remainder of the division */
#define DIVIDEND_PTR      8
#define DIVISOR         12
#define REMAINDER_PTR   16

      MOVEM.L  D2-D7,-(SP)           // save work registers
      CLR.L    D0                    //
      CLR.L    D1                // D0-D1 is the quotient accumulator
      MOVE.L   DIVIDEND_PTR(A6),A0   //
      MOVE.L   WIDE_HI(A0),D2        //
      MOVE.L   WIDE_LO(A0),D3        // D2-D3 = remainder accumulator
      CLR.L    D4                    //
      MOVE.L   D2,D5                 // D5 = copy of dividend.hi
      MOVE.L   DIVISOR(A6),D6        // D6 = copy of divisor

      MOVEQ.L  #31,D7                // FOR number of bits in divisor
      LSL.L    #1,D0             // shift quotient.hi accum left once
      LSL.L    #1,D1             // shift quotient.lo accum left once
      LSL.L    #1,D4                 //
      LSL.L    #1,D3                 //
      ROXL.L   #1,D2             // shift remainder accum left once
      SUB.L    D6,D2                 // remainder -= divisor
      BCS      @div50                // If CS, remainder is negative
      BSET     #0,D1                 // quotient.lo |= 1
      BRA.S    @div77                //
      ADD.L    D6,D2                 // remainder += divisor
      BTST     D7,D5                 //
      BEQ      @div90            // If EQ, bit not set in dividend.hi
      BSET     #0,D4                  //
      CMP.L    D6,D4                  //
      BCS      @div99                 // If CS, divisor < D4
      SUB.L    D6,D4                  // D4 -= divisor
      BSET     #0,D0                  // quotient.hi |= 1
      DBF      D7,@divloop            // loop until D7 == -1
      MOVE.L   DIVIDEND_PTR(A6),A0    // output the remainder
      MOVE.L   D0,WIDE_HI(A0)         //
      MOVE.L   D1,WIDE_LO(A0)         //
      MOVE.L   REMAINDER_PTR(A6),A0   // output the remainder
      MOVE.L   D2,(A0)                //
      MOVEM.L  (SP)+,D2-D7            // restore work registers
The top of the assembly-language loop starts at the @divloop label. For each loop, the algorithm shifts the quotient and the remainder left one bit position before trying to subtract the divisor from the remainder. If the subtraction can be done, the least-significant bit in quotient.lo is set; otherwise, the subtraction is undone by the add instruction near the @div50 label. Then, if the divisor is greater than the loop bits that are accumulating in register D4, the least-significant bit in quotient.hi is set.

Notice that the first assembly-language statement in Wide_DivideU is a MOVEM.L instruction that saves on the stack all the registers that the division loop uses; the last instruction is a MOVEM.L instruction that restores these registers. Fortunately, this subroutine can place all its working variables in registers and avoid the stack for its loop, thus improving performance.


There you have it. Now 64-bit integer math can be handled with the same API on both the 680x0 and PowerPC platforms. Having the same function-level interface on these two very different processors makes life a lot easier for application programmers. Don't you wish all libraries had the same interface regardless of the CPU or system software version?

DALE SEMCHISHEN ( lives in Vancouver, British Columbia, with his wife Josephine. He works for Glenayre Technologies as a paging software developer (they make the control systems that send messages to your belt beeper). Recently, he had to accept the fact that the world is changing when his retired father started talking about his Internet provider.*

Thanks to our technical reviewers Dave Evans, Quinn "The Eskimo!", and Dave Radcliffe. Special thanks to Dave Johnson for software testing.*


Community Search:
MacTech Search:

Software Updates via MacUpdate

Bartender 4.1.7 - Organize your menu-bar...
Bartender lets you organize your menu-bar apps by hiding them, rearranging them, or moving them to Bartender's Bar. You can display the full menu bar, set options to have menu-bar items show in the... Read more
VirtualBox 6.1.28 - x86 virtualization s...
VirtualBox is a family of powerful x86 virtualization products for enterprise as well as home use. Not only is VirtualBox an extremely feature rich, high performance product for enterprise customers... Read more
Pro Video Formats 2.2.2 - Updates for pr...
Pro Video Formats includes support for the following professional video codecs: Apple Intermediate Codec Apple ProRes AVC-Intra 50 / 100 / 200 / 4:4:4 / LT AVC-LongG XAVC XF-AVC DVCPRO HD HDV XDCAM... Read more
MySQL 8.0.27 - Industry-leading open-sou...
MySQL, the industry-leading open-source SQL database, is an accessible, easy-to-use relational database management system (RDBMS). As an alternative to Oracle and SQL server, MySQL offers features... Read more
Typinator 8.10 - Speedy and reliable tex...
Typinator turbo-charges your typing productivity. Type a little. Typinator does the rest. We've all faced projects that require repetitive typing tasks. With Typinator, you can store commonly used... Read more
Motion 5.6 - Create and customize Final...
Motion is designed for video editors, Motion 5 lets you customize Final Cut Pro titles, transitions, and effects. Or create your own dazzling animations in 2D or 3D space, with real-time feedback as... Read more
Suitcase Fusion 21.4.4 - Font management...
Suitcase Fusion is the creative professional's font manager. Every professional font manager should deliver the basics: spectacular previews, powerful search tools, and efficient font organization.... Read more
EtreCheck Pro 6.5.3 - For troubleshootin...
EtreCheck is an app that displays the important details of your system configuration and allow you to copy that information to the Clipboard. It is meant to be used with Apple Support Communities to... Read more
iMovie 10.3 - Edit personal videos and s...
With a streamlined design and intuitive editing features, iMovie lets you create Hollywood-style trailers and beautiful movies like never before. Browse your video library, share favorite moments,... Read more
Compressor 4.6 - Adds power and flexibil...
Compressor adds power and flexibility to Final Cut Pro X export. Customize output settings, work faster with distributed encoding, and tap into a comprehensive set of delivery features. Features:... Read more

Latest Forum Discussions

See All

Divinity - Original Sin 2 - Gameplay Jou...
When I last touched Divinity - Original Sin 2, I thought I was done with my seafaring shenanigans aboard the Lady Vengeance. I was away from Fort Joy on course for Driftwood, but my journey got interrupted. This entry deals head on with this... | Read more »
SwitchArcade Round-Up: ‘Spectacular Spar...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for October 20th, 2021. In today’s article, we’ve got several new games to check out. A few of them seem like they could be a good time, which is always a pleasant bonus. There’s a... | Read more »
‘Horizon Chase’ Senna Forever Expansion...
Following the reveal of the Horizon Chase Senna Forever expansion, the game was updated today on all platforms. If you’ve not kept up with the excellent racer, Horizon Chase Senna Forever is a new expansion that pays homage to the legendary driver... | Read more »
Square Enix Opens Square Enix London Mob...
Square Enix just announced its newest studio today in the form of Square Enix London Mobile. Square Enix London Mobile is a new studio in London aimed to publish and deliver high quality free to play mobile games worldwide. The studio is currently... | Read more »
Beginner’s guide to becoming a fearsome...
The Lord of the Rings: Rise to War is NetEase’s brand new strategy game based on JRR Tolkien’s epic fantasy trilogy. Choose your favourite characters as Commanders, assemble your army, and march to fight until you claim the One Ring for your own.... | Read more »
‘The Lord of the Rings: Rise to War’ Tip...
If you’re looking for some of the best ways to progress quickly in The Lord of the Rings: Rise to War, then you’re in luck, because below we’re going to take a look at all the important features a new Commander needs to take into consideration when... | Read more »
‘LEGO Star Wars: Castaways’ Is a New Onl...
After a vague tease from the official Apple Arcade Twitter account, LEGO Star Wars: Castaways () by Gameloft was officially announced for release next month on the service. The original teaser Tweet had people speculating about vastly different... | Read more »
Steam Link’s New iOS Update Adds Support...
Ever since Valve brought Steam Link (Free) to iOS following a long period of uncertainty, it has been updated quite a bit with useful features and enhancements. | Read more »
‘Call of Duty Mobile’ Season 9 “Nightmar...
It is Halloween season in all sorts of mobile games, and yes that includes Call of Duty Mobile with the arrival of the Season 9 update this week. In fact, Season 9 is titled Nightmare because it’s soooo spoooooky. The flagship spooky feature is the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for October 19th, 2021. In today’s article, we’ve got a couple of reviews that work out to a few more somehow. The latest from Onion Games, Mon Amour, and the recent Cotton Guardian... | Read more »

Price Scanner via

So… When Will All The NEW Products Apple Anno...
FEATURE: 10.20.21 – By now, you’ve probably heard everything there is to know about the products that were unveiled at Apple’s second Fall product announcement of the year, but when will they finally... Read more
M1 Mac minis on sale for $50-$100 off MSRP at...
Looking for a discount on one of Apple’s popular new Mac minis with Apple M1 Silicon CPUs? These Apple Authorized Resellers are offering $50-$100 discounts on standard mini configurations today. (1... Read more launches new Apple 14″ MacBook...
Apple introduced the new 14″ M1 MacBook Pros yesterday (and 16″ models) with an updated design, new 10- to 32-Core M1 Pro and M1 Max CPUs, magic keyboards, edge-to-edge displays, and more. Apple and... Read more
Base 21″ 2.3GHz iMac on sale for $990, plus t...
Expercom has the base 21″ 2.3GHz Dual-Core Intel-based iMac on sale for $990.85 shipped. Their price is $109 off Apple’s MSRP, and it’s the cheapest price for a new iMac from any Apple reseller. You... Read more
On sale again: MagSafe Chargers for up to $25...
Verizon has Apple MagSafe Chargers and Apple’s MagSafe Battery on sale again for up to 25% off MSRP. Verizon service is not required to take advantage of these savings. With the discount, Verizon’s... Read more
Expercom is offering 14″ and 16″ M1 MacBook P...
Apple reseller Expercom is offering discounts on new M1 Pro and M1 Max 14″ and 16″ MacBook Pros ranging up to $305 off Apple’s MSRP. Preorders start today. Order now, and Expercom estimates free... Read more
Expercom offers $50 AppleCare+ discount when...
Apple reseller Expercom is offering a $50 discount on the purchase of an AppleCare+ Plan alongside a new 2021 14″ MacBook Pro or 16″ MacBook Pro with an Apple M1 Pro or M1 Max CPU. Their discount... Read more
Apple drops prices on Certified Refurbished 1...
With the introduction of the 2021 M1 Pro and M1 Max 16″ MacBook Pros, Apple has dropped prices on Certified Refurbished 2020 16″ 6-Core & 8-Core MacBook Pros, with models now available for up to... Read more
Apple to introduce new Macs today, perhaps mo...
Apple is widely anticipated to introduce several new Macs at their special fall event today. Watch their presentation here, at 10am PT. We’re expecting new MacBooks and maybe some new desktops. It’s... Read more
Are The Days Upon Us When Apple (Inc., Not Th...
FEATURE: 10.16.21 – Don’t be surprised if at your next doctor’s appointment, your physician tells you to take two AirPods and call them in the morning. Back in June — in a story from Macworld... Read more

Jobs Board

Sr Infrastructure Engineer - *Apple* MacInt...
**Sr Infrastructure Engineer - Apple MacIntosh** **Description** At Bank of the West, our people are having a positive impact on the world. We're investing where we Read more
Geek Squad Advanced Repair *Apple* Professi...
**836454BR** **Job Title:** Geek Squad Advanced Repair Apple Professional **Job Category:** Store Associates **Store Number or Department:** 000215-Greenspoint-Store Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States ( - Apple Blossom Mall Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States ( - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States ( - Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.