<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>Fyi, 32bit DIV/MOD by any constant is optimizable by any decent compiler with 64bit operations available. Worst case, they get implemented as 64bit fixed point operations.<BR>This works for all 32bit values. The AMD optimization manual prescribes this and includes code for computing reciprocals.<BR>For example, division by 10:<BR> <BR> <BR>E:\>type 1.c<br>int div10(int a) { return a / 10; }<BR> <BR>E:\>cl -Ox -c 1.c<br>Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.278 for x64<br>Copyright (C) Microsoft Corporation. All rights reserved.<BR> <BR> <BR>E:\>link -dump -disasm 1.obj<br>div10:<br> 0000000000000000: B8 67 66 66 66 mov eax,66666667h<br> 0000000000000005: F7 E9 imul ecx<br> 0000000000000007: C1 FA 02 sar edx,2<br> 000000000000000A: 8B C2 mov eax,edx<br> 000000000000000C: C1 E8 1F shr eax,1Fh<br> 000000000000000F: 03 C2 add eax,edx<br> 0000000000000011: C3 ret<BR> <BR> <BR>That's still not free, but it is much cheaper than general division.<BR> <BR> <BR>I should fix M3x86 do to this. :)<BR>On the other hand, it makes for larger code.<BR> <BR> <BR>E:\>cl -c 1.c<br>Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.278 for x64<br>Copyright (C) Microsoft Corporation. All rights reserved.<BR>1.c<BR>E:\>link -dump -disasm 1.obj<br>Microsoft (R) COFF/PE Dumper Version 8.00.50727.278<br>Copyright (C) Microsoft Corporation. All rights reserved.<BR><br>Dump of file 1.obj<BR>File Type: COFF OBJECT<BR>div10:<br> 0000000000000000: 89 4C 24 08 mov dword ptr [rsp+8],ecx<br> 0000000000000004: 8B 44 24 08 mov eax,dword ptr [rsp+8]<br> 0000000000000008: 99 cdq<br> 0000000000000009: B9 0A 00 00 00 mov ecx,0Ah<br> 000000000000000E: F7 F9 idiv eax,ecx<br> 0000000000000010: C3 ret<br><BR> <BR> - Jay<br> <BR><div><div id="SkyDrivePlaceholder"></div>> Date: Tue, 4 Sep 2012 16:20:48 +0000<br>> To: m3commit@elegosoft.com<br>> From: hosking@elego.de<br>> Subject: [M3commit] CVS Update: cm3<br>> <br>> CVSROOT: /usr/cvs<br>> Changes by: hosking@birch. 12/09/04 16:20:48<br>> <br>> Modified files:<br>> cm3/m3-sys/m3middle/src/: M3Buf.m3 <br>> <br>> Log message:<br>> Undo gratuitous damage:<br>> <br>> 1) ChunkSize is carefully designed to have an alignable size.<br>> 2) Comparison order is logical given dependent IF clause.<br>> 3) digits arrays are designed for speed to avoid overhead of DIV/MOD when not<br>> needed.<br>> <br></div> </div></body>
</html>