public static ref T or <T>(ref T lhs, T rhs) where T : unmanaged { if (typeof(T) == typeof(float)) { BitConverter.Int32BitsToSingle(BitConverter.SingleToInt32Bits(float32(lhs)) | BitConverter.SingleToInt32Bits(float32(rhs))); } else if (typeof(T) == typeof(float)) { BitConverter.Int64BitsToDouble(BitConverter.DoubleToInt64Bits(float64(lhs)) | BitConverter.DoubleToInt64Bits(float64(rhs))); } else { throw unsupported <T>(); } return(ref lhs); }
public static ShaderIrOperImmf GetOperImmf19_20(long OpCode) { uint Imm = (uint)(OpCode >> 20) & 0x7ffff; bool Neg = ((OpCode >> 56) & 1) != 0; Imm <<= 12; if (Neg) { Imm |= 0x80000000; } float Value = BitConverter.Int32BitsToSingle((int)Imm); return(new ShaderIrOperImmf(Value)); }
public override async Task <Vertex[]> GetVerticesAsync() { if (CachedVerticies != null) { return(CachedVerticies); } var xArr = await Stream.GetAttributeArray(VertexAttribute.X); var yArr = await Stream.GetAttributeArray(VertexAttribute.Y); var zArr = await Stream.GetAttributeArray(VertexAttribute.Z); var cArr = await Stream.GetAttributeArray(VertexAttribute.Color); var uvArr = await GetUVsAsync(); var vtxArr = new Vertex[Stream.Length]; for (int i = 0; i < vtxArr.Length; i++) { var x = BitConverter.Int32BitsToSingle(xArr[i]); var y = BitConverter.Int32BitsToSingle(yArr[i]); var z = BitConverter.Int32BitsToSingle(zArr[i]); var xyz = new Vector3(x, y, z); var uv = uvArr[i]; var c = BitConverter.GetBytes(cArr[i]); var r = c[0] / 255.0f; var g = c[1] / 255.0f; var b = c[2] / 255.0f; var a = c[3] / 255.0f; var color = new Color(r, g, b, a); vtxArr[i] = new Vertex(i, xyz, uv, color); } CachedVerticies = vtxArr; return(vtxArr); }
public static float PreviousFloat(float value) { if (value <= 0) { return(-NextFloat(-value)); } if (float.IsNaN(value)) { return(value); } if (float.IsNegativeInfinity(value)) { return(value); } int bits = BitConverter.SingleToInt32Bits(value); return(BitConverter.Int32BitsToSingle(bits - 1)); }
public static TestStruct Create() { var testStruct = new TestStruct(); var random = new Random(); for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = TestLibrary.Generator.GetSingle(); } Unsafe.CopyBlockUnaligned(ref Unsafe.As <Vector256 <Single>, byte>(ref testStruct._fld1), ref Unsafe.As <Single, byte>(ref _data1[0]), (uint)Unsafe.SizeOf <Vector256 <Single> >()); for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = BitConverter.Int32BitsToSingle(1); } Unsafe.CopyBlockUnaligned(ref Unsafe.As <Vector256 <Single>, byte>(ref testStruct._fld2), ref Unsafe.As <Single, byte>(ref _data2[0]), (uint)Unsafe.SizeOf <Vector256 <Single> >()); return(testStruct); }
public unsafe static void ConvertD32FS8ToD24S8(Span <byte> output, ReadOnlySpan <byte> input) { Span <uint> outputUint = MemoryMarshal.Cast <byte, uint>(output); ReadOnlySpan <uint> inputUint = MemoryMarshal.Cast <byte, uint>(input); int i = 0; for (; i < inputUint.Length; i += 2) { float depth = BitConverter.Int32BitsToSingle((int)inputUint[i]); uint stencil = inputUint[i + 1]; uint depthStencil = (Math.Clamp((uint)(depth * 0xffffff), 0, 0xffffff) << 8) | (stencil & 0xff); int j = i >> 1; outputUint[j] = depthStencil; } }
public static float Exp2(float power) { if (MathF.Abs(power) > MathV.FloatMaximumPower) { throw new ArgumentOutOfRangeException(nameof(power)); } // R for polynomial generation // exponent2 = data.frame(x = seq(-0.536, 0.536, by = 0.001)) // exponent2$exp = 2 ^ (exponent2$x) // exponentFit = lm(exp ~x + I(x ^ 2) + I(x ^ 3) + I(x ^ 4) + I(x ^ 5), data = exponent2) float integerPart = MathF.Round(power); float x = power - integerPart; // fractional part float integerExponent = BitConverter.Int32BitsToSingle(((int)integerPart + MathV.FloatMantissaZero) << MathV.FloatMantissaBits); float fractionalExponent = MathV.One + MathV.Exp2Beta1 * x + MathV.Exp2Beta2 * x * x + MathV.Exp2Beta3 * x * x * x + MathV.Exp2Beta4 * x * x * x * x; return(integerExponent * fractionalExponent); }
public static float VectorExtractSingle(Vector128 <float> Vector, byte Index) { if (Sse41.IsSupported) { return(Sse41.Extract(Vector, Index)); } else if (Sse2.IsSupported) { Vector128 <ushort> ShortVector = Sse.StaticCast <float, ushort>(Vector); int Low = Sse2.Extract(ShortVector, (byte)(Index * 2 + 0)); int High = Sse2.Extract(ShortVector, (byte)(Index * 2 + 1)); return(BitConverter.Int32BitsToSingle(Low | (High << 16))); } throw new PlatformNotSupportedException(); }
public static float NextFloat(float value) { if (value < 0) { return(-PreviousFloat(-value)); } value = System.Math.Abs(value); // needed to handle -0 if (float.IsNaN(value)) { return(value); } if (float.IsPositiveInfinity(value)) { return(value); } int bits = BitConverter.SingleToInt32Bits(value); return(BitConverter.Int32BitsToSingle(bits + 1)); }
public unsafe void SetExtraData(float FlipX, float FlipY, int Instance) { BindProgram(); EnsureExtraBlock(); GL.BindBuffer(BufferTarget.UniformBuffer, ExtraUboHandle); float *Data = stackalloc float[ExtraDataSize]; Data[0] = FlipX; Data[1] = FlipY; Data[2] = BitConverter.Int32BitsToSingle(Instance); //Invalidate buffer GL.BufferData(BufferTarget.UniformBuffer, ExtraDataSize * sizeof(float), IntPtr.Zero, BufferUsageHint.StreamDraw); GL.BufferSubData(BufferTarget.UniformBuffer, IntPtr.Zero, ExtraDataSize * sizeof(float), (IntPtr)Data); }
public static void RegisterAllLittleEndian() { Register(BinaryPrimitives.ReadInt16LittleEndian, BinaryPrimitives.WriteInt16LittleEndian); Register(BinaryPrimitives.ReadInt32LittleEndian, BinaryPrimitives.WriteInt32LittleEndian); Register(BinaryPrimitives.ReadInt64LittleEndian, BinaryPrimitives.WriteInt64LittleEndian); Register(BinaryPrimitives.ReadUInt16LittleEndian, BinaryPrimitives.WriteUInt16LittleEndian); Register(BinaryPrimitives.ReadUInt32LittleEndian, BinaryPrimitives.WriteUInt32LittleEndian); Register(BinaryPrimitives.ReadInt64LittleEndian, BinaryPrimitives.WriteInt64LittleEndian); Register(BinaryPrimitives.ReadUInt64LittleEndian, BinaryPrimitives.WriteUInt64LittleEndian); Register( src => BitConverter.Int32BitsToSingle(BinaryPrimitives.ReadInt32LittleEndian(src)), (dst, val) => BinaryPrimitives.WriteInt32LittleEndian(dst, BitConverter.SingleToInt32Bits(val))); Register( src => BitConverter.Int64BitsToDouble(BinaryPrimitives.ReadInt64LittleEndian(src)), (dst, val) => BinaryPrimitives.WriteInt64LittleEndian(dst, BitConverter.DoubleToInt64Bits(val))); IsAlreadySet = true; Endianness = Endianness.LittleEndian; }
/// <summary>Parses PID coefficient floating point values from the raw bytes</summary> private static PID ParsePid(List <byte> pidRaw) { if (pidRaw?.Count() != 12) { return(0, 0, 0); } byte[] raw = new byte[12]; for (uint x = 0; x < 12; x++) { raw[x] = pidRaw[(int)(4 * (x / 4) + 3 - x % 4)]; } float p = BitConverter.Int32BitsToSingle(BitConverter.ToInt32(raw, 0)); float i = BitConverter.Int32BitsToSingle(BitConverter.ToInt32(raw, 4)); float d = BitConverter.Int32BitsToSingle(BitConverter.ToInt32(raw, 8)); return(p, i, d); }
private long[] UploadShaders(NvGpuVmm Vmm) { long[] Tags = new long[5]; long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress); for (int Index = 0; Index < 6; Index++) { int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + Index * 0x10); int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + Index * 0x10); //Note: Vertex Program (B) is always enabled. bool Enable = (Control & 1) != 0 || Index == 1; if (!Enable) { continue; } long Tag = BasePosition + (uint)Offset; GalShaderType ShaderType = GetTypeFromProgram(Index); Tags[(int)ShaderType] = Tag; Gpu.Renderer.CreateShader(Vmm, Tag, ShaderType); Gpu.Renderer.BindShader(Tag); } int RawSX = ReadRegister(NvGpuEngine3dReg.ViewportScaleX); int RawSY = ReadRegister(NvGpuEngine3dReg.ViewportScaleY); float SX = BitConverter.Int32BitsToSingle(RawSX); float SY = BitConverter.Int32BitsToSingle(RawSY); float SignX = MathF.Sign(SX); float SignY = MathF.Sign(SY); Gpu.Renderer.SetUniform2F(GalConsts.FlipUniformName, SignX, SignY); return(Tags); }
public static FQuat ReadQuatFloat32NoW(this FArchive Ar) { const int XShift = 21; const int YShift = 10; const uint ZMask = 0x000003ff; const uint YMask = 0x001ffc00; const uint XMask = 0xffe00000; var packed = Ar.Read <uint>(); var unpackedX = packed >> XShift; var unpackedY = (packed & YMask) >> YShift; var unpackedZ = (packed & ZMask); var x = BitConverter.Int32BitsToSingle((int)(((((unpackedX >> 7) & 7) + 123) << 23) | ((unpackedX & 0x7F | 32 * (unpackedX & 0xFFFFFC00)) << 16))); var y = BitConverter.Int32BitsToSingle((int)(((((unpackedY >> 7) & 7) + 123) << 23) | ((unpackedY & 0x7F | 32 * (unpackedY & 0xFFFFFC00)) << 16))); var z = BitConverter.Int32BitsToSingle((int)(((((unpackedZ >> 6) & 7) + 123) << 23) | ((unpackedZ & 0x3F | 32 * (unpackedZ & 0xFFFFFE00)) << 17))); var wSquared = 1.0f - x * x - y * y - z * z; return(new FQuat(x, y, z, wSquared > 0.0f ? MathF.Sqrt(wSquared) : 0.0f)); }
/// <summary> /// Sets the constant values required by the FSR RCAS shader on the provided command buffer /// /// Logic ported from "FsrRcasCon()" in Runtime/PostProcessing/Shaders/ffx/ffx_fsr1.hlsl /// For a more user-friendly version of this function, see SetRcasConstantsLinear(). /// </summary> /// <param name="cmd">Command buffer to modify</param> /// <param name="sharpnessStops">The scale is {0.0 := maximum, to N>0, where N is the number of stops(halving) of the reduction of sharpness</param> public static void SetRcasConstants(CommandBuffer cmd, float sharpnessStops = kDefaultSharpnessStops) { // Transform from stops to linear value. float sharpnessLinear = Mathf.Pow(2.0f, -sharpnessStops); Vector4 constants; uint sharpnessAsHalf = Mathf.FloatToHalf(sharpnessLinear); int packedSharpness = (int)(sharpnessAsHalf | (sharpnessAsHalf << 16)); float packedSharpnessAsFloat = BitConverter.Int32BitsToSingle(packedSharpness); constants.x = sharpnessLinear; constants.y = packedSharpnessAsFloat; // Fill the last constant with zeros to avoid using uninitialized memory constants.z = 0.0f; constants.w = 0.0f; cmd.SetGlobalVector(ShaderConstants._FsrRcasConstants, constants); }
public override string ToString() { var opcodeString = EnumUtils.GetDescription(OpCode); switch (GetOperandKind(OpCode)) { case OperandKind.None: return(opcodeString); case OperandKind.BrTableOperand: return($"{opcodeString} {string.Join("-", uIntArrayOperand!.Select(x => $"0x{x:X}"))} 0x{operand:x}"); case OperandKind.BlockType: return($"{opcodeString} {(ValueKind)operand}"); case OperandKind.LabelIndex: case OperandKind.FuncIndex: case OperandKind.IndirectCallTypeIndex: return($"{opcodeString} 0x{operand:X}"); case OperandKind.MemArg: return($"{opcodeString} 0x{operand & 0xFFFFFFFF00000000:X} 0x{operand & 0xFFFFFFFF:X}"); case OperandKind.Zero: case OperandKind.LocalIndex: case OperandKind.GlobalIndex: case OperandKind.ImmediateI32: case OperandKind.ImmediateI64: return($"{opcodeString} {operand}"); case OperandKind.ImmediateF32: return($"{opcodeString} {BitConverter.Int32BitsToSingle((int)operand)}"); case OperandKind.ImmediateF64: return($"{opcodeString} {BitConverter.Int64BitsToDouble((long)operand)}"); default: throw new ArgumentOutOfRangeException(); } }
public Single ReadSingle(Int64 position) { int sizeOfType = sizeof(Single); EnsureSafeToRead(position, sizeOfType); Single result; unsafe { byte *pointer = null; RuntimeHelpers.PrepareConstrainedRegions(); try { _buffer.AcquirePointer(ref pointer); pointer += (_offset + position); #if ALIGN_ACCESS // check if pointer is aligned if (((int)pointer & (sizeOfType - 1)) == 0) { #endif result = BitConverter.Int32BitsToSingle(*((int *)(pointer))); #if ALIGN_ACCESS } else { UInt32 tempResult = (UInt32)(*pointer | *(pointer + 1) << 8 | *(pointer + 2) << 16 | *(pointer + 3) << 24); result = *((float *)&tempResult); } #endif } finally { if (pointer != null) { _buffer.ReleasePointer(); } } } return(result); }
public static float HalfToSingle(int value) { int mantissa = (value >> 0) & 0x3ff; int exponent = (value >> 10) & 0x1f; int sign = (value >> 15) & 0x1; if (exponent == 0x1f) { // NaN or Infinity. mantissa <<= 13; exponent = 0xff; } else if (exponent != 0 || mantissa != 0) { if (exponent == 0) { // Denormal. int e = -1; int m = mantissa; do { e++; m <<= 1; }while ((m & 0x400) == 0); mantissa = m & 0x3ff; exponent = e; } mantissa <<= 13; exponent = 127 - 15 + exponent; } int output = (sign << 31) | (exponent << 23) | mantissa; return(BitConverter.Int32BitsToSingle(output)); }
/// <summary> /// Reads a <see cref="float"/> value from an array of bytes. /// </summary> /// <param name="bytes">The array of bytes to read from.</param> /// <param name="position">The position in the array where the value should be read.</param> /// <param name="endianness">The endianness.</param> /// <returns>The value.</returns> public static float ReadFloat(this byte[] bytes, int position, Endianness endianness) { Debug.Assert(bytes != null && position >= 0 && bytes.Length >= position + SizeOfFloat); int value; unchecked { value = endianness.IsBigEndian() ? bytes[position] << 24 | bytes[position + 1] << 16 | bytes[position + 2] << 8 | bytes[position + 3] : bytes[position] | bytes[position + 1] << 8 | bytes[position + 2] << 16 | bytes[position + 3] << 24; } #if NETSTANDARD2_0 return(BitConverter.ToSingle(BitConverter.GetBytes(value), 0)); #else // this is essentially an unsafe *((float*)&value) return(BitConverter.Int32BitsToSingle(value)); #endif }
public static float ConvertHalfToSingle(ushort x) { uint x_sign = (uint)(x >> 15) & 0x0001; uint x_exp = (uint)(x >> 10) & 0x001F; uint x_mantissa = (uint)x & 0x03FF; if (x_exp == 0 && x_mantissa == 0) { // Zero return(BitConverter.Int32BitsToSingle((int)(x_sign << 31))); } if (x_exp == 0x1F) { // NaN or Infinity return(BitConverter.Int32BitsToSingle((int)((x_sign << 31) | 0x7F800000 | (x_mantissa << 13)))); } int exponent = (int)x_exp - 15; if (x_exp == 0) { // Denormal x_mantissa <<= 1; while ((x_mantissa & 0x0400) == 0) { x_mantissa <<= 1; exponent--; } x_mantissa &= 0x03FF; } uint new_exp = (uint)((exponent + 127) & 0xFF) << 23; return(BitConverter.Int32BitsToSingle((int)((x_sign << 31) | new_exp | (x_mantissa << 13)))); }
public void Step1() { var num = new[] { 0.001f, 0.01f, 0.0625f, 0.1f, 0.25f, 0.5f, 1f, 2f, 4f, 8f, 10f, 16f, 100f, 1000f }; foreach (var n in num) { var c1 = 0x5f3759df; var c2 = 1597488759; var ans = 1.0f / Math.Sqrt(n); // Console.WriteLine(FloatToString(n)); // Console.WriteLine(FloatToString(1.0f / (float)Math.Sqrt(n))); var i = BitConverter.SingleToInt32Bits(n); var sum1 = c1 - (i >> 1); var y1 = BitConverter.Int32BitsToSingle(sum1); var sum2 = c2 - (i >> 1); var y2 = BitConverter.Int32BitsToSingle(sum2); var diff = Math.Abs(ans - y1) < Math.Abs(ans - y2) ? "<" : ">"; Console.WriteLine("{0}, {1}, {2}", ans - y1, ans - y2, diff); } }
private void CheckOneSingle(string s, uint expectedBits) { CheckOneSingle(s, BitConverter.Int32BitsToSingle((int)(expectedBits))); }
private float ReadRegisterFloat(NvGpuEngine3dReg Reg) { return(BitConverter.Int32BitsToSingle(ReadRegister(Reg))); }
public static float ReplacementInt32ToSingleBits(this int value) => BitConverter.Int32BitsToSingle(value);
public float ReadFloat() { return(BitConverter.Int32BitsToSingle(this.ReadInt())); }
public static float ReadFloatRaw <TInput>(ref Reader <TInput> reader) => BitConverter.Int32BitsToSingle(reader.ReadInt32());
public virtual unsafe float ReadSingle() => BitConverter.Int32BitsToSingle(BinaryPrimitives.ReadInt32LittleEndian(InternalRead(4)));
private static ShaderIrOperImmf Immf32_20(this long OpCode) { return(new ShaderIrOperImmf(BitConverter.Int32BitsToSingle((int)(OpCode >> 20)))); }
public float AsFloat() { return BitConverter.Int32BitsToSingle((int)Value); }
public static float ReadSingleLittleEndian(ReadOnlySpan <byte> source) { return(!BitConverter.IsLittleEndian ? BitConverter.Int32BitsToSingle(ReverseEndianness(MemoryMarshal.Read <int>(source))) : MemoryMarshal.Read <float>(source)); }