Unit Unsigned |
Classes |
Functions |
CompareUnsigned - Result < 0 if A 0 if A>B
Do not make any other assumptions about the Result.
CompToUInt - Convert a 64-bit signed integer to a 32-bit unsigned integer.
StrToUInt - Convert between a string and an unsigned long integer.
UDiv - Unsigned, 32-bit division & remainder.
UGE - if A > B then return True
UGT - if A <= B then return True
UIntToComp - You can perform 32-bit unsigned arithmetic using Delphi's
Comp type.
UIntToStr - Convert a 32-bit unsigned value to a string.
ULE - if A < B then return True
ULT - Comparison of unsigned 32-bit integers.
UMod - Divide two 32-bit unsigned numbers to produce a 32-bit
unsigned remainder.
Types |
Constants |
Variables |
Functions |
Compare two unsigned 32-bit integers. Return <0, =0, or >0. Start by comparing the high-order words. If they are not equal, then we are done and can return -1 or +1. If the high order words are equal, then we need to compare the low-order words. If they are not equal, then return -1 or +1. If the low order words are also equal, then return 0. If anyone has a faster way to do this, I am open to suggestions. Of course, using 386 instructions would certainly be faster and smaller, but also not functional on 286 machines. Just for reference, here is a 32-bit implementation: function CompareUnsigned32(A, B: TUnsigned): Integer; assembler; asm xor ax, ax db 66h mov cx, A.Word[0] ; mov ecx, A.DWord[0] db 66h mov dx, B.Word[0] ; mov edx, B.DWord[0] db 66h sub cx, dx ; sub ecx, edx ja @Greater je @End dec ax jmp @End @Greater: inc ax @End: end;
Convert a string to an unsigned long integer, just like StrToInt, but it does not recognize a leading sign, and it can produce any 32-bit, unsigned value.
Doing 32-bit division on a 16-bit machine is a nightmare. Instead, convert to Comp and use floating point arithmetic to perform the division. Since Comp is 64 bits, the full precision is preserved. When running on a 32-bit machine, then use 32-bit integer arithmetic. } { Divide two 32-bit unsigned numbers to produce a 32-bit unsigned result.
Convert an unsigned 32-bit integer to a Comp. The Comp type is a 64-bit signed integer, and can be used for 32-bit unsigned arithmetic.
Because the Delphi 2.0 compiler passes arguments in registers, and 32-bit instructions are available, it is best to code the Win32 comparison routines explicitly. For Delphi 1.0 and Windows 3.1, call the common CompareUnsigned routine, for simplicity.
Types |
TUnsigned = LongIntUnsigned 32-bit integers. Delphi Pascal does not support unsigned 32-bit integers, but only signed 32-bit integers. The difference matters when comparing values, doing division, converting to and from a string, or checking for overflow errors. For the latter, the simplest solution is to turn of overflow checking with the $Q- compiler directive. This unit supplies the other functions. The names are a bit terse, I admit, but UnsignedLessOrEqual is too long, and I could not think of anything in between. Some of code is a little hard to read because there are three distinct sets of source code intertwingled. 1. 16-bit Win 3.1 code. 2. 32-bit Win 3.1 code. Some routines are written in assembly language and take advantage of 32-bit instructions when running on a 386 class CPU or better. 3. 32-bit Win 32 code. The Delphi 2.0 compiler passes arguments in registers, which allows for much faster and simpler code. Ray Lischner Tempest Software 5 January 1996 } { To help the human reader, declare 32-bit unsigned integer values with the TUnsigned type. It is just a LongInt, but it notifies the reader of the programmer's intent.
Constants |
Variables |