Skip to content

Commit

Permalink
several changes
Browse files Browse the repository at this point in the history
  • Loading branch information
UbitUmarov committed Feb 23, 2024
1 parent 3f705d1 commit abe4474
Show file tree
Hide file tree
Showing 12 changed files with 563 additions and 264 deletions.
135 changes: 16 additions & 119 deletions CSJ2K/j2k/util/ArrayUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,125 +44,22 @@
*
*/
using System;
using System.Runtime.CompilerServices;

namespace CSJ2K.j2k.util
{

/// <summary> This class contains a colleaction of utility static methods for arrays.
///
/// </summary>
public class ArrayUtil
{

/// <summary>The maximum array size to do element by element copying, larger
/// arrays are copyied in a n optimized way.
/// </summary>
public const int MAX_EL_COPYING = 8;

/// <summary>The number of elements to copy initially in an optimized array copy </summary>
public const int INIT_EL_COPYING = 4;

/// <summary> Reinitializes an int array to the given value in an optimized way. If
/// the length of the array is less than MAX_EL_COPYING, then the array
/// is set element by element in the normal way, otherwise the first
/// INIT_EL_COPYING elements are set element by element and then
/// System.arraycopy is used to set the other parts of the array.
///
/// </summary>
/// <param name="arr">The array to set.
///
/// </param>
/// <param name="val">The value to set the array to.
///
///
///
/// </param>
public static void intArraySet(int[] arr, int val)
{
int i, len, len2;

len = arr.Length;
// Set array to 'val' in an optimized way
if (len < MAX_EL_COPYING)
{
// Not worth doing optimized way
for (i = len - 1; i >= 0; i--)
{
// Set elements
arr[i] = val;
}
}
else
{
// Do in optimized way
len2 = len >> 1;
for (i = 0; i < INIT_EL_COPYING; i++)
{
// Set first elements
arr[i] = val;
}
for (; i <= len2; i <<= 1)
{
// Copy values doubling size each time
Array.Copy(arr, 0, arr, i, i);
}
if (i < len)
{
// Copy values to end
Array.Copy(arr, 0, arr, i, len - i);
}
}
}

/// <summary> Reinitializes a byte array to the given value in an optimized way. If
/// the length of the array is less than MAX_EL_COPYING, then the array
/// is set element by element in the normal way, otherwise the first
/// INIT_EL_COPYING elements are set element by element and then
/// System.arraycopy is used to set the other parts of the array.
///
/// </summary>
/// <param name="arr">The array to set.
///
/// </param>
/// <param name="val">The value to set the array to.
///
///
///
/// </param>
public static void byteArraySet(byte[] arr, byte val)
{
int i, len, len2;

len = arr.Length;
// Set array to 'val' in an optimized way
if (len < MAX_EL_COPYING)
{
// Not worth doing optimized way
for (i = len - 1; i >= 0; i--)
{
// Set elements
arr[i] = val;
}
}
else
{
// Do in optimized way
len2 = len >> 1;
for (i = 0; i < INIT_EL_COPYING; i++)
{
// Set first elements
arr[i] = val;
}
for (; i <= len2; i <<= 1)
{
// Copy values doubling size each time
Array.Copy(arr, 0, arr, i, i);
}
if (i < len)
{
// Copy values to end
Array.Copy(arr, 0, arr, i, len - i);
}
}
}
}
public class ArrayUtil
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void intArraySet(int[] arr, int val)
{
Array.Fill(arr, val);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void byteArraySet(byte[] arr, byte val)
{
Array.Fill(arr, val);
}
}
}
33 changes: 6 additions & 27 deletions CSJ2K/j2k/util/MathUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* Copyright (c) 1999/2000 JJ2000 Partners.
* */
using System;
using System.Runtime.CompilerServices;
namespace CSJ2K.j2k.util
{

Expand All @@ -50,34 +51,12 @@ namespace CSJ2K.j2k.util
/// </summary>
public class MathUtil
{

/// <summary> Method that calculates the floor of the log, base 2, of 'x'. The
/// calculation is performed in integer arithmetic, therefore, it is exact.
///
/// </summary>
/// <param name="x">The value to calculate log2 on.
///
/// </param>
/// <returns> floor(log(x)/log(2)), calculated in an exact way.
///
/// </returns>
public static int log2(int x)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int log2(int x)
{
int y, v;
// No log of 0 or negative
if (x <= 0)
{
throw new System.ArgumentException("" + x + " <= 0");
}
// Calculate log2 (it's actually floor log2)
v = x;
y = - 1;
while (v > 0)
{
v >>= 1;
y++;
}
return y;
if (x <= 0)
throw new System.ArgumentException("" + x + " <= 0");
return System.Numerics.BitOperations.Log2((uint)x);
}

/// <summary> Method that calculates the Least Common Multiple (LCM) of two strictly
Expand Down
52 changes: 27 additions & 25 deletions OpenMetaverse.StructuredData/LLSD/XmlLLSD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,17 +450,17 @@ public static unsafe void base64Encode(byte[] data, osUTF8 mb)
int lenMod3 = data.Length % 3;
int len = data.Length - lenMod3;

mb.CheckCapacity(4 * data.Length / 3);
mb.CheckCapacity(((data.Length + 2) / 3) * 4);

fixed (byte* d = data, b64 = osUTF8Const.base64Bytes)
{
int i = 0;
while (i < len)
{
mb.Append(b64[d[i] >> 2]);
mb.Append(b64[((d[i] & 0x03) << 4) | ((d[i + 1] & 0xf0) >> 4)]);
mb.Append(b64[((d[i + 1] & 0x0f) << 2) | ((d[i + 2] & 0xc0) >> 6)]);
mb.Append(b64[d[i + 2] & 0x3f]);
mb.AppendSafe(b64[d[i] >> 2]);
mb.AppendSafe(b64[((d[i] & 0x03) << 4) | ((d[i + 1] & 0xf0) >> 4)]);
mb.AppendSafe(b64[((d[i + 1] & 0x0f) << 2) | ((d[i + 2] & 0xc0) >> 6)]);
mb.AppendSafe(b64[d[i + 2] & 0x3f]);
i += 3;
}

Expand All @@ -469,19 +469,19 @@ public static unsafe void base64Encode(byte[] data, osUTF8 mb)
case 2:
{
i = len;
mb.Append(b64[d[i] >> 2]);
mb.Append(b64[((d[i] & 0x03) << 4) | ((d[i + 1] & 0xf0) >> 4)]);
mb.Append(b64[((d[i + 1] & 0x0f) << 2)]);
mb.Append((byte)'=');
mb.AppendSafe(b64[d[i] >> 2]);
mb.AppendSafe(b64[((d[i] & 0x03) << 4) | ((d[i + 1] & 0xf0) >> 4)]);
mb.AppendSafe(b64[((d[i + 1] & 0x0f) << 2)]);
mb.AppendSafe((byte)'=');
break;
}
case 1:
{
i = len;
mb.Append(b64[d[i] >> 2]);
mb.Append(b64[(d[i] & 0x03) << 4]);
mb.Append((byte)'=');
mb.Append((byte)'=');
mb.AppendSafe(b64[d[i] >> 2]);
mb.AppendSafe(b64[(d[i] & 0x03) << 4]);
mb.AppendSafe((byte)'=');
mb.AppendSafe((byte)'=');
break;
}
}
Expand All @@ -493,15 +493,16 @@ public static unsafe void base64Encode(byte[] data, int start, int lenght, osUTF
int lenMod3 = lenght % 3;
int len = lenght - lenMod3;

mb.CheckCapacity(((lenght + 2) / 3) * 4);
fixed (byte* d = &data[start], b64 = osUTF8Const.base64Bytes)
{
int i = 0;
while (i < len)
{
mb.Append(b64[d[i] >> 2]);
mb.Append(b64[((d[i] & 0x03) << 4) | ((d[i + 1] & 0xf0) >> 4)]);
mb.Append(b64[((d[i + 1] & 0x0f) << 2) | ((d[i + 2] & 0xc0) >> 6)]);
mb.Append(b64[d[i + 2] & 0x3f]);
mb.AppendSafe(b64[d[i] >> 2]);
mb.AppendSafe(b64[((d[i] & 0x03) << 4) | ((d[i + 1] & 0xf0) >> 4)]);
mb.AppendSafe(b64[((d[i + 1] & 0x0f) << 2) | ((d[i + 2] & 0xc0) >> 6)]);
mb.AppendSafe(b64[d[i + 2] & 0x3f]);
i += 3;
}

Expand All @@ -510,19 +511,19 @@ public static unsafe void base64Encode(byte[] data, int start, int lenght, osUTF
case 2:
{
i = len;
mb.Append(b64[d[i] >> 2]);
mb.Append(b64[((d[i] & 0x03) << 4) | ((d[i + 1] & 0xf0) >> 4)]);
mb.Append(b64[((d[i + 1] & 0x0f) << 2)]);
mb.Append((byte)'=');
mb.AppendSafe(b64[d[i] >> 2]);
mb.AppendSafe(b64[((d[i] & 0x03) << 4) | ((d[i + 1] & 0xf0) >> 4)]);
mb.AppendSafe(b64[((d[i + 1] & 0x0f) << 2)]);
mb.AppendSafe((byte)'=');
break;
}
case 1:
{
i = len;
mb.Append(b64[d[i] >> 2]);
mb.Append(b64[(d[i] & 0x03) << 4]);
mb.Append((byte)'=');
mb.Append((byte)'=');
mb.AppendSafe(b64[d[i] >> 2]);
mb.AppendSafe(b64[(d[i] & 0x03) << 4]);
mb.AppendSafe((byte)'=');
mb.AppendSafe((byte)'=');
break;
}
}
Expand All @@ -540,6 +541,7 @@ public static unsafe void base64Encode(byte[] data, StringBuilder sb)
int lenMod3 = data.Length % 3;
int len = data.Length - lenMod3;

sb.EnsureCapacity(((data.Length + 2) / 3) * 4);
fixed (byte* d = data)
{
fixed (char* b64 = base64Chars)
Expand Down
32 changes: 16 additions & 16 deletions OpenMetaverse/AgentThrottle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,14 @@ public AgentThrottle(byte[] data, int pos)
adjData = data;
}

Resend = BitConverter.ToSingle(adjData, pos); pos += 4;
Land = BitConverter.ToSingle(adjData, pos); pos += 4;
Wind = BitConverter.ToSingle(adjData, pos); pos += 4;
Cloud = BitConverter.ToSingle(adjData, pos); pos += 4;
Task = BitConverter.ToSingle(adjData, pos); pos += 4;
Texture = BitConverter.ToSingle(adjData, pos); pos += 4;
Asset = BitConverter.ToSingle(adjData, pos);
Resend = Utils.BytesToFloat(adjData, 0);
Land = Utils.BytesToFloat(adjData, 4);
Wind = Utils.BytesToFloat(adjData, 8);
Cloud = Utils.BytesToFloat(adjData, 12);
Task = Utils.BytesToFloat(adjData, 16);
Texture = Utils.BytesToFloat(adjData, 20);
Asset = Utils.BytesToFloat(adjData, 24);
pos += 28;
}

/// <summary>
Expand Down Expand Up @@ -220,15 +221,14 @@ public void Set(Simulator simulator)
public byte[] ToBytes()
{
byte[] data = new byte[7 * 4];
int i = 0;

Buffer.BlockCopy(Utils.FloatToBytes(Resend), 0, data, i, 4); i += 4;
Buffer.BlockCopy(Utils.FloatToBytes(Land), 0, data, i, 4); i += 4;
Buffer.BlockCopy(Utils.FloatToBytes(Wind), 0, data, i, 4); i += 4;
Buffer.BlockCopy(Utils.FloatToBytes(Cloud), 0, data, i, 4); i += 4;
Buffer.BlockCopy(Utils.FloatToBytes(Task), 0, data, i, 4); i += 4;
Buffer.BlockCopy(Utils.FloatToBytes(Texture), 0, data, i, 4); i += 4;
Buffer.BlockCopy(Utils.FloatToBytes(Asset), 0, data, i, 4); i += 4;

Utils.FloatToBytes(Resend, data, 0);
Utils.FloatToBytes(Land, data, 4);
Utils.FloatToBytes(Wind, data, 8);
Utils.FloatToBytes(Cloud, data, 12);
Utils.FloatToBytes(Task, data, 16);
Utils.FloatToBytes(Texture, data, 20);
Utils.FloatToBytes(Asset, data, 24);

return data;
}
Expand Down
Loading

0 comments on commit abe4474

Please sign in to comment.