Non-zero lower bounds

Kevin Martin kpmartin at watmath.UUCP
Mon Aug 27 05:03:11 AEST 1984


With non-zero lower bounds, but no bounds checking, there is no need for
subtracting each lower subscript bound from the corresponding subscript.
Since the lower bounds and multipliers are constants, all the constant
terms can be pulled out, to form one constant, e.g. for a zero-origin
10 by 20 by 30 array,
  z_offset(i, j, k) = 20 * (30 * i + j) + k
For one-origin,
  general_offset(i, j, k) = 20 * (30 * (i-1) + (j-1)) + (k-1)
                          = 20 * (30 * i - 30 + j - 1) + (k-1)
                          = 20 * (30 * i + j - 31) + (k-1)
                          = 20 * (30 * i + j) - 620 + k - 1
                          = 20 * (30 * i + j) + k - 621
                          = z_offset(i, j, k) - z_offset(1, 1, 1)

Note that the second term is a compile-time constant.
                    Kevin Martin, UofW Software Development Group



More information about the Comp.lang.c mailing list