8319423: Improve Year.isLeap by checking divisibility by 16

Reviewed-by: naoto, rriggs
This commit is contained in:
Claes Redestad 2023-11-08 15:18:53 +00:00
parent 59e9981ec2
commit 7d25f1c6cb
6 changed files with 125 additions and 7 deletions

View file

@ -315,7 +315,10 @@ public final class Year
* @return true if the year is leap, false otherwise
*/
public static boolean isLeap(long year) {
return ((year & 3) == 0) && ((year % 100) != 0 || (year % 400) == 0);
// A year that is a multiple of 100, 200 and 300 is not divisible by 16, but 400 is.
// So for a year that's divisible by 4, checking that it's also divisible by 16
// is sufficient to determine it must be a leap year.
return (year & 15) == 0 ? (year & 3) == 0 : (year & 3) == 0 && year % 100 != 0;
}
//-----------------------------------------------------------------------

View file

@ -475,7 +475,7 @@ public final class IsoChronology extends AbstractChronology implements Serializa
*/
@Override
public boolean isLeapYear(long prolepticYear) {
return ((prolepticYear & 3) == 0) && ((prolepticYear % 100) != 0 || (prolepticYear % 400) == 0);
return Year.isLeap(prolepticYear);
}
@Override