system verilog $clog2的使用

本文介绍了SystemVerilog中的系统函数$clog2的使用,它返回参数的对数2的上限(即向上取整到整数值)。讨论了参数类型、特殊值处理以及其在计算内存地址宽度和状态表示中的应用。举例说明了$clog2的计算规则,并结合$ceil函数展示了如何在实际计算中进行位宽确定。同时,提到了浮点数转换和向上取整函数$ceil的使用细节。
摘要由CSDN通过智能技术生成

下面使system verilog手册的原文描述
The system function $clog2 shall return the ceiling of the log base 2 of the argument (the log rounded up to
an integer value). The argument can be an integer or an arbitrary sized vector value. The argument shall be
treated as an unsigned value, and an argument value of 0 shall produce a result of 0.
This system function can be used to compute the minimum address width necessary to address a memory of
a given size or the minimum vector width necessary to represent a given number of states.
For example:
integer result;
result = $clog2(n);

  1. $clog2(n)的参数n必须为整数,否则会报错,所以我们用的时候如果n是浮点型的,需要使用int’(n)转换一下,然后转换的规则是四舍五入
  2. n=0时,得到的结果是0,这个算是强制规定,因为log2(x) 的定义域x>0,正常是取不到0的
  3. n=1时,得到的结果是0,这个是合理的
  4. n=2时,得到的结果是1
  5. n=3~4时,得到的结果是2
  6. n=5~8时,得到的结果是3
  7. …以此类推,由于此函数是向上取整,所以如果有n个数我们使用$clog2(n)的位宽变量正好能够放下,即 [ $clog2(n)-1 : 0]

顺带$ceil(x)是向上取整,x可以是浮点型, $ceil(1.1)=2.0, $ceil(1.8)=2.0
我们也可以像下面这样联合使用这三个函数,在一些计算中比较有用,比如我们需要使用两个寄存器来表示数N,
这两个寄存器位宽都是3,N={reg2,reg1},我们知道N的值可以通过下面来计算reg2和reg1的值
当N<7时 reg2=0,reg1=N
当N>=63时 reg2=7, reg1=7
当7=<N<63 reg2=$clog2(int’($ceil(1.0*N/7))) reg1=N%7 (这里面重要的是,当7<N<14时候我们需要让reg2=1, 另外前面需要加1.0要不10/7直接等于1,前面的ceil函数起不了作用)
$clog2(int’($ceil(1.2)))=1
$clog2(int’(1.2))=0

  • 5
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值