public static void ToHex(ReadOnlySpan <byte> data, Span <char> into) { CoreException.Assert(into.Length >= data.Length * 2); for (int i = 0; i < data.Length; i++) { into[i * 2] = s_valueToHex[data[i] >> 4]; into[i * 2 + 1] = s_valueToHex[data[i] & 0b1111];
public void AppendFormat(ReadOnlySpan <char> format, double value, IFormatProvider provider) { bool ok = value.TryFormat(m_remaining, out int written, format: format, provider: provider); CoreException.Assert(ok); m_remaining = m_remaining.Slice(written); }
private void RunItem(object ob) { // yay; perform work var item = (IScheduleItem <TItem, TArg>)ob; item.Execute(m_runArgument); var completed = Interlocked.Increment(ref m_completedItemsCount); var idx = Array.IndexOf(m_items, item); lock (m_itemStatus) { // work completed might mean some other can now run CoreException.Assert(m_itemStatus[idx] == ItemStatus.Running); m_itemStatus[idx] = ItemStatus.Done; } if (completed == m_items.Length) { // ALL DONE! m_onCompleted(m_onCompletedArgument); return; } FireReadyJobs(); }
public static void FatalAssert(bool isOk, string message = "") { if (isOk == false) { CoreException.Throw(message); } }
/// <summary> /// Enqueue a list of actions to be performed concurrently; calls continuation(continuationArgument) when all completed /// </summary> public static void ForEachAction(string name, ReadOnlySpan <Action <object> > works, object argument, Action <object> continuation, object continuationArgument) { CoreException.Assert(s_workers != null, "JobService not initialized"); int numJobs = works.Length; if (numJobs == 0) { return; } if (numJobs == 1) { lock (s_instances) EnqueueInternal(name, works[0], argument); return; } var completion = JobCompletion.Acquire(); completion.ContinuationAtCount = numJobs; completion.Continuation = continuation; completion.ContinuationArgument = continuationArgument; #if DEBUG completion.ContinuationName = name + "Contd"; #endif lock (s_instances) { foreach (var work in works) { EnqueueInternal(name, work, argument, completion); } } }
public int EmitNodsIndex; // where in m_nodEmits to start looking public DynamicOrder OrderAgainst(object other) { var ot = (ItemInfo)other; var aOrder = Item.ScheduleAgainst(ot.Item); var bOrder = ot.Item.ScheduleAgainst(Item); aOrder = ScheduleOrderUtil.ResolveOrder(aOrder, bOrder, out var conflict); if (conflict) { CoreException.Throw("Bad dynamic order"); } if (aOrder == ScheduleOrder.RunBefore) { return(DynamicOrder.RequireABeforeB); } if (aOrder == ScheduleOrder.RunAfter) { return(DynamicOrder.RequireBBeforeA); } // ok, soft order by hint var diff = (int)Item.Hint - (int)ot.Item.Hint; if (diff > 0) { return(DynamicOrder.PreferBBeforeA); } if (diff < 0) { return(DynamicOrder.PreferABeforeB); } return(DynamicOrder.AnyOrder); }
public void Append(int value) { bool ok = value.TryFormat(m_remaining, out int written); CoreException.Assert(ok); m_remaining = m_remaining.Slice(written); }
public void AppendFormat(ReadOnlySpan <char> format, byte value) { bool ok = value.TryFormat(m_remaining, out int written, format: format); CoreException.Assert(ok); m_remaining = m_remaining.Slice(written); }
public static float NextFloat(ref ulong state) { uint v = (NextUInt32(ref state) & 0b01111111_11111111_11111111u) | 0x3F800000u; float zeroToOne = SingleUIntUnion.ReinterpretCast(v) - 1.0f; CoreException.Assert(zeroToOne >= 0.0f && zeroToOne < 1.0); return(zeroToOne); }
internal static void EnqueueInternal(string name, Action <object> work, object argument, JobCompletion completion = null) { #if DEBUG CoreException.Assert(Monitor.IsEntered(s_instances)); #endif int idx = (s_nextInstance + s_instancesCount) & k_instancesMask; ref var job = ref s_instances[idx];
private void VerifyIndex(int index) { #if DEBUG if (index < 0 || index >= m_exactSize) { CoreException.Throw("Index out of range"); } #endif }
public static double NextDouble(ref ulong state) { // generate 52 random mantissa bits and an unbiased exponent of 0 ulong dval = (NextUInt64(ref state) & 0xFFFFFFFFFFFFFUL) | 0x3FF0000000000000UL; double zeroToOne = DoubleULongUnion.ReinterpretCast(dval) - 1.0; CoreException.Assert(zeroToOne >= 0.0f && zeroToOne < 1.0); return(zeroToOne); }
public void AddEntry(string name, long started, int duration) { if (duration < 0) { CoreException.Assert(duration >= 0, "AddEntry negative duration"); return; } // no need for interlocked; this is a thread local object ref var complete = ref m_completed[m_completedCount++];
public static void CheckNaN(Vector2 vector) { CoreException.Assert( float.IsNaN(vector.X) == false && float.IsNaN(vector.Y) == false && float.IsInfinity(vector.X) == false && float.IsInfinity(vector.Y) == false ); }
public static double NextDouble(ref ulong state, double minValue, double maxValue) { CoreException.Assert(maxValue > minValue); var dval = (NextUInt64(ref state) & 0xFFFFFFFFFFFFFUL) | 0x3FF0000000000000UL; double zeroToOne = DoubleULongUnion.ReinterpretCast(dval) - 1.0; double retval = minValue + ((maxValue - minValue) * zeroToOne); CoreException.Assert(retval >= minValue && retval < maxValue); return(retval); }
public static double NextDouble(ref ulong state) { DoubleULongUnion union; union.DoubleValue = 0; union.ULongValue = (NextUInt64(ref state) & 0xFFFFFFFFFFFFFUL) | 0x3FF0000000000000UL; double zeroToOne = union.DoubleValue - 1.0; CoreException.Assert(zeroToOne >= 0.0f && zeroToOne < 1.0); return(zeroToOne); }
public static byte[] FromHexArray(ReadOnlySpan <char> hexString) { if (hexString.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) { hexString = hexString.Slice(2); } var bytes = new byte[hexString.Length / 2]; int len = FromHexArray(hexString, bytes); CoreException.Assert(len == bytes.Length); return(bytes); }
public void Dispose() { long now = Stopwatch.GetTimestamp(); CoreException.Assert(now >= Started); long duration = now - Started; if (duration > 0 && duration < int.MaxValue) { TimingThread.Instance.AddEntry(Name, Started, (int)duration); } }
public static float FasterSin(float radians) { CoreException.Assert(radians >= -MathUtils.Pi && radians <= MathUtils.Pi); if (radians < 0) { return(radians * (1.27323954f + 0.405284735f * radians)); } else { return(radians * (1.27323954f - 0.405284735f * radians)); } }
public void Remove(int index, int count) { int curLen = this.Length; int tail = curLen - (index + count); CoreException.Assert(tail >= 0); m_remaining = m_buffer.Slice(curLen - count); if (tail == 0) { return; } m_buffer.Slice(index + count, tail).CopyTo(m_buffer.Slice(index, tail)); }
public static double NextDouble(ref ulong state, double minValue, double maxValue) { CoreException.Assert(maxValue > minValue); DoubleULongUnion union; union.DoubleValue = 0; union.ULongValue = (NextUInt64(ref state) & 0xFFFFFFFFFFFFFUL) | 0x3FF0000000000000UL; double zeroToOne = union.DoubleValue - 1.0; double retval = minValue + ((maxValue - minValue) * zeroToOne); CoreException.Assert(retval >= minValue && retval < maxValue); return(retval); }
public bool Equals(ReadOnlySpan <char> text) { CoreException.Assert(text.Length == 4); if (text.Length < 4) { return(false); } Span <char> arr = stackalloc char[4]; ToString(arr); return(arr.SequenceEqual(text)); }
private void Compact() { CoreException.Assert(m_offset > 0, "Unnecessary compact!"); var buffer = m_buffer; var off = m_offset; var cnt = m_count; // TODO: compare to aliased Span.CopyTo for (int i = 0; i < cnt; i++) { buffer[i] = buffer[off + i]; } m_offset = 0; }
public void WaitAndRelease(int completionCount) { CoreException.Assert(Continuation == null, "Can't use WaitAndRelease while also using a continuation"); for (; ;) { var comp = Volatile.Read(ref m_numCompleted); if (comp >= completionCount) { break; } Thread.Sleep(0); } JobCompletion.Release(this); }
public static JobCompletion Acquire() { lock (s_free) { if (s_free.TryPop(out var retval)) { retval.ResetForReuse(); return(retval); } #if DEBUG s_totalNumCreated++; CoreException.Assert(s_totalNumCreated < 64, "Unlikely high number of in-flight completions; leaking Completion objects?"); #endif return(new JobCompletion()); } }
public void AddEntry(string name, long started, int duration) { if (duration < 0) { CoreException.Assert(duration >= 0, "AddEntry negative duration"); return; } int ccnt = m_completedCount; if (ccnt > 0 && m_completed[ccnt - 1].Start == started) { started++; // push one tick to make better hierarchies } // no need for interlocked; this is a thread local object ref var complete = ref m_completed[ccnt];
public static int FromHexArray(ReadOnlySpan <char> hexString, Span <byte> into) { if (hexString.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) { hexString = hexString.Slice(2); } CoreException.Assert(into.Length >= hexString.Length / 2); int len = 0; for (int i = 0; i < hexString.Length; i += 2) { var value = HexCharToInteger(hexString[i]) << 4 | (HexCharToInteger(hexString[i + 1])); into[len] = (byte)value; len++; } return(len); }
public override JsonConverter CreateConverter( Type typeToConvert, JsonSerializerOptions options) { CoreException.Assert(typeToConvert.IsGenericType && typeToConvert.GetGenericTypeDefinition() == typeof(FastList <>)); Type elementType = typeToConvert.GetGenericArguments()[0]; JsonConverter converter = (JsonConverter)Activator.CreateInstance( typeof(JsonConverterForFastListOfT <>) .MakeGenericType(new Type[] { elementType }), BindingFlags.Instance | BindingFlags.Public, binder: null, args: null, culture: null) !; return(converter); }
public DynamicScheduler(ReadOnlySpan <TItem> scheduleItems) { var items = scheduleItems.ToArray(); m_itemStatus = new ItemStatus[items.Length]; m_order = new ScheduleOrder[items.Length * items.Length]; Array.Sort(items, (a, b) => { int aval = (int)a.Hint; int bval = (int)b.Hint; if (aval > bval) { return(1); } if (aval < bval) { return(-1); } return(0); }); // create matrices for (int a = 0; a < items.Length; a++) { for (int b = 0; b < items.Length; b++) { if (a == b) { continue; } var order = items[a].ScheduleAgainst(items[b]); var antiorder = items[b].ScheduleAgainst(items[a]); var resolvedOrder = ResolveOrder(order, antiorder, out bool conflict); if (conflict) { CoreException.Throw("Clashing schedule; {items[a].Item.ToString()} and {items[b].Item.ToString()} both wants to run " + order); } m_order[a * items.Length + b] = resolvedOrder; } } m_items = items; }
public static void Assert(bool isOk, string message = "") { if (isOk == false) { #if DEBUG if (Debugger.IsAttached) { Debugger.Break(); } else { CoreException.Throw(message); } #else CoreException.Throw(message); #endif } }