From patchwork Tue Sep 13 16:01:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?R=C3=A9mi_Denis-Courmont?= X-Patchwork-Id: 37903 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1c:b0:96:9ee8:5cfd with SMTP id c28csp787428pzh; Tue, 13 Sep 2022 09:01:27 -0700 (PDT) X-Google-Smtp-Source: AA6agR7GgczHbuRhVGKfCzief3Jx2UeU43J8JfSyusNRVTq1maioNzVv0580IxmMeOML93YTR1AW X-Received: by 2002:a17:907:d07:b0:72e:ec79:ad0f with SMTP id gn7-20020a1709070d0700b0072eec79ad0fmr22965456ejc.296.1663084887633; Tue, 13 Sep 2022 09:01:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663084887; cv=none; d=google.com; s=arc-20160816; b=X8Mccp9mhAF1XnjnwQlLbeUyLyXgh5HZcLKdK3JnBNiEA3vkcMKDUUxA2VgmmbYQyJ +itNXD951X3Y8n3w34zIEPXr4vaWpiSwU9KL6SUtk0Qaw88mwRE74tzn+X+aQprhBoDF JR2/I58C+S0aw8czeVlp0Wif3mV+ImO7n6yPkV/O23nzNtQKKq2rcV0L+J/yMo3k2ajN qKD7XRS5tmdxI/oGirwUvjQwdutOSbqVYn9uYtOay0RF5F7YH0RxPxKSuTkR65zj14FU yeFwlOp2lAgA9bmIX+7/KUViTFmpnpi0HlQPpb7ZO3uhc11HxYvtAld46rcPAP4KUzdU eneQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :delivered-to; bh=9s/K59FNo0PtRTUZCWO5tMWyOFibp8yHVM7scG1nI/8=; b=0hX8Alfcn2hZ1hd2p+maW4xR/XoQrtN6qu+ZzQpJnOGANr90+zISpq/OvSbafZ/ZhE nGQXJNUZ9rEBoG4/ZKrltTwEiWajq8KizJa/85IFe5Z/3bi8MdsKHsTmVG2r++E1qEsi PIzkBZJyYRsidazejvxRM5e9jQa2/b4PguXvgnLK2A/iMNncxKvIr75UPMFl/5ajnr/D QO/Ac41d6yOEKtN9D3vNf0h66DLdgM1ABDIFLJiiJkUO8jX6+Jg2T0wv5I/vzzx048S9 NyrxFVolgmTOhtbJvbx7/joMKEpqI+lVk9z9gl06gBSpOicc4brbtdQlPXIAecl8DUp1 qxCw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id e3-20020a17090658c300b00779a07b4e90si12058434ejs.711.2022.09.13.09.01.26; Tue, 13 Sep 2022 09:01:27 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8EC4C68BA4F; Tue, 13 Sep 2022 19:01:23 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from ursule.remlab.net (vps-a2bccee9.vps.ovh.net [51.75.19.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 18FA368BA4F for ; Tue, 13 Sep 2022 19:01:17 +0300 (EEST) Received: from basile.remlab.net (localhost [IPv6:::1]) by ursule.remlab.net (Postfix) with ESMTP id 9DD69C0012 for ; Tue, 13 Sep 2022 19:01:16 +0300 (EEST) From: remi@remlab.net To: ffmpeg-devel@ffmpeg.org Date: Tue, 13 Sep 2022 19:01:16 +0300 Message-Id: <20220913160116.15158-1-remi@remlab.net> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] lavc/audiodsp: fix aliasing violation X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: MqAMpflS7dhi From: RĂ©mi Denis-Courmont Even though they have the same size, and typically the same alignment, uint32_t and float are under no circumstances compatible types in C. The casts from float * to uint32_t * are invalid here. Insofar as the resulting pointers are dereferenced, this is undefined behaviour. This patch replaces the invalid cast with explicit type aliasing via union. Fortunately, compilers are able to optimise the "conversion" away and skip floating pointer registers here. --- libavcodec/audiodsp.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/libavcodec/audiodsp.c b/libavcodec/audiodsp.c index ff43e87dce..1781ad8bc4 100644 --- a/libavcodec/audiodsp.c +++ b/libavcodec/audiodsp.c @@ -22,6 +22,18 @@ #include "libavutil/common.h" #include "audiodsp.h" +static inline uint32_t pf2u(const float *f) +{ + union { uint32_t u; float f; } u = { .f = *f }; + return u.u; +} + +static inline float u2f(uint_fast32_t x) +{ + union { uint32_t u; float f; } u = { .u = x }; + return u.f; +} + static inline uint32_t clipf_c_one(uint32_t a, uint32_t mini, uint32_t maxi, uint32_t maxisign) { @@ -37,21 +49,19 @@ static void vector_clipf_c_opposite_sign(float *dst, const float *src, float *min, float *max, int len) { int i; - uint32_t mini = *(uint32_t *) min; - uint32_t maxi = *(uint32_t *) max; + uint32_t mini = pf2u(min); + uint32_t maxi = pf2u(max); uint32_t maxisign = maxi ^ (1U << 31); - uint32_t *dsti = (uint32_t *) dst; - const uint32_t *srci = (const uint32_t *) src; for (i = 0; i < len; i += 8) { - dsti[i + 0] = clipf_c_one(srci[i + 0], mini, maxi, maxisign); - dsti[i + 1] = clipf_c_one(srci[i + 1], mini, maxi, maxisign); - dsti[i + 2] = clipf_c_one(srci[i + 2], mini, maxi, maxisign); - dsti[i + 3] = clipf_c_one(srci[i + 3], mini, maxi, maxisign); - dsti[i + 4] = clipf_c_one(srci[i + 4], mini, maxi, maxisign); - dsti[i + 5] = clipf_c_one(srci[i + 5], mini, maxi, maxisign); - dsti[i + 6] = clipf_c_one(srci[i + 6], mini, maxi, maxisign); - dsti[i + 7] = clipf_c_one(srci[i + 7], mini, maxi, maxisign); + dst[i + 0] = u2f(clipf_c_one(pf2u(src + i + 0), mini, maxi, maxisign)); + dst[i + 1] = u2f(clipf_c_one(pf2u(src + i + 1), mini, maxi, maxisign)); + dst[i + 2] = u2f(clipf_c_one(pf2u(src + i + 2), mini, maxi, maxisign)); + dst[i + 3] = u2f(clipf_c_one(pf2u(src + i + 3), mini, maxi, maxisign)); + dst[i + 4] = u2f(clipf_c_one(pf2u(src + i + 4), mini, maxi, maxisign)); + dst[i + 5] = u2f(clipf_c_one(pf2u(src + i + 5), mini, maxi, maxisign)); + dst[i + 6] = u2f(clipf_c_one(pf2u(src + i + 6), mini, maxi, maxisign)); + dst[i + 7] = u2f(clipf_c_one(pf2u(src + i + 7), mini, maxi, maxisign)); } }