From 85be827bb0a34c89a051711d7760cb151c0bc0f6 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 18 Mar 2026 10:16:37 -1000 Subject: [PATCH] =?UTF-8?q?Use=20union=20instead=20of=20memcpy=20in=20floa?= =?UTF-8?q?t=5Fto=5Fraw=20=E2=80=94=20xtensa-gcc=20doesn't=20optimize=20me?= =?UTF-8?q?mcpy=20in=20all=20contexts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- esphome/components/api/proto.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index c05ce78f43a..5a49ad2d37c 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -27,9 +27,12 @@ constexpr uint8_t WIRE_TYPE_MASK = 0b111; // Mask to extract wire type // Reinterpret float bits as uint32_t without floating-point comparison. // Used by both encode_float() and calc_float() to ensure identical zero checks. inline uint32_t float_to_raw(float value) { - uint32_t raw; - memcpy(&raw, &value, sizeof(raw)); - return raw; + union { + float f; + uint32_t u; + } v; + v.f = value; + return v.u; } // Helper functions for ZigZag encoding/decoding