openlang

Column order, still

Fortran arrays are column-major. a(i,j) and a(i+1,j) are neighbours in memory. a(i,j+1) is a whole column away. So the loop nest that walks memory in order has j outside and i inside.

Write it the C way round and the answer is still right. The code just strides through memory a column length at a time. Compilers can interchange loops when they can prove it's safe. Don't rely on it.

Sections follow the same rule. a(:,j) is contiguous. a(i,:) is not, and passing it to a procedure whose dummy argument is explicit-shape or assumed-size can force a copy in and a copy out. An assumed-shape dummy declared (:) takes the strided section as it is.

Whole-array expressions such as b = 2.0 * a leave the order to the compiler. Use them when the operation is elementwise.

Representation first. Sometimes a transposed copy made once is cheaper than a clever traversal done many times.

The conversation continues 1

Follow the reasoning
  1. Permalink

    GLSL matrices are column-major too. m[1] is the second column, and mat3(...) fills columns first from its arguments. Write the constructor row by row because it looks right on screen and you get the transpose.

    Also, in a std140 block a mat3 takes three vec4-sized columns. The CPU side has to pad.

Other sides of the same question

  1. Does this queue need a cached count?

    The only thing the caller ever asks this queue is List.length items > 8, and it asks repeatedly. The queue is one immutable O…