home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 3
/
PDCD_3.iso
/
utilities
/
utilss
/
signature
/
!Signature
/
NumModTxt
< prev
next >
Wrap
Text File
|
1992-08-01
|
18KB
|
537 lines
NUMBERS MODULE version 0.08
===========================
(C) Copyright Nick Craig-Wood 1992
The module "Numbers" and this documentation "NumModTxt" are public domain.
You may distribute them freely provided that
1) both files are always kept together
2) the files are not altered in any way
3) no profit is made
If you wish to break any of these conditions get in touch with me at the
address below first.
The software and documentation was first published in BBC Acorn User
Magazine, September 1992, and for more information see that issue.
These routines started life in 1989 written in C on the university mainframe
in response to a "Numbers Count" puzzle in PCW. The problem (or part of
it) was to calculate the biggest prime you could find of the form N!+1.
However the routines did not get finished in time for the puzzle deadline.
In the quest for speed I converted the routines to 68000 code for my Atari
ST, and then a year later into ARM code. With an ARM3 the routines run at
the same speed as they did on the mainframe!
Numbers are held in application memory area in a heap, and maintained by
OS_Heap. Before the module can be used, it must be told about this
heap with Num_InitHeap. Numbers come in two pieces, the head and the tail.
All numbers have a head, but need not have a tail. The head of the number
is a short block in the heap, which points to the tail and keeps the length
and sign of the number, and various other things. Numbers are passed by
reference only, as pointers to the head of the number. These are refered to
as NUMs. For example NUM r0 indicates that r0 is a pointer to the head of a
number.
Since NUMs are passed by reference only, if you pass them into procedures
and then alter the values of the input parameters, you are actually
altering the NUM. So to avoid problems, if you write a procedure which
takes NUMs as parameters for both input and output, it is best to define
some temporary NUMs using Num_Init for the output, and then at the end
Num_Swap the results into the correct place, and Num_Remove the temporary
variables. All the internal routines of the module work like this, so there
is never any problem with passing the same NUM as input and output.
Some of the routines take either NUMs or scalars as arguments. Scalars are
normal signed or unsigned 32 bit numbers. All routines expect NUMs to be
tidy (except Num_Tidy). (A num is tidy if its most significant digit is
non-zero, and if it is zero, then it is positive, and of length 1. Num_Tidy
accomplishes this. It is unlikely that a user will need this function.)
Any error from the numbers module will be prefixed with "Numbers: ". If you
pass an invalid NUM to any of the routines, the chances are that OS_Heap
will reply with an error, but not one that makes sense always. The most
confusing is "Numbers: Heap Full" so beware! See the end for a short example
program.
If you find any bugs, have any comments or queries or wish to use the module
in commercial software please write to
Nick Craig-Wood
4 Victoria Street
Wolverton
Bucks
MK12 5HL.
-----------------------------------------------------------------------------
NUMBERS SWI
===========
On Entry: r0-r3 are parameters
On Exit: r0-r3 are returned as results or corrupted
r4-r14 are preserved
Interrupts: Interrupts are enabled
Fast interrupts are enabled
Processor mode: Processor is in SVC mode
Re-entrancy: SWIs are not re-entrant
Use: See QUICK REFERENCE, and DEFINITIONS for details
All SWIs that are capable of looping respond to ESCAPE
See ERRORS section for details of errors returned
-----------------------------------------------------------------------------
QUICK REFERENCE
===============
NUMBERS HEAP: area in application workspace where all the number are kept
HEAD: 16 byte parameter block in the numbers heap pointing to
TAIL: variable length block containing the value of the number
NUM: meaning a 32 bit integer pointing to a HEAD
INT: meaning a 32 bit integer
SCALAR: meaning a 32 bit integer
FLAG: is either 0 for false, or 1 for true
STRING: a pointer to a 0 terminated ASCII string
a,b,c,d represent pointers to NUMs
i,j,k represent SCALARS
p,q represent a pointer to memory
f represents a FLAG
h represents a HeapPointer as returned by Num_HeapInit in r0
SWI Parameters Results Comments
=== ========== ======= ========
Num_Author - - Prints out some info
Num_InitHeap p,i h,a,b,c h<-HeapPointer, a<-0, b<-1, c<-2
Num_MakeSmallPrimes i j Makes primes up to i, # found to j
(All the SWIs below need a valid NUM or HeapPointer in r0)
Num_Allocate a,i - Allocates i words to TAIL of a
Num_Deallocate a - Removes the TAIL of a
Num_Set a,i - Sets a to signed i
Num_USet a,i - Sets a to unsigned i
Num_Init h a Makes a HEAD and TAIL and pointer a
Num_Remove a - Removes the HEAD and TAIL of a
Num_Equals a,i f Returns truth of a = i
Num_Swap a,b - Swaps a and b. This is quick.
Num_Move a,b - Sets b to a. Not so quick.
Num_Clear a - Sets TAIL of a to all zeroes
Num_Tidy a - Reduces a to shortest length
Num_UCmp a,b i Unsigned compare of a-b to i
Num_Cmp a,b i Returns the sign of a-b to i
Num_ScalarCmp a,i j Returns the sign of a-i to j
Num_ScalarAdd a,i,c - c <- a + i
Num_ScalarSub a,i,c - c <- a - i
Num_ScalarMul a,i,c - c <- a * i
Num_ScalarDiv a,i,c j c <- a / i, j <- a MOD i
Num_ScalarMod a,i j j <- a MOD i
Num_SmallFactorN a,i k k=smallest factor of a or 0, try i
Num_SmallFactor a k k=smallest factor of a or 0, try all
Num_Add a,b,c - c <- a + b
Num_Sub a,b,c - c <- a - b
Num_Mul a,b,c - c <- a * b
Num_Div a,b,c,d - c <- a / b, d <- a MOD b
Num_Mod a,b,c - c <- a MOD b
Num_Dump a - Prints a in hex, and other info
Num_ToString a p,q Makes STRING of a to p, end=q
After use do SYS "Num_Deallocate",b
Num_Print a - Prints a in decimal
Num_FromString a,p f Converts STRING at p to a, f=ok
Num_Input a f Gets a decimal input to a, f=ok
Num_Info h - Prints memory usage info
Num_RndScalar h i Makes a random number 0-&FFFFFFFF
Num_SetSeed h,i - Sets seed of rnd generator
Num_Rnd a,b - Makes random number to a, 0 <= a < b
Num_Gcd a,b,c - c <- greatest common divisor a,b
Num_Sqr a,b - b <- square root of a
Num_Pow a,b,c - c <- a ^ b (a to the power b)
Num_PowMod a,d,c,b - d <- (a^b) MOD c
Num_Factorial a,b - b <- a! = a*(a-1)*(a-1)*..*3*2*1
Num_Inv a,b,c,d - Finds c,d: a*c MOD b=d & d=GCD(a,b)
Num_FermatTest a,i j Computes truth of i^(a-1) MOD a = 1
Num_ProbablyPrime a j j <- 0 if not prime, 1 probablyprime
-----------------------------------------------------------------------------
DEFINITIONS
===========
Num_InitHeap
------------
This expects r0 as a pointer to a block of memory to be used as a heap, and
r1 as the length of this memory in bytes. This returns a pointer to the
heap in r0 and some nums which are useful consants r1 <- zero, r2 <- one, r3
<- two. The HeapPointer returned in r0 is required by some of the other
routi