From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x229.google.com (mail-oi1-x229.google.com [IPv6:2607:f8b0:4864:20::229]) by sourceware.org (Postfix) with ESMTPS id 5E2E53858D32 for ; Tue, 26 Dec 2023 20:12:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5E2E53858D32 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5E2E53858D32 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::229 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703621532; cv=none; b=QorIB415ctIIbuXZHlEq9QKkZc6oSqV+JsJ0q0h26XojohYcHsTUffLGXBnwKoJpfRAAgSpH0f6zVUvDTM2enZR+QB+j8iCFcIo1kH7I5iIlIoUbA5GM7ePfwtyY3+J0zOjVfj2HUF7Mc3m1a8RiP4arLAl4Vp8fTVUSdt4EY28= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703621532; c=relaxed/simple; bh=Ep1+03OGeZMiOn6j0ourrIkG5g9yRJFquFuVQQM4ywQ=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=rW7FhYvpA6lISHvni18JSYZb7yzpMJ3tRROh9OxXuFIH8LKY8u/Xq58BBzcZ+cCw150zHDc0rkNDveYyJWvuJ0SNCOWM1cT6cPCR693xztgHLt5UlcJ/aOfcM5cCYfNjT0WiqxOpnIPLq0kek9Zy3nw0vMo1LJy4tBsUcBOGalU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oi1-x229.google.com with SMTP id 5614622812f47-3bbbf5a59b7so392537b6e.3 for ; Tue, 26 Dec 2023 12:12:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1703621529; x=1704226329; darn=sourceware.org; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=FaBkjHlzvKIIA4c/ewZvqnTmLW7/qXNKHdGKGk1hDG0=; b=gYoXH7wboGRZZboLGpDhTuq0TdgZI6X4X2xpizQdnaGQiU3TfCj/zpTlrPVhvrh8rZ Dxxd13GLgLkcC22JHFRevwi3p9qRh5GQe3qB4ceLwNuBIe/96s3iG5MKQnNiP+rA9WJj 5PJCk9HvamKNSfoXocw1Odb56e2ETEQJnLw5w86uMYRcbil6P/bvJXVUurdPlWqJt12i AI3jlRRQamGBQhDlMZ2o9TWpEkgIhgM6yAmrGcnv5/bcQpIzbPiQQi9LRk2zXsr5u+yY pwqJwbxwCbqQMfnc1X/3r99QzCpEY4GFe9wlQTurIX7ZeQkOfAr6UJucok5ELGUsJ8k6 pW7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703621529; x=1704226329; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=FaBkjHlzvKIIA4c/ewZvqnTmLW7/qXNKHdGKGk1hDG0=; b=QoNoqqBZMhrqcCt/idUarSH/AHaibqjiXlqDYms3v9gZbGNhC11hflLcyU+iulHGfU tJ9VcMhr74/Jrgi2vXCMBrtCbLKeRwJ6FaBIyQSeu5eowvnHmUPwgr4XzXrDUfBCa3/K 0/JTDsoUtJixjArMIcTEiFcPvnOaTWV6iqba3/06F0C8TwmeLT/EPe7cVlf4Z4eVIXe7 NGBhGaIV991F20tavMOHIioAn0pZwrHatgOUvo4L/EFvu6ooucVRYPxgjuyrj9w3QTBM L05AbYjPcrX+Vrm95z6tE1VKcwCCe3cWu+kSk49xiaGUvP+H/ki7CLcqXx30KvmF8xVc SkWg== X-Gm-Message-State: AOJu0Ywf9lIzgICPD2ctHsCEjtXvYFma4dK4rmbu4tODJLz1JzmxfOL0 CFhkpm9rJC5DGvH5xDf86mxuvnjvJ3vj8t3duLFwnImO8zY= X-Google-Smtp-Source: AGHT+IGjW1lP5THSjkGXAGTQXku9zXUITLTpI9NJab1jOzQXRKA8Kki264DGb7fogvSNx07+/yBKyQ== X-Received: by 2002:a05:6359:2c43:b0:174:c5fb:7f1a with SMTP id qv3-20020a0563592c4300b00174c5fb7f1amr3369431rwb.52.1703621529194; Tue, 26 Dec 2023 12:12:09 -0800 (PST) Received: from ?IPV6:2804:1b3:a7c0:8192:d80:8251:3d98:1794? ([2804:1b3:a7c0:8192:d80:8251:3d98:1794]) by smtp.gmail.com with ESMTPSA id h6-20020a17090a2ec600b0028afdb88d08sm14441222pjs.23.2023.12.26.12.12.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 26 Dec 2023 12:12:08 -0800 (PST) Message-ID: <61ecc506-3796-49e1-a4f3-7a39807a1fc3@linaro.org> Date: Tue, 26 Dec 2023 17:12:04 -0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 2/2] MIPS: Hard-float rounding instructions support Content-Language: en-US To: Xi Ruoyao , Junxian Zhu Cc: libc-alpha@sourceware.org References: <20231225103548.1615-2-zhujunxian@oss.cipunited.com> <20231225103548.1615-4-zhujunxian@oss.cipunited.com> From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-5.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 26/12/23 05:29, Xi Ruoyao wrote: > On Tue, 2023-12-26 at 10:37 +0800, Junxian Zhu wrote: >> 在 2023/12/25 18:51, Xi Ruoyao 写道: >>> On Mon, 2023-12-25 at 18:35 +0800, Junxian Zhu wrote: >>> >>> /* snip */ >>> >>>> +/* >>>> + * ceil(x) >>>> + * Return x rounded toward -inf to integral value >>>> + * Method: >>>> + * Bit twiddling. >>>> + */ >>>> + >>>> +#if ((__mips_fpr == 64) && (__mips_hard_float == 1) && ((__mips == 32 && __mips_isa_rev > 1) || __mips == 64)) >>>> +#include >>>> +#include >>>> +#include >>>> + >>>> +ENTRY(__ceil) >>>> + .set push >>>> + .set noreorder >>>> + .set noat >>>> +# $f0=ret, $f12=double, a0=int64/int32_h, a1=int32_l, a2=sign, a3=exp >>>> +#if __mips == 64 >>>> + dmfc1   a0, $f12 # assign int64 >>>> +#else >>>> + mfhc1   a0, $f12 # assign int64 >>>> +#endif >>>> + cfc1    t0, $f26 >>>> + ceil.l.d    $f0, $f12 >>> No, C23 does not allow this function to raise an INEXACT exception, but >>> ceil.l.d will do so. >>> >>> Such optimizations should be performed in GCC which can be controlled by >>> the programmer with -std=c23 and/or -f[no-]fp-int-builtin-inexact, not >>> in Glibc where we cannot know if the programmer wants to deviate from >>> C23. >> >> The cfc1 instruction will backup float point exception status before >> running ceil.l.d, and the following ctc1 will restore float point >> exception status to avoid INEXACT exception raised by ceil.l.d. It's the >> same way like what have been done in s_ceil.S for i386. > > Still incorrect because when the Enable field of FCSR contains INEXACT a > SIGFPE will be immediately delivered and there is no way to recover. A > demonstration: > > #define _GNU_SOURCE > #include > #include > > int main() > { > printf("%d\n", feenableexcept(FE_INEXACT)); > > double data = 114.514; > long control; > asm("cfc1\t%1,$f26\n\t" > "ceil.l.d\t%0,%0\n\t" > "cvt.d.l\t%0,%0\n\t" > "ctc1\t%1,$f26": "+f"(data), "=r"(control)); > printf("%.15f\n", data); > return 0; > } > > On i386 the fnstenv instruction also masks out all the FP exceptions so > this is not a problem. See commit 26b0bf96000a. > And we lack proper tests for ceil, floor, round and trunc similar to test-nearbyint-except2.c to check if the implementation does not wrongly raise inexact floating point exception. Also, I see no point in implementing this optimizations with assembly where a C implementation would be way simpler and generate similar code. Similar to what I did for powerpc with sysdeps/powerpc/fpu/round_to_integer.h, I implemented a similar approach for MIPS [1]. The resulting code should be similar to the assembly implementation, taking in consideration the correct fix to save/restore floating-point exceptions. I did see no math regression on cfarm23 with a glibc built with -mabi=64 -mips64r2. As a side note, it seems that x86 long double implementation are not fully c23 conformant (as they are wrongly raising inexact exception if the exception is already set). [1] https://sourceware.org/git/?p=glibc.git;a=shortlog;h=refs/heads/azanella/mips-hw-fp-round