Showing posts with label Easter. Show all posts
Showing posts with label Easter. Show all posts

Wednesday, 22 February 2017

Computus 4

I noticed a few days ago that Dr J R Stockton's web pages on Computus are now longer available. The last impression of the site in the Wayback Machine was 2015-09-07. In it he lists a number of JavaScript algorithms for the calculation of the date of Easter Sunday in the Gregorian calendar. The result of the following functions are "the day of March" such that 1 indicates March 1st, 32 indicates April 1st, etc.

The first version is my own humble attempt (here translated to C/C++):

  int EasterDayOfMarch(int year) {
    // 25 ops = 2 div, 3 rem, 2 mul, 5 shift, 8 add, 5 sub
    int a = year % 19;
    int b = year >> 2;
    int c = (b / 25) + 1;
    int d = (c * 3) >> 2;
    int e = ((c << 3) + 5) / 25;
    int f = (a * 19 + d - e + 15) % 30;
    int g = f + ((29578 - a - (f << 5)) >> 10);
    int h = g - ((year + b - d + g + 2) % 7);
    return h;
  }

This is derived from Gauss's algorithm and subsequent revisions. However, the calculation of 'g' is my own work and chosen such that it can be computed on a 16-bit machine efficiently (though it works for all integer sizes).

There are other minor variations outlined on Dr Stockton's page:

    // Lichtenberg
    int g = 28 + f - ((f + a / 11) / 29);

and

    // Hutchins
    int g = 28 + f - ((a + f * 11) / 319);  

There is also a C implementation which is the equivalent of:

    // Pemberton
    int g = 28 + f - (int)((f + g + (int)(a > 10)) > 28);

As mentioned previously, Al Petrofsky came up with another variation on the theme:

  int EasterDayOfMarchPetrofsky(int year) {
    // 21 ops = 4 div, 3 rem, 4 mul, 1 shift, 6 add, 3 sub
    int a = (year % 19) * 6060;
    int b = year >> 2;
    int c = b / 25;
    int p = (c * 2267) - ((b / 100) * 6775) + 3411;
    int q = ((a + ((p / 25) * 319) - 1) % 9570) / 330;
    int r = 28 + q - (year + b + p + q) % 7;
    return r;
  }

This cleverly uses fewer operations, though requires integer sizes greater than sixteen bits for larger years (not a great imposition, these days!)

To convert a "day of March" 'h' to an actual date, the following code can be used:

  void Easter(int year, int* month, int* day) {
    int a = year % 19;
    int b = year >> 2;
    int c = (b / 25) + 1;
    int d = (c * 3) >> 2;
    int e = ((c << 3) + 5) / 25;
    int f = (a * 19 + d - e + 15) % 30;
    int g = f + ((29578 - a - (f << 5)) >> 10);
    int h = g - ((year + b - d + g + 2) % 7);
    int i = h >> 5; // is it April?
    *month = i + 3;
    *day = (h & 31) + i;
  }

Sunday, 16 March 2014

Computus 3

It's been a few years since my last look at calculating the date of Easter Sunday using only Z80 machine code, but I thought I'd finally re-visit it. In particular, I wanted to look in detail at Al Petrofsky's awesome optimizations of my lame attempts.



If you want a master-class in optimizing integer calculations on archaic CPUs (and who doesn't?) look no further than Al's solution. It's the product of deep understanding of:
  • The Z80 instruction set,
  • Integer multiplication-by-constants,
  • Integer division-by-constants,
  • Modulo arithmetic, and
  • Easter computus.
At only two hundred or so opcodes, and no branches, it's a beautiful distillation.

Sunday, 6 February 2011

Z80 Easter Egg T-Shirt

It's the Z80 asssembler for calculating the date of Easter Sunday:

Friday, 10 December 2010

Computus 2

Of course, if you're as old as I am, you'll remember programming on machines with no native division instruction (neither integer nor floating-point), so all those divisions and modulos aren't going to get you very far on a "old school" 16-bit computer. Using the information from Hacker's Delight, you can re-cast these operations to multiplications and shifts:

void easter_u16_nodiv(u16 year, u16* month, u16* day)
{
    u16 a = year >> 1;
    u16 b = year >> 2;
    u16 c = year >> 4;
    u16 d = b + c;
    u16 e = (a + d + (d >> 4) + (d >> 5) + (d >> 10)) >> 4;
    u16 f = year - e * 19;
    u16 g = f - ((f + 13) >> 5) * 19;
    d = (b >> 1) + (b >> 3);
    e = (d + (d >> 6) + (d >> 7)) >> 4;
    f = b - e * 25 + 39;
    d = e + (f >> 5);
    u16 h = (d * 3) >> 2;
    d = (d * 8) + 5;
    e = (d >> 1) + (d >> 3);
    e = (e + (e >> 6) + (e >> 7)) >> 4;
    f = d - (e * 25) + 7;
    d = (g * 19) + h - e - (f >> 5) + 15;
    e = ((d >> 1) + (d >> 5)) >> 4;
    f = d - (e * 30);
    d = f - ((f + 2) >> 5) * 30;
    g = d + ((29578 - g - (d * 32)) >> 10);
    f = b - h + g + 2;
    d = a + c + (f >> 1) + (f >> 4);
    e = (d + (d >> 6) + (d >> 12)) >> 2;
    f = year + f - (e * 7);
    e = g - f + ((f * 2341) >> 14) * 7;
    d = e >> 5;
    *day = e - d * 31;
    *month = d + 3;
}

Again, if you want to verify the algorithm (only valid for Gregorian years 0 to 65535), you can do so with Dr J R Stockton's test harness with the following Javascript snippet:

    var a, b, c, d, e, f, g, h;
    a = YR >>> 1;
    b = YR >>> 2;
    c = YR >>> 4;
    d = b + c;
    e = (a + d + (d >>> 4) + (d >>> 5) + (d >>> 10)) >>> 4;
    f = YR - e * 19;
    g = f - ((f + 13) >>> 5) * 19;
    d = (b >>> 1) + (b >>> 3);
    e = (d + (d >>> 6) + (d >>> 7)) >>> 4;
    f = b - e * 25 + 39;
    d = e + (f >>> 5);
    h = (d * 3) >>> 2;
    d = (d * 8) + 5;
    e = (d >>> 1) + (d >>> 3);
    e = (e + (e >>> 6) + (e >>> 7)) >>> 4;
    f = d - (e * 25) + 7;
    d = (g * 19) + h - e - (f >>> 5) + 15;
    e = ((d >>> 1) + (d >>> 5)) >>> 4;
    f = d - (e * 30);
    d = f - ((f + 2) >>> 5) * 30;
    g = d + ((29578 - g - (d * 32)) >>> 10);
    f = b - h + g + 2;
    d = a + c + (f >>> 1) + (f >>> 4);
    e = (d + (d >>> 6) + (d >>> 12)) >>> 2;
    f += YR - (e * 7);
    e = g - f + ((f * 2341) >>> 14) * 7;
    return e;

Computus 1

Computus is the calculation of Easter. Calendrical calculations being a bit of an obsession of mine, I've been looking at this with a view to an implementation using limited-range integer arithmetic. There's a lot of interesting stuff still being researched on this, but I haven't seen a good implementation for 16-bit unsigned arithmetic. So here's my attempt:

void easter_u16(u16 year, u16* month, u16* day)
{
    u16 a = year % 19;
    u16 b = year >> 2;
    u16 c = (b / 25) + 1;
    u16 d = (c * 3) >> 2;
    u16 e = ((a * 19) - ((c * 8 + 5) / 25) + d + 15) % 30;
    e += (29578 - a - e * 32) >> 10;
    e -= ((year % 7) + b - d + e + 2) % 7;
    d = e >> 5;
    *day = e - d * 31;
    *month = d + 3;
}

This is valid for all Gregorian years 0 to 65535. In fact, the core algorithm is valid for all non-negative years: just change the integer type to a wider, signed one.

If you want to test it using Dr J R Stockton's wonderful script tester, use the following Javascript snippet:

    var a = YR % 19;
    var b = YR >>> 2;
    var c = ((b / 25)|0) + 1;
    var d = (c * 3) >>> 2;
    var e = ((a * 19) - (((c * 8 + 5) / 25)|0) + d + 15) % 30;
    e += (29578 - a - (e << 5)) >>> 10;
    e -= ((YR % 7) + b - d + e + 2) % 7;
    return e;