Skip to content

Scalar bitwise implementation #667

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

Merged
merged 3 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
111 changes: 111 additions & 0 deletions src/Maths/Silk.NET.Maths.Tests/Scalar.Bitwise.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Globalization;
using System.Runtime.InteropServices;
using Xunit;

namespace Silk.NET.Maths.Tests
{
public class ScalarBitwiseTest
{
[Fact]
public void RotateLeft1()
{
Assert.Equal((byte)0b1110_0001, Scalar.RotateLeft((byte)0b1111_0000, 1));
}

[Fact]
public void RotateLeft2()
{
Assert.Equal((ushort)0b1100_1000_0000_0011, Scalar.RotateLeft((ushort)0b1111_0010_0000_0000, 2));
}

[Fact]
public void RotateRight1()
{
Assert.Equal((byte)0b1111_0000, Scalar.RotateRight((byte)0b1110_0001, 1));
}

[Fact]
public void RotateRight2()
{
Assert.Equal((ushort)0b1111_0010_0000_0000, Scalar.RotateRight((ushort)0b1100_1000_0000_0011, 2));
}

[Fact]
public void And1()
{
Assert.Equal(0b1010, Scalar.And(0b1110, 0b1011));
}

[Fact]
public void And2()
{
Assert.Equal((byte)0b1010, Scalar.And((byte)0b1110, (byte)0b1011));
}

[Fact]
public void And3()
{
Assert.Equal((long)0b1010, Scalar.And((long)0b1110, (long)0b1011));
}

[Fact]
public void Or1()
{
Assert.Equal(0b1111, Scalar.Or(0b1110, 0b1011));
}

[Fact]
public void Or2()
{
Assert.Equal((byte)0b1111, Scalar.Or((byte)0b1110, (byte)0b1011));
}

[Fact]
public void Or3()
{
Assert.Equal((long)0b1111, Scalar.Or((long)0b1110, (long)0b1011));
}

[Fact]
public void Xor1()
{
Assert.Equal(0b0101, Scalar.Xor(0b1110, 0b1011));
}

[Fact]
public void Xor2()
{
Assert.Equal((byte)0b0101, Scalar.Xor((byte)0b1110, (byte)0b1011));
}

[Fact]
public void Xor3()
{
Assert.Equal((long)0b0101, Scalar.Xor((long)0b1110, (long)0b1011));
}

[Fact]
public void Not1()
{
Assert.Equal(~0b1110, Scalar.Not(0b1110));
}

[Fact]
public void Not2()
{
var b = ~(byte)0b1110;
// ReSharper disable once IntVariableOverflowInUncheckedContext
Assert.Equal((byte)b, Scalar.Not((byte)0b1110));
}

[Fact]
public void Not3()
{
Assert.Equal(~(ulong)0b1110, Scalar.Not((ulong)0b1110));
}
}
}
137 changes: 137 additions & 0 deletions src/Maths/Silk.NET.Maths/Scalar.Bitwise/Scalar.And.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;

namespace Silk.NET.Maths
{
static partial class Scalar
{
/// <summary>
/// Performs And on supported types
/// </summary>
public static T And<T>(T left, T right) where T : unmanaged
{
return Byte(left, right);

[MethodImpl(MaxOpt)]
static T Byte(T left, T right)
{
if (typeof(T) == typeof(byte))
{
return (T) (object) (byte) ((byte) (object) left & (byte) (object) right);
}

return SByte(left, right);
}

[MethodImpl(MaxOpt)]
static T SByte(T left, T right)
{
if (typeof(T) == typeof(sbyte))
{
return (T) (object) (sbyte) ((sbyte) (object) left & (sbyte) (object) right);
}

return UInt16(left, right);
}

[MethodImpl(MaxOpt)]
static T UInt16(T left, T right)
{
if (typeof(T) == typeof(ushort))
{
return (T) (object) (ushort) ((ushort) (object) left & (ushort) (object) right);
}

return Int16(left, right);
}

[MethodImpl(MaxOpt)]
static T Int16(T left, T right)
{
if (typeof(T) == typeof(short))
{
return (T) (object) (short) ((short) (object) left & (short) (object) right);
}

return UInt32(left, right);
}

[MethodImpl(MaxOpt)]
static T UInt32(T left, T right)
{
if (typeof(T) == typeof(uint))
{
return (T) (object) (uint) ((uint) (object) left & (uint) (object) right);
}

return Int32(left, right);
}

[MethodImpl(MaxOpt)]
static T Int32(T left, T right)
{
if (typeof(T) == typeof(int))
{
return (T) (object) (int) ((int) (object) left & (int) (object) right);
}

return UInt64(left, right);
}

[MethodImpl(MaxOpt)]
static T UInt64(T left, T right)
{
if (typeof(T) == typeof(ulong))
{
return (T) (object) (ulong) ((ulong) (object) left & (ulong) (object) right);
}

return Int64(left, right);
}

[MethodImpl(MaxOpt)]
static T Int64(T left, T right)
{
if (typeof(T) == typeof(long))
{
return (T) (object) (long) ((long) (object) left & (long) (object) right);
}

return Single(left, right);
}

[MethodImpl(MaxOpt)]
static T Single(T left, T right)
{
if (typeof(T) == typeof(float))
{
var res = Unsafe.As<T, uint>(ref left) & Unsafe.As<T, uint>(ref right);
return Unsafe.As<uint, T>(ref res);
}

return Double(left, right);
}

[MethodImpl(MaxOpt)]
static T Double(T left, T right)
{
if (typeof(T) == typeof(double))
{
var res = Unsafe.As<T, ulong>(ref left) & Unsafe.As<T, ulong>(ref right);
return Unsafe.As<ulong, T>(ref res);
}

return Other(left, right);
}

[MethodImpl(MaxOpt)]
static T Other(T left, T right)
{
ThrowUnsupportedType();
return default;
}
}
}
}
137 changes: 137 additions & 0 deletions src/Maths/Silk.NET.Maths/Scalar.Bitwise/Scalar.Not.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;

namespace Silk.NET.Maths
{
static partial class Scalar
{
/// <summary>
/// Performs Not on supported types
/// </summary>
public static T Not<T>(T value) where T : unmanaged
{
return Byte(value);

[MethodImpl(MaxOpt)]
static T Byte(T value)
{
if (typeof(T) == typeof(byte))
{
return (T) (object) (byte) (~ (byte) (object) value);
}

return SByte(value);
}

[MethodImpl(MaxOpt)]
static T SByte(T value)
{
if (typeof(T) == typeof(sbyte))
{
return (T) (object) (sbyte) (~ (sbyte) (object) value);
}

return UInt16(value);
}

[MethodImpl(MaxOpt)]
static T UInt16(T value)
{
if (typeof(T) == typeof(ushort))
{
return (T) (object) (ushort) (~ (ushort) (object) value);
}

return Int16(value);
}

[MethodImpl(MaxOpt)]
static T Int16(T value)
{
if (typeof(T) == typeof(short))
{
return (T) (object) (short) (~ (short) (object) value);
}

return UInt32(value);
}

[MethodImpl(MaxOpt)]
static T UInt32(T value)
{
if (typeof(T) == typeof(uint))
{
return (T) (object) (uint) (~ (uint) (object) value);
}

return Int32(value);
}

[MethodImpl(MaxOpt)]
static T Int32(T value)
{
if (typeof(T) == typeof(int))
{
return (T) (object) (int) (~ (int) (object) value);
}

return UInt64(value);
}

[MethodImpl(MaxOpt)]
static T UInt64(T value)
{
if (typeof(T) == typeof(ulong))
{
return (T) (object) (ulong) (~ (ulong) (object) value);
}

return Int64(value);
}

[MethodImpl(MaxOpt)]
static T Int64(T value)
{
if (typeof(T) == typeof(long))
{
return (T) (object) (long) (~ (long) (object) value);
}

return Single(value);
}

[MethodImpl(MaxOpt)]
static T Single(T value)
{
if (typeof(T) == typeof(float))
{
var res = ~ Unsafe.As<T, uint>(ref value);
return Unsafe.As<uint, T>(ref res);
}

return Double(value);
}

[MethodImpl(MaxOpt)]
static T Double(T value)
{
if (typeof(T) == typeof(double))
{
var res = ~ Unsafe.As<T, ulong>(ref value);
return Unsafe.As<ulong, T>(ref res);
}

return Other(value);
}

[MethodImpl(MaxOpt)]
static T Other(T value)
{
ThrowUnsupportedType();
return default;
}
}
}
}
Loading