Faster Checked Signed Addition in C#

Signed Integer Overflow Detection A similar method can be developed for summation of signed long arrays. When adding singed integers, arithmetic overflow is possible. In fact, arithmetic underflow is also possible – where adding two negatives results in a positive value. When adding two signed integers, four cases are possible: both positive both negative first […]

Read more "Faster Checked Signed Addition in C#"

Checked SIMD/SSE Addition in C#

In the blog “Faster Checked Addition in C#” we saw how to add numbers safely in C# without using the checked key work and without exceptions. This raised performance, since exceptions have quite a bit of overhead. In this blog, I’ll extend this idea to the data parallel SIMD/SSE instructions of Intel and AMD processors, […]

Read more "Checked SIMD/SSE Addition in C#"

Better C# .Sum() in More Ways

Several improvements to C# .Sum() were shown in the “Better C# .Sum() in Many Ways” blog and made available in the HPCsharp nuget package. In this blog, I’ll explore more ways to improve .Sum() to raise its capabilities to the next level in performance while not giving up accuracy. BigInteger Summation C# provides a BigInteger […]

Read more "Better C# .Sum() in More Ways"

Faster Copying in C#

Copying List to Array To convert a List to an Array, C# provides a convenient¬†ToArray¬†function: var listSource = new List<int> { 5, 7, 16, 3 }; int[] arrayDestination1 = listSource.ToArray(); // C# standard conversion int[] arrayDestination2 = listSource.ToArrayPar(); // HPCsharp parallel conversion ToArray comes up often, whenever some method requires an Array, but you happen […]

Read more "Faster Copying in C#"

Faster Checked Addition in C#

Summary I show that care must be taken when adding two integers in C#, which can cause arithmetic overflow. C# has a checked keyword to help catch arithmetic overflow, and throws an overflow exception, used by Linq .Sum(). Catching this exception is shown in summation of ulong[] arrays, which performs poorly in the worst case, […]

Read more "Faster Checked Addition in C#"

Better C# .Sum() in Many Ways

C# Linq provides a convenient way to add up all of the array elements. var arr = new int[] { 5, 7, 16, 3 }; int arrSum = arr.Sum(); This .Sum() supports some of the built-in numeric types: int, long, float, double, and decimal. It provides a nicely consistent and convenient way of adding up […]

Read more "Better C# .Sum() in Many Ways"