Small Integer Arithmetics in FBSL v3.5

(Include Files with PP Macros, Classes and User Controls)

Small Integer Arithmetics in FBSL v3.5

Unread postby Mike Lobanovsky » Wed Feb 11, 2015 12:18 pm

Hello community,


In most use cases, FBSL BASIC's 32- and 64-bit signed integer data types (Integer a.k.a. Long/Boolean and Quad, respectively) are sufficient for common arith operations.

More elaborate cases when FBSL is supposed to interact with the OS, or 3rd-party libraries, or its own DynC and DynAsm layers may also require integer entities that are shorter than 32 bits in size. Here the user can utilize BASIC UDTs whose integer members (called bitfields in this particular case) would emulate efficiently the respective integer data types that FBSL's BASIC doesn't support natively. For example,


    Default %w * 16
End Type

would emulate a 16-bit integer variable, and the keyword Default would enable the user to access the integer value of the variable declared As WORD directly, i.e. omitting the usual .w notation that would've been obligatory otherwise.

Yet such UDT emulation has a restriction that can be significant in certain cases. The problem is that pure arithmetic operations on UDT bitfileds are all unsigned because it is only the 32nd bit (or the 64th one in case of 64-bit Quads), i.e. the high-order bit, that FBSL's BASIC recognizes as the sign bit of an integer quantity. All lower-order bits would denote ordinary significant bits of the quantity's numeric value.

This effectively means that the user is required to code a custom adjustment procedure to allow a shorter signed integer to "rotate" its values within its entire permissible range of bits through the range of negative values similar to how it is done automatically for full 32- and 64-bit integers that FBSL supports natively as of this writing. For example, the following ToBasicShort() procedure will be the fastest FBSL BASIC code to emulate the conversion of a larger integer into a corresponding 16-bit signed integer, conventionally called short, respecting the 16th bit as the number's sign bit similar to how other languages that support short integers natively would expect such conversion to behave:

Function ToBasicShort(arg As Integer) As Integer

    If arg <= 32767 Then Return arg
    Return -(65536 - arg)
End Function


Casts and direct arithmetic operations on FBSL BASIC's native data types are performed approx. 5 times faster than the fastest and best optimized intrinsic conversion and arithmetic functions. The above function is not intrinsic nor can its code be made any shorter or optimized any further. It will run approx. 10 times slower than a native cast to the short data type could be executed had shorts been supported natively by FBSL's BASIC. Neither does this function support negative arguments, which would otherwise require yet more BASIC code for some more checks and adjustments to obtain perfect conversion results at all times.

FBSL's BASIC will not support signed or unsigned integers smaller than 32 bits in size natively to allow for faster direct arithmetics and casts. Adding them as intrinsic data types would bloat FBSL's existing engine and slow it down to some extent too, for dubious benefits of being able to perform faster arith on data types that you don't encounter in your everyday practice. On the other hand, adding them to FBSL BASIC's intrinsic function list would bloat the vocabulary and make the core binaries fatter.


So what do we do about it?

The way out is FBSL's DynC. Function call overhead for intrinsic functions, BASIC subs and functions, DynC and DynAsm blocks, and external DLL calls including FunCall() and ApiCall() variations is exactly the same because calls to their entry points are made by a common intrinsic engine. Implementing small integer arithmetics and casts as DynC blocks will make them only 5 times slower than direct arithmetics could've been, i.e. exactly as fast as if they were implemented as intrinsic functions. C language integer casts and discrete arithmetics are as fast in DynC as they are in highly optimized GCC v4.3.3 that FBSL is written in.

Attached please find a short include file called that is a cast/conversion library for your projects requiring precise conversion of, and math operations on, 8- and 16-bit integer quantities. It supports unsigned 16- and 8-bit integers conventionally defined in the C language as words and bytes, respectively, and signed 16- and 8-bit integers conventionally called shorts and chars. The functions accept any FBSL BASIC numeric types (Single and Double args are automatically converted to Integers by usual truncation), and their integer return values are guaranteed to be valid within the corresponding type's (un)signed bit range. Maths includes addition, subtraction, multiplication, division ("divide by zero" exception intercepted), and left and right shifts.

Enjoy! :)
8- and 16-bit arith include file
(779 Bytes) Downloaded 68 times
"Я старый солдат, мадам, и не знаю слов любви."
"I am an old soldier, ma'am, and I don't know the words of love."
"Je suis un vieux soldat, madame, et je ne connais pas les mots d'amour."
"Ich bin ein alter Soldat, gnädige Frau, und ich weiß nicht die Worte der Liebe."

(3.2GHz i5 Core Quad, 8GB RAM / 2 x nVidia GTX 550Ti SLI-bridged, 2GB VRAM)
(x86 Win XP Pro Russian Sp3/x86 Win Vista Ultimate Sp2/x64 Win 7 Ultimate Sp1/Wine in x64 elementaryOS Luna)
User avatar
Mike Lobanovsky
FBSL Administrator
FBSL Administrator
Posts: 1824
Joined: Tue Apr 19, 2005 8:22 am
Location: Republic of Belarus

Re: Small Integer Arithmetics in FBSL v3.5

Unread postby Gerome » Thu Feb 12, 2015 12:09 am

Hi Mike,

Thanks for this excellent include file about Small Integer Arithmetics!
Hope this include file will be part of the incoming FBSL distro! :)

(¯`·._.·[Gerome GUILLEMIN]·._.·´¯)
:: Full SETUP w. HELP 05th of December 2011 :: [full v3.4.10 installation pack] [minimal upgrade to v3.4.10]
Laissons les jolies femmes aux hommes sans imagination. / Let us leave pretty women to men without imagination.(M.Proust)
The success is a defeat for the one who does not want to dance any more! (H.F. Thiefaine)
User avatar
FBSL Administrator
FBSL Administrator
Posts: 3149
Joined: Sat Mar 12, 2005 9:06 pm
Location: Paris -- France

Re: Small Integer Arithmetics in FBSL v3.5

Unread postby Codeman » Wed Feb 18, 2015 2:01 pm

i agree for adding it to fbsl distro !!
_-=<(¯`·._.·[ CodeMan ]·._.·´¯)>=-_
Your only limit : Your imagination


Chris 1 (Corei5-4460S 2.9 GHZ w/ 16GB RAM, GeForce GTX 745 w/ 4GB VRAM, Windows 8 x64 Fr)
Chris 2 (2.0GHz AMD Atlon 64 X2 3800+ w/ 4GB RAM, GeForce 9600GT w/ 1GB VRAM, Windows 7 x64 Fr)
Chris 3 (Tablet pc : ASUS Transformer Book T100)
User avatar
FBSL Administrator
FBSL Administrator
Posts: 497
Joined: Sat Nov 24, 2007 4:33 pm
Location: France

Return to FBSL v3 Reusable Code Modules

Who is online

Users browsing this forum: No registered users and 1 guest