如何在 MATLAB/Octave 中获得真正的整数溢出? [英] How do I get real integer overflows in MATLAB/Octave?
问题描述
我正在为 MATLAB/Octave 中的某些 VHDL 代码开发验证工具.因此,我需要生成真实"溢出的数据类型:
I'm working on a verification-tool for some VHDL-Code in MATLAB/Octave. Therefore I need data types which generate "real" overflows:
intmax('int32') + 1
ans = -2147483648
以后,如果我可以定义变量的位宽会很有帮助,但现在这并不重要.
Later on, it would be helpful if I can define the bit width of a variable, but that is not so important right now.
当我构建一个类似 C 的示例时,其中一个变量会增加直到它小于零,它会永远旋转:
When I build a C-like example, where a variable gets increased until it's smaller than zero, it spins forever and ever:
test = int32(2^30);
while (test > 0)
test = test + int32(1);
end
我尝试的另一种方法是自定义溢出"例程,每次更改数字后都会调用该例程.这种方法非常缓慢,不切实际,而且根本不适用于所有情况.有什么建议吗?
Another approach I tried was a custom "overflow"-routine which was called every time after a number is changed. This approach was painfully slow, not practicable and not working in all cases at all. Any suggestions?
推荐答案
在 MATLAB 中,您可以选择重载处理 算术运算 整数数据类型,创建您自己的自定义溢出行为,这将导致整数值的环绕".如文档中所述:
In MATLAB, one option you have is to overload the methods that handle arithmetic operations for integer data types, creating your own custom overflow behavior that will result in a "wrap-around" of the integer value. As stated in the documentation:
您可以定义或重载自己的int*
的方法(对于任何对象)通过适当地放置@int*
文件夹中的命名方法路径上的文件夹.输入<代码>帮助方法名称的数据类型你可以超载.
You can define or overload your own methods for
int*
(as you can for any object) by placing the appropriately named method in an@int*
folder within a folder on your path. Typehelp datatypes
for the names of the methods you can overload.
文档的这一页列出算术运算符的等效方法.二进制加法运算A+B
实际上是由函数plus(A,B)
处理的.因此,您可以创建一个名为 @int32
的文件夹(放置在您的 MATLAB 路径) 并在其中放置一个函数 plus.m
将用于代替内置方法用于 int32
数据类型.
This page of the documentation lists the equivalent methods for the arithmetic operators. The binary addition operation A+B
is actually handled by the function plus(A,B)
. Therefore, you can create a folder called @int32
(placed in another folder on your MATLAB path) and put a function plus.m
in there that will be used instead of the built-in method for int32
data types.
以下示例说明了如何设计重载的 plus
函数以创建所需的上溢/下溢行为:
Here's an example of how you could design your overloaded plus
function in order to create the overflow/underflow behavior you want:
function C = plus(A,B)
%# NOTE: This code sample is designed to work for scalar values of
%# the inputs. If one or more of the inputs is non-scalar,
%# the code below will need to be vectorized to accommodate,
%# and error checking of the input sizes will be needed.
if (A > 0) && (B > (intmax-A)) %# An overflow condition
C = builtin('plus',intmin,...
B-(intmax-A)-1); %# Wraps around to negative
elseif (A < 0) && (B < (intmin-A)) %# An underflow condition
C = builtin('plus',intmax,...
B-(intmin-A-1)); %# Wraps around to positive
else
C = builtin('plus',A,B); %# No problems; call the built-in plus.m
end
end
请注意,我调用了内置的 plus
方法(使用 BUILTIN 函数)来执行 int32
值的添加,我知道不会遇到上溢/下溢问题.如果我改为使用操作 A+B
执行整数加法,则会导致递归调用重载的 plus
方法,这可能会导致额外的计算开销或(在最坏的情况下,最后一行是 C = A+B;
)无限递归.
Notice that I call the built-in plus
method (using the BUILTIN function) to perform addition of int32
values that I know will not suffer overflow/underflow problems. If I were to instead perform the integer addition using the operation A+B
it would result in a recursive call to my overloaded plus
method, which could lead to additional computational overhead or (in the worst-case scenario where the last line was C = A+B;
) infinite recursion.
这是一个测试,显示了实际的环绕溢出行为:
Here's a test, showing the wrap-around overflow behavior in action:
>> A = int32(2147483642); %# A value close to INTMAX
>> for i = 1:10, A = A+1; disp(A); end
2147483643
2147483644
2147483645
2147483646
2147483647 %# INTMAX
-2147483648 %# INTMIN
-2147483647
-2147483646
-2147483645
-2147483644
这篇关于如何在 MATLAB/Octave 中获得真正的整数溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!