home *** CD-ROM | disk | FTP | other *** search
- {$B-,D-,F-,I+,N-,R-,S+,V+}
-
- (*
- Timo Salmi UNiT B
- A Turbo Pascal unit for (a) bit (of) manipulation
- All rights reserved 22-Jul-89,
- Updated 26-Jul-89, 19-Aug-89, 18-Oct-89, 17-Jul-90, 4-Jan-91, 27-Oct-91
-
- Here are some power functions, bit manipulation and base conversions.
- There is nothing really novel about them, just that I have needed them
- myself, and thought to make some available to [a/be]muse the general public.
- Some of the functions are, however, hopefully both smaller faster than the
- corresponding routines in average Turbo Pascal guides, or have features
- that are normally lacking.
-
- This unit may be used and distributed freely for PRIVATE, NON-COMMERCIAL,
- NON-INSTITUTIONAL purposes, provided it is not changed in any way. For
- ANY other usage, such as use in a business enterprise or at a university,
- contact the author for registration.
-
- The units are under development. Comments and contacts are solicited. If
- you have any questions, please do not hesitate to use electronic mail for
- communication.
- InterNet address: ts@chyde.uwasa.fi (preferred)
- Funet address: GADO::SALMI
- Bitnet address: SALMI@FINFUN
-
- The author shall not be liable to the user for any direct, indirect or
- consequential loss arising from the use of, or inability to use, any unit,
- program or file howsoever caused. No warranty is given that the units and
- programs will work under all circumstances.
-
- Timo Salmi
- Professor of Accounting and Business Finance
- School of Business Studies, University of Vaasa
- P.O. BOX 297, SF-65101 Vaasa, Finland
-
- The following changes were made 4-Jan-91:
- POWERGFN function has been rewritten
- DECBINFN function has been omitted
- DECHEXFN function has been omitted
-
- The following routines were added 27-Oct-91:
- BTEWRDFN
- WRDLNGFN
- HIWORDFN
- LOWORDFN
- *)
-
- unit TSUNTB;
-
- (* ======================================================================= *)
- interface
- (* ======================================================================= *)
-
- uses Dos;
-
- (* =======================================================================
- Timer routines
- ======================================================================= *)
-
- (*
- If one wants to test and compare speeds of various procedures and functions
- one needs a procedure to give the elapsed time. TIMERFN does that. It is
- very simple. It just gives the seconds elapsed since midnight to the apparent
- precision of a hundredth of a second.
- *)
-
- (* Time (seconds) elapsed since midnight *)
- function TIMERFN : real;
-
- (* The timer is actually updated 18.2 times per seconds by the 8284
- oscillator at 1193180 Hetz frequency. These updates are called ticks.
- Ticks are calculated from midnight, and can be used as an alternative
- timing. *)
- function TICKSFN : longint;
-
- (*
- Pascal lacks a power function and therefore one has to build it oneself.
- The simplest way to calculate a power function is Exp(exponent*Ln(number)),
- which many guides suggest. It is unsatisfactory, however, from the point
- of view that powers of zero or negative numbers cause an error. The
- POWERGFN does not have this hitch, but is, of course, slightly slower.
- Invalid operations such as -0.55 to -3.5 are detected and running halted.
- In special cases a power function can be made very fast. TWOTOFN is such a
- function raising two to a power.
- *)
-
- (* =======================================================================
- Mathematical routines for raising to powers
- ======================================================================= *)
-
- (* Raise a positive number to a power the traditional way *)
- function POWERFN (number, exponent : real) : real;
-
- (* Raise any number to a power, the generalized power function.
- Invalid expressions and overflows are trapped properly *)
- function POWERGFN (number, exponent : real) : real;
-
- (* Raise a longint to a power, fast; Do not use negative exponents *)
- function POWERLFN (number, exponent : longint) : longint;
-
- (* Raise two to a power, that is 2^exponent, very fast *)
- function TWOTOFN (exponent : word) : word;
-
- (* Raise sixteen to a power, that is 16^exponent, very fast *)
- function R16TOFN (exponent : word) : word;
-
- (* =======================================================================
- Mathematical number base conversion routines
- ======================================================================= *)
-
- (*
- Base conversion is a quite commonly occurring task in programming. It
- involves a similar problem to the one explained in discussing the
- routines for raising a number to a power: The routines can be made
- general, or they can be made fast. Here are some of the routines.
- *)
-
- (* Convert a number from any base to any base (2-36),
- The result may not execeed the range of a longint *)
- function CONVBFN (number : string; frombase, tobase : byte) : string;
-
- (* Convert a binary string fast to a decimal word *)
- function BINDECFN (binary : string) : word;
-
- (* Convert a decimal word to a binary string fast *)
- function BINFN (decimal : word) : string;
-
- (* Convert a decimal longint to a binary string fast *)
- function LBINFN (decimal : longint) : string;
-
- (* Convert a hexadecimal string fast to a decimal word *)
- function HEXDECFN (hexadecimal : string) : word;
-
- (* Convert a decimal word to a hexadecimal string very fast *)
- function HEXFN (decimal : word) : string;
-
- (* Convert a decimal longint to a hexadecimal string fast *)
- function LHEXFN (decimal : longint) : string;
-
- (* =======================================================================
- Bit, byte, word, and longint datatype manipulation routines
- ======================================================================= *)
-
- (*
- The next function is specialized, but occasionally very useful. As is
- known a word is made up of 16 bits numbered from 0 to 15. The following
- function establishes whether a particular bit is on or off in a word
- *)
- (* Is an individual bit on in a word, bits are numbered from 0-15 as usual *)
- function BITONFN (status : word; bit : byte) : boolean;
-
- (* Combines two bytes into a single word.
- This is the inverse of the inbuilt Hi and Lo byte functions *)
- function BTEWRDFN (high, low : byte) : word;
-
- (* Combines two words into a longint *)
- function WRDLNGFN (high, low : word) : longint;
-
- (* Returns the high-order word of the longint argument
- Similar to Hi, but returns a word instead of a byte *)
- function HIWORDFN (x : longint) : word;
-
- (* Returns the low-order word of the longint argument
- Similar to Lo, but returns a word instead of a byte *)
- function LOWORDFN (x : longint) : word;
-