•  Back 
  •  Main 
  •  Index 
  •  Tree View 
  •  Cross references 
  •  Help 
  •  Show info about hypertext 
  •  View a new file 
Topic       : The GFA-Basic Compendium
Author      : GFA Systemtechnik GmbH
Version     : GFABasic.HYP v2.98 (12/31/2023)
Subject     : Documentation/Programming
Nodes       : 899
Index Size  : 28056
HCP-Version : 3
Compiled on : Atari
@charset    : atarist
@lang       : 
@default    : Document not found
@help       : Help
@options    : +g -i -s +z
@width      : 75
@hostname   : STRNGSRV
@hostname   : CAB     
@hostname   : HIGHWIRE
@hostname   : THING   
View Ref-FileDivisions of integer variables can produce decimal points, for instance x%=5,
y%=2, x%/y%=2.5. In contrast to multiplication, the compiler would therefore
have to perform division generally as floating point division. This is indeed
what it does, as long as the programmer does not expressly tell it to perform
the division of two integer variables as an integer division with an integer
result.

You can do this by using the compiler option $%3, (the complement $%0 option),
which causes every division to be performed as a floating point division, is
the default. You can see the difference by analysing the code produced by the
line

    x=y%/z%

Using the default $%0 option, this produces:

    move.l  -$7ff8(a5),d0
    bsr     FITOF
    move.l  d0,-(a7)
    move.w  d2,-(a7)
    move.w  d1,-(a7)
    move.l  -$7ff4(a5),d0
    bsr     FITOF
    move.w  (a7)+,d4
    move.w  (a7)+,d5
    move.l  (a7)+,d3
    bsr     FXDIV
    lea.l   -$8000(a5),a0
    move.l  d0,(a0)+
    move.w  d1,(a0)+
    move.w  d2,(a0)+

You can see that the two variables to be divided are transformed into floating
point variables using the FITOF (integer to float) routine. The routine for the
division of two floating point values is called (FXDIV) and the result assigned
to the variable x from -$8000(a5).

If the same line is compiled using the $%3 option, the following code results:

    move.l  -$7ffc(a5),d0
    move.l  -$7ff8(a5),d1
    bsr     LDIV
    lea.l   -$8000(a5),a0
    bsr     ISTOF

The ISTOF routine performs the assignment to x. Here the LDIV routine for the
division of two long-integer variables is called. If the two values are such
that decimal places are the result of the division, LDIV will not, of course,
return these.

If the line were to read x%=y%/z% instead of, as is the case here, x=y%/z%,
this would be of no consequence as the result would be returned to an integer
variable. In such a case, the GFA BASIC compiler automatically uses LDIV for
the division, even with $%0 enabled.

If two two-byte variables are to be divided, the Motorola 68000 processor's div
instruction can be used, which is of course faster than the LDIV routine. The
code, in this case, is particularly simple and fast.

The line

    x&=y&/z&

produces:

    move.w  -$7ffe(a5),d0
    ext.l   d0
    divs    -$7ffc(a5),d0
    move.w  d0,-$8000(a5)

The conclusion: If at all possible, use two-byte variables for a division and
enable the $%3 option if you are sure that no integer variables which would
produce significant decimal places are being processed.

When dividing by constants, the optimization carried out are similar to those
with multiplications. For example, the division by a value to the power of two
is converted into bit-shift instructions.