固定小数点数学属性の設定
この例では、MATLAB® コードで固定小数点数学属性を設定する方法を説明します。
代入、加算、減算、乗算の固定小数点数学属性は、fimath
オブジェクトを使用して制御できます。setfimath
を使用すると、fimath
オブジェクトを fi
オブジェクトに付加できます。removefimath
を使用すると、fimath
オブジェクトを fi
オブジェクトから削除できます。
MATLAB Coder™ ソフトウェアをご利用の場合は、例から C コードを生成できます。
固定小数点数学属性の設定と削除
関数 setfimath
および関数 removefimath
を使用すると、固定小数点演算がグローバルおよびローカル fimath
設定の影響を受けないようにすることができます。出力変数に fimath
が付加されていない関数から返すこともできます。これにより、他の関数の設定に影響を与えずに、固定小数点の数学設定をローカル制御できるようになります。
MATLAB コード
function y = user_written_sum(u) % Setup F = fimath('RoundingMethod','Floor',... 'OverflowAction','Wrap',... 'SumMode','KeepLSB',... 'SumWordLength',32); u = setfimath(u,F); y = fi(0,true,32,get(u,'FractionLength'),F); % Algorithm for i=1:length(u) y(:) = y + u(i); end % Cleanup y = removefimath(y); end
FIMATH が付加されていない出力
コードを実行すると fimath
が関数内の演算を制御しますが、戻り値には fimath
が付加されません。これは、関数 user_written_sum
内で setfimath
と removefimath
を使用するためです。
>> u = fi(1:10,true,16,11); >> y = user_written_sum(u)
y =
55
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 32
FractionLength: 11
生成した C コード
MATLAB Coder ソフトウェアをご利用の場合は、次のコマンドを使って C コードを生成できます。
>> u = fi(1:10,true,16,11); >> codegen user_written_sum -args {u} -config:lib -launchreport
関数 fimath
、setfimath
および removefimath
は固定小数点演算を制御しますが、変数に格納されている元のデータは変わらないため、生成 C コードではデータのコピーが生成されません。
int32_T user_written_sum(const int16_T u[10]) { int32_T y; int32_T i; /* Setup */ y = 0; /* Algorithm */ for (i = 0; i < 10; i++) { y += u[i]; } /* Cleanup */ return y; }
FIMATH の不一致
複数の fi
オブジェクトに対して演算を行う場合、これらの fimath
プロパティは等しくなければなりません。そうでないとエラーが発生します。
>> A = fi(pi,'ProductMode','KeepLSB'); >> B = fi(2,'ProductMode','SpecifyPrecision'); >> C = A * B
Error using embedded.fi/mtimes The embedded.fimath of both operands must be equal.
このエラーが発生しないようにするには、fimath
を式の変数のいずれかで削除します。次の例では、fimath
が B
自体を変更せずに式のコンテキストで B
から削除されており、A
に付加されている fimath
を使って積が計算されています。
>> C = A * removefimath(B)
C =
6.283203125
DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 26
RoundingMethod: Nearest OverflowAction: Saturate ProductMode: KeepLSB ProductWordLength: 32 SumMode: FullPrecision
一時変数での FIMATH の変更
fimath
が付加されていない変数があり、特定の演算を制御する場合、変数を変更せずに式のコンテキストで fimath
を付加することができます。
たとえば、積は F
で定義された fimath
で計算されます。
>> F = fimath('ProductMode','KeepLSB','OverflowAction','Wrap','RoundingMethod','Floor'); >> A = fi(pi); >> B = fi(2); >> C = A * setfimath(B,F)
C =
6.2832
DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 26
RoundingMethod: Floor OverflowAction: Wrap ProductMode: KeepLSB ProductWordLength: 32 SumMode: FullPrecision MaxSumWordLength: 128
変数 B
は変更されていないことに注意してください。
>> B
B =
2
DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13
ループで競合する FIMATH の削除
負方向の丸めとオーバーフローのラップのある DSP アキュムレータに一致するように積と和を計算し、最も近い正の整数方向への丸めと飽和オーバーフローを出力で使用することができます。fimath
の不一致エラーの発生を回避するには、他の変数と計算する場合に fimath
を出力変数から削除します。
MATLAB コード
この例では、積は 32 ビット、アキュムレータは 40 ビットであり、C のネイティブな整数ルールと同様に、最下位のビットに負方向への丸めとオーバーフローのラップが維持されています。出力は、最も近い正の整数方向への丸めと飽和オーバーフローを使用します。
function [y,z] = setfimath_removefimath_in_a_loop(b,a,x,z) % Setup F_floor = fimath('RoundingMethod','Floor',... 'OverflowAction','Wrap',... 'ProductMode','KeepLSB',... 'ProductWordLength',32,... 'SumMode','KeepLSB',... 'SumWordLength',40); F_nearest = fimath('RoundingMethod','Nearest',... 'OverflowAction','Wrap'); % Set fimaths that are local to this function b = setfimath(b,F_floor); a = setfimath(a,F_floor); x = setfimath(x,F_floor); z = setfimath(z,F_floor); % Create y with nearest rounding y = coder.nullcopy(fi(zeros(size(x)),true,16,14,F_nearest)); % Algorithm for j=1:length(x) % Nearest assignment into y y(j) = b(1)*x(j) + z(1); % Remove y's fimath conflict with other fimaths z(1) = (b(2)*x(j) + z(2)) - a(2) * removefimath(y(j)); z(2) = b(3)*x(j) - a(3) * removefimath(y(j)); end % Cleanup: Remove fimath from outputs y = removefimath(y); z = removefimath(z); end
コード生成の手順
MATLAB Coder ソフトウェアをご利用の場合は、次のコマンドを使って指定ハードウェアの特性をもつ C コードを生成できます。
N = 256; t = 1:N; xstep = [ones(N/2,1);-ones(N/2,1)]; num = [0.0299545822080925 0.0599091644161849 0.0299545822080925]; den = [1 -1.4542435862515900 0.5740619150839550];
b = fi(num,true,16); a = fi(den,true,16); x = fi(xstep,true,16,15); zi = fi(zeros(2,1),true,16,14);
B = coder.Constant(b); A = coder.Constant(a);
config_obj = coder.config('lib'); config_obj.GenerateReport = true; config_obj.LaunchReport = true; config_obj.TargetLang = 'C'; config_obj.GenerateComments = true; config_obj.GenCodeOnly = true; config_obj.HardwareImplementation.ProdBitPerChar=8; config_obj.HardwareImplementation.ProdBitPerShort=16; config_obj.HardwareImplementation.ProdBitPerInt=32; config_obj.HardwareImplementation.ProdBitPerLong=40;
codegen -config config_obj setfimath_removefimath_in_a_loop -args {B,A,x,zi} -launchreport
生成した C コード
関数 fimath
、setfimath
および removefimath
は固定小数点演算を制御しますが、変数に格納されている元のデータは変わらないため、生成 C コードではデータのコピーが生成されません。
void setfimath_removefimath_in_a_loop(const int16_T x[256], int16_T z[2], int16_T y[256]) { int32_T j; int40_T i0; int16_T b_y;
/* Setup */ /* Set fimaths that are local to this function */ /* Create y with nearest rounding */ /* Algorithm */ for (j = 0; j < 256; j++) { /* Nearest assignment into y */ i0 = 15705 * x[j] + ((int40_T)z[0] << 20); b_y = (int16_T)((int32_T)(i0 >> 20) + ((i0 & 524288L) != 0L));
/* Remove y's fimath conflict with other fimaths */ z[0] = (int16_T)(((31410 * x[j] + ((int40_T)z[1] << 20)) - ((int40_T)(-23826 * b_y) << 6)) >> 20); z[1] = (int16_T)((15705 * x[j] - ((int40_T)(9405 * b_y) << 6)) >> 20); y[j] = b_y; }
/* Cleanup: Remove fimath from outputs */ }
ポリモーフィック コード
setfimath
と removefimath
を使って、浮動小数点と固定小数点の両方の型で使用できる MATLAB コードを記述できます。
function y = user_written_function(u) % Setup F = fimath('RoundingMethod','Floor',... 'OverflowAction','Wrap',... 'SumMode','KeepLSB'); u = setfimath(u,F); % Algorithm y = u + u; % Cleanup y = removefimath(y); end
固定小数点入力
関数が固定小数点入力で呼び出されて fimath
F
が演算に使用される場合、出力には fimath
が付加されません。
>> u = fi(pi/8,true,16,15,'RoundingMethod','Convergent'); >> y = user_written_function(u)
y =
0.785400390625
DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 15
固定小数点の C コードの生成
MATLAB Coder ソフトウェアをご利用の場合は、次のコマンドを使って C コードを生成できます。
>> u = fi(pi/8,true,16,15,'RoundingMethod','Convergent'); >> codegen user_written_function -args {u} -config:lib -launchreport
関数 fimath
、setfimath
および removefimath
は固定小数点演算を制御しますが、変数に格納されている元のデータは変わらないため、生成 C コードではデータのコピーが生成されません。
int32_T user_written_function(int16_T u) { /* Setup */ /* Algorithm */ /* Cleanup */ return u + u; }
double 入力
setfimath
と removefimath
は浮動小数点型にはパススルーであるため、user_written_function
の例は浮動小数点型でも機能します。
function y = user_written_function(u) % Setup F = fimath('RoundingMethod','Floor',... 'OverflowAction','Wrap',... 'SumMode','KeepLSB'); u = setfimath(u,F); % Algorithm y = u + u; % Cleanup y = removefimath(y); end
double 向け C コードの生成
浮動小数点入力でコンパイルするとき、次の C コードが生成されます。
>> codegen user_written_function -args {0} -config:lib -launchreport
real_T user_written_function(real_T u) { return u + u; }
ここで、real_T
型は次のように double
として定義されます。
typedef double real_T;
ポリモーフィック コードの詳細
次の関数は、出力が入力と同じ型で作成されるように記述されています。そのため、浮動小数点と固定小数点の両方で使用できます。
function y = user_written_sum_polymorphic(u) % Setup F = fimath('RoundingMethod','Floor',... 'OverflowAction','Wrap',... 'SumMode','KeepLSB',... 'SumWordLength',32);
u = setfimath(u,F);
if isfi(u) y = fi(0,true,32,get(u,'FractionLength'),F); else y = zeros(1,1,class(u)); end
% Algorithm for i=1:length(u) y(:) = y + u(i); end
% Cleanup y = removefimath(y);
end
固定小数点の C コードの生成
MATLAB Coder ソフトウェアをご利用の場合は、次のコマンドを使って固定小数点 C コードを生成できます。
>> u = fi(1:10,true,16,11); >> codegen user_written_sum_polymorphic -args {u} -config:lib -launchreport
関数 fimath
、setfimath
および removefimath
は固定小数点演算を制御しますが、変数に格納されている元のデータは変わらないため、生成 C コードではデータのコピーが生成されません。
int32_T user_written_sum_polymorphic(const int16_T u[10]) { int32_T y; int32_T i;
/* Setup */ y = 0;
/* Algorithm */ for (i = 0; i < 10; i++) { y += u[i]; }
/* Cleanup */ return y; }
浮動小数点の C コードの生成
MATLAB Coder ソフトウェアをご利用の場合は、次のコマンドを使って浮動小数点 C コードを生成できます。
>> u = 1:10; >> codegen user_written_sum_polymorphic -args {u} -config:lib -launchreport
real_T user_written_sum_polymorphic(const real_T u[10]) { real_T y; int32_T i;
/* Setup */ y = 0.0;
/* Algorithm */ for (i = 0; i < 10; i++) { y += u[i]; }
/* Cleanup */ return y; }
ここで、real_T
型は次のように double
として定義されます。
typedef double real_T;
整数型の SETFIMATH
fi
オブジェクトのような組み込み整数の確立した処理パターンに従って、setfimath
は fimath
が付加された同等の fi
に整数入力を変換します。
>> u = int8(5); >> codegen user_written_u_plus_u -args {u} -config:lib -launchreport
function y = user_written_u_plus_u(u) % Setup F = fimath('RoundingMethod','Floor',... 'OverflowAction','Wrap',... 'SumMode','KeepLSB',... 'SumWordLength',32); u = setfimath(u,F); % Algorithm y = u + u; % Cleanup y = removefimath(y); end
出力型は、fimath
で 32 ビットと指定されています。
int32_T user_written_u_plus_u(int8_T u) { /* Setup */ /* Algorithm */ /* Cleanup */ return u + u; }