@@ -32,11 +32,12 @@ object Periods {
3232 /** A period is a contiguous sequence of phase ids in some run.
3333 * It is coded as follows:
3434 *
35- * sign, always 0: 1 bit
36- * run id: 17 bits
37- * last phase id: 7 bits
38- * first phase id: 7 bits
35+ * sign, always 0: 1 bit
36+ * run id: 17 bits
37+ * last phase id: 7 bits
38+ * #phases before last: 7 bits
3939 *
40+ * This encoding ensures the < and > operators can be delegated as-is to `code`, making them cheap.
4041 */
4142 class Period private [Periods ] (private val code : Int ) extends AnyVal with Showable {
4243
@@ -47,7 +48,7 @@ object Periods {
4748 def lastPhaseId : PhaseId = (code >>> PhaseWidth ) & PhaseMask
4849
4950 /** The first phase of this period */
50- def firstPhaseId : PhaseId = code & PhaseMask
51+ def firstPhaseId : PhaseId = lastPhaseId - ( code & PhaseMask )
5152
5253 def containsPhaseId (id : PhaseId ): Boolean = firstPhaseId <= id && id <= lastPhaseId
5354
@@ -80,10 +81,10 @@ object Periods {
8081 this .lastPhaseId max that.lastPhaseId)
8182
8283 inline def < (that : Period ): Boolean =
83- this .runId < that.runId || ( this .runId == that.runId && this .lastPhaseId < that.firstPhaseId)
84+ this .code < that.code
8485
8586 inline def > (that : Period ): Boolean =
86- this .runId > that.runId || ( this .runId == that.runId && this .firstPhaseId > that.lastPhaseId)
87+ this .code > that.code
8788
8889 def toText (p : Printer ): Text =
8990 inContext(p.printerContext):
@@ -113,11 +114,11 @@ object Periods {
113114
114115 /** The single-phase period consisting of given run id and phase id */
115116 def apply (rid : RunId , pid : PhaseId ): Period =
116- new Period (((( rid << PhaseWidth ) | pid) << PhaseWidth ) | pid )
117+ new Period (((rid << PhaseWidth ) | pid) << PhaseWidth )
117118
118119 /** The period consisting of given run id, and lo/hi phase ids */
119120 def apply (rid : RunId , loPid : PhaseId , hiPid : PhaseId ): Period =
120- new Period ((((rid << PhaseWidth ) | hiPid) << PhaseWidth ) | loPid)
121+ new Period ((((rid << PhaseWidth ) | hiPid) << PhaseWidth ) | (hiPid - loPid) )
121122
122123 /** The interval consisting of all periods of given run id */
123124 def allInRun (rid : RunId ): Period =
0 commit comments