I suggest you ...

Optimize Math functions

Currently the JITer doesn't do a good job with a lot of the .Net Framework, and the Math functions in particular.

Example: Math.Abs

The emitted assembly is pretty much the same for all types. It should optimize to take advantage of x86 instructions that have been around for more than a decade.

cdq
xor eax,edx
sub eax,edx
ret

314 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    WarWar shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    5 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • David Berg [msft]David Berg [msft] commented  ·   ·  Flag as inappropriate

        FYI. There's a difference between the results your code returns and the Math.Abs function. Specifically ABS(int.MinValue) in your code returns int.MinValue (a negative number) while Math.Abs will throw an exception.

      • Martin CostelloMartin Costello commented  ·   ·  Flag as inappropriate

        From what I've seen in the .NET source (via Rotor), most of System.Math is implemented in native code from within the runtime, so I'm guessing it would never be JITed.

      • WarWar commented  ·   ·  Flag as inappropriate

        The problem is that they are unwilling to write code the is specific to a specific type. They don't want to have to test and maintain several different versions of the same function. I was told this by a member of the .Net team.

        The idea solution would be that the JITer is smart enough see the type, the operation, and emit the correct optimized assembly. Then all code would get faster, not just the .Net Framework.

      Feedback and Knowledge Base