1
[System.Security.SecurityCritical]  // auto-generated
        [ResourceExposure(ResourceScope.None)]
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private static extern void FCallAddSub(ref Decimal d1, ref Decimal d2, byte bSign);

I can't find the source code for this function. Could you help me to construct a number from its bit components.

1 Answers1

2

You're probably not going to find source for this. It's likely implemented in something that is not C# such as C++ and isn't going to be found in the reference source code. This question delves into the details of InternalCall extern:

C# internal static extern with InternalCall attribute - internal or external?

I would speculate this is a placeholder for allowing the JIT compiler to replace this call with platform specific CPU instructions for this operation. It is likely implemented within the CLR itself. I am not an expert on this though, so at what point in the pipeline this is handled I'm fuzzy on.

When they finish putting the C# core source up on github (open sourcing .NET Core framework currently is a work in progress), then you might find an implementation for this: https://github.com/dotnet/corefx

Mono's Implementation

You could instead take a look at mono's implementation of Decimal here, which does not use FCallAddSub:

https://github.com/mono/mono/blob/master/mcs/class/corlib/System/Decimal.cs

They implement a similar abstraction in native code with the function mono_decimalIncr which is implemented in C and the source can be found here:

https://github.com/mono/mono/blob/ee90d5ff1d521f92c3dd9cca79b002dc4bc65ee3/mono/metadata/decimal.c

Community
  • 1
  • 1
AaronLS
  • 37,329
  • 20
  • 143
  • 202
  • Hi, @AaronLS I am stuck at how to turn the type `decimal_repr` back into `Decimal`. Or I don't know how to construct the struct data `decimal_repr` into a number . – user3305259 Jan 09 '15 at 07:55
  • @loggeruser `repr` stands for reference pointer. If you look at the C# that calls the Decimal parameters are declared with `ref`. I'm not sure on the details of how the native-to-managed interface converts the C ref pointers to C# references. The details of unmanaged to managed calls are way outside of my knowledge. Good luck. – AaronLS Jan 09 '15 at 19:46
  • [This](https://github.com/mono/mono/blob/master/mono/metadata/decimal-ms.c) should be quite close to the Microsoft source code. – user1622959 Mar 23 '15 at 16:27