码迷,mamicode.com
首页 > 系统相关 > 详细

Rocket - diplomacy - TransferSizes

时间:2019-04-08 13:58:29      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:block   wrap   code   对象   rsize   div   apply   val   tran   

 
介绍TransferSizes的实现。
 
?技术图片?
 
1. 基本定义
 
从min到max的闭合区间:[min, max]
 
判断条件:
a. min和max为2的幂;
b. min <= max;
c. min和max为非负整数;
d. min和max必须同时为0;
 
?技术图片?
 
2. none
 
因为min和max必须同时为0,所以只需要判断min是否为0即可:
?技术图片?
 
3. contains
 
判断this是否包含x,即x落在this的范围内:
?技术图片?
 
4. containsLg
 
判断this是否包含2^x。
 
5. intersect
 
计算this和x的交集。
 
6. 伴生对象
 
?技术图片?
a. 构造方法:min = max = x;
b. none对象:min = max = 0;
c. asBool:x是否为空;
 
7. 附录
 
TransferSizes:
// An potentially empty inclusive range of 2-powers [min, max] (in bytes)
case class TransferSizes(min: Int, max: Int)
{
def this(x: Int) = this(x, x)
 
require (min <= max, s"Min transfer $min > max transfer $max")
require (min >= 0 && max >= 0, s"TransferSizes must be positive, got: ($min, $max)")
require (max == 0 || isPow2(max), s"TransferSizes must be a power of 2, got: $max")
require (min == 0 || isPow2(min), s"TransferSizes must be a power of 2, got: $min")
require (max == 0 || min != 0, s"TransferSize 0 is forbidden unless (0,0), got: ($min, $max)")
 
def none = min == 0
def contains(x: Int) = isPow2(x) && min <= x && x <= max
def containsLg(x: Int) = contains(1 << x)
def containsLg(x: UInt) =
if (none) Bool(false)
else if (min == max) { UInt(log2Ceil(min)) === x }
else { UInt(log2Ceil(min)) <= x && x <= UInt(log2Ceil(max)) }
 
def contains(x: TransferSizes) = x.none || (min <= x.min && x.max <= max)
 
def intersect(x: TransferSizes) =
if (x.max < min || max < x.min) TransferSizes.none
else TransferSizes(scala.math.max(min, x.min), scala.math.min(max, x.max))
 
override def toString() = "TransferSizes[%d, %d]".format(min, max)
 
}
 
object TransferSizes {
def apply(x: Int) = new TransferSizes(x)
val none = new TransferSizes(0)
 
implicit def asBool(x: TransferSizes) = !x.none
}
 

Rocket - diplomacy - TransferSizes

标签:block   wrap   code   对象   rsize   div   apply   val   tran   

原文地址:https://www.cnblogs.com/wjcdx/p/10669788.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!