Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HAST-164: SDK static code analysis integration #3

Merged
merged 49 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
878d0d7
Fix documentation and spacing issues in Lombiq.Arithmetics.
sarahelsaig Jun 24, 2021
7722912
Fix all missing parenthesis.
sarahelsaig Jun 25, 2021
71809b1
Remove trailing whitespace across the solution.
sarahelsaig Dec 14, 2021
0e3ed71
Fix S103 across the solution.
sarahelsaig Dec 14, 2021
f5685a6
Fix SA1507 (spacing).
sarahelsaig Dec 15, 2021
a13041f
Fix SA1139 (Use literal suffix notation instead of casting).
sarahelsaig Dec 15, 2021
bb7c8fb
Remove unnecessary usings.
sarahelsaig Dec 16, 2021
e86fbe1
Fix SA1116 (The parameters should begin on the line after the declara…
sarahelsaig Dec 16, 2021
87cf9f4
Fix SA1117(The parameters should all be placed on the same line or ea…
sarahelsaig Dec 16, 2021
1c0fcbb
Fix SA1413(Use trailing comma in multi-line initializers)
sarahelsaig Dec 16, 2021
b3bc247
Fix QuireTests.
sarahelsaig Dec 16, 2021
274d282
Fix S2148 (Add underscores to this numeric value for readability).
sarahelsaig Dec 16, 2021
fd9108d
Fix IDE0047 (Parentheses can be removed).
sarahelsaig Dec 16, 2021
13d3a0a
Fix S1481 (Remove the unused local variables).
sarahelsaig Dec 16, 2021
85c0fd6
Fix IDE0090 ('new' expression can be simplified).
sarahelsaig Dec 17, 2021
ad9a01e
Replace single line methods.
sarahelsaig Dec 17, 2021
493e35f
Fix CA1822 (Member does not access instance data and can be marked as…
sarahelsaig Dec 17, 2021
b120676
Add readonly.
sarahelsaig Dec 17, 2021
2057588
Fix IDE0004 (Cast is redundant).
sarahelsaig Dec 17, 2021
27505f4
Fix S3257 (Remove the array type; it is redundant).
sarahelsaig Dec 17, 2021
5b14bf9
Fix S125 (Remove this commented out code).
sarahelsaig Dec 17, 2021
f569d4b
Fix SA1520 (Use braces consistently).
sarahelsaig Dec 17, 2021
64313ad
Fix SA1513 (Closing brace should be followed by blank line).
sarahelsaig Dec 17, 2021
9a20fcc
Fix SA1128 and CA1305.
sarahelsaig Dec 17, 2021
6a321e9
Fix SA1505 (An opening brace should not be followed by a blank line).
sarahelsaig Dec 17, 2021
1da205c
Fix SA1108 and SA1117.
sarahelsaig Dec 17, 2021
2461b95
Fix IDE0021 (Use expression body for constructors).
sarahelsaig Dec 18, 2021
fff4b8f
Fix S113 (Add a new line at the end of the file).
sarahelsaig Dec 18, 2021
f339657
Fix Posit32Tests.
sarahelsaig Dec 18, 2021
22dd16c
More ternary usage.
sarahelsaig Dec 18, 2021
34a7de2
Fix Posit.
sarahelsaig Dec 18, 2021
8e81a82
Refactor BitMask.
sarahelsaig Dec 18, 2021
6beedb1
Posit32 refactor.
sarahelsaig Dec 18, 2021
4dc92fe
Fix UnumHelper.
sarahelsaig Dec 18, 2021
800f228
Fix UnumConfiguration.
sarahelsaig Dec 18, 2021
1666ccd
Fix UnumEnvironment.
sarahelsaig Dec 18, 2021
b17dd7d
Refactor Quire and Unum.
sarahelsaig Dec 18, 2021
e067574
Go back to Quire.Segments property because Hastlayer can't deal with …
sarahelsaig Dec 18, 2021
5a23edf
Fix transformation issue in Quire equation op.
sarahelsaig Dec 18, 2021
28a93ae
Fix CA1825 (Use Array.Empty<uint>() instead).
sarahelsaig Dec 18, 2021
b9d00dc
Fix Lombiq.Arithmetics.Tests.
sarahelsaig Dec 18, 2021
487ae08
Update note on CA1819.
sarahelsaig Dec 20, 2021
00b207f
Revert CompatibilityAssert.
sarahelsaig Dec 20, 2021
88a4443
Make test project files more uniform.
sarahelsaig Dec 21, 2021
6c788de
Remove unnecessary "Service" entries from csproj.
sarahelsaig Dec 21, 2021
ff50594
Update Shouldly to the same version
sarahelsaig Dec 21, 2021
4bbd391
Update Posit/Posit.cs
sarahelsaig Dec 22, 2021
0ea1c54
Update Posit/Quire.cs
sarahelsaig Dec 22, 2021
b316660
Remove unintelligible comment
sarahelsaig Dec 22, 2021
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
86 changes: 46 additions & 40 deletions BitMask/BitMask.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
using System;
using System;
using System.Collections.Immutable;

namespace Lombiq.Arithmetics
{
public struct BitMask
public struct BitMask : IEquatable<BitMask>
{
private const uint SegmentMaskWithLeadingOne = 0x80000000; // 1000 0000 0000 0000 0000 0000 0000 0000
private const uint SegmentMaskWithClosingOne = 1; // 0000 0000 0000 0000 0000 0000 0000 0001
public ushort Size { get; }
public ushort SegmentCount { get; }
public ImmutableArray<uint> Segments { get; }

public uint Lowest32Bits => Segments[0];

#region Constructors


public BitMask(uint segment, ushort size)
{
Size = size;
Expand All @@ -31,9 +33,8 @@ public BitMask(uint[] segments, ushort size = 0)
var segmentBits = (ushort)(segments.Length << 5);

Size = size < segmentBits ? segmentBits : size;
SegmentCount = size > segmentBits ?
(ushort)((size >> 5) + (size % 32 == 0 ? 0 : 1)) :
(ushort)segments.Length;
SegmentCount = (ushort)segments.Length;
if (size > segmentBits) SegmentCount = (ushort)((size >> 5) + (size % 32 == 0 ? 0 : 1));

if (SegmentCount > segments.Length)
{
Expand All @@ -44,7 +45,10 @@ public BitMask(uint[] segments, ushort size = 0)
Array.Copy(segments, extendedSegments, segments.Length);
Segments = ImmutableArray.CreateRange(extendedSegments);
}
else Segments = ImmutableArray.CreateRange(segments);
else
{
Segments = ImmutableArray.CreateRange(segments);
}
}

public BitMask(ushort size, bool allOne = false)
Expand All @@ -53,7 +57,7 @@ public BitMask(ushort size, bool allOne = false)
SegmentCount = (ushort)((size >> 5) + (partialSegment == 0 ? 0 : 1));
Size = size;

// Creating a temporary array, so the items aren't added using ImmutableArray.Add, because that instantiates
// Creating a temporary array, so the items aren't added using ImmutableArray.Add, because that instantiates
// a new array for each execution.
var segments = new uint[SegmentCount];

Expand Down Expand Up @@ -109,6 +113,7 @@ public BitMask SetOne(ushort index)

return new BitMask(this);
}

/// <summary>
/// Returns a new BitMask, where the given bit is set to zero.
/// </summary>
Expand All @@ -122,7 +127,7 @@ public BitMask SetZero(ushort index)
var segmentPosition = index >> 5;

if ((Segments[segmentPosition] >> bitPosition) % 2 == 1)
return FromImmutableArray(Segments.SetItem(segmentPosition, Segments[segmentPosition] & ~((uint)1 << bitPosition)), Size);
return FromImmutableArray(Segments.SetItem(segmentPosition, Segments[segmentPosition] & ~(1U << bitPosition)), Size);

return new BitMask(this);
}
Expand All @@ -133,12 +138,13 @@ public BitMask SetZero(ushort index)
/// <returns>A BitMask where the trailing zeros are shifted out to the right.</returns>
public BitMask ShiftOutLeastSignificantZeros()
{
var leastSignificantOnePosition = GetLeastSignificantOnePosition();
var leastSignificantOnePosition = FindLeastSignificantOnePosition();
var mask = new BitMask(this);
if (leastSignificantOnePosition == 0) return mask;

return mask >> leastSignificantOnePosition - 1;
return mask >> (leastSignificantOnePosition - 1);
}

/// <summary>
/// Sets the segment on the given index to the segment given as an argument.
/// </summary>
Expand Down Expand Up @@ -201,19 +207,20 @@ public BitMask SetSegment(int index, uint segment)
public static BitMask operator +(BitMask left, BitMask right)
{
if (left.SegmentCount == 0 || right.SegmentCount == 0) return left;
bool carry = false, leftBit, rightBit;
bool carry = false;
byte buffer;
ushort segmentPosition = 0, position = 0;
var segments = new uint[left.SegmentCount];

for (ushort i = 0; i < (left.Size > right.Size ? left.Size : right.Size); i++)
{
leftBit = (left.Segments[segmentPosition] >> position) % 2 == 1;
rightBit = i >= right.Size ? false : (right.Segments[segmentPosition] >> position) % 2 == 1;
var leftBit = (left.Segments[segmentPosition] >> position) % 2;
var rightBit = i >= right.Size ? 0 : (right.Segments[segmentPosition] >> position) % 2;
var carryBit = carry ? 1 : 0;

buffer = (byte)((leftBit ? 1 : 0) + (rightBit ? 1 : 0) + (carry ? 1 : 0));
buffer = (byte)(leftBit + rightBit + carryBit);

if (buffer % 2 == 1) segments[segmentPosition] += (uint)(1 << position);
if (buffer % 2 != 0) segments[segmentPosition] += (uint)(1 << position);
Piedone marked this conversation as resolved.
Show resolved Hide resolved
carry = buffer >> 1 == 1;

position++;
Expand All @@ -237,19 +244,19 @@ public BitMask SetSegment(int index, uint segment)
{
if (left.SegmentCount == 0 || right.SegmentCount == 0) return left;

bool carry = false, leftBit, rightBit;
bool carry = false;
byte buffer;
ushort segmentPosition = 0, position = 0;
var segments = new uint[left.SegmentCount];

for (ushort i = 0; i < (left.Size > right.Size ? left.Size : right.Size); i++)
{
leftBit = (left.Segments[segmentPosition] >> position) % 2 == 1;
rightBit = i >= right.Size ? false : (right.Segments[segmentPosition] >> position) % 2 == 1;
var leftBit = (left.Segments[segmentPosition] >> position) % 2;
var rightBit = i >= right.Size ? 0 : (right.Segments[segmentPosition] >> position) % 2;

buffer = (byte)(2 + (leftBit ? 1 : 0) - (rightBit ? 1 : 0) - (carry ? 1 : 0));
buffer = (byte)(2 + leftBit - rightBit - (carry ? 1 : 0));

if (buffer % 2 == 1) segments[segmentPosition] += (uint)(1 << position);
if (buffer % 2 != 0) segments[segmentPosition] += (uint)(1 << position);
carry = buffer >> 1 == 0;

position++;
Expand Down Expand Up @@ -322,10 +329,8 @@ public BitMask SetSegment(int index, uint segment)
public static BitMask operator >>(BitMask left, int right)
{
if (right < 0) return left << -right;
//if (right > left.Size) return new BitMask(left.Size);

bool carryOld, carryNew;
var segmentMaskWithLeadingOne = 0x80000000; // 1000 0000 0000 0000 0000 0000 0000 0000
var segments = new uint[left.SegmentCount];
left.Segments.CopyTo(segments);
ushort currentIndex;
Expand All @@ -339,7 +344,7 @@ public BitMask SetSegment(int index, uint segment)
currentIndex = (ushort)(segments.Length - j);
carryNew = segments[currentIndex] % 2 == 1;
segments[currentIndex] >>= 1;
if (carryOld) segments[currentIndex] |= segmentMaskWithLeadingOne;
if (carryOld) segments[currentIndex] |= SegmentMaskWithLeadingOne;
carryOld = carryNew;
}
}
Expand All @@ -356,11 +361,8 @@ public BitMask SetSegment(int index, uint segment)
public static BitMask operator <<(BitMask left, int right)
{
if (right < 0) return left >> -right;
//if (right > left.Size) return new BitMask(left.Size);

bool carryOld, carryNew;
var segmentMaskWithLeadingOne = 0x80000000; // 1000 0000 0000 0000 0000 0000 0000 0000
uint segmentMaskWithClosingOne = 1; // 0000 0000 0000 0000 0000 0000 0000 0001
var segments = new uint[left.SegmentCount];
left.Segments.CopyTo(segments);

Expand All @@ -370,9 +372,9 @@ public BitMask SetSegment(int index, uint segment)

for (ushort j = 0; j < segments.Length; j++)
{
carryNew = ((segments[j] & segmentMaskWithLeadingOne) == segmentMaskWithLeadingOne);
carryNew = (segments[j] & SegmentMaskWithLeadingOne) == SegmentMaskWithLeadingOne;
segments[j] <<= 1;
if (carryOld) segments[j] |= segmentMaskWithClosingOne;
if (carryOld) segments[j] |= SegmentMaskWithClosingOne;
carryOld = carryNew;
}
}
Expand All @@ -389,7 +391,7 @@ public BitMask SetSegment(int index, uint segment)
/// </summary>
/// <returns>Returns the position (not index!) of the most significant 1-bit
/// or zero if there is none.</returns>
public ushort GetMostSignificantOnePosition()
public ushort FindMostSignificantOnePosition()
{
ushort position = 0;
uint currentSegment;
Expand All @@ -402,7 +404,7 @@ public ushort GetMostSignificantOnePosition()
{
currentSegment >>= 1;
position++;
if (currentSegment == 0) return (ushort)((SegmentCount - i) * 32 + position);
if (currentSegment == 0) return (ushort)(((SegmentCount - i) * 32) + position);
}
}

Expand All @@ -412,7 +414,7 @@ public ushort GetMostSignificantOnePosition()
public BitMask GetTwosComplement(ushort size)
{
var mask = new BitMask(this);
return ((~mask + 1) << (SegmentCount * 32 - size)) >> (SegmentCount * 32 - size);
return ((~mask + 1) << ((SegmentCount * 32) - size)) >> ((SegmentCount * 32) - size);
}

public ushort LengthOfRunOfBits(ushort startingPosition)
Expand All @@ -423,35 +425,39 @@ public ushort LengthOfRunOfBits(ushort startingPosition)
mask <<= 1;
for (var i = 0; i < startingPosition; i++)
{
if (mask.Segments[0] >> 31 > 0 != startingBit) return length;
if ((mask.Segments[0] >> 31 > 0) != startingBit) return length;
mask <<= 1;
length++;
}

return (length > startingPosition) ? startingPosition : length;
}


/// <summary>
/// Finds the least significant 1-bit.
/// </summary>
/// <returns>Returns the position (not index!) of the least significant 1-bit
/// or zero if there is none.</returns>
public ushort GetLeastSignificantOnePosition()
public ushort FindLeastSignificantOnePosition()
{
ushort position = 1;
uint currentSegment;

for (ushort i = 0; i < SegmentCount; i++)
{
currentSegment = Segments[i];
if (currentSegment == 0) position += 32;
if (currentSegment == 0)
{
position += 32;
}
else
{
while (currentSegment % 2 == 0)
{
position++;
currentSegment >>= 1;
}

if (currentSegment % 2 == 1) return position;
}
}
Expand All @@ -460,15 +466,15 @@ public ushort GetLeastSignificantOnePosition()
}

// Array indexer is not supported by Hastlayer yet.
//public uint this[int i] => Segments[i];

public uint GetLowest32Bits() => Segments[0];
//// public uint this[int i] => Segments[i];

#endregion

#region Overrides

public override bool Equals(object obj) => this == (BitMask)obj;
public override bool Equals(object obj) => obj is BitMask other && this == other;

public bool Equals(BitMask other) => this == other;

public override int GetHashCode()
{
Expand Down
Loading