Skip to content

Commit

Permalink
Add support for math.bitmask(float4) (#72)
Browse files Browse the repository at this point in the history
Add support for math.bitmask(float4)
  • Loading branch information
xoofx authored Jun 27, 2019
1 parent 48b44c0 commit 0e9c6ad
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Changelog

## [1.0.2] - 2019-05-23
## [1.1.0] - 2019-06-xx

- WIP
- Add new math.bitmask to return a bit mask from a bool4

## [1.0.1] - 2019-04-15

Expand Down
20 changes: 18 additions & 2 deletions src/Tests/Tests/Shared/TestBitmanipulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ namespace Unity.Mathematics.Tests
[TestFixture]
public class TestBitmanipulation
{
[TestCompiler]
public static void bitmask_bool4()
{
TestUtils.AreEqual(0b0000, bitmask(new bool4(false, false, false, false)));
TestUtils.AreEqual(0b0001, bitmask(new bool4(true, false, false, false)));
TestUtils.AreEqual(0b0010, bitmask(new bool4(false, true, false, false)));
TestUtils.AreEqual(0b0100, bitmask(new bool4(false, false, true, false)));
TestUtils.AreEqual(0b1000, bitmask(new bool4(false, false, false, true)));

TestUtils.AreEqual(0b1111, bitmask(new bool4(true, true, true, true)));
TestUtils.AreEqual(0b1110, bitmask(new bool4(false, true, true, true)));
TestUtils.AreEqual(0b1101, bitmask(new bool4(true, false, true, true)));
TestUtils.AreEqual(0b1011, bitmask(new bool4(true, true, false, true)));
TestUtils.AreEqual(0b0111, bitmask(new bool4(true, true, true, false)));
}

[TestCompiler]
public static void countbits_int1()
{
Expand Down Expand Up @@ -392,7 +408,7 @@ public static void rol_int4()
{
TestUtils.AreEqual(rol(int4(219257022, -1586446996, -279484078, -1692078607), 11), int4(-1933184920, -2048170741, -1152739462, 661621977));
}

[TestCompiler]
public static void rol_uint()
{
Expand All @@ -414,7 +430,7 @@ public static void rol_uint3()
{
TestUtils.AreEqual(rol(uint3(219257022u, 2708520300u, 4015483218u), 11), uint3(2361782376u, 2246796555u, 3142227834u));
}

[TestCompiler]
public static void rol_uint4()
{
Expand Down
3 changes: 2 additions & 1 deletion src/Unity.Mathematics.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
Expand All @@ -11,6 +11,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "General", "General", "{B11E
ProjectSection(SolutionItems) = preProject
package.json = package.json
..\readme.md = ..\readme.md
CHANGELOG.md = CHANGELOG.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Mathematics.Tests", "Tests\Unity.Mathematics.Tests.csproj", "{2E7C74D3-42F1-482F-8BB7-E199D9E3B412}"
Expand Down
20 changes: 20 additions & 0 deletions src/Unity.Mathematics/math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,26 @@ public static float asfloat(int x)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float4 asfloat(uint4 x) { return float4(asfloat(x.x), asfloat(x.y), asfloat(x.z), asfloat(x.w)); }

/// <summary>
/// Returns a bitmask representation of a bool4. Storing one 1 bit per component
/// in LSB order, from lower to higher bits (so 4 bits in total).
/// The component x is stored at bit 0,
/// The component y is stored at bit 1,
/// The component z is stored at bit 2,
/// The component w is stored at bit 3
/// The bool4(x = true, y = true, z = false, w = true) would produce the value 1011 = 0xB
/// </summary>
/// <param name="value">The input bool4 to calculate the bitmask for</param>
/// <returns>A bitmask representation of the bool4, in LSB order</returns>
public static int bitmask(bool4 value)
{
int mask = 0;
if (value.x) mask |= 0x01;
if (value.y) mask |= 0x02;
if (value.z) mask |= 0x04;
if (value.w) mask |= 0x08;
return mask;
}

/// <summary>Returns the bit pattern of a long as a double.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.unity.mathematics",
"displayName": "Mathematics",
"version": "1.0.2",
"version": "1.1.0",
"unity": "2018.3",
"description": "Unity's C# SIMD math library providing vector types and math functions with a shader like syntax.",
"keywords": [
Expand Down

0 comments on commit 0e9c6ad

Please sign in to comment.