Mathematical functions of Verilog syntax

        Verilog-2005 supports some simple mathematical functions, and the data types of its parameters can only be integer and real.

Integer type math function

        $clog2 is a base 2 logarithmic function, the result is rounded up, and the typical format of the return value is:

integer result;

result = $clog2(n);

        The most typical application is to find the bit width of a variable by means of parameterization. The usage has been introduced in detail in another article: How to match the bit width of a variable in Verilog design? ($clog2 system function)

Real type mathematical function

        Its parameter data type is real type, and the return value is also real type, which means that the following mathematical functions cannot be synthesized:

Function Description
$ln(x) N natural logarithm (logarithm to base e)
$log10(x) Decimal logarithm (logarithm to base 10)
exp(x) e^x ,e=2.718281828...
sqrt(x) square root
$pow(x, y) x^y
$floor(x) round down
$ceil(x) Rounded up
$hypot(x, y) sqrt(xx + yy). Square and root of two numbers
$sin(x) sin
$cos(x) cos
$tan(x) tan
$asin(x) arcsin
$acos(x) arccos
$atan(x) arccos
$atan2(x, y) arctangent of x/y
$birth(x) hyperbolic sine
$cosh(x) hyperbolic cosine
$tan(x) hyperbolic tangent
$asinh(x) inverse hyperbolic sine
$acosh(x) inverse hyperbolic cosine
$atanh(x) inverse hyperbolic tangent

        Write a simple testbench to modelsim to verify:

module tb_math_fuc;
	real x, y;		//这些函数的参数需要是real类型,返回也是real类型

initial begin		//0.3f表示取小数点后3位,下同
    x = 10000;$display("$log10(%0.3f) = %0.3f", x, $log10(x));				//以10为底的对数	
    x = 1;$display("$ln(%0.3f) = %0.3f", x, $ln(x));						//以e为底的对数
    x = 2;$display("$exp(%0.3f) = %0.3f", x, $exp(x));						//e^x
    x = 25;$display("$sqrt(%0.3f) = %0.3f", x, $sqrt(x));					//开平方
    x = 5;y = 3;$display("$pow(%0.3f, %0.3f) = %0.3f", x, y, $pow(x, y));	//x^y
    x = 2.7813;$display("$floor(%0.3f) = %0.3f", x, $floor(x));				//向下取整
    x = 7.1111;$display("$ceil(%0.3f) = %0.3f", x, $ceil(x));				//向上取整
	
    x = 30 * (22.0/7.0) / 180;$display("$sin(%0.3f) = %0.3f", x, $sin(x));	//sin函数
    x = 90 * (22.0/7.0) / 180;$display("$cos(%0.3f) = %0.3f", x, $cos(x));	//cos函数
    x = 45 * (22.0/7.0) / 180;$display("$tan(%0.3f) = %0.3f", x, $tan(x));	//tan函数
	
    x = 0.5;$display("$asin(%0.3f) = %0.3f rad, %0.3f deg", x, $asin(x), $asin(x) * 7.0/22.0 * 180);//arcsin函数
    x = 0;$display("$acos(%0.3f) = %0.3f rad, %0.3f deg", x, $acos(x), $acos(x) * 7.0/22.0 * 180);	//arccos函数
    x = 1;$display("$atan(%0.3f) = %0.3f rad, %f deg", x, $atan(x), $atan(x) * 7.0/22.0 * 180);		//arctan函数
	
end

endmodule

        Here is the verification result:

Guess you like

Origin blog.csdn.net/wuzhikaidetb/article/details/128990633