FPGA開発日記

カテゴリ別記事インデックス https://msyksphinz.github.io/github_pages , English Version https://fpgadevdiary.hatenadiary.com/

"iota"ベクトルとは何なのか

きっかけは、RISC-Vのベクトル命令一覧で、viota.m命令が存在していることからだった。 命令の説明文によると、

The viota.m instruction reads a source vector mask register and writes to each element of the destination vector register group the sum of all the bits of elements in the mask register whose index is less than the element, e.g., a parallel pre x sum of the mask values.

viota.m命令は、ソースベクトルマスクレジスタを読み出し、その要素のインデックスが小さい要素の全ビットの合計を、書き込みベクトルレジスタの各要素に書き込むものである。

Spikeにおける実装は以下の通りだ。とりあえずマスクの実装を無視して読むと、その要素の下位1ビット (vs2_lsb) が1であった場合にカウントアップし、その要素の前の要素までのカウント値の合計を格納するようになっている。

int cnt = 0;
for (reg_t i = 0; i < vl; ++i) {
  const int midx = i / 64;
  const int mpos = i % 64;

  bool vs2_lsb = ((P.VU.elt<uint64_t>(rs2_num, midx) >> mpos) & 0x1) == 1;
  bool do_mask = (P.VU.elt<uint64_t>(0, midx) >> mpos) & 0x1;

  bool has_one = false;
  if (insn.v_vm() == 1 || (insn.v_vm() == 0 && do_mask)) {
    if (vs2_lsb) {
      has_one = true;
    }
  }

  bool use_ori = (insn.v_vm() == 0) && !do_mask;
  switch (sew) {
  case e8:
    P.VU.elt<uint8_t>(rd_num, i, true) = use_ori ?
                                   P.VU.elt<uint8_t>(rd_num, i) : cnt;
    break;
  /* ... 途中省略 ... */
  if (has_one) {
    cnt++;
  }
}

なぜこれをiotaと呼ぶのか?検索してみるとこれは略称で吐く、ギリシャ文字 \iota が語源になっているということだった。

stackoverflow.com

どうもC++にもstd::iota()という関数があるらしい。

The original SGI STL documentation gives this explanation:

The name iota is taken from the programming language APL.

APL (A programming language)という書籍の中でシーケンシャル値を持つベクトルのこととして  \iota が紹介されたらしい。この辺のプログラミング言語論のことはあまり詳しいことは分からない。

The interval vector ⍳j(n) is defined as the vector of integers beginning with j.

So the likeliest answer is that Dr. Iverson wanted a symbol which would remind the user of the word “integer, the word “interval”, and the use of the letter “i” as a typical integer variable, especially for array subscripting.

(Google翻訳)

したがって、最も可能性の高い答えは、アイバーソン博士は、ユーザーに「整数」という言葉、「間隔」という言葉、および文字「i」を典型的な整数変数として、特に配列の添え字として使用することを思い出させるシンボルを望んでいたということです。

なるほどなあ。このiotaという文字列自体は略称ではなく、こういった歴史的な経緯から受け継がれている意味合いなのか。勉強になった。