| View previous topic :: View next topic |
| 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 <stdio.h>
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;
}
|
|
|
| Back to top |
|
 |
|
| Subject |
Author |
Date/Time |
question about symmertic evaluation |
Uri Blass |
Wed May 23, 2007 7:53 pm |
Re: question about symmertic evaluation |
Uri Blass |
Wed May 23, 2007 8:45 pm |
Re: question about symmertic evaluation |
Gerd Isenberg |
Wed May 23, 2007 8:46 pm |
Re: question about symmertic evaluation |
Uri Blass |
Wed May 23, 2007 9:04 pm |
Re: question about symmertic evaluation |
Robert Hyatt |
Wed May 23, 2007 9:51 pm |
Re: question about symmertic evaluation |
Uri Blass |
Wed May 23, 2007 10:04 pm |
Re: question about symmertic evaluation |
Gerd Isenberg |
Wed May 23, 2007 10:09 pm |
Re: question about symmertic evaluation |
Robert Hyatt |
Thu May 24, 2007 12:01 am |
Re: question about symmertic evaluation |
Gerd Isenberg |
Thu May 24, 2007 7:37 am |
Re: question about symmertic evaluation |
H.G.Muller |
Thu May 24, 2007 8:06 am |
Re: question about symmertic evaluation |
Gerd Isenberg |
Thu May 24, 2007 9:24 am |
Re: question about symmertic evaluation |
H.G.Muller |
Thu May 24, 2007 10:39 am |
Re: question about symmertic evaluation |
Gerd Isenberg |
Thu May 24, 2007 11:36 am |
Re: question about symmertic evaluation |
H.G.Muller |
Thu May 24, 2007 11:55 am |
Re: question about symmertic evaluation |
Gerd Isenberg |
Thu May 24, 2007 7:46 pm |
Re: question about symmertic evaluation |
Robert Hyatt |
Thu May 24, 2007 8:18 pm |
Re: question about symmertic evaluation |
Gerd Isenberg |
Thu May 24, 2007 8:55 pm |
Re: question about symmertic evaluation |
Uri Blass |
Fri May 25, 2007 10:44 am |
Re: question about symmertic evaluation |
Gerd Isenberg |
Fri May 25, 2007 6:28 pm |
Re: question about symmertic evaluation |
Uri Blass |
Fri May 25, 2007 7:42 pm |
Re: question about symmertic evaluation |
Gerd Isenberg |
Fri May 25, 2007 8:00 pm |
Re: question about symmertic evaluation |
Robert Hyatt |
Thu May 24, 2007 8:13 pm |
Re: question about symmertic evaluation |
Gerd Isenberg |
Thu May 24, 2007 8:33 pm |
Re: question about symmertic evaluation |
Robert Hyatt |
Sat May 26, 2007 1:05 am |
Re: question about symmertic evaluation |
Gerd Isenberg |
Sat May 26, 2007 7:18 am |
Re: question about symmertic evaluation |
Robert Hyatt |
Sat May 26, 2007 5:08 pm |
Re: question about symmertic evaluation |
Robert Hyatt |
Sat May 26, 2007 5:25 pm |
Re: question about symmertic evaluation |
Gerd Isenberg |
Sat May 26, 2007 6:23 pm |
Re: question about symmertic evaluation |
Robert Hyatt |
Sun May 27, 2007 2:48 pm |
Re: question about symmertic evaluation |
Steven Edwards |
Thu May 24, 2007 3:42 am |
Re: question about symmertic evaluation |
Robert Hyatt |
Thu May 24, 2007 8:20 pm |
Re: question about symmertic evaluation |
Robert Hyatt |
Wed May 23, 2007 9:50 pm |
Re: question about symmertic evaluation |
William H. Rogers |
Fri May 25, 2007 7:48 pm |
Re: question about symmertic evaluation |
Uri Blass |
Sat May 26, 2007 11:38 am |
Re: question about symmertic evaluation |
H.G.Muller |
Sat May 26, 2007 2:07 pm |
Re: question about symmertic evaluation |
Vincent Diepeveen |
Sat May 26, 2007 7:27 pm |
Re: question about symmertic evaluation |
Gerd Isenberg |
Sat May 26, 2007 8:20 pm |
Re: question about symmertic evaluation |
Uri Blass |
Sat May 26, 2007 9:29 pm |
|
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
|
|