4
$\begingroup$

In the early 1970s, I had university a co-op job at a place where one of the main languages used was like a glorified calculator. It provided two stacks, and all variables and operators were a single character. Depending upon context each letter could be overloaded and interpreted as a scalar (x+2), a vector (x@2), a function (x:2), or whatever.
(I'm guessing about the syntax; it could have been quite different.)

I'm pretty sure it was either in-house, or provided by some external company that wrote and maintained it for them.

The interesting thing for me is that all arrays were powers of 2 in length. I no longer remember the declaration syntax, but I imagine that each array was stored with an appropriate bit mask, so that x@y was a reference to address x+(y&mask).

All arrays were thus circular. This meant that there was no possibility of having indexes out of range.
It also meant that x@-2 would naturally be the second from the end of the array, without requiring any different processing than for x@2.
(Remember, this was at a time when an extra instruction or two was considered expensive.)

There were times when this behaviour came in handy (though I really can't remember any specific examples).

I'm wondering if any "real" languages ever provided arrays like this.

$\endgroup$
8
  • 2
    $\begingroup$ While it's not an answer to the question as described, a related fun fact is that the growth factor in many languages' implementations of dynamic arrays is 2, so if the initial size of the array is a power of two, it will always have a power of two capacity. $\endgroup$ Commented Jun 30, 2023 at 1:21
  • 2
    $\begingroup$ @RydwolfPrograms, I remember implementations of malloc() , perhaps the original BSD, where the allocated blocks were a power of 2, but the first 4 bytes were used to store the size of the block for use by free() . This meant that the common practice of buffer=malloc(1024) actually used 2048 bytes. 1024 was an obvious choice, but in fact it was the worst possible choice. $\endgroup$ Commented Jun 30, 2023 at 2:25
  • $\begingroup$ @RydwolfPrograms, I should add that the documentation for standard stdio functions also encouraged this use of inefficient buffer size. e.g. setvbuf(…, malloc(BUFSIZ), …, BUFSIZ), where BUFSIZ is almost always defined as a power of 2. $\endgroup$ Commented Jun 30, 2023 at 2:38
  • 2
    $\begingroup$ Amusingly, my archives record that I posted this to a newsgroup in 1984: "… any program that uses stdio can save as much as 50% in buffer space if we just redefined BUFSIZ as 1020 instead of 1024.". In a style typical of the flame-wars of the time, my blasphemy was kindly and logically refuted: "I don't get it. You're obviously just being a f___ing a__h____.". $\endgroup$ Commented Jun 30, 2023 at 18:44
  • 1
    $\begingroup$ Historically, gpus frequently only supported textures with power-of-two sizes, though nowadays they are not so restricted. Behaviour of out-of-bounds accesses is configurable; see this graphic. $\endgroup$ Commented Aug 15, 2023 at 22:52

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.