From 9973e8da3b531f272f6079054e69d80303494ea7 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Fri, 10 Sep 2021 09:01:41 +0300 Subject: [PATCH] Removed excess code, added benchmarks --- .../Formats/Jpeg/Components/Block8x8F.cs | 30 +++++-------------- .../FastFloatingPointDCT.Intrinsic.cs | 3 +- .../Jpeg/Components/FastFloatingPointDCT.cs | 3 -- .../BlockOperations/Block8x8F_Quantize.cs | 23 ++++++++++++++ .../BlockOperations/Block8x8F_Transpose.cs | 16 +++++----- tests/ImageSharp.Benchmarks/Program.cs | 11 +++---- 6 files changed, 44 insertions(+), 42 deletions(-) create mode 100644 tests/ImageSharp.Benchmarks/Codecs/Jpeg/BlockOperations/Block8x8F_Quantize.cs diff --git a/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs b/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs index a25c572aec..d93375f398 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs @@ -424,32 +424,16 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components else #endif { - Multiply(ref block, ref qt); - block.RoundInto(ref dest); + for (int i = 0; i < Size; i++) + { + int idx = ZigZag.ZigZagOrder[i]; + float quantizedVal = block[idx] * qt[idx]; + quantizedVal += quantizedVal < 0 ? -0.5f : 0.5f; + dest[i] = (short)quantizedVal; + } } } - [MethodImpl(InliningOptions.ShortMethod)] - private static void Multiply(ref Block8x8F a, ref Block8x8F b) - { - a.V0L *= b.V0L; - a.V0R *= b.V0R; - a.V1L *= b.V1L; - a.V1R *= b.V1R; - a.V2L *= b.V2L; - a.V2R *= b.V2R; - a.V3L *= b.V3L; - a.V3R *= b.V3R; - a.V4L *= b.V4L; - a.V4R *= b.V4R; - a.V5L *= b.V5L; - a.V5R *= b.V5R; - a.V6L *= b.V6L; - a.V6R *= b.V6R; - a.V7L *= b.V7L; - a.V7R *= b.V7R; - } - public void RoundInto(ref Block8x8 dest) { for (int i = 0; i < Size; i++) diff --git a/src/ImageSharp/Formats/Jpeg/Components/FastFloatingPointDCT.Intrinsic.cs b/src/ImageSharp/Formats/Jpeg/Components/FastFloatingPointDCT.Intrinsic.cs index acc83e2799..d9a04befb9 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/FastFloatingPointDCT.Intrinsic.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/FastFloatingPointDCT.Intrinsic.cs @@ -3,12 +3,11 @@ #if SUPPORTS_RUNTIME_INTRINSICS using System; -using System.Collections.Generic; +using System.Diagnostics; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; -using System.Text; namespace SixLabors.ImageSharp.Formats.Jpeg.Components { diff --git a/src/ImageSharp/Formats/Jpeg/Components/FastFloatingPointDCT.cs b/src/ImageSharp/Formats/Jpeg/Components/FastFloatingPointDCT.cs index 181f18185b..6f68881cd1 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/FastFloatingPointDCT.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/FastFloatingPointDCT.cs @@ -1,11 +1,8 @@ // Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. -using System.Diagnostics; -using System.Numerics; using System.Runtime.CompilerServices; #if SUPPORTS_RUNTIME_INTRINSICS -using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; #endif diff --git a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/BlockOperations/Block8x8F_Quantize.cs b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/BlockOperations/Block8x8F_Quantize.cs new file mode 100644 index 0000000000..b826193c3d --- /dev/null +++ b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/BlockOperations/Block8x8F_Quantize.cs @@ -0,0 +1,23 @@ +// Copyright (c) Six Labors. +// Licensed under the Apache License, Version 2.0. + +using BenchmarkDotNet.Attributes; +using SixLabors.ImageSharp.Formats.Jpeg.Components; + +namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg.BlockOperations +{ + [Config(typeof(Config.HwIntrinsics_SSE_AVX))] + public class Block8x8F_Quantize + { + private Block8x8F block = default; + private Block8x8F quant = default; + private Block8x8 result = default; + + [Benchmark] + public short Quantize() + { + Block8x8F.Quantize(ref this.block, ref this.result, ref this.quant); + return this.result[0]; + } + } +} diff --git a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/BlockOperations/Block8x8F_Transpose.cs b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/BlockOperations/Block8x8F_Transpose.cs index 8e8787475a..47f7d2fbc4 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/BlockOperations/Block8x8F_Transpose.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/BlockOperations/Block8x8F_Transpose.cs @@ -9,25 +9,27 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg.BlockOperations [Config(typeof(Config.HwIntrinsics_SSE_AVX))] public class Block8x8F_Transpose { - private static readonly Block8x8F Source = Create8x8FloatData(); + private Block8x8F source = Create8x8FloatData(); [Benchmark] - public void TransposeInto() => Source.Transpose(); + public float TransposeInto() + { + this.source.Transpose(); + return this.source[0]; + } private static Block8x8F Create8x8FloatData() { - float[] result = new float[64]; + Block8x8F block = default; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { - result[(i * 8) + j] = (i * 10) + j; + block[(i * 8) + j] = (i * 10) + j; } } - var source = default(Block8x8F); - source.LoadFrom(result); - return source; + return block; } } } diff --git a/tests/ImageSharp.Benchmarks/Program.cs b/tests/ImageSharp.Benchmarks/Program.cs index 8080825d9f..f6ffa6f809 100644 --- a/tests/ImageSharp.Benchmarks/Program.cs +++ b/tests/ImageSharp.Benchmarks/Program.cs @@ -1,8 +1,6 @@ -// Copyright (c) Six Labors. +// Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. -using System.Reflection; - using BenchmarkDotNet.Running; namespace SixLabors.ImageSharp.Benchmarks @@ -15,9 +13,8 @@ namespace SixLabors.ImageSharp.Benchmarks /// /// The arguments to pass to the program. /// - public static void Main(string[] args) - { - new BenchmarkSwitcher(typeof(Program).GetTypeInfo().Assembly).Run(args); - } + public static void Main(string[] args) => BenchmarkSwitcher + .FromAssembly(typeof(Program).Assembly) + .Run(args); } }