[Mono-dev] mcs math performance enhancement

Jaroslav Hajek highegg at gmail.com
Sat Oct 20 05:01:12 EDT 2007


Hello,

problem:
As C# does not have an exponentiation operato, programmers will
typically use the
expression "x*x" to get a square of x. There is Math.Pow, but only for
doubles and likely
to be inefficient for this simple case. This is a fairly common idiom
even for C/C++ math programmers who can use pow(T,int).
Mcs (1.2.5.1 release) does not recognize this special case, and emits
two loads for the variable (or struct or class field, which is
longer), instead of the obvious dup-mul sequence (which can easily
identify the "calculate square" idiom for JIT)

solution:
the attached tiny patch for source file mcs/mcs/expression.cs resolves
the problem.
also attached is a testcase that verifies that the optimization works correctly
(this must be, however, compiled against .NET 2.0, i.e. with gmcs, to
be able to acces the method body at runtime).

This is my first contribution to any OS project; therefore, I
apologize if I'm (unknowingly) not following some normal conventions.

Q: Can I build just the C# compiler without building the runtime (i.e.
against the existing installation)? I couldn't figure out a simple
way.

best,

-- 
Jaroslav Hajek
research engineer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch_sqr_optimization
Type: application/octet-stream
Size: 208 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20071020/71554e8b/attachment.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_sqr_optimization.cs
Type: text/x-csharp
Size: 1800 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20071020/71554e8b/attachment.bin 


More information about the Mono-devel-list mailing list