Skip to content

Commit

Permalink
Optimized Block8x8F
Browse files Browse the repository at this point in the history
  • Loading branch information
gfoidl committed Mar 26, 2023
1 parent 4532194 commit a534328
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 45 deletions.
25 changes: 9 additions & 16 deletions src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,17 @@ internal float this[nuint idx]
set => this[((uint)y * 8) + (uint)x] = value;
}

public static Block8x8F Load(Span<float> data)
{
Block8x8F result = default;
result.LoadFrom(data);
return result;
}

/// <summary>
/// Load raw 32bit floating point data from source.
/// </summary>
/// <param name="source">Source</param>
/// <param name="data">Source</param>
[MethodImpl(InliningOptions.ShortMethod)]
public void LoadFrom(Span<float> source)
public static Block8x8F Load(Span<float> data)
{
ref byte s = ref Unsafe.As<float, byte>(ref MemoryMarshal.GetReference(source));
ref byte d = ref Unsafe.As<Block8x8F, byte>(ref this);
DebugGuard.MustBeGreaterThanOrEqualTo(data.Length, Size, "data is too small");

Unsafe.CopyBlock(ref d, ref s, Size * sizeof(float));
ref byte src = ref Unsafe.As<float, byte>(ref MemoryMarshal.GetReference(data));
return Unsafe.ReadUnaligned<Block8x8F>(ref src);
}

/// <summary>
Expand All @@ -144,10 +137,10 @@ public unsafe void LoadFrom(Span<int> source)
[MethodImpl(InliningOptions.ShortMethod)]
public unsafe void ScaledCopyTo(float[] dest)
{
fixed (void* ptr = &this.V0L)
{
Marshal.Copy((IntPtr)ptr, dest, 0, Size);
}
DebugGuard.MustBeGreaterThanOrEqualTo(dest.Length, Size, "dest is too small");

ref byte destRef = ref Unsafe.As<float, byte>(ref MemoryMarshal.GetArrayDataReference(dest));
Unsafe.WriteUnaligned(ref destRef, this);
}

public float[] ToArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ private static Block8x8F Create8x8FloatData()
}
}

var source = default(Block8x8F);
source.LoadFrom(result);
return source;
return Block8x8F.Load(result);
}
}
12 changes: 4 additions & 8 deletions tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ public void Load_Store_FloatArray()
Times,
() =>
{
var b = default(Block8x8F);
b.LoadFrom(data);
Block8x8F b = Block8x8F.Load(data);
b.ScaledCopyTo(mirror);
});

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

var block8x8 = default(Block8x8F);
block8x8.LoadFrom(Create8x8FloatData());
Block8x8F block8x8 = Block8x8F.Load(Create8x8FloatData());

block8x8.TransposeInplace();

Expand Down Expand Up @@ -153,9 +151,8 @@ private static float[] Create8x8ColorCropTestData()
[Fact]
public void NormalizeColors()
{
var block = default(Block8x8F);
float[] input = Create8x8ColorCropTestData();
block.LoadFrom(input);
Block8x8F block = Block8x8F.Load(input);
this.Output.WriteLine("Input:");
this.PrintLinearData(input);

Expand Down Expand Up @@ -242,8 +239,7 @@ public void RoundInto()
{
float[] data = Create8x8RandomFloatData(-1000, 1000);

var source = default(Block8x8F);
source.LoadFrom(data);
Block8x8F source = Block8x8F.Load(data);
var dest = default(Block8x8);

source.RoundInto(ref dest);
Expand Down
15 changes: 5 additions & 10 deletions tests/ImageSharp.Tests/Formats/Jpg/DCTTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ static void RunTest(string serialized)
int seed = FeatureTestRunner.Deserialize<int>(serialized);

Span<float> src = Create8x8RandomFloatData(MinInputValue, MaxInputValue, seed);
var srcBlock = default(Block8x8F);
srcBlock.LoadFrom(src);
Block8x8F srcBlock = Block8x8F.Load(src);

float[] expectedDest = new float[64];
float[] temp = new float[64];
Expand Down Expand Up @@ -162,8 +161,7 @@ static void RunTest(string serialized)
public void TranformIDCT_4x4(int seed)
{
Span<float> src = Create8x8RandomFloatData(MinInputValue, MaxInputValue, seed, 4, 4);
var srcBlock = default(Block8x8F);
srcBlock.LoadFrom(src);
Block8x8F srcBlock = Block8x8F.Load(src);

float[] expectedDest = new float[64];
float[] temp = new float[64];
Expand Down Expand Up @@ -224,8 +222,7 @@ static void AssertScaledElementEquality(Span<float> expected, Span<float> actual
public void TranformIDCT_2x2(int seed)
{
Span<float> src = Create8x8RandomFloatData(MinInputValue, MaxInputValue, seed, 2, 2);
var srcBlock = default(Block8x8F);
srcBlock.LoadFrom(src);
Block8x8F srcBlock = Block8x8F.Load(src);

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

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

Span<float> src = Create8x8RandomFloatData(MinInputValue, MaxInputValue, seed);
var block = default(Block8x8F);
block.LoadFrom(src);
Block8x8F block = Block8x8F.Load(src);

float[] expectedDest = new float[64];
float[] temp1 = new float[64];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public void ForwardThenInverse(int seed)
{
float[] data = Create8x8RandomFloatData(-1000, 1000, seed);

var b0 = default(Block8x8F);
b0.LoadFrom(data);
Block8x8F b0 = Block8x8F.Load(data);

Block8x8F b1 = ReferenceImplementations.AccurateDCT.TransformFDCT(ref b0);
Block8x8F b2 = ReferenceImplementations.AccurateDCT.TransformIDCT(ref b1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public void LLM_FDCT_IsEquivalentTo_AccurateImplementation(int seed)
{
float[] floatData = Create8x8RandomFloatData(-1000, 1000);

Block8x8F source = default;
source.LoadFrom(floatData);
Block8x8F source = Block8x8F.Load(floatData);

Block8x8F expected = ReferenceImplementations.AccurateDCT.TransformFDCT(ref source);
Block8x8F actual = ReferenceImplementations.LLM_FloatingPoint_DCT.TransformFDCT_UpscaleBy8(ref source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public static Block8x8F TransformIDCT(ref Block8x8F source)
float[] temp = new float[64];

IDCT2D_llm(s, d, temp);
Block8x8F result = default;
result.LoadFrom(d);
Block8x8F result = Block8x8F.Load(d);
return result;
}

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

FDCT2D_llm(s, d, temp);
Block8x8F result = default;
result.LoadFrom(d);
Block8x8F result = Block8x8F.Load(d);
return result;
}

Expand Down

0 comments on commit a534328

Please sign in to comment.