@@ -361,60 +361,65 @@ public boolean validateOrdering() {
361361 * 2. Assume any subsequent clock decrease is a rollover.
362362 */
363363 public void fixClockRollover () {
364- boolean modified = true ;
365- // ungated clock
366- while (modified ) {
367- modified = false ;
368- for (int i =this .scalers .size ()-1 ; i >0 ; --i ) {
369- Dsc2Scaler previous = this .scalers .get (i -1 ).dsc2 ;
370- Dsc2Scaler next = this .scalers .get (i ).dsc2 ;
371- long corr = previous .clock - next .clock + 1 ;
372- boolean is_rollover = previous .clock > next .clock ;
373- boolean is_gap = corr <= -2 *(long )Integer .MAX_VALUE ;
374- if (is_rollover || is_gap ) {
375- if (is_gap ) corr = -corr ;
376- for (int j =i ; j <this .scalers .size (); ++j ) {
377- if (j ==i ) System .out .print ( String .format ("FIXING UNGATED CLOCK ROLLOVER: %d -> " ,this .scalers .get (j ).dsc2 .clock ));
378- this .scalers .get (j ).dsc2 .clock += corr ;
379- if (j ==i ) {
380- System .out .println (String .format ("%d" ,this .scalers .get (j ).dsc2 .clock ));
381- System .out .println ((double )corr /(2 *(long )Integer .MAX_VALUE ));
382- if (is_gap ) System .out .println ("GAP!" );
383- }
364+ // fixed rollover size
365+ final long ROLLOVER = 2 *(long )Integer .MAX_VALUE ;
366+ // loop over ungated and gated, to apply correction separately for each
367+ final int is_ungated = 0 ;
368+ final int is_gated = 1 ;
369+ for (int clk : List .of (is_ungated , is_gated )) {
370+ boolean modified = true ;
371+ while (modified ) {
372+ modified = false ;
373+ for (int i =this .scalers .size ()-1 ; i >0 ; --i ) {
374+ Dsc2Scaler previous = this .scalers .get (i -1 ).dsc2 ;
375+ Dsc2Scaler next = this .scalers .get (i ).dsc2 ;
376+ String clock_name ;
377+ long diff ;
378+ boolean is_rollover ;
379+ switch (clk ) {
380+ case is_ungated :
381+ clock_name = "ungated clock" ;
382+ diff = previous .clock - next .clock + 1 ;
383+ is_rollover = previous .clock > next .clock ;
384+ break ;
385+ default : // is_gated
386+ clock_name = "gated clock" ;
387+ diff = previous .gatedClock - next .gatedClock + 1 ;
388+ is_rollover = previous .gatedClock > next .gatedClock ;
389+ break ;
384390 }
385- modified = true ;
386- break ;
387- }
388- }
389- }
390- // repeat for gated clock
391- modified = true ;
392- while (modified ) {
393- modified = false ;
394- for (int i =this .scalers .size ()-1 ; i >0 ; --i ) {
395- Dsc2Scaler previous = this .scalers .get (i -1 ).dsc2 ;
396- Dsc2Scaler next = this .scalers .get (i ).dsc2 ;
397- long corr = previous .gatedClock - next .gatedClock + 1 ;
398- boolean is_rollover = previous .gatedClock > next .gatedClock ;
399- boolean is_gap = corr <= -2 *(long )Integer .MAX_VALUE ;
400- if (is_rollover || is_gap ) {
401- if (is_gap ) corr = -corr ;
402- for (int j =i ; j <this .scalers .size (); ++j ) {
403- if (j ==i ) System .out .print ( String .format ("FIXING GATED CLOCK ROLLOVER: %d -> " ,this .scalers .get (j ).dsc2 .gatedClock ));
404- this .scalers .get (j ).dsc2 .gatedClock += corr ;
405- if (j ==i ) {
406- System .out .println (String .format ("%d" ,this .scalers .get (j ).dsc2 .gatedClock ));
407- System .out .println ((double )corr /(2 *(long )Integer .MAX_VALUE ));
408- if (is_gap ) System .out .println ("GAP!" );
391+ boolean is_gap = diff <= -ROLLOVER / 2 ;
392+ if (is_rollover || is_gap ) {
393+ for (int j =i ; j <this .scalers .size (); ++j ) {
394+ switch (clk ) {
395+ case is_ungated :
396+ if (j ==i ) logger .info ( String .format ("fixing ungated clock rollover: %d ->" , this .scalers .get (j ).dsc2 .clock ));
397+ if (is_gap ) this .scalers .get (j ).dsc2 .clock -= ROLLOVER ;
398+ else this .scalers .get (j ).dsc2 .clock += ROLLOVER ;
399+ if (j ==i ) logger .info ( String .format (" -> %d" , this .scalers .get (j ).dsc2 .clock ));
400+ break ;
401+ default : // is_gated
402+ if (j ==i ) logger .info ( String .format ("fixing gated clock rollover: %d ->" , this .scalers .get (j ).dsc2 .gatedClock ));
403+ if (is_gap ) this .scalers .get (j ).dsc2 .gatedClock -= ROLLOVER ;
404+ else this .scalers .get (j ).dsc2 .gatedClock += ROLLOVER ;
405+ if (j ==i ) logger .info ( String .format (" -> %d" , this .scalers .get (j ).dsc2 .gatedClock ));
406+ break ;
407+ }
408+ if (j ==i ) {
409+ if (Math .abs ( ((double )diff /ROLLOVER ) - 1 ) > 0.01 ) {
410+ logger .warning ("found " + clock_name + " rollover of unexpected size " + diff + " (expected about " + ROLLOVER + ")" );
411+ }
412+ }
409413 }
414+ modified = true ;
415+ break ;
410416 }
411- modified = true ;
412- break ;
413417 }
414418 }
415419 }
416420 }
417-
421+
422+
418423 public static void main (String [] args ) {
419424
420425 final String dir = System .getenv ("HOME" )+"/data/" ;
0 commit comments