// matrix multiplication
// inline index to compare with matmulRC

// N = 4 simulate (i,j) = i*N + j

enum
  4 N

// using shift and double add
// d is a * 2^n, accumulate is s
// s partial sum, d double

to mul3 a b | s c d =
  s = 0
  d = a
  if b < 0 c = 0-b else c = b
  while c > 0
    if c & 1 s = s + d    
    d = d + d   
    c = c >> 1
  if b < 0 s = 0-s
  s   

: index i j = (mul3 i N) + j

to inita | i j =
  for i 0 N-1
    for j 0 N-1
      a[index i j] = i

to initb | i j =
  for i 0 N-1
    for j 0 N-1
      b[index i j] = j

//to matmul | i j s k =
//  for i 0 N-1
//    for j 0 N-1
//      s = 0
//      for k 0 N-1
//        s = s + (mul3 a[index i k] b[index k j])
//      c[index i j] = s

to matmul | i j s k iN kN =
  for i 0 N-1
    for j 0 N-1
      s = 0
      iN = mul3 i N
      for k 0 N-1
        kN = mul3 k N
        s = s + (mul3 a[iN+k]  b[kN+j])
      c[iN+j] = s

to show | i j =
  for i 0 N-1
    for j 0 N-1
      print c[index i j] space
    nl

to main = 
  a = array 16
  b = array 16
  c = array 16
  inita initb matmul  show

main

