FastLED 3.9.15
Loading...
Searching...
No Matches

◆ classify_array()

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

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

660 {
661 if (arr.empty()) return GENERIC_ARRAY;
662
663 bool all_numeric = true;
666 bool has_float = false;
667 bool has_float_beyond_precision = false;
668
669 for (const auto& elem : arr) {
670 if (!elem) {
671 all_numeric = false;
672 break;
673 }
674
675 if (elem->is_int()) {
676 auto val = elem->as_int();
677 if (val) {
678 i64 v = *val;
679 min_val = (v < min_val) ? v : min_val;
680 max_val = (v > max_val) ? v : max_val;
681 } else {
682 all_numeric = false;
683 break;
684 }
685 } else if (elem->is_float()) {
686 has_float = true;
687 auto val = elem->as_float();
688 if (!val) {
689 all_numeric = false;
690 break;
691 }
692 // Check if float is beyond integer precision (>2^24 or <-2^24).
693 // Integer biased-exp comparison -- no FP arithmetic (#3022 phase 2).
694 const u32 fbits = fl::bit_cast<u32>(*val);
696 has_float_beyond_precision = true;
697 }
698 } else {
699 all_numeric = false;
700 break;
701 }
702 }
703
704 if (!all_numeric) return GENERIC_ARRAY;
705
706 // Classification
707 if (has_float) {
708 // If floats are beyond integer precision, don't optimize
709 if (has_float_beyond_precision) {
710 return GENERIC_ARRAY;
711 }
712 return ALL_FLOATS;
713 }
714
715 // Integer arrays
716 if (min_val >= 0 && max_val <= 255) {
717 return ALL_UINT8;
718 }
719
720 if (min_val >= -32768 && max_val <= 32767) {
721 return ALL_INT16;
722 }
723
724 // Large integers (>32767 or <-32768) that don't fit in int16 but can be represented as float
725 // Convert to float if within float's integer precision range (+/-2^24)
726 if (min_val >= -16777216 && max_val <= 16777216) {
727 return ALL_FLOATS;
728 }
729
730 return GENERIC_ARRAY;
731}
bool empty() const FL_NOEXCEPT
fl::i64 i64
Definition s16x16x4.h:222
To bit_cast(const From &from) FL_NOEXCEPT
Definition bit_cast.h:48
static bool float_bits_magnitude_exceeds_2_24(u32 bits) FL_NOEXCEPT
Definition json.cpp.hpp:56
static constexpr T min() FL_NOEXCEPT
Definition limits.h:107
static constexpr T max() FL_NOEXCEPT
Definition limits.h:108

References ALL_FLOATS, ALL_INT16, ALL_UINT8, fl::bit_cast(), classify_array(), fl::vector_basic::empty(), 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: