Page 1 of 1

Smart-Basic Array Order

Posted: Sun Dec 06, 2020 1:19 am
by MarkP
I have no idea whether the Apple CPU chips in the iphone and ipad devices benefit from cache memory, but just in case they do, I wanted to set the order of array indexes properly to take advantage of memory caching, especially with using large 3-dimensional arrays.

To do this, I needed to determine whether arrays in smart BASIC are ordered in "column-major-order" (first index causes memory-contiguous elements) or "row-major-order" (last index causes memory-contiguous elements).

Since SB shields the programmer from memory layout information, I figured I'd use "debug" to determine the layout of 3-dimensional arrays.

To my surprise, based on the order that debug indicates, SB is neither column-major nor row-major.

Try the following code:

option base 1
n=0
dim a(4,4,4)
for x=1 to 4
for y=1 to 4
for z=1 to 4
n+=1
a(y,z,x)=n 'NOTE THIS ORDER
next z
next y
next x
debug pause

Can anyone assist with this?
I expected either (x,y,z) or (z,y,x), but NOT (y,z,x).

Re: Smart-Basic Array Order

Posted: Sun Dec 06, 2020 4:25 am
by Mr. Kibernetik
Internally smart BASIC uses column-major ordering.
But you cannot detect this by your BASIC program because BASIC language does not use pointers. Also don't forget that smart BASIC uses complex numbers, not real numbers.

Re: Smart-Basic Array Order

Posted: Sun Dec 06, 2020 10:46 pm
by MarkP
Yes, I am aware that I cannot detect the array ordering by myself. (I tried using debug) However, it is still good to know so that I can properly order the array indexes so that sequential reading of the array is via the major index to allow the processor to cache data accesses, if possible.