FastLED 3.10.8
Loading...
Searching...
No Matches

◆ classify_array()

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

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

672 {
673 if (arr.empty()) return GENERIC_ARRAY;
674
675 bool all_numeric = true;
678 bool has_float = false;
679 bool has_float_beyond_precision = false;
680
681 for (const auto& elem : arr) {
682 if (!elem) {
683 all_numeric = false;
684 break;
685 }
686
687 if (elem->is_int()) {
688 auto val = elem->as_int();
689 if (val) {
690 i64 v = *val;
691 min_val = (v < min_val) ? v : min_val;
692 max_val = (v > max_val) ? v : max_val;
693 } else {
694 all_numeric = false;
695 break;
696 }
697 } else if (elem->is_float()) {
698 has_float = true;
699 auto val = elem->as_float();
700 if (!val) {
701 all_numeric = false;
702 break;
703 }
704 // Check if float is beyond integer precision (>2^24 or <-2^24).
705 // Integer biased-exp comparison -- no FP arithmetic (#3022 phase 2).
706 const u32 fbits = fl::bit_cast<u32>(*val);
708 has_float_beyond_precision = true;
709 }
710 } else {
711 all_numeric = false;
712 break;
713 }
714 }
715
716 if (!all_numeric) return GENERIC_ARRAY;
717
718 // Classification
719 if (has_float) {
720 // If floats are beyond integer precision, don't optimize
721 if (has_float_beyond_precision) {
722 return GENERIC_ARRAY;
723 }
724 return ALL_FLOATS;
725 }
726
727 // Integer arrays
728 if (min_val >= 0 && max_val <= 255) {
729 return ALL_UINT8;
730 }
731
732 if (min_val >= -32768 && max_val <= 32767) {
733 return ALL_INT16;
734 }
735
736 // Large integers (>32767 or <-32768) that don't fit in int16 but can be represented as float
737 // Convert to float if within float's integer precision range (+/-2^24)
738 if (min_val >= -16777216 && max_val <= 16777216) {
739 return ALL_FLOATS;
740 }
741
742 return GENERIC_ARRAY;
743}
bool empty() const FL_NO_EXCEPT
static bool float_bits_magnitude_exceeds_2_24(u32 bits) FL_NO_EXCEPT
Definition json.cpp.hpp:56
To bit_cast(const From &from) FL_NO_EXCEPT
Definition bit_cast.h:48
fl::i64 i64
Definition stdint.h:221
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: