TalkChess.com
Hosted by Your Move Chess & Games

Author Message
Gerd Isenberg

Joined: 08 Mar 2006
Posts: 1785
Location: Hattingen, Germany

Post subject: Re: question about symmertic evaluation    Posted: Thu May 24, 2007 8:55 pm

 bob wrote: I actually don't know how (nor do I care) the compiler handles a/b for ints. All I care about is symmetrical answers, which >> won't give directly. Whatever it does the cost inside Crafty is nil. And I am not dividing by a power of 2 either. I have this in a couple of key places: ((s) * (62 - Min(TotalWhitePieces + TotalBlackPieces, 42)) / 86) which is not exactly a power of 2...

You would care if really an idiv instruction would be generated

But i agree that using div-operator "/" as C-instruction to divide by constant is fine and good for symmetrical scaling - since compiler are smart enough.

Here the output how to divide by 86 and the source from AMD64 optimization manual:

 Code: Signed division by constant =========================== enter divisor: 86 ; dividend: memory location or register other than EAX or EDX MOV EAX, 02FA0BE83h IMUL dividend MOV EAX, dividend SAR EDX, 4 SHR EAX, 31 ADD EDX, EAX ; quotient now in EDX

 Code: /* This program determines the algorithm (a), multiplier (m), and shift factor (s) to be used to accomplish *signed* division by a constant divisor. Compile with MSVC. */ #include typedef unsigned __int64 U64; typedef unsigned long U32; U32 log2(U32 i) {    U32 t = 0;    i = i >> 1;    while (i) {       i = i >> 1;       t++;    }    return(t); } int main(void) {    long e;    U32 d, l, s, m, a;    U64 m_low, m_high, j, k;    fprintf(stderr, "\n");    fprintf(stderr, "Signed division by constant\n");    fprintf(stderr, "===========================\n\n");    fprintf(stderr, "enter divisor: ");    scanf("%ld", &d);    fprintf(stderr, "\n");    e = d;    if ( e < 0 ) d = -d;    if (d == 0) goto printed_code;    if (e == (-1)) {       printf("; dividend: register or memory location\n");       printf("\n");       printf("NEG dividend\n");       printf("\n");       printf("; quotient replaced dividend\n");       goto printed_code;    }    if (d == 2) {       printf("; dividend expected in EAX\n");       printf("\n");       printf("CMP EAX, 080000000h\n");       printf("SBB EAX, -1\n");       printf("SAR EAX, 1\n");       if (e < 0) printf("NEG EAX\n");       printf("\n");       printf("; quotient now in EAX\n");       goto printed_code;    }    if (!(d & (d - 1))) {       printf("; dividend expected in EAX\n");       printf("\n");       printf("CDQ\n");       printf("AND EDX, 0%08lXh\n", (d-1));       printf("ADD EAX, EDX\n");       if (log2(d)) printf("SAR EAX, %d\n", log2(d));       if (e < 0) printf("NEG EAX\n");       printf("\n");       printf("; quotient now in EAX\n");       goto printed_code;    }    /* Determine algorithm (a), multiplier (m), and shift factor (s) for 32-bit    signed integer division. Based on: Granlund, T.; Montgomery, P.L.:    "Division by Invariant Integers using Multiplication". SIGPLAN Notices,    Vol. 29, June 1994, page 61.    */    l = log2(d);    j = (((U64)(0x80000000)) % ((U64)(d)));    k = (((U64)(1)) << (32 + l)) / ((U64)(0x80000000 - j));    m_low = (((U64)(1)) << (32 + l)) / d;    m_high = ((((U64)(1)) << (32 + l)) + k) / d;    while (((m_low >> 1) < (m_high >> 1)) && (l > 0)) {       m_low = m_low >> 1;       m_high = m_high >> 1;       l = l - 1;    }    m = ((U32)(m_high));    s = l;    a = (m_high >> 31) ? 1 : 0;    if (a) {       printf("; dividend: memory location or register other than EAX or EDX\n");       printf("\n");       printf("MOV EAX, 0%08LXh\n", m);       printf("IMUL dividend\n");       printf("MOV EAX, dividend\n");       printf("ADD EDX, EAX\n");       if (s) printf("SAR EDX, %d\n", s);       printf("SHR EAX, 31\n");       printf("ADD EDX, EAX\n");       if (e < 0) printf("NEG EDX\n");       printf("\n");       printf("; quotient now in EDX\n");    }    else {       printf("; dividend: memory location or register other than EAX or EDX\n");       printf("\n");       printf("MOV EAX, 0%08LXh\n", m);       printf("IMUL dividend\n");       printf("MOV EAX, dividend\n");       if (s) printf("SAR EDX, %d\n", s);       printf("SHR EAX, 31\n");       printf("ADD EDX, EAX\n");       if (e < 0) printf("NEG EDX\n");       printf("\n");       printf("; quotient now in EDX\n");    }    printed_code:    fprintf(stderr, "\n");    return 0; }
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
Subject Author Date/Time
Uri Blass Wed May 23, 2007 7:53 pm
Uri Blass Wed May 23, 2007 8:45 pm
Gerd Isenberg Wed May 23, 2007 8:46 pm
Uri Blass Wed May 23, 2007 9:04 pm
Robert Hyatt Wed May 23, 2007 9:51 pm
Uri Blass Wed May 23, 2007 10:04 pm
Gerd Isenberg Wed May 23, 2007 10:09 pm
Robert Hyatt Thu May 24, 2007 12:01 am
Gerd Isenberg Thu May 24, 2007 7:37 am
H.G.Muller Thu May 24, 2007 8:06 am
Gerd Isenberg Thu May 24, 2007 9:24 am
H.G.Muller Thu May 24, 2007 10:39 am
Gerd Isenberg Thu May 24, 2007 11:36 am
H.G.Muller Thu May 24, 2007 11:55 am
Gerd Isenberg Thu May 24, 2007 7:46 pm
Robert Hyatt Thu May 24, 2007 8:18 pm
Re: question about symmertic evaluation Gerd Isenberg Thu May 24, 2007 8:55 pm
Uri Blass Fri May 25, 2007 10:44 am
Gerd Isenberg Fri May 25, 2007 6:28 pm
Uri Blass Fri May 25, 2007 7:42 pm
Gerd Isenberg Fri May 25, 2007 8:00 pm
Robert Hyatt Thu May 24, 2007 8:13 pm
Gerd Isenberg Thu May 24, 2007 8:33 pm
Robert Hyatt Sat May 26, 2007 1:05 am
Gerd Isenberg Sat May 26, 2007 7:18 am
Robert Hyatt Sat May 26, 2007 5:08 pm
Robert Hyatt Sat May 26, 2007 5:25 pm
Gerd Isenberg Sat May 26, 2007 6:23 pm
Robert Hyatt Sun May 27, 2007 2:48 pm
Steven Edwards Thu May 24, 2007 3:42 am
Robert Hyatt Thu May 24, 2007 8:20 pm
Robert Hyatt Wed May 23, 2007 9:50 pm
William H. Rogers Fri May 25, 2007 7:48 pm
Uri Blass Sat May 26, 2007 11:38 am
H.G.Muller Sat May 26, 2007 2:07 pm
Vincent Diepeveen Sat May 26, 2007 7:27 pm
Gerd Isenberg Sat May 26, 2007 8:20 pm
Uri Blass Sat May 26, 2007 9:29 pm

 Jump to: Select a forum Computer Chess Club Forums----------------Computer Chess Club: General TopicsComputer Chess Club: Tournaments and MatchesComputer Chess Club: Programming and Technical DiscussionsComputer Chess Club: Engine Origins Other Forums----------------Chess Thinkers ForumForum Help and Suggestions
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum