How do I convert an unsigned integer to a signed integer without causing an overflow or using TYPECAST in Simulink 7.9 (R2012a)?
56 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2013 年 9 月 19 日
回答済み: Kiran Kintali
2014 年 5 月 17 日
When complying to the DO-254 standard you must detect arithmetic overflows as errors in a Simulink model. This means that using a Data Type Conversion block to convert from an unsigned integer to a signed integer is not possible if an unsigned N-bit integer >= 2^(N - 1) is to be converted, as this will cause an overflow.
Further more when generating HDL Code a calls to the function TYPECAST are not supported.
採用された回答
MathWorks Support Team
2013 年 9 月 19 日
The workaround is to carry out the conversion manually, in Simulink or in MATLAB. As an example the MATLAB code to convert a 16-bit integer is outlined below and the Simulink alternative can be found in the attached model 'convert.mdl'.
isNegative = int16(bitget(u,16));
convertedValue = int16(bitset(u,16,0)) + (-2^15)*isNegative;
The code checks the most significant bit of the unsigned integer to determine if the value will become a positive or negative signed integer.
-If positive the converted value is just a simple conversion to a signed integer as no overflow will take place.
- If negative the the first 15 bits are converted to a signed integer and the smallest signed integer value (-2^15) is added to achieve the wrapped signed value of the original unsigned integer.
0 件のコメント
その他の回答 (1 件)
Kiran Kintali
2014 年 5 月 17 日
Use reinterpretcast to convert an unsigned number to signed number without causing overflow. The stored integer bits of the number will be reinterpreted.
>> a = fi(7, 0,3,0);
>> a.bin
111
>> b = reinterpretcast(a, numerictype(1,3,0));
>> b.bin
111 % notice no change in stored integer bits
>> a
a =
7
>> b
b =
-1 % notice change in real world value
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Configure and View Diagnostics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!