CurtHagenlocher commented on issue #96:
URL: https://github.com/apache/arrow-dotnet/issues/96#issuecomment-3352010781

   Part of the problem is that the CLR decimal type is not a good match for 
either of these. `SqlDecimal` works pretty well for `decimal128`, but there's 
no other remotely-standard type that works for `decimal256`. 
   
   Here's a sketch of an approach that could work for `decimal128`. I haven't 
tested it yet and I'm fairly sure it doesn't work for negative numbers because 
(I seem to recall that) the `SqlDecimal` representation of a negative number 
uses a sign bit with a positive number instead of a two's-complement value.
   ```
           const int marker = 0x12345678;
           static readonly int decimalOffsetLow = ComputeSqlDecimalOffset(new 
SqlDecimal(38, 0, true, marker, 0, 0, 0));
           static readonly int decimalOffsetMid = ComputeSqlDecimalOffset(new 
SqlDecimal(38, 0, true, 0, marker, 0, 0));
           static readonly int decimalOffsetHigh = ComputeSqlDecimalOffset(new 
SqlDecimal(38, 0, true, 0, 0, marker, 0));
           static readonly int decimalOffsetHighHigh = 
ComputeSqlDecimalOffset(new SqlDecimal(38, 0, true, 0, 0, 0, marker));
   
           unsafe static int ComputeSqlDecimalOffset(SqlDecimal d)
           {
               int* ptr = (int*)&d;
               for (int i = 0; i < 7; i++)
               {
                   if (ptr[i] == marker) { return i; }
               }
               throw new InvalidOperationException("Unable to find index of 
offset for SqlDecimal");
           }
   
           internal static unsafe void GetBytes128(decimal value, int 
precision, int scale, Span<byte> bytes)
           {
               SqlDecimal.ConvertToPrecScale(value, precision, scale);
   
               Span<uint> buffer = MemoryMarshal.Cast<byte, uint>(bytes);
               uint* ptr = (uint*)&value;
               ptr[0] = ptr[decimalOffsetLow];
               ptr[1] = ptr[decimalOffsetMid];
               ptr[2] = ptr[decimalOffsetHigh];
               ptr[3] = ptr[decimalOffsetHighHigh];
           }
   ```
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to