Rocket - diplomacy - TransferSizes

 
介绍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
}
 
posted @ 2019-04-08 12:34  wjcdx  阅读(361)  评论(0编辑  收藏  举报