News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Day Of The Week Algo

Started by lingo, April 06, 2009, 10:56:38 PM

Previous topic - Next topic

xmetal

Well, the original code "should" have been written this way:


unsigned dayOfWeek(unsigned day, unsigned month, unsigned year)
{
static unsigned t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};

unsigned x = year;

if(month < 3)
--x;

return (x + x/4 - x/100 + x/400 + t[month-1] + day) % 7;
}


It should generate more efficient code.

MichaelW

QuoteIt should generate more efficient code.

It did. Running on my P3, the improved code was 5 cycles faster compiled with Visual C++ Toolkit 2003 and 7 cycles faster compiled with GCC. I have updated the attachment and the results.
eschew obfuscation

xmetal

Apparently, more recent versions do an even better job; here's the assembler output from my FreeBSD's GCC:

        .file   "dayOfWeek.c"
        .text
        .p2align 4,,15
.globl dayOfWeek
        .type   dayOfWeek, @function
dayOfWeek:
        pushl   %ebp
        movl    %esp, %ebp
        movl    12(%ebp), %ecx
        movl    16(%ebp), %edx
        movl    8(%ebp), %eax
        pushl   %ebx
        cmpl    $3, %ecx
        sbbl    $0, %edx
        addl    t.1543-4(,%ecx,4), %eax
        movl    %edx, %ecx
        shrl    $2, %ecx
        addl    %edx, %eax
        leal    (%eax,%ecx), %ebx
        movl    $1374389535, %ecx
        movl    %edx, %eax
        mull    %ecx
        movl    %edx, %ecx
        shrl    $7, %ecx
        addl    %ecx, %ebx
        shrl    $5, %edx
        subl    %edx, %ebx
        movl    $613566757, %edx
        movl    %ebx, %eax
        movl    %ebx, %ecx
        mull    %edx
        subl    %edx, %ecx
        shrl    %ecx
        addl    %ecx, %edx
        shrl    $2, %edx
        leal    0(,%edx,8), %ecx
        subl    %edx, %ecx
        subl    %ecx, %ebx
        movl    %ebx, %eax
        popl    %ebx
        popl    %ebp
        ret
        .size   dayOfWeek, .-dayOfWeek
        .section        .rodata
        .align 32
        .type   t.1543, @object
        .size   t.1543, 48
t.1543:
        .long   0
        .long   3
        .long   2
        .long   5
        .long   0
        .long   3
        .long   5
        .long   1
        .long   4
        .long   6
        .long   2
        .long   4
        .ident  "GCC: (GNU) 4.2.1 20070719  [FreeBSD]"


Darrel

On my soon to be posted new moonphase program it allows you to enter a date to see different astronomical positions. but you can not enter the dates october 5-14 in the year 1582 since this is the commonly used dates of transition from Julian to Gregorian calendar, oct. 4, 1582 julian calendar followed by the next day of oct. 15, 1582 gregorian calendar.

Regards,  Darrel

Adamanteus

 I could say enough uncomplements to inventors of calendar magic numbers as oct. 4, 1582 julian  :bdg, insted of using original formule from it's papa and no chance if not to understand astronomers :
including 4000 years correlation, Grigorian calendar repeating on (((365*4+1)*25-1)*4+1)*10-1 = 1460969 days, so formule of week day is :
  dayOfWeek = (total_days(day, month, year) - 1) % 7;