From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtpout2.vodafonemail.de (smtpout2.vodafonemail.de [145.253.239.133]) by sourceware.org (Postfix) with ESMTPS id E343C385383F for ; Thu, 5 Aug 2021 07:32:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E343C385383F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nexgo.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nexgo.de Received: from smtp.vodafone.de (smtpa05.fra-mediabeam.com [10.2.0.36]) by smtpout2.vodafonemail.de (Postfix) with ESMTP id CBB77125350 for ; Thu, 5 Aug 2021 09:32:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nexgo.de; s=vfde-smtpout-mb-15sep; t=1628148750; bh=syPGqZh7dQpcNGzpulu1In/hUvz40DIlIPCFtzQCvvc=; h=From:To:Subject:Date; b=KUSF9zaA6amElejWhA1S1G9ymAwgnsjfqyJN61mqZb5GQPlJPKUqQjPLa6bCdP6Y6 GKkGMQk+NaM9fQCMUiVRdik1ZSwLlKZRy2awUioXY4luLlhmXsTKG3W/UlEJJDEJlp 9t/4EzVmisIbee4g9bUMREejK5ZbRGsnijwuukbw= Received: from H270 (p5b38f1bc.dip0.t-ipconnect.de [91.56.241.188]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by smtp.vodafone.de (Postfix) with ESMTPSA id 8CB92140192 for ; Thu, 5 Aug 2021 07:32:30 +0000 (UTC) Message-ID: <0532D36E954348E28A2B22812297A5F2@H270> From: "Stefan Kanthak" To: Subject: Suboptimal code generated for __buitlin_ceil on AMD64 without SS4_4.1 Date: Thu, 5 Aug 2021 09:31:28 +0200 Organization: Me, myself & IT MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Windows Mail 6.0.6002.18197 X-MimeOLE: Produced By Microsoft MimeOLE V6.1.7601.24158 X-purgate-type: clean X-purgate-Ad: Categorized by eleven eXpurgate (R) http://www.eleven.de X-purgate: This mail is considered clean (visit http://www.eleven.de for further information) X-purgate: clean X-purgate-size: 3800 X-purgate-ID: 155817::1628148750-00004EF9-02F97426/0/0 X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_NUMSUBJECT, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 05 Aug 2021 07:32:35 -0000 Hi, targeting AMD64 alias x86_64 with -O3, GCC 10.2.0 generates the following code (17 instructions using 78 bytes, plus 6 quadwords using 48 bytes) for __builtin_ceil() when -msse4.1 is NOT given: .text 0: f2 0f 10 15 10 00 00 00 movsd .LC1(%rip), %xmm2 4: R_X86_64_PC32 .rdata 8: f2 0f 10 25 00 00 00 00 movsd .LC0(%rip), %xmm4 c: R_X86_64_PC32 .rdata 10: 66 0f 28 d8 movapd %xmm0, %xmm3 14: 66 0f 28 c8 movapd %xmm0, %xmm1 18: 66 0f 54 da andpd %xmm2, %xmm3 1c: 66 0f 2e e3 ucomisd %xmm3, %xmm4 20: 76 2b jbe 4d <_ceil+0x4d> 22: f2 48 0f 2c c0 cvttsd2si %xmm0, %rax 27: 66 0f ef db pxor %xmm3, %xmm3 2b: f2 0f 10 25 20 00 00 00 movsd 0x20(%rip), %xmm4 2f: R_X86_64_PC32 .rdata 33: 66 0f 55 d1 andnpd %xmm1, %xmm2 37: f2 48 0f 2a d8 cvtsi2sd %rax, %xmm3 3c: f2 0f c2 c3 06 cmpnlesd %xmm3, %xmm0 41: 66 0f 54 c4 andpd %xmm4, %xmm0 45: f2 0f 58 c3 addsd %xmm3, %xmm0 49: 66 0f 56 c2 orpd %xmm2, %xmm0 4d: c3 retq .rdata .align 8 0: 00 00 00 00 .LC0: .quad 0x1.0p52 00 00 30 43 00 00 00 00 00 00 00 00 .align 16 10: ff ff ff ff .LC1: .quad ~(-0.0) ff ff ff 7f 18: 00 00 00 00 .quad 0.0 00 00 00 00 .align 8 20: 00 00 00 00 .LC2: .quad 0x1.0p0 00 00 f0 3f 00 00 00 00 00 00 00 00 .end JFTR: in the best case, the memory accesses cost several cycles, while in the worst case they yield a page fault! Properly optimized, faster and shorter code, using just 15 instructions in 65 bytes, WITHOUT superfluous constants, thus avoiding costly memory accesses and saving at least 32 bytes, follows: .intel_syntax .equ BIAS, 1023 .text 0: f2 48 0f 2c c0 cvttsd2si rax, xmm0 # rax = trunc(argument) 5: 48 f7 d8 neg rax # jz .L0 # argument zero? 8: 70 36 jo .L0 # argument indefinite? # argument overflows 64-bit integer? a: 48 f7 d8 neg rax d: f2 48 0f 2a c8 cvtsi2sd xmm1, rax # xmm1 = trunc(argument) 12: 48 a1 00 00 00 mov rax, BIAS << 52 19: 00 00 00 f0 3f 1c: 66 48 0f 6e d0 movq xmm2, rax # xmm2 = 0x1.0p0 21: f2 0f 10 d8 movsd xmm3, xmm0 # xmm3 = argument 25: f2 0f c2 d9 02 cmplesd xmm3, xmm1 # xmm3 = (argument <= trunc(argument)) ? ~0L : 0L 2a: 66 0f 55 da andnpd xmm3, xmm2 # xmm3 = (argument <= trunc(argument)) ? 0.0 : 1.0 2e: f2 0f 58 d9 addsd xmm3, xmm1 # xmm3 = (argument > trunc(argument)) ? 1.0 : 0.0 # + trunc(argument) # = ceil(argument) 32: 66 0f 73 d0 3f psrlq xmm0, 63 37: 66 0f 73 f0 3f psllq xmm0, 63 # xmm0 = (argument & -0.0) ? -0.0 : 0.0 3c: 66 0f 56 c3 orpd xmm0, xmm3 # xmm0 = ceil(argument) 40: c3 .L0: ret .end regards Stefan