GNAT's mapping function for arrays

Hi,
Where can I find details on how GNAT maps array indexes to the element’s memory location (the mapping function) ?
I know it’s left to implementation by the manual, but I there’s no mention on the adacore website, nor in the gnat’s guide.

The question comes from the book

Obtain a general storage mapping function for an array of D dimensions stored in row-major form.
Obtain a general storage mapping function for an array of D dimensions stored in column-major form.
To understand the value of the Ada 95 interface to Fortran, suppose that it did not exist, and that a specific Ada compiler stores multi-dimensional arrays in row- major form. We know that Fortran stores them in column-major form.Suppose an Ada program creates a three-dimensional array, then needs to pass it to a subrou tine written in Fortran. Assume that in both languages, subroutine linkage arrangements just pass the address of the array, thus the same physical copy of the array is used by both programs. A reference to, say M(1,5,4) in the Ada program refers to a different physical location from that referred to by the same reference in the Fortran program. What has to be done to make the two languages communicate better? Write whatever programs you need.

I understood the formula, but I don’t understand what the last qustion is supposed to mean. I guess that I can’t force fortran to change its mapping function, so instead transposing matrices of any dimensions before passing them on to the fortran program should allow it to continue to use it transparently,
I don’t know what the book could be asking otherwise.
If it comes to details of memory mapping I don’t know, maybe you can point to some doc on gnat.

Ada (the language) supports communication with Fortran as specified in Annex B.5 Interfacing with Fortran.

GNAT (the implementation) supports Annex B.5.

My experience only involved 2-D matrices, so simple transpositions did the trick.

1 Like

It is what happens under the hood that interests and must investigate, as the text explains.
I want to know because it was asked of me, but also because in the future I will absolutely do low level, system or embedded programmer. Beside, there are other possible array storage mappings, like tree-structured mapping.
Matters of representing software onto the hardware fascinate me, since I want to contribute to OSes later on, when I’m through with all these books.

Start with a 2D example. When it is row-wise, imagine row 2 appended after row 1, and so on.
When it is column-wise, column 2 is appended after column 1, and so on. In each case you can make a formula for computing the address. I guess it is what you have to do, actually.

Hm, it looks like it’s showing you the difference between row- and column-major ordering.

One thing that can be helpful with dealing with memory-layout issues, is to write your own storage-pool, being sure to give it a Print function, and observing the impact of storing various elements therein.

Though that is perhaps a bit more work than is really required, considering how Ada defines the Convention aspect/attribute — which, I think, is what this is really trying to get at. And to that end, consider the following:

  Type Seqs is new Character range 'A'..'Z';
  Type Rows is range 1..128;
  Type Cols is range 1..1024;

  Type Data_Array is Array(Seqs range <>, Rows range <>, Cols range <>) of Integer
  Type Input_Array is new Data_Array
    with Convention => COBOL;
  Type Output_Array is new Data_Array
    with Convention => FORTRAN;

Have fun.