Example #1
0
        /// <summary>
        /// Sorts the input float array based on each element's IEEE 754 bit representation.
        /// </summary>
        public void Sort(Span <float> span)
        {
            for (int i = 0; i < 32; i += 4)
            {
                bool hasRemain = false;

                for (int j = 0; j < span.Length; j++)
                {
                    uint number = Scalars.SingleToUInt32Bits(span[j]);

                    uint remain = number >> i;
                    uint digit  = remain & 0b1111;

                    buckets[digit].Add(number);
                    hasRemain |= remain != 0;
                }

                if (i == 28)                 //The last pass requires a special copy to support negative numbers
                {
                    int index = 0;           //The filling index targeting array
                    int half  = buckets.Length / 2;

                    for (int j = buckets.Length - 1; j >= half; j--)
                    {
                        List <uint> bucket = buckets[j];

                        for (int k = bucket.Count - 1; k >= 0; k--)
                        {
                            span[index++] = Scalars.UInt32ToSingleBits(bucket[k]);
                        }
                    }

                    for (int j = 0; j < half; j++)
                    {
                        List <uint> bucket = buckets[j];

                        for (int k = 0; k < bucket.Count; k++)
                        {
                            span[index++] = Scalars.UInt32ToSingleBits(bucket[k]);
                        }
                    }
                }
                else
                {
                    Copy(span, uintToFloatConverter);
                }

                Clear();

                if (!hasRemain)
                {
                    break;
                }
            }
        }