固定小数点型において、動的に整数幅を変える

SystemC Forumにて、

How to change the word length of "sc_fix" dynamically? Any examples?

高位合成(RTLへの実装)とかを考えると、どうしてもデータ幅(bit幅)を意識します。
予め bit幅が決まりきっている場合だと問題ないかもしれませんが、
bit幅を見積もる場合だと何回もシミュレーションを行い確認する必要があります。
もしかしたら、スレッドを立てた人はそういったことをやりたいのかもしれません。

さて、その中で、sc_context_switch が出てきます。
詳細は LRMの 7.2.3章、7.11章の部分です。

sc_context_switch

固定小数点型 ⇔ 浮動小数点型に切り替えられます。
「SC_OFF」- flotingですね。

sc_fxtype_params

defaultのパラメータを変更出来るみたい。
ココら辺って闇の領域ですよね・・・
せっかくなので、記述してみました。

#include <systemc.h>

int sc_main( int argc, char* argv[] ) {

  sc_fxtype_params fxt(32,16);
  sc_fxtype_context fcxt(fxt);
  sc_fix A,B,res; // wl = 32, iwl = 16

  A = 10.0;
  B = 0.1;
  res = A * B; // res = .999908447265625

  cout << "A = "   ; A.print();   cout << ", ";
  cout << "B = "   ; B.print();   cout << ", ";
  cout << "res = " ; res.print(); cout << endl;

  sc_fxtype_params fxt2(16,8);
  sc_fxtype_context fcxt2(fxt2);
  sc_fix C,D; // wl = 16, iwl = 8
  C = 10.0;
  D = 0.1;
  res = C * D; // res = .9765625

  cout << "C = "   ; C.print();   cout << ", ";
  cout << "D = "   ; D.print();   cout << ", ";
  cout << "res = " ; res.print(); cout << endl;

  return 0;
}
  • 実行結果
A = 10, B = .0999908447265625, res = .999908447265625
C = 10, D = .09765625, res = .9765625

といった感じで、bit幅を色々と変えれるみたいです。
うーん。。。闇っぽい。

最初にあった bit幅を選定するですが、GDB使ってやることもできます。
見積もる変数の数が多くなるとなかなか厳しいですが。