TweetFollow Us on Twitter

68881 Access
Volume Number:4
Issue Number:4
Column Tag:Forth Forum

Direct 68881 Floating Point Access

By Jörg Langowski, MacTutor Editorial Board, Grenoble, France

Being the proud owner of a MacII since the beginning of this year, I was - like so many others - disappointed at the relative slowness of the SANE package. Even with the floating point coprocessor, the speedup is only a factor of 5-10 compared to the Mac SE. The coprocessor itself allows you to go much faster than that, and it is Apple’s SANE implementation that slows down the operations.

The reason for this is, of course, that Apple is trying to guarantee the ‘compatibility’ of the SANE on the MacII with the old SANE implementation. That means that the results are supposed to be the same, down to the least significant bit (or even to the last guard bit?). The 68881 uses different algorithms for calculating transcendental functions than SANE does, therefore some of its built-in operations could not be used and had to be replaced by software. Of course, that slows down things a lot.

As a side note, I’m not at all in favor of such a strategy. Artificially restraining a high performance chip just because its results, although accurate enough, don’t match the old - also accurate - results to the last bit seems a little exaggerated. Stable numerical algorithms should take into account the possibility that the hardware changes slightly and the machine errors are different, and they should be immune against such changes; or at least the new SANE should have had an option built in that uses the 68881 directly! [Amen to that statement! -Ed]

Many of the development systems for the Macintosh now come with the possibility to generate code that supports the floating point coprocessor directly, many others don’t; working with Forth, we don’t have a real problem since we can easily redefine our floating point operators. This month I’ll show you how to do that.

The 68881

Let’s first have a look at the floating point coprocessor itself.

The 68881 is accessed from the 68020 in a special address area. When the 68020 encounters an instruction of the form $Fxxx (previously the F line trap), it will set its function code lines (pins FC0-FC2) all to high, indicating ‘CPU space’. It will then exchange information with the coprocessor’s internal registers to perform the floating point operation requested.

This information exchange occurs automatically, it is part of the 68020’s design; when using the floating point instructions, you don’t notice the communication between the two processors at all. The 68881 appears as an extension to the 68020, just as if we had a new set of registers available, with special instructions operating on them.

The registers FP0-FP7 can each hold one extended precision (80-bit) floating point number, in the format given by the IEEE standard: bits 0-63 contain the mantissa, 64-78 the 15-bit exponent offset by 16383, and bit 79 the sign. The instruction set of the 68881 allows you to do floating point operations on any or between any two of these registers, and transfer data between them, the 68020’s registers and memory.

Mach2’s assembler fully supports all 68881 instructions. The redefinition of the words in the SANE vocabulary is therefore quite straightforward. Register D7 is used by Mach2 as the floating point stack pointer, all we have to do is to transfer floating point numbers from the FP stack to the 68881’s registers, do the FP operation and transfer back the result.

The 68881 expects floating point numbers in a different format than SANE does. In order to keep FP numbers aligned to the boundaries of a 32-bit long word, which speeds up operations, an extended precision number in memory will be 96 bits long instead of 80; there is a 16 bit gap between the 64-bit mantissa and the 16-bit sign and exponent field. Since SANE and the floating point stack in Mach2 use 80 bit numbers, we have to convert their format when we use the coprocessor.

Listing 1 provides the macros f>2, f>1 and 1>f for this purpose. The general format for a binary floating point operation is then

 f>2  
 fop.x  FPn, FPm 
 1>f

where fop.x is an extended format floating point instruction that operates on floating point registers FPn and FPm. The example always uses registers FP0 and FP1. Unary operations are encoded the same way, using only FP0.

In the example, I provided a new vocabulary f68881 that contains redefinitions of the most important SANE operations for the 68881. Of course, the concept can be far extended. We have eight registers at our disposition and can use them to optimize more complicated numerical algorithms, using only assembly language. I might give some examples how to do this in a later column.

For the moment, let’s content ourselves with the speed improvement that we have achieved so far, which is already remarkable. Some simple benchmarks are listed at the end of listing 1, and the results are given here:

( MacII, direct 68881 access, 100000 loops each )

bmark1 2 Secs 18 Ticks ok <0> [0]

bmark2 4 Secs 48 Ticks ok <0> [0]

bmark3 4 Secs 14 Ticks ok <0> [0]

bmark4 4 Secs 17 Ticks ok <0> [0]

bmark5 4 Secs 14 Ticks ok <0> [0]

( MacII, SANE w/68881, 10000 loops each )

smark1 15 Ticks ok <0> [0]

smark2 37 Secs 12 Ticks ok <0> [0]

smark3 24 Ticks ok <0> [0]

smark4 1 Secs 33 Ticks ok <0> [0]

smark5 1 Secs 33 Ticks ok <0> [0]

( Mac +, SANE, 10000 loops each )

ok <0> [0]

smark1 24 Ticks ok <0> [0]

smark2 181 Secs 54 Ticks ok <0> [0]

smark3 49 Ticks ok <0> [0]

smark4 4 Secs 47 Ticks ok <0> [0]

smark5 5 Secs 14 Ticks ok <0> [0]

For the Mac+, the fnull1 and fnull2 operations had been replaced by simple fdrops. As you see, the speedup going from Mac+ to MacII’s SANE is not so breathtaking: a factor of 6 for the exponential, 4 for addition and subtraction; but when we access the 68881 directly, we gain another factor of 3 for simple addition and multiplication and 78 for the exponential. It is in the calculation of the transcendentals where the 68881 really shines.

Pop up menus

Someone approached me lately on the question of how to do pop up menu selection. Since the technical notes contain only sketchy references to popup menus at the time I write this (e.g. TN156, TN172), I’d like to give you a practical example how to use pop up menus from Mach2 in a simple way.

Listing 2 explains the process. The PopUpMenuSelect trap takes four parameters:

- a handle to the menu to be displayed (32 bits),

- the top and left global coordinate of the point at which to display the menu (2*16 bits),

- the menu item which should be positioned at that point for the default selection.

Although Mach2 knows the trap name, the interface to this routine is not (yet) correct, so we have to redefine it in assembler. Note that the point returned by the @mouse function is in local coordinates, while PopUpMenuSelect expects it in global coordinates.

The example defines a menu using the Mach2 interface; the menu is created with -1 as the insertion parameter (-1 150 mymenu BOUNDS) so that after insertion into the tasks’s menu bar the menu will stay invisible (just as we did for the hierarchical menus). Note that a pop up menu has to be inserted into the menu bar before using it.

The content handler for the default Mach2 window is then rewritten so that on a mouse down event it will select the example pop up menu. The menu handler will just beep a number of times depending on the item selected. dopop activates the new content handler while nopop deactivates it.

Feedback dept.

This letter comes from Vassili Dzuba, Paris:

“In January’s issue ‘Mousehole Report’, Alan Dall put in his wish list the ability to define ‘ghost copies’ of applications. Even without Unix’ capability of defining links, this can be done with a small program using the _Launch trap. This program takes only 1K on the disk. The path name of the application to launch is stored in ‘STR ‘ resource 1000. It’s possible to set the creator and the bundle bit of the ghost to have it share the same icon as the real thing. Of course, double-clicking on a document can then launch the ghost instead of the application, but the slowing down is only marginal.

The program is the following

(using MPW’s assembler):

 INCLUDE‘traps.a’
ghost MAIN
 MOVE.W #0,-(SP) 
 ; the context data (_launch parameter)
 MOVE.L #’STR ‘,-(SP)
 ; 1st parameter of GetResource
 MOVE.W #1000,-(SP)
 ; 2nd parameter of GetResource
 _GetResource
 ; handle to string in (SP)
 TST.L  (SP)
 ; test if null handle (no resource available)
 BEQ.B  exit
 ; if null, go to exit
 MOVE.L (SP),A0
 MOVE.L (A0),(SP)
 ; handle dereferenced
 MOVE.L SP,A0
 ; stack pointer in A0
 _Launch
exit  _ExitToShell
 END

The resource file is something like this (using Rez format):

#include “types.r”
resource ‘STR ‘ (1000) {
 “Sys:myDirectory:MyApp”
};

A ghost can be easily created using a small shell script (assuming the original ghost’s directory being {MPW}dev) which sets up the string resource:

duplicate {MPW}’dev:ghost’ {2}
echo ‘#include “types.r”’n
‘resource ‘’’STR ‘’’ (1000) { “‘{1}’” };’
| rez -a -o {2}

Assuming this script is named ‘summon’, the creation in the current directory of a ghost of MacPaint would be something like:

summon ‘sys:appli ƒ:mac paint’ ‘macpaint.Ghost’

Sincerely yours”

Thank you, Vassili, for this helpful little utility.


Listing 1: direct access 68881 floating point words for Mach2
\ 68881 access, © J. Langowski/MacTutor Jan 1988

only forth also assembler also sane
vocabulary f68881
also f68881 definitions

code f>2
 add.l  #20,d7
 move.l d7,a0
 move.l -(a0),-(a7); move mantissa
 move.l -(a0),-(a7); in two 32-bit chunks
 subq.l #2,a7    ; 16-bit gap
 move.w -(a0),-(a7); move exponent + sign
 fmove.x(a7)+,fp0; transfer from stack to fp0
 move.l -(a0),-(a7); same for fp1...
 move.l -(a0),-(a7)
 subq.l #2,a7
 move.w -(a0),-(a7)
 fmove.x(a7)+,fp1
 add.l  #10,a0 ; a0 points to 2nd fp stack item
 rts
end-code mach

code f>1
 add.l  #10,d7
 move.l d7,a0
 move.l -(a0),-(a7)
 move.l -(a0),-(a7)
 subq.l #2,a7
 move.w -(a0),-(a7)
 fmove.x(a7)+,fp0
 rts
end-code mach

code 1>f
 fmove.xfp0,-(a7)
 move.w (a7)+,(a0)+; transfer exponent + sign
 addq.l #2,a7    ; skip16 bit gap
 move.l (a7)+,(a0)+; transfer mantissa 
 move.l (a7)+,(a0)+; in 2 steps
 sub.l  #10,d7   ; adjust FP stack pointer
 rts
end-code mach

( note: f>1 or f>2 and 1>f should always occur in pairs since 1>f expects 
A0 to point to second floating point stack position; this is assured 
by f>1 and f>2 )

code f+
 f>2
 fadd.x fp1,fp0
 1>f
 rts
end-code

code f-
 f>2
 fsub.x fp1,fp0
 1>f
 rts
end-code

code f/
 f>2
 fdiv.x fp1,fp0
 1>f
 rts
end-code

code f*
 f>2
 fmul.x fp1,fp0
 1>f
 rts
end-code

code fmod
 f>2
 fmod.x fp1,fp0
 1>f
 rts
end-code

code frem
 f>2
 frem.x fp1,fp0
 1>f
 rts
end-code

code fabs
 f>1
 fabs.x fp0
 1>f
 rts
end-code

code facos
 f>1
 facos.xfp0
 1>f
 rts
end-code

code fasin
 f>1
 fasin.xfp0
 1>f
 rts
end-code

code fatan
 f>1
 fatan.xfp0
 1>f
 rts
end-code

code fatanh
 f>1
 fatanh.x fp0
 1>f
 rts
end-code

code fcos
 f>1
 fcos.x fp0
 1>f
 rts
end-code

code fcosh
 f>1
 fcosh.xfp0
 1>f
 rts
end-code

code fe^x
 f>1
 fetox.xfp0
 1>f
 rts
end-code
 
code fe^x-1
 f>1
 fetoxm1.x fp0
 1>f
 rts
end-code

code fgetexp
 f>1
 fgetexp.x fp0
 1>f
 rts
end-code

code fgetman
 f>1
 fgetexp.x fp0
 1>f
 rts
end-code

code fint
 f>1
 fint.x fp0
 1>f
 rts
end-code

code fintrz
 f>1
 fintrz.x fp0
 1>f
 rts
end-code

code fln
 f>1
 flogn.x  fp0
 1>f
 rts
end-code

code fln+1
 f>1
 flognp1.x fp0
 1>f
 rts
end-code

code flog10
 f>1
 flog10.x fp0
 1>f
 rts
end-code

code flog2
 f>1
 flog2.x  fp0
 1>f
 rts
end-code

code fneg
 f>1
 fneg.x fp0
 1>f
 rts
end-code

code fsin
 f>1
 fsin.x fp0
 1>f
 rts
end-code

code fsinh
 f>1
 fsinh.x  fp0
 1>f
 rts
end-code

code fsqrt
 f>1
 fsqrt.x  fp0
 1>f
 rts
end-code

code ftan
 f>1
 ftan.x fp0
 1>f
 rts
end-code

code ftanh
 f>1
 ftanh.x  fp0
 1>f
 rts
end-code

code f10^x
 f>1
 ftentox.x fp0
 1>f
 rts
end-code

code f2^x
 f>1
 ftwotox.x fp0
 1>f
 rts
end-code

fp

also forth definitions
code fnull1
 f>1
 1>f
 rts
end-code

code fnull2
 f>2
 1>f
 rts
end-code

: bmark1 counter 1.0 
 100000 0 do fdup fnull1 fdrop loop 
timer fdrop ;

: bmark2 counter 4.3352 
 100000 0 do fdup fe^x fdrop loop 
timer fdrop ;

: bmark3 counter 
 3.5 4.5 100000 0 do fover fover fnull2 fdrop loop 
timer fdrop fdrop ;

: bmark4 counter 
 3.5 4.5 100000 0 do fover fover f+ fdrop loop 
timer fdrop fdrop ;

: bmark5 counter 
 3.5 4.5 100000 0 do fover fover f* fdrop loop 
timer fdrop fdrop ;

also sane

: smark1 counter 
 1.0 10000 0 do fdup fnull1 fdrop loop 
timer fdrop ;

: smark2 counter 
 4.3352 10000 0 do fdup fe^x fdrop loop 
timer fdrop ;

: smark3 counter 
 3.5 4.5 10000 0 do fover fover fnull2 fdrop loop 
timer fdrop fdrop ;

: smark4 counter 
 3.5 4.5 10000 0 do fover fover f+ fdrop loop 
timer fdrop fdrop ;

: smark5 counter 
 3.5 4.5 10000 0 do fover fover f* fdrop loop 
timer fdrop fdrop ;
Listing 2: Pop up menus
\ popup menu interface from Forth
\ © J. Langowski/MacTutor Jan 1988

\ FUNCTION PopUpMenuSelect 
\ (menu: MenuHandle; top, left, popUpItem: INTEGER):
\LONGINT;
\  INLINE $A80B;

code popup ( hMenu top left item# | menuID item  -- )
 exg  d4,a7
 clr.l  -(a7)
 move.l 12(a6),-(a7) \ handle
 move.w 10(a6),-(a7) \ top
 move.w 6(a6),-(a7)\ left
 move.w 2(a6),-(a7)\ item
 adda.l #16,A6
 _popupmenuselect
 clr.l  d0
 clr.l  d1
 move.w (a7)+,d0
 move.w (a7)+,d1
 exg  d4,a7
 move.l d0,-(a6)
 move.l d1,-(a6)
 rts
end-code

CODE  @MOUSE
    SUBQ.L    #4,A6
    MOVE.L    A6,-(A7)
    _GETMOUSE
    RTS
END-CODE

CODE unpack
 MOVE.L (A6),D0
 CLR.L  D1
 MOVE.W D0,D1
 CLR.W  D0
 SWAP.W D0
 MOVE.L D0,(A6)
 MOVE.L D1,-(A6)
 RTS
END-CODE MACH

108 user taskmenubar
152 user content-hook

NEW.MENU myMenu
“ TestMenu” myMenu TITLE
-1 150 myMenu BOUNDS \ invisible menu
“ Item 1;Item 2;Item 3” myMenu ITEMS

taskmenubar @ mymenu add
call drawmenubar

: beep 
 ?dup if 
 0 do 5 call sysbeep loop 
 then
;

: content-handler { | pt -- }
 @mouse -> pt
 ^ pt call localtoglobal
 mymenu @
 pt unpack
 1
 popup
 beep drop
 run-content
;

: dopop [‘] content-handler content-hook ! ;
: nopop [‘] run-content content-hook ! ;
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Ableton Live 11.3.11 - Record music usin...
Ableton Live lets you create and record music on your Mac. Use digital instruments, pre-recorded sounds, and sampled loops to arrange, produce, and perform your music like never before. Ableton Live... Read more
Affinity Photo 2.2.0 - Digital editing f...
Affinity Photo - redefines the boundaries for professional photo editing software for the Mac. With a meticulous focus on workflow it offers sophisticated tools for enhancing, editing and retouching... Read more
SpamSieve 3.0 - Robust spam filter for m...
SpamSieve is a robust spam filter for major email clients that uses powerful Bayesian spam filtering. SpamSieve understands what your spam looks like in order to block it all, but also learns what... Read more
WhatsApp 2.2338.12 - Desktop client for...
WhatsApp is the desktop client for WhatsApp Messenger, a cross-platform mobile messaging app which allows you to exchange messages without having to pay for SMS. WhatsApp Messenger is available for... Read more
Fantastical 3.8.2 - Create calendar even...
Fantastical is the Mac calendar you'll actually enjoy using. Creating an event with Fantastical is quick, easy, and fun: Open Fantastical with a single click or keystroke Type in your event details... Read more
iShowU Instant 1.4.14 - Full-featured sc...
iShowU Instant gives you real-time screen recording like you've never seen before! It is the fastest, most feature-filled real-time screen capture tool from shinywhitebox yet. All of the features you... Read more
Geekbench 6.2.0 - Measure processor and...
Geekbench provides a comprehensive set of benchmarks engineered to quickly and accurately measure processor and memory performance. Designed to make benchmarks easy to run and easy to understand,... Read more
Quicken 7.2.3 - Complete personal financ...
Quicken makes managing your money easier than ever. Whether paying bills, upgrading from Windows, enjoying more reliable downloads, or getting expert product help, Quicken's new and improved features... Read more
EtreCheckPro 6.8.2 - For troubleshooting...
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
iMazing 2.17.7 - Complete iOS device man...
iMazing is the world’s favourite iOS device manager for Mac and PC. Millions of users every year leverage its powerful capabilities to make the most of their personal or business iPhone and iPad.... Read more

Latest Forum Discussions

See All

‘Junkworld’ Is Out Now As This Week’s Ne...
Epic post-apocalyptic tower-defense experience Junkworld () from Ironhide Games is out now on Apple Arcade worldwide. We’ve been covering it for a while now, and even through its soft launches before, but it has returned as an Apple Arcade... | Read more »
Motorsport legends NASCAR announce an up...
NASCAR often gets a bad reputation outside of America, but there is a certain charm to it with its close side-by-side action and its focus on pure speed, but it never managed to really massively break out internationally. Now, there's a chance... | Read more »
Skullgirls Mobile Version 6.0 Update Rel...
I’ve been covering Marie’s upcoming release from Hidden Variable in Skullgirls Mobile (Free) for a while now across the announcement, gameplay | Read more »
Amanita Design Is Hosting a 20th Anniver...
Amanita Design is celebrating its 20th anniversary (wow I’m old!) with a massive discount across its catalogue on iOS, Android, and Steam for two weeks. The announcement mentions up to 85% off on the games, and it looks like the mobile games that... | Read more »
SwitchArcade Round-Up: ‘Operation Wolf R...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for September 21st, 2023. I got back from the Tokyo Game Show at 8 PM, got to the office here at 9:30 PM, and it is presently 11:30 PM. I’ve done what I can today, and I hope you enjoy... | Read more »
Massive “Dark Rebirth” Update Launches f...
It’s been a couple of months since we last checked in on Diablo Immortal and in that time the game has been doing what it’s been doing since its release in June of last year: Bringing out new seasons with new content and features. | Read more »
‘Samba De Amigo Party-To-Go’ Apple Arcad...
SEGA recently released Samba de Amigo: Party-To-Go () on Apple Arcade and Samba de Amigo: Party Central on Nintendo Switch worldwide as the first new entries in the series in ages. | Read more »
The “Clan of the Eagle” DLC Now Availabl...
Following the last paid DLC and free updates for the game, Playdigious just released a new DLC pack for Northgard ($5.99) on mobile. Today’s new DLC is the “Clan of the Eagle" pack that is available on both iOS and Android for $2.99. | Read more »
Let fly the birds of war as a new Clan d...
Name the most Norse bird you can think of, then give it a twist because Playdigious is introducing not the Raven clan, mostly because they already exist, but the Clan of the Eagle in Northgard’s latest DLC. If you find gathering resources a... | Read more »
Out Now: ‘Ghost Detective’, ‘Thunder Ray...
Each and every day new mobile games are hitting the App Store, and so each week we put together a big old list of all the best new releases of the past seven days. Back in the day the App Store would showcase the same games for a week, and then... | Read more »

Price Scanner via MacPrices.net

Apple AirPods 2 with USB-C now in stock and o...
Amazon has Apple’s 2023 AirPods Pro with USB-C now in stock and on sale for $199.99 including free shipping. Their price is $50 off MSRP, and it’s currently the lowest price available for new AirPods... Read more
New low prices: Apple’s 15″ M2 MacBook Airs w...
Amazon has 15″ MacBook Airs with M2 CPUs and 512GB of storage in stock and on sale for $1249 shipped. That’s $250 off Apple’s MSRP, and it’s the lowest price available for these M2-powered MacBook... Read more
New low price: Clearance 16″ Apple MacBook Pr...
B&H Photo has clearance 16″ M1 Max MacBook Pros, 10-core CPU/32-core GPU/1TB SSD/Space Gray or Silver, in stock today for $2399 including free 1-2 day delivery to most US addresses. Their price... Read more
Switch to Red Pocket Mobile and get a new iPh...
Red Pocket Mobile has new Apple iPhone 15 and 15 Pro models on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide service using all the major... Read more
Apple continues to offer a $350 discount on 2...
Apple has Studio Display models available in their Certified Refurbished store for up to $350 off MSRP. Each display comes with Apple’s one-year warranty, with new glass and a case, and ships free.... Read more
Apple’s 16-inch MacBook Pros with M2 Pro CPUs...
Amazon is offering a $250 discount on new Apple 16-inch M2 Pro MacBook Pros for a limited time. Their prices are currently the lowest available for these models from any Apple retailer: – 16″ MacBook... Read more
Closeout Sale: Apple Watch Ultra with Green A...
Adorama haș the Apple Watch Ultra with a Green Alpine Loop on clearance sale for $699 including free shipping. Their price is $100 off original MSRP, and it’s the lowest price we’ve seen for an Apple... Read more
Use this promo code at Verizon to take $150 o...
Verizon is offering a $150 discount on cellular-capable Apple Watch Series 9 and Ultra 2 models for a limited time. Use code WATCH150 at checkout to take advantage of this offer. The fine print: “Up... Read more
New low price: Apple’s 10th generation iPads...
B&H Photo has the 10th generation 64GB WiFi iPad (Blue and Silver colors) in stock and on sale for $379 for a limited time. B&H’s price is $70 off Apple’s MSRP, and it’s the lowest price... Read more
14″ M1 Pro MacBook Pros still available at Ap...
Apple continues to stock Certified Refurbished standard-configuration 14″ MacBook Pros with M1 Pro CPUs for as much as $570 off original MSRP, with models available starting at $1539. Each model... Read more

Jobs Board

Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Retail Key Holder- *Apple* Blossom Mall - Ba...
Retail Key Holder- APPLE BLOSSOM MALL Brand: Bath & Body Works Location: Winchester, VA, US Location Type: On-site Job ID: 03YM1 Job Area: Store: Sales and Support Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.