Skip to content

Commit a534328

Browse files
committed
Optimized Block8x8F
1 parent 4532194 commit a534328

File tree

7 files changed

+23
-45
lines changed

7 files changed

+23
-45
lines changed

src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,17 @@ internal float this[nuint idx]
101101
set => this[((uint)y * 8) + (uint)x] = value;
102102
}
103103

104-
public static Block8x8F Load(Span<float> data)
105-
{
106-
Block8x8F result = default;
107-
result.LoadFrom(data);
108-
return result;
109-
}
110-
111104
/// <summary>
112105
/// Load raw 32bit floating point data from source.
113106
/// </summary>
114-
/// <param name="source">Source</param>
107+
/// <param name="data">Source</param>
115108
[MethodImpl(InliningOptions.ShortMethod)]
116-
public void LoadFrom(Span<float> source)
109+
public static Block8x8F Load(Span<float> data)
117110
{
118-
ref byte s = ref Unsafe.As<float, byte>(ref MemoryMarshal.GetReference(source));
119-
ref byte d = ref Unsafe.As<Block8x8F, byte>(ref this);
111+
DebugGuard.MustBeGreaterThanOrEqualTo(data.Length, Size, "data is too small");
120112

121-
Unsafe.CopyBlock(ref d, ref s, Size * sizeof(float));
113+
ref byte src = ref Unsafe.As<float, byte>(ref MemoryMarshal.GetReference(data));
114+
return Unsafe.ReadUnaligned<Block8x8F>(ref src);
122115
}
123116

124117
/// <summary>
@@ -144,10 +137,10 @@ public unsafe void LoadFrom(Span<int> source)
144137
[MethodImpl(InliningOptions.ShortMethod)]
145138
public unsafe void ScaledCopyTo(float[] dest)
146139
{
147-
fixed (void* ptr = &this.V0L)
148-
{
149-
Marshal.Copy((IntPtr)ptr, dest, 0, Size);
150-
}
140+
DebugGuard.MustBeGreaterThanOrEqualTo(dest.Length, Size, "dest is too small");
141+
142+
ref byte destRef = ref Unsafe.As<float, byte>(ref MemoryMarshal.GetArrayDataReference(dest));
143+
Unsafe.WriteUnaligned(ref destRef, this);
151144
}
152145

153146
public float[] ToArray()

tests/ImageSharp.Benchmarks/Codecs/Jpeg/BlockOperations/Block8x8F_MultiplyInPlaceBlock.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ private static Block8x8F Create8x8FloatData()
2929
}
3030
}
3131

32-
var source = default(Block8x8F);
33-
source.LoadFrom(result);
34-
return source;
32+
return Block8x8F.Load(result);
3533
}
3634
}

tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ public void Load_Store_FloatArray()
9999
Times,
100100
() =>
101101
{
102-
var b = default(Block8x8F);
103-
b.LoadFrom(data);
102+
Block8x8F b = Block8x8F.Load(data);
104103
b.ScaledCopyTo(mirror);
105104
});
106105

@@ -117,8 +116,7 @@ static void RunTest()
117116
float[] expected = Create8x8FloatData();
118117
ReferenceImplementations.Transpose8x8(expected);
119118

120-
var block8x8 = default(Block8x8F);
121-
block8x8.LoadFrom(Create8x8FloatData());
119+
Block8x8F block8x8 = Block8x8F.Load(Create8x8FloatData());
122120

123121
block8x8.TransposeInplace();
124122

@@ -153,9 +151,8 @@ private static float[] Create8x8ColorCropTestData()
153151
[Fact]
154152
public void NormalizeColors()
155153
{
156-
var block = default(Block8x8F);
157154
float[] input = Create8x8ColorCropTestData();
158-
block.LoadFrom(input);
155+
Block8x8F block = Block8x8F.Load(input);
159156
this.Output.WriteLine("Input:");
160157
this.PrintLinearData(input);
161158

@@ -242,8 +239,7 @@ public void RoundInto()
242239
{
243240
float[] data = Create8x8RandomFloatData(-1000, 1000);
244241

245-
var source = default(Block8x8F);
246-
source.LoadFrom(data);
242+
Block8x8F source = Block8x8F.Load(data);
247243
var dest = default(Block8x8);
248244

249245
source.RoundInto(ref dest);

tests/ImageSharp.Tests/Formats/Jpg/DCTTests.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ static void RunTest(string serialized)
114114
int seed = FeatureTestRunner.Deserialize<int>(serialized);
115115

116116
Span<float> src = Create8x8RandomFloatData(MinInputValue, MaxInputValue, seed);
117-
var srcBlock = default(Block8x8F);
118-
srcBlock.LoadFrom(src);
117+
Block8x8F srcBlock = Block8x8F.Load(src);
119118

120119
float[] expectedDest = new float[64];
121120
float[] temp = new float[64];
@@ -162,8 +161,7 @@ static void RunTest(string serialized)
162161
public void TranformIDCT_4x4(int seed)
163162
{
164163
Span<float> src = Create8x8RandomFloatData(MinInputValue, MaxInputValue, seed, 4, 4);
165-
var srcBlock = default(Block8x8F);
166-
srcBlock.LoadFrom(src);
164+
Block8x8F srcBlock = Block8x8F.Load(src);
167165

168166
float[] expectedDest = new float[64];
169167
float[] temp = new float[64];
@@ -224,8 +222,7 @@ static void AssertScaledElementEquality(Span<float> expected, Span<float> actual
224222
public void TranformIDCT_2x2(int seed)
225223
{
226224
Span<float> src = Create8x8RandomFloatData(MinInputValue, MaxInputValue, seed, 2, 2);
227-
var srcBlock = default(Block8x8F);
228-
srcBlock.LoadFrom(src);
225+
Block8x8F srcBlock = Block8x8F.Load(src);
229226

230227
float[] expectedDest = new float[64];
231228
float[] temp = new float[64];
@@ -286,8 +283,7 @@ static void AssertScaledElementEquality(Span<float> expected, Span<float> actual
286283
public void TranformIDCT_1x1(int seed)
287284
{
288285
Span<float> src = Create8x8RandomFloatData(MinInputValue, MaxInputValue, seed, 1, 1);
289-
var srcBlock = default(Block8x8F);
290-
srcBlock.LoadFrom(src);
286+
Block8x8F srcBlock = Block8x8F.Load(src);
291287

292288
float[] expectedDest = new float[64];
293289
float[] temp = new float[64];
@@ -330,8 +326,7 @@ static void RunTest(string serialized)
330326
int seed = FeatureTestRunner.Deserialize<int>(serialized);
331327

332328
Span<float> src = Create8x8RandomFloatData(MinInputValue, MaxInputValue, seed);
333-
var block = default(Block8x8F);
334-
block.LoadFrom(src);
329+
Block8x8F block = Block8x8F.Load(src);
335330

336331
float[] expectedDest = new float[64];
337332
float[] temp1 = new float[64];

tests/ImageSharp.Tests/Formats/Jpg/ReferenceImplementationsTests.AccurateDCT.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public void ForwardThenInverse(int seed)
2525
{
2626
float[] data = Create8x8RandomFloatData(-1000, 1000, seed);
2727

28-
var b0 = default(Block8x8F);
29-
b0.LoadFrom(data);
28+
Block8x8F b0 = Block8x8F.Load(data);
3029

3130
Block8x8F b1 = ReferenceImplementations.AccurateDCT.TransformFDCT(ref b0);
3231
Block8x8F b2 = ReferenceImplementations.AccurateDCT.TransformIDCT(ref b1);

tests/ImageSharp.Tests/Formats/Jpg/ReferenceImplementationsTests.FastFloatingPointDCT.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ public void LLM_FDCT_IsEquivalentTo_AccurateImplementation(int seed)
7070
{
7171
float[] floatData = Create8x8RandomFloatData(-1000, 1000);
7272

73-
Block8x8F source = default;
74-
source.LoadFrom(floatData);
73+
Block8x8F source = Block8x8F.Load(floatData);
7574

7675
Block8x8F expected = ReferenceImplementations.AccurateDCT.TransformFDCT(ref source);
7776
Block8x8F actual = ReferenceImplementations.LLM_FloatingPoint_DCT.TransformFDCT_UpscaleBy8(ref source);

tests/ImageSharp.Tests/Formats/Jpg/Utils/ReferenceImplementations.LLM_FloatingPoint_DCT.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ public static Block8x8F TransformIDCT(ref Block8x8F source)
3636
float[] temp = new float[64];
3737

3838
IDCT2D_llm(s, d, temp);
39-
Block8x8F result = default;
40-
result.LoadFrom(d);
39+
Block8x8F result = Block8x8F.Load(d);
4140
return result;
4241
}
4342

@@ -49,8 +48,7 @@ public static Block8x8F TransformFDCT_UpscaleBy8(ref Block8x8F source)
4948
float[] temp = new float[64];
5049

5150
FDCT2D_llm(s, d, temp);
52-
Block8x8F result = default;
53-
result.LoadFrom(d);
51+
Block8x8F result = Block8x8F.Load(d);
5452
return result;
5553
}
5654

0 commit comments

Comments
 (0)