re-discovering the QL... mandelbrot 1989-2023

Anything QL Software or Programming Related.
User avatar
pjw
QL Wafer Drive
Posts: 1316
Joined: Fri Jul 11, 2014 8:44 am
Location: Norway
Contact:

Re: re-discovering the QL... mandelbrot 1989-2023

Post by pjw »

I havent touched my "QL" since last I wrote and have only landed on this site by accident once or twice in the past 3 or 4 months, so it was nice to see when I did take a peek that theres actually been some recent neural activity! For example a guy who hasnt touched his QL since the eighties, but has realised that screen sizes are not limited to 512x256 in four colours and whose product just worked on my system right out of the box..

I may have some catching up to do, as my RSS feed only downloaded the past 2 or 3 weeks' worth of posts. I doubt Ill have the will to go through everything..

Anyway, this thread spurred me on to have a go at the 32bit multiply. Of course, QPC2 can already do that since it emulates a 020 CPU which has it built in. Im sure a lot of others have had a go, so I decided to try an algorithm unlikely to have been tried here before. I really havent spent a lot of time on it, so it may be unreliable and rubbish. I really just wrote it down in the shortest possible time, then I wrote a harness for it (not included here unless anyone asks) and ran a few tests. Then I did a 32x32=>64 bit version, which is only 7 instructions longer, and also seems to work! Amazing! No refinements and only unsigned multiply.

Code: Select all

mulu32ss
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Multiply two 32 bit unsigned integers using the sheep shit method
*
* This method of multiplication is (or at least was) used by sheep and camel
* herders in the desserts of Arabia. The only skill you need to work this is
* to be able to count!
*
* You dig two parallel columns of small holes in the ground. (The number of
* holes required will depend on the size of of your calculation. It is not
* fixed; you can always add more if you need.)
* In the topmost hole of the left column you put a number of beads (or
* stones or sheep or camel droppings, depending on what's available)
* corresponding to the number of, say, sheep you wish to sell; in the
* topmost right hole the number of beads corresponding to the price per head.
* Then, operating on the second row of the right column, for every two beads
* in the hole above, you put one bead in the hole, rounded down to the nearest
* whole number.
* Continue until there is only one bead left.
* In the left column you double the number of beads for each hole down the
* column. Each row on the left corresponds to the row on the right, so stop
* when you get to the row where the right column contains only one bead.
* Now, starting from the topmost row, take all the the beads from the left
* column, where the corresponding right column contains an ODD number of beads,
* and put them in the accumulator.
* The contents of the accumulator now, magically, contains the number of beads
* that correspond to the total amount of rials, shekels or whatever, for the
* sale; ie multiplication.
*
* The code below emulates that algorithm.
*
*
*       Entry                Return
* d2.l = multiplier       d2.l smashed
* d1.l multiplicand       d1.l smashed
* d0.l                    d0.l accumulator (= result)
*
* cc set on overflow
*
        clr.l d0        clear accumulator

        tst.l d2        0 x N = 0
        beq.s done

        tst.l d1        N x 0 = 0
        beq.s done

        cmp.l d1,d2     smallest number is multiplier
        bcs.s begin

        exg d1,d2
        bra.s begin

*
loop
        lsl.l #1,d1     double multiplicand
        bvs.s done      oops. Overflow!

        lsr.l #1,d2     halve multiplier
        beq.s done       no more to do

begin
        btst #0,d2      if even..
        beq.s loop       ..skip, else..

        add.l d1,d0      ..accumulate
        bvc.s loop      continue unless overflow

*
done
        rts

*
        end
It seems slightly faster on QPC2 than a previous version I wrote using a more "standard" algorithm. Notice theres not a mulu in sight!


Per
dont be happy. worry
- ?
User avatar
NormanDunbar
Forum Moderator
Posts: 2281
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: re-discovering the QL... mandelbrot 1989-2023

Post by NormanDunbar »

Spooky!!!


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
User avatar
pjw
QL Wafer Drive
Posts: 1316
Joined: Fri Jul 11, 2014 8:44 am
Location: Norway
Contact:

Re: re-discovering the QL... mandelbrot 1989-2023

Post by pjw »

NormanDunbar wrote: Sat Apr 01, 2023 5:53 pmSpooky!!!
What, the neural activity or the algorithm? ;)

Regarding the latter, I picked it up from some book in my childhood. Unfortunately I had no luck in finding out any more about it now on the Interweb. I dont even know what its called.

Of course the routine depends on the inputs, but in many cases it beats the number of iterations a mechanical calculator algorithm would require. Eg below:

Code: Select all

  63  x  49
 126     24	-
 252     12	-
 504      6	-
1008      3
2016      1
============
63 + 1008 + 2016 = 3087
----------------   ====
this would take 13 iterations on an old fashioned mechanical calculator. Here it only takes 5; 3 full-fat runs through the loop (shift and add) and 2 lite (shift only). (Pardon my limited vocabulary.) On average for a 32 bit number it has to do 16 iterations, most of them simple shifts, so its not like we're just adding 63 49 times or anything like that! It would probably do quite well on a RISC CPU..


Per
dont be happy. worry
- ?
User avatar
NormanDunbar
Forum Moderator
Posts: 2281
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: re-discovering the QL... mandelbrot 1989-2023

Post by NormanDunbar »

Hi Per,

when I said "Spooky!" I was referring to the algorithm used. I need to have a read through it and try to understand it better.

EDIT: I found this: https://en.wikipedia.org/wiki/Multiplic ... iplication on Wikipedia.


Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
User avatar
mrzap000
ROM Dongle
Posts: 11
Joined: Fri May 08, 2015 9:33 am
Location: Milan, Italy

Re: re-discovering the QL... mandelbrot 1989-2023

Post by mrzap000 »

NormanDunbar wrote: Fri Mar 31, 2023 7:43 pm If you need/want a 32 bit by 32 bit multiply routine, then Issue 11 of the extremely random eMagazine on Assembly Programming has on which I wrote and is free for all to use. You can get the PDF and code files at https://github.com/NormanDunbar/QLAssem ... g/Issue_11 if you wish.
Update: I'm 99% sure my 32x32->64 multiplication is flawed. Will try to use the example you published in the magazine, thanks for the hint!


neozeed
ROM Dongle
Posts: 15
Joined: Thu Jan 06, 2022 7:54 am

Re: re-discovering the QL... mandelbrot 1989-2023

Post by neozeed »

anyone ever look at the doom fixed point code? I'd imagine there should be a 68k version of fixed mul somewhere..?


Post Reply