• Print

Author Topic: a peek at the new Fastmath array functions  (Read 497 times)

DarthWho

  • Hero Member
  • *****
  • Posts: 3876
  • Timelord of the Sith
a peek at the new Fastmath array functions
« on: April 14, 2012, 01:22:19 PM »
well believe it or not I have been doing some work on the Fastmath Library recently; including some specialized wrappers for C++ functions here are some that are designed to work with arrays of different types:
Fastarr.h
Code: [Select]
float Test_intarreq(void * ptr1, void * ptr2, size_t num)
 {
  int k;
  k = memcmp(ptr1, ptr2, 2 * num);
  if (k == 0) {
   return -1.0f;
  }
  else {
  return 0.0f;
  }
 }
float Test_sngarreq(void * ptr1, void * ptr2, size_t num)
 {
  int k;
  k = memcmp(ptr1, ptr2, 4 * num);
  if (k == 0) {
   return -1.0f;
  }
  else {
  return 0.0f;
  }
 }
float Test_lngarreq(void * ptr1, void * ptr2, size_t num)
 {
  int k;
  k = memcmp(ptr1, ptr2, 4 * num);
  if (k == 0) {
   return -1.0f;
  }
  else {
  return 0.0f;
  }
 }
float Test_dblarreq(void * ptr1, void * ptr2, size_t num)
 {
  int k;
  k = memcmp(ptr1, ptr2, 8 * num);
  if (k == 0) {
   return -1.0f;
  }
  else {
  return 0.0f;
  }
 }
float Test_bytearreq(void * ptr1, void * ptr2, size_t num)
 {
  int k;
  k = memcmp(ptr1, ptr2, num);
  if (k == 0) {
   return -1.0f;
  }
  else {
  return 0.0f;
  }
 }

other array functions that I am working on include ones that search for a specific value in an array and setting the values in an array equal to a provided value (sort of like REDIM but you can fill it all with values other than zero)

example usage:
Code: [Select]
DECLARE CUSTOMTYPE LIBRARY "fastarr"
  FUNCTION Test_intarreq (BYVAL dest AS _OFFSET, BYVAL source AS _OFFSET, BYVAL bytes AS LONG)
END DECLARE


DIM a(40) AS INTEGER
DIM B(40) AS INTEGER
PRINT Test_intarreq(_OFFSET(a(0)), _OFFSET(B(0)), 41)
B(40) = 6
PRINT Test_intarreq(_OFFSET(a(0)), _OFFSET(B(0)), 41)
Rassilon: My lord Doctor; My lord Master; My lord DarthWho
The Doctor and the master at the same time :WHAT!?!?!

FastMath 1.1.0 released: http://dl.dropbox.com/u/12359848/fastmath.h

Clippy

  • Hero Member
  • *****
  • Posts: 16446
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: a peek at the new Fastmath array functions
« Reply #1 on: April 14, 2012, 01:42:24 PM »
Fast array? Where is it going? Why not just toss the stuff straight into memory directly?
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

LeChuck

  • Hero Member
  • *****
  • Posts: 895
  • 18 * 37
Re: a peek at the new Fastmath array functions
« Reply #2 on: January 25, 2013, 12:04:49 PM »
DarthWho,

First of all, is your fastmath lib displayed on the WIKI the latest one? http://qb64.net/wiki/index.php?title=C_Libraries

High_Sign and TrailZCount, what do they do?

Thanks
Two or more, use a FOR!

Clippy

  • Hero Member
  • *****
  • Posts: 16446
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: a peek at the new Fastmath array functions
« Reply #3 on: January 25, 2013, 12:22:46 PM »
It probably needs updated. Use the link to Drop box in his signature.
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

DarthWho

  • Hero Member
  • *****
  • Posts: 3876
  • Timelord of the Sith
Re: a peek at the new Fastmath array functions
« Reply #4 on: January 25, 2013, 01:20:13 PM »
the dropbox link contains the latest non experimental version of the library;
High_Sign is directly equivalent to the SGN function but actually runs faster than the built in version when one is dealing with just singles EX:
Code: [Select]
DECLARE LIBRARY "fastmath"
    FUNCTION Fast_Sign (BYVAL x AS SINGLE)
    FUNCTION High_Sign (BYVAL x AS SINGLE)
END DECLARE
X = -2.25
PRINT USING "SGN(##.##)=##"; X; SGN(X)
PRINT USING "Fast_Sign(##.##)=##"; X; Fast_Sign(X)
PRINT USING "High_Sign(##.##)=##"; X; High_Sign(X)
X = 0
PRINT USING "SGN(##.##)=##"; X; SGN(X)
PRINT USING "Fast_Sign(##.##)=##"; X; Fast_Sign(X)
PRINT USING "High_Sign(##.##)=##"; X; High_Sign(X)
X = 2.25
PRINT USING "SGN(##.##)=##"; X; SGN(X)
PRINT USING "Fast_Sign(##.##)=##"; X; Fast_Sign(X)
PRINT USING "High_Sign(##.##)=##"; X; High_Sign(X)
DIM I AS _UNSIGNED LONG
t = TIMER(0.001)
FOR I = 1 TO 90000000
    k = SGN(X)
NEXT I
PRINT USING "SGN time for 90,000,000 loops ##.###"; TIMER(0.001) - t
t = TIMER(0.001)
FOR I = 1 TO 90000000
    k = Fast_Sign(X)
NEXT I
PRINT USING "Fast_Sign time for 90,000,000 loops ##.###"; TIMER(0.001) - t
t = TIMER(0.001)
FOR I = 1 TO 90000000
    k = High_Sign(X)
NEXT I
PRINT USING "High_Sign time for 90,000,000 loops ##.###"; TIMER(0.001) - t

TrailZCount counts the number of consecutive trailing zero bits in the value that it is given; this may not seem to be useful at first but it is actually used in bitmap indexes (a type of database index see: http://en.wikipedia.org/wiki/Index_%28database%29) but also has other uses such as you can use it as the first step in a prime factoring algorithm EX:
Code: [Select]
DECLARE LIBRARY "fastmath"
    FUNCTION Misc_TrailZCount (BYVAL x AS _UNSIGNED LONG)
END DECLARE
K = 320
PRINT Misc_TrailZCount(K)
PRINT K
PRINT 2 ^ Misc_TrailZCount(K)
PRINT K / (2 ^ Misc_TrailZCount(K))

EDIT: to be completely clear Fast_Sign and High_Sign are faster than SGN when you are dealing with any use of the following data types in the same equation as well as feeding the functions their assigned data type: SINGLE, DOUBLE and _FLOAT.
« Last Edit: January 25, 2013, 11:19:01 PM by DarthWho »
Rassilon: My lord Doctor; My lord Master; My lord DarthWho
The Doctor and the master at the same time :WHAT!?!?!

FastMath 1.1.0 released: http://dl.dropbox.com/u/12359848/fastmath.h

Clippy

  • Hero Member
  • *****
  • Posts: 16446
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: a peek at the new Fastmath array functions
« Reply #5 on: January 25, 2013, 01:35:45 PM »
I just updated it. I had to copy it from Drop Box.
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

DarthWho

  • Hero Member
  • *****
  • Posts: 3876
  • Timelord of the Sith
Re: a peek at the new Fastmath array functions
« Reply #6 on: January 25, 2013, 10:24:28 PM »
Thanks for that Clippy.
Rassilon: My lord Doctor; My lord Master; My lord DarthWho
The Doctor and the master at the same time :WHAT!?!?!

FastMath 1.1.0 released: http://dl.dropbox.com/u/12359848/fastmath.h

Clippy

  • Hero Member
  • *****
  • Posts: 16446
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: a peek at the new Fastmath array functions
« Reply #7 on: January 26, 2013, 09:33:18 AM »
Anything for you kid...  ;)
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

LeChuck

  • Hero Member
  • *****
  • Posts: 895
  • 18 * 37
Re: a peek at the new Fastmath array functions
« Reply #8 on: January 27, 2013, 09:50:49 AM »
Thanks DarthWho for the explanation. I think it would be nice to have your fastmath lib integrated in QB64 but I don't know how difficult this would be.
Two or more, use a FOR!

DarthWho

  • Hero Member
  • *****
  • Posts: 3876
  • Timelord of the Sith
Re: a peek at the new Fastmath array functions
« Reply #9 on: January 27, 2013, 08:29:55 PM »
I am glad you like it that much but you would have to ask Galleon... though I doubt that he would be willing to integrate it (other than perhaps High_Sign...). My big library project is the Fast_CalcSV project which is designed (on the base level anyway) to be a QB64 compatible C++ library for single variable calculus.
Rassilon: My lord Doctor; My lord Master; My lord DarthWho
The Doctor and the master at the same time :WHAT!?!?!

FastMath 1.1.0 released: http://dl.dropbox.com/u/12359848/fastmath.h

  • Print