private unsafe void RetroAudioSampleBatch(Int16 *data, uint frames) { soundBuffer = new Sample(); soundBuffer.data = (IntPtr)data; soundBuffer.frames = frames; return; }
/// <summary> /// minimum of all elements of array A /// </summary> /// <param name="A">n-dim array</param> /// <returns><para>scalar minimum of all elements of A.</para> /// <para>If A is empty, an empty array will be returned.</para></returns> /// <exception cref="ILNumerics.Exceptions.ILArgumentException">if A was null.</exception> /// <seealso cref="ILNumerics.BuiltInFunctions.ILMath.min(ILArray<double>)"/> public static ILArray <Int16> minall(ILArray <Int16> A) { if (object.Equals(A, null)) { throw new ILArgumentException("minall: argument must not be null!"); } if (A.IsEmpty) { return(ILArray <Int16> .empty(A.Dimensions)); } Int16 retArr = Int16.MaxValue; unsafe { fixed(Int16 *inArrStart = A.m_data) { Int16 *inArrWalk = inArrStart; Int16 *inArrEnd = inArrStart + A.Dimensions.NumberOfElements; while (inArrWalk < inArrEnd) { if (retArr > *inArrWalk) { retArr = *inArrWalk; } inArrWalk++; } } } return(new ILArray <Int16> (new Int16 [1] { retArr }, 1, 1)); }
/// <summary>Converts the given byte array into a <see cref="Int16"/></summary> /// <param name="Data">The byte array to convert</param> /// <returns>Converts the given byte array into a <see cref="Int16"/></returns> public static Int16 ToInt16(Byte[] Data) { Int16 Out; if (Data == null) { throw new ArgumentNullException(nameof(Data)); } if (Data.Length < sizeof(Int16)) { throw new ArgumentException($"{nameof(Data)} is not of length 4 atleast"); } #if UNSAFE unsafe { fixed(Byte *s = Data) { Int16 *d = &Out; *d = *(Int16 *)s; } } #else Out = Data[0]; Out |= (Int16)(Data[1] << 8); #endif return(Out); }
/// <summary> /// convert numerical ILArray to single precision floating point precision /// </summary> /// <param name="X">numeric input array</param> /// <returns><![CDATA[ILArray<float>]]> of same size as X having all elements converted to /// single precision floating point format.</returns> /// <remarks>All overloads of this function will return a solid physical copy /// of the input array X.</remarks> public static ILArray <float> single(ILArray <Int16> X) { int nrX = X.m_dimensions.NumberOfElements; float [] retArr = new float [nrX]; ILArray <float> ret = new ILArray <float> (retArr, X.m_dimensions); if (X.IsReference) { for (int i = 0; i < nrX; i++) { retArr[i] = (float)X.GetValue(i); } } else { unsafe { fixed(float *outArr = ret.m_data) fixed(Int16 * inArr = X.m_data) { float *lastElementAfter = outArr + nrX; float *outArrP = outArr; Int16 *inArrP = inArr; while (outArrP < lastElementAfter) { *outArrP++ = (float)*inArrP++; } } } } return(ret); }
public unsafe static void MoveImage(Int32 dx, Int32 dy, Int16 *p) { if (SFX.currentEffectID == 149) { return; } if (SFX.currentEffectID == 274) { PSXTextureMgr.isCaptureSS = true; return; } PSXTextureMgr.ClearKey(dx, dy); Int16 num = *p; Int16 num2 = p[1]; Int16 num3 = p[2]; Int16 num4 = p[3]; for (Int32 i = 0; i < (Int32)num4; i++) { Int32 num5 = (dy + i) * 1024 + dx; Int32 num6 = ((Int32)num2 + i) * 1024 + (Int32)num; for (Int32 j = 0; j < (Int32)num3; j++) { PSXTextureMgr.originalVram[num5] = PSXTextureMgr.originalVram[num6]; num5++; num6++; } } }
unsafe internal static AudioFrame GetAudioFrame(DataReader reader) { var numBytes = reader.UnconsumedBufferLength; var headerSize = 44; var bytes = new byte[headerSize]; reader.ReadBytes(bytes); var numSamples = (uint)(numBytes - headerSize); AudioFrame frame = new AudioFrame(numSamples); using (var buffer = frame.LockBuffer(AudioBufferAccessMode.Write)) using (IMemoryBufferReference reference = buffer.CreateReference()) { byte *dataInBytes; uint capacityInBytes; ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacityInBytes); Int16 *dataInInt16 = (Int16 *)dataInBytes; for (int i = 0; i < capacityInBytes / sizeof(Int16); i++) { dataInInt16[i] = reader.ReadInt16(); } } return(frame); }
/// <summary> /// sum all elements of array A /// </summary> /// <param name="A">n-dim array</param> /// <returns><para>scalar sum of all elements of A.</para> /// <para>If A is empty, an empty array will be returned.</para></returns> /// <exception cref="ILNumerics.Exceptions.ILArgumentException">if A was null.</exception> /// <seealso cref="ILNumerics.BuiltInFunctions.ILMath.sum(ILArray<double>)"/> public static ILArray<Int16> sumall ( ILArray<Int16> A) { if (object.Equals(A,null)) throw new ILArgumentException("sumall: argument must not be null!"); if (A.IsEmpty) { return ILArray<Int16> .empty(0,0); } Int16 retArr = 0 ; if (A.m_indexOffset == null) { unsafe { fixed ( Int16 * inArrStart = A.m_data) { Int16 * inArrWalk = inArrStart; Int16 * inArrEnd = inArrStart + A.Dimensions.NumberOfElements; while (inArrWalk < inArrEnd) { retArr += *inArrWalk++; } } } } else { unsafe { fixed ( Int16 * inArrStart = A.m_data) { for (int i = A.Dimensions.NumberOfElements; i -->0;) { retArr += *(inArrStart + A.getBaseIndex(i)); } } } } return new ILArray<Int16> (new Int16 [1]{retArr},1,1); }
/// <summary> /// Escribe un valor entero en la posicion dada /// </summary> /// <param name="position"></param> /// <param name="value"></param> public unsafe void Write(int position, Int16 value) { Trace.Assert(position >= 0); Int16 *ptr = (Int16 *)(ptrMemAccessor + position); *ptr = value; }
/// <summary> /// convert numerical ILArray to single precision floating point precision /// </summary> /// <param name="X">numeric input array</param> /// <returns><![CDATA[ILArray<float>]]> of same size as X having all elements converted to /// single precision floating point format.</returns> /// <remarks>All overloads of this function will return a solid physical copy /// of the input array X.</remarks> public static ILArray <float> single(ILArray <Int16> X) { int nrX = X.m_dimensions.NumberOfElements; float [] retArr = new float [nrX]; ILArray <float> ret = new ILArray <float> (retArr, X.m_dimensions); unsafe { fixed(float *outArr = ret.m_data) fixed(Int16 * inArr = X.m_data) { float *lastElementAfter = outArr + nrX; float *outArrP = outArr; Int16 *inArrP = inArr; while (outArrP < lastElementAfter) { *outArrP++ = (float)*inArrP++; } } } return(ret); }
public void Read(Int16 *p, Int32 min, Int32 max) { var token = _GetNumber(false); var value = Int16.Parse(token); Runtime.RTCheckRange(value, min, max); *p = value; }
/// <summary> /// Kopiert die Werte einzeln via *ptr++. /// </summary> /// <param name="Source"></param> /// <param name="Target"></param> /// <param name="Length"></param> unsafe static public void CopyInt16s(Int16 *Source, Int16 *Target, int Length) { for (int i = 0; i < Length; i++) { *Target = *Source; // Source++; Target++; } }
/// <summary> /// Liefert den Wert von der angegebenen Stelle im Byte-Array. /// </summary> /// <param name="Array"></param> /// <param name="Offset"></param> unsafe static public Int16 CopyInt16(this byte [] Array, Int32 Offset) { fixed(byte *ptr = Array) { Int16 *vpInt16 = (Int16 *)(ptr + Offset); return(*vpInt16); } }
/// <summary> /// Liefert den Wert (und neuen Offset) von der angegebenen Stelle im Byte-Array. /// </summary> /// <param name="Array"></param> /// <param name="Offset">Position, ab der gelesen wird.</param> unsafe static public (Int16, Int32) CopyInt16Offset(this byte [] Array, Int32 Offset) { fixed(byte *ptr = Array) { Int16 *vpInt16 = (Int16 *)(ptr + Offset); return(*vpInt16, Offset + sizeof(Int16)); } }
private unsafe void ReceiveSamples_sync() { int status = 0; while (_isStreaming) { uint cur_len, new_len; lock (syncLock) { cur_len = new_len = _readLength; } if (_iqBuffer == null || _iqBuffer.Length != cur_len) { _iqBuffer = UnsafeBuffer.Create((int)cur_len, sizeof(Complex)); _iqPtr = (Complex *)_iqBuffer; } if (_samplesBuffer == null || _samplesBuffer.Length != (2 * cur_len)) { _samplesBuffer = UnsafeBuffer.Create((int)(2 * cur_len), sizeof(Int16)); _samplesPtr = (Int16 *)_samplesBuffer; } if ((status = NativeMethods.bladerf_sync_config(_dev, bladerf_module.BLADERF_MODULE_RX, bladerf_format.BLADERF_FORMAT_SC16_Q11, NumBuffers, cur_len, NumBuffers / 2, SampleTimeoutMs)) != 0) { _isStreaming = false; } while (status == 0 && cur_len == new_len) { try { status = NativeMethods.bladerf_sync_rx(_dev, _samplesPtr, cur_len, IntPtr.Zero, SampleTimeoutMs); if (status != 0) { throw new ApplicationException(String.Format("bladerf_rx() error. {0}", NativeMethods.bladerf_strerror(status))); } var ptrIq = _iqPtr; var ptrSample = _samplesPtr; for (int i = 0; i < cur_len; i++) { ptrIq->Imag = _lutPtr[*ptrSample & 0x0fff]; ptrSample++; ptrIq->Real = _lutPtr[*ptrSample & 0x0fff]; ptrSample++; ptrIq++; } ComplexSamplesAvailable(_iqPtr, _iqBuffer.Length); } catch { break; } lock (syncLock) { new_len = _readLength; } } } }
/// <summary> /// Kopiert den Wert an die angegebene Stelle in das Byte-Array. /// </summary> /// <param name="Array"></param> /// <param name="Offset">Position, ab der geschrieben wird. Erhöht sich um die Anzhal der gelesenen Bytes!</param> /// <param name="Value"></param> unsafe static public void CopyInt16(this byte [] Array, ref Int32 Offset, Int16 Value) { fixed(byte *ptr = Array) { Int16 *vp = (Int16 *)(ptr + Offset); *vp = Value; Offset += sizeof(Int16); } }
// Experiment to write short to int widening conversion in pure C# code vs z80 assembly // TODO: This requires conv.i2 to be implemented in the AOT/CodeGenerator private unsafe static int Widen(short s) { Int32 retval; Int16 *ptr = (Int16 *)&retval; *ptr++ = (short)(s & 0x7F); *ptr = (short)(s & 0x80); return(retval); }
private unsafe void ScaleInt16(IntPtr data, ulong size, double volume) { Int16 *sample = (Int16 *)data; for (ulong i = 0; i < size; i++) { *sample = ClampInt16(*sample * volume); sample++; } }
/// <summary> /// Liefert die Bytes ab der Stelle <paramref name="Offset"/> als Wert. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="Array"></param> /// <param name="Offset">Position, ab der gelesen wird. Erhöht sich um die Anzhal der gelesenen Bytes!</param> /// <returns></returns> unsafe static public dynamic CopyValue <T> (this byte [] Array, ref Int32 Offset) where T : unmanaged { try { fixed(byte *ptr = Array) { switch (Type.GetTypeCode(typeof(T))) { case TypeCode.SByte: SByte *vpSByte = (SByte *)(ptr + Offset); return(*vpSByte); case TypeCode.Byte: Byte *vpByte = (Byte *)(ptr + Offset); return(*vpByte); case TypeCode.Int16: Int16 *vpInt16 = (Int16 *)(ptr + Offset); return(*vpInt16); case TypeCode.UInt16: UInt16 *vpUInt16 = (UInt16 *)(ptr + Offset); return(*vpUInt16); case TypeCode.Int32: Int32 *vpInt32 = (Int32 *)(ptr + Offset); return(*vpInt32); case TypeCode.UInt32: UInt32 *vpUInt32 = (UInt32 *)(ptr + Offset); return(*vpUInt32); case TypeCode.Int64: Int64 *vpInt64 = (Int64 *)(ptr + Offset); return(*vpInt64); case TypeCode.UInt64: UInt64 *vpUInt64 = (UInt64 *)(ptr + Offset); return(*vpUInt64); case TypeCode.Char: Char *vpChar = (Char *)(ptr + Offset); return(*vpChar); case TypeCode.Boolean: Boolean *vpBoolean = (Boolean *)(ptr + Offset); return(*vpBoolean); } } } finally { Offset += sizeof(T); } throw new Exception("Not implemented!"); }
public override unsafe void Encode(void *Output, void *Input, int Count, int ChannelId) { Int16 *pOutput = (Int16 *)Output + ChannelId; float *pInput = (float *)Input; for (int n = 0; n < Count; n++) { *pOutput = (Int16)((*pInput++) * (float)(Int16.MaxValue - 1)); pOutput += _ChannelCount; } }
/// <summary> /// maximum for all elements of A /// </summary> /// <param name="A">n-dim array</param> /// <returns><para>scalar maximum of all elements for A.</para> /// <para>If A is empty, an empty array will be returned.</para></returns> /// <exception cref="ILNumerics.Exceptions.ILArgumentException">if A was null.</exception> /// <seealso cref="ILNumerics.BuiltInFunctions.ILMath.max(ILArray<double>)"/> public static ILArray <Int16> maxall(ILArray <Int16> A) { if (object.Equals(A, null)) { throw new ILArgumentException("maxall: argument must not be null!"); } if (A.IsEmpty) { return(ILArray <Int16> .empty(0, 0)); } Int16 retArr = Int16.MinValue; if (A.m_indexOffset == null) { unsafe { fixed(Int16 *inArrStart = A.m_data) { Int16 *inArrWalk = inArrStart; Int16 *inArrEnd = inArrStart + A.Dimensions.NumberOfElements; while (inArrWalk < inArrEnd) { if (retArr < *inArrWalk) { retArr = *inArrWalk; } inArrWalk++; } } } } else { Int16 tmp; unsafe { fixed(Int16 *inArrStart = A.m_data) { for (int i = A.Dimensions.NumberOfElements; i-- > 0;) { tmp = *(inArrStart + A.getBaseIndex(i)); if (retArr < tmp) { retArr = tmp; } } } } } return(new ILArray <Int16> (new Int16 [1] { retArr }, 1, 1)); }
/// <summary> /// Kopiert den Wert an die angegebene Stelle in das Byte-Array und liefert den neuen Offset zurück. /// </summary> /// <param name="Array"></param> /// <param name="Offset"></param> /// <param name="Value"></param> /// <returns>Liefert den neuen Offset.</returns> unsafe static public Int32 CopyInt16(this byte [] Array, Int32 Offset, Int16 Value) { fixed(byte *ptr = Array) { Int16 *vp = (Int16 *)(ptr + Offset); *vp = Value; } return(Offset + sizeof(Int16)); }
/// <summary> /// Serves as a hash function for this byte array using system algorithm provided by Microsoft.NET 4 platform. /// <para>NOTE that the <c>GetHashCode</c> of a byte array DOES NOT hash the content. Use this method instead.</para> /// </summary> /// <param name="bytes">A byte array.</param> /// <returns>A 32-bit signed integer hash code.</returns> public unsafe static int SystemHash(this byte[] bytes) { fixed(byte *dataPtr = bytes) { int length = bytes.Length; int num = 0x15051505; int num2 = num; int *numPtr = (int *)dataPtr; for (int i = length; i > 0; i -= 8) { if (i == 1) { byte *numPtr8 = (byte *)numPtr; num = (((num << 5) + num) + (num >> 0x1b)) ^ ((((int)numPtr8[0] << 24) & 0x0000FFFF)); break; } else if (i == 2) { Int16 *numPtr16 = (Int16 *)numPtr; num = (((num << 5) + num) + (num >> 0x1b)) ^ ((((int)numPtr16[0] << 16) & 0x0000FFFF)); break; } else if (i == 3) { Int16 *numPtr16 = (Int16 *)numPtr; byte * numPtr8 = (byte *)numPtr; num = (((num << 5) + num) + (num >> 0x1b)) ^ ((((int)numPtr16[0] << 16) | ((int)numPtr8[2] << 8))); } else if (i == 4) { num = (((num << 5) + num) + (num >> 0x1b)) ^ numPtr[0]; break; } else { num = (((num << 5) + num) + (num >> 0x1b)) ^ numPtr[0]; } if (i == 5 || i == 6) { num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ ((int)(numPtr[1] & 0x0000FFFF)); } else { num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr[1]; } numPtr += 2; } return(num + (num2 * 0x5d588b65)); } }
public static void GetBytes(Int16 primitive, byte[] bytes, int offset = 0) { unsafe { fixed(byte *ptr = &bytes[offset]) { Int16 *primitivePtr = (Int16 *)ptr; *primitivePtr = primitive; } } }
public static Int16 ToInt16(byte[] bytes, int offset = 0) { unsafe { fixed(byte *ptr = &bytes[offset]) { Int16 *primitivePtr = (Int16 *)ptr; return(*primitivePtr); } } }
public static void GetBytes(Int16 primitive, byte[] bytes, ref int offset) { unsafe { fixed(byte *ptr = &bytes[offset]) { offset += sizeof(Int16); Int16 *primitivePtr = (Int16 *)ptr; *primitivePtr = primitive; } } }
public static Int16 ToInt16(byte[] bytes, ref int offset) { unsafe { fixed(byte *ptr = &bytes[offset]) { offset += sizeof(Int16); Int16 *primitivePtr = (Int16 *)ptr; return(*primitivePtr); } } }
/// <summary> /// Liefert den Wert von der angegebenen Stelle im Byte-Array. /// </summary> /// <param name="Array"></param> /// <param name="Offset">Position, ab der geschrieben wird. Erhöht sich um die Anzhal der gelesenen Bytes!</param> unsafe static public Int16 CopyInt16(this byte [] Array, ref Int32 Offset) { try { fixed(byte *ptr = Array) { Int16 *vpInt16 = (Int16 *)(ptr + Offset); return(*vpInt16); } } finally { Offset += sizeof(Int16); } }
/// <summary> /// invert elements of A, return result as out argument /// </summary> /// <param name="A"></param> /// <param name="outArray"></param> public static void invert(ILArray <Int16> A, ILArray <Int16> outArray) { #region array + array ILDimension inDim = A.Dimensions; if (!inDim.IsSameSize(outArray.Dimensions)) { throw new ILDimensionMismatchException(); } int leadDim = 0, leadDimLen = inDim [0]; // walk along the longest dimension (for performance reasons) for (int i = 1; i < inDim.NumberOfDimensions; i++) { if (leadDimLen < inDim [i]) { leadDimLen = inDim [i]; leadDim = i; } } unsafe { fixed(Int16 *inA1 = A.m_data) fixed(Int16 * inA2 = outArray.m_data) { Int16 *pInA1 = inA1; Int16 *pInA2 = inA2; int c = 0; Int16 *outEnd = inA2 + outArray.m_data.Length; if (A.IsReference) { while (pInA2 < outEnd) //HC07 { *pInA2++ = (Int16)(*(pInA1 + A.getBaseIndex(c++)) * (-1)); } } else { while (pInA2 < outEnd) //HC11 { *pInA2++ = (Int16)(*pInA1++ /*HC:*/ *(-1)); } } } #endregion array + array } return; }
unsafe void GetTable(Int32 *p, int index, byte *rs) { Int16 *sp = (Int16 *)p; buff[index].rc = *sp; sp++; buff[index].type = *sp; p++; buff[index].size = *p; p++; Int32 offset = *p; rs += offset; buff[index].obj = GetObject(rs, (DataType)buff[index].type, buff[index].size); }
public unsafe void RunBasicScenario_LoadAligned() { TestLibrary.TestFramework.BeginScenario(nameof(RunBasicScenario_LoadAligned)); Int16 localData = (short)2; Int16 *ptr = &localData; var result = Sse2.Insert( Sse2.LoadAlignedVector128((Int16 *)(_dataTable.inArrayPtr)), *ptr, 1 ); Unsafe.Write(_dataTable.outArrayPtr, result); ValidateResult(_dataTable.inArrayPtr, *ptr, _dataTable.outArrayPtr); }
private unsafe void ReceiveSamples_sync() { int status = 0; while (_isStreaming) { uint cur_len, new_len; lock (syncLock) { cur_len = new_len = _readLength; } if (_iqBuffer == null || _iqBuffer.Length != cur_len) { _iqBuffer = UnsafeBuffer.Create((int)cur_len, sizeof(Complex)); _iqPtr = (Complex*)_iqBuffer; } if (_samplesBuffer == null || _samplesBuffer.Length != (2 * cur_len)) { _samplesBuffer = UnsafeBuffer.Create((int)(2 * cur_len), sizeof(Int16)); _samplesPtr = (Int16*)_samplesBuffer; } if ((status = NativeMethods.bladerf_sync_config(_dev, bladerf_module.BLADERF_MODULE_RX, bladerf_format.BLADERF_FORMAT_SC16_Q11, NumBuffers, cur_len, NumBuffers / 2, SampleTimeoutMs)) != 0) _isStreaming = false; while (status == 0 && cur_len == new_len) { try { status = NativeMethods.bladerf_sync_rx(_dev, _samplesPtr, cur_len, IntPtr.Zero, SampleTimeoutMs); if (status != 0) throw new ApplicationException(String.Format("bladerf_rx() error. {0}", NativeMethods.bladerf_strerror(status))); var ptrIq = _iqPtr; var ptrSample = _samplesPtr; for (int i = 0; i < cur_len; i++) { ptrIq->Imag = _lutPtr[*ptrSample & 0x0fff]; ptrSample++; ptrIq->Real = _lutPtr[*ptrSample & 0x0fff]; ptrSample++; ptrIq++; } ComplexSamplesAvailable(_iqPtr, _iqBuffer.Length); } catch { break; } lock (syncLock) { new_len = _readLength; } } } }