FastLED 3.10.9
Loading...
Searching...
No Matches

◆ classify_array()

ArrayType fl::anonymous_namespace{json.cpp.hpp}::classify_array ( const json_array & arr)

Definition at line 729 of file json.cpp.hpp.

729 {
730 if (arr.empty()) return GENERIC_ARRAY;
731
732 bool all_numeric = true;
735 bool has_float = false;
736 bool has_float_beyond_precision = false;
737
738 for (const auto& elem : arr) {
739 if (!elem) {
740 all_numeric = false;
741 break;
742 }
743
744 if (elem->is_int()) {
745 auto val = elem->as_int();
746 if (val) {
747 i64 v = *val;
748 min_val = (v < min_val) ? v : min_val;
749 max_val = (v > max_val) ? v : max_val;
750 } else {
751 all_numeric = false;
752 break;
753 }
754 } else if (elem->is_float()) {
755 has_float = true;
756 auto val = elem->as_float();
757 if (!val) {
758 all_numeric = false;
759 break;
760 }
761 // Check if float is beyond integer precision (>2^24 or <-2^24).
762 // Integer biased-exp comparison -- no FP arithmetic (#3022 phase 2).
763 const u32 fbits = fl::bit_cast<u32>(*val);
765 has_float_beyond_precision = true;
766 }
767 } else {
768 all_numeric = false;
769 break;
770 }
771 }
772
773 if (!all_numeric) return GENERIC_ARRAY;
774
775 // Classification
776 if (has_float) {
777 // If floats are beyond integer precision, don't optimize
778 if (has_float_beyond_precision) {
779 return GENERIC_ARRAY;
780 }
781 return ALL_FLOATS;
782 }
783
784 // Integer arrays
785 if (min_val >= 0 && max_val <= 255) {
786 return ALL_UINT8;
787 }
788
789 if (min_val >= -32768 && max_val <= 32767) {
790 return ALL_INT16;
791 }
792
793 // Large integers (>32767 or <-32768) that don't fit in int16 but can be represented as float
794 // Convert to float if within float's integer precision range (+/-2^24)
795 if (min_val >= -16777216 && max_val <= 16777216) {
796 return ALL_FLOATS;
797 }
798
799 return GENERIC_ARRAY;
800}
bool empty() const FL_NO_EXCEPT
static bool float_bits_magnitude_exceeds_2_24(u32 bits) FL_NO_EXCEPT
Definition json.cpp.hpp:55
fl::i64 i64
Definition s16x16x4.h:222
To bit_cast(const From &from) FL_NO_EXCEPT
Definition bit_cast.h:48
static constexpr T min() FL_NO_EXCEPT
Definition limits.h:107
static constexpr T max() FL_NO_EXCEPT
Definition limits.h:108

References ALL_FLOATS, ALL_INT16, ALL_UINT8, fl::bit_cast(), classify_array(), fl::FL_NO_EXCEPT, fl::float_bits_magnitude_exceeds_2_24(), GENERIC_ARRAY, fl::numeric_limits< T >::max(), and fl::numeric_limits< T >::min().

Referenced by classify_array(), and optimize_array().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: