Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 6 additions & 29 deletions src/Microsoft.Windows.CsWin32/templates/DECIMAL.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,16 @@
internal partial struct DECIMAL
{
public DECIMAL(decimal value)
public unsafe DECIMAL(decimal value)
{
unchecked
{
const int SignMask = (int)0x80000000;
#if NET5_0_OR_GREATER
Span<int> bits = stackalloc int[4];
decimal.GetBits(value, bits);
#else
int[] bits = decimal.GetBits(value);
#endif
uint lo32 = (uint)bits[0];
uint mid32 = (uint)bits[1];
uint hi32 = (uint)bits[2];
byte scale = (byte)(bits[3] >> 16);
byte sign = (bits[3] & SignMask) == SignMask ? (byte)0x80 : (byte)0x00;
this.Anonymous2 = new _Anonymous2_e__Union() { Anonymous = new _Anonymous2_e__Union._Anonymous_e__Struct() { Lo32 = lo32, Mid32 = mid32 } };
this.Hi32 = hi32;
this.Anonymous1 = new _Anonymous1_e__Union() { Anonymous = new _Anonymous1_e__Union._Anonymous_e__Struct() { scale = scale, sign = sign } };
this.wReserved = 0;
}
// DECIMAL is layout-compatible with decimal.
this = *(DECIMAL*)&value;
}

public static implicit operator decimal(DECIMAL value)
public static unsafe implicit operator decimal(DECIMAL value)
{
return new decimal(
(int)value.Anonymous2.Anonymous.Lo32,
(int)value.Anonymous2.Anonymous.Mid32,
(int)value.Hi32,
value.Anonymous1.Anonymous.sign == 0x80,
value.Anonymous1.Anonymous.scale);
// DECIMAL is layout-compatible with decimal.
return *(decimal*)&value;
}

#if NET5_0_OR_GREATER
public static implicit operator DECIMAL(decimal value) => new DECIMAL(value);
#endif
}
2 changes: 0 additions & 2 deletions test/GenerationSandbox.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,9 @@ public void DecimalConversion(decimal value)
decimal valueRoundTripped = nativeDecimal;
Assert.Equal(value, valueRoundTripped);

#if NET5_0_OR_GREATER
nativeDecimal = value;
valueRoundTripped = nativeDecimal;
Assert.Equal(value, valueRoundTripped);
#endif
}

[Fact]
Expand Down
10 changes: 10 additions & 0 deletions test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ public void SimplestMethod(string tfm)
}
}

[Fact]
public void DecimalWorksInNet35()
{
this.compilation = this.starterCompilations["net35"];
this.generator = this.CreateGenerator();
Assert.True(this.generator.TryGenerate("VarDecDiv", CancellationToken.None));
this.CollectGeneratedCode(this.generator);
this.AssertNoDiagnostics();
}

[Fact]
public void DbgHelpExternMethodsCanLoadAppLocal()
{
Expand Down
Loading