Mandlebrot set

Introduction

The Mandlebrot set is a fractal shape that is computationally expensive to draw, and as such forms a useful performance test for basic floating-point arithmetic.

As the Memotech MTX is a 4MHz Z80 based computer dating back to 1983, its fair to say that any program written for it that draws a Mandlebrot set is going to be one of the slowest around.

Memotech MTX computers with REMEMOrizer r3 or later have access to numeric accelerator hardware.

Questions

The MTX BASIC ROM supports floating-point numbers and includes calculator routines to operate on them. How much of a speed-up can be achieved by patching these routines to exploit floating-point hardware?

MTX BASIC is interpreted and is less efficient that the equivelent assembly language code. How much of a speed-up can be achieved by rewriting MTX BASIC code in assembler?

The MTX BASIC ROM calculator routines implement a number stack in software. A hardware floating-point implementation can implement the stack in hardware. The result of one calculation can be left on the stack, ready to be used as an operand to the next, thus reducing copying of floating-point numbers in memory. How much of a speed-up can be achieved by exploiting a hardware floating-point stack? To answer this question we must bypass the existing MTX BASIC ROM calculator routines (not simply patch them), so we necessarily also include any benefit of doing this also.

Investigation

Three test programs were written :-

MANDBAS and MANDASM can be run with or without the numeric accelerator, but MANDNUM requires it.

Results

  MANDBAS MANDASM MANDNUM
No numeric accelerator07:12:28 (=25948s)01:49:26 (=6566s) N/A
Numeric accelerator 05:55:38 (=21338s)00:35:14 (=2114s)00:09:11 (=551s)

The effect of switching the MTX BASIC ROM calculator rouines from software floating-point to use hardware floating-point can be as little as 1.2x for a BASIC program, or as much as 3.1x for an assembler program.

The effect of switching from a BASIC program to an assembler program can be as little as 3.9x when the MTX BASIC ROM calculator routines are using software floating-point, to as much as 10x when they are using hardware floating-point.

The effect of bypassing the MTX BASIC ROM calculator routines altogether and writing code that directly uses the floating-point hardware results in a further 3.8x speed-up.

                              MANDBAS        MANDASM        MANDNUM

No numeric accelerator               --3.9x->
                                 |              |
                                1.2x           3.1x
                                 |              |
                                 v              v
Numeric accelerator                  --10x-->       --3.8x->

In total, with focus on both software and hardware, a speed-up of around 47x was acheived. A little more is probably possible.

Its not surprising that the effect of switching to hardware floating-point is more pronounced when the program is written in assembler, or that the effect of switching to assembler is more pronounced when the MTX BASIC ROM calculator routines are using hardware floating point, because any saving they introduce is a larger proportion of a smaller whole.

What is surprising is that the MTX BASIC overhead is 3x larger than the overhead of the MTX BASIC ROM calculator routines having to do software floating-point.

The numeric accelerator does most operations in a single Z80 cycle, ie: 1T, so you might expect the acceleration to be massively higher. The Z80 overhead of marshalling operands, instructing the operations, checking the result and unmarshalling the results is the problem.

Download

MAND can be downloaded from http://www.nyangau.org/mand/mand.zip.

Copying of this program is encouraged, as it is fully public domain. The source code is included in the package. It was created on the authors time and equipment. Caveat Emptor.

The author of MAND and this documentation is Andy Key (email andy.z.key@googlemail.com).

{{{ Andy