From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 622F638515F0 for ; Fri, 4 Jun 2021 18:44:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 622F638515F0 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622832279; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Ci4lyDvY19A6SkvEjsfXRmSHK9iMlyeYlfjVE5Kc258=; b=Kfq8iNZGtelHwD7ACDeMuAIWIM+c8lvcjNnKQcQqtVoiqNo195R9r1fWLQq/UcXXt7AJKr aQuH5gDzTRwqLsQGblCu4o9yKChLRMER7gqCUaErDtNs6zW3uuoHxtvhev3t4WoyaCEmR8 WrNwpfgmpDN5FL98dVHNR3vCMnLJ1VQ= Received: from mail-pf1-f200.google.com (mail-pf1-f200.google.com [209.85.210.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-435-JOKNpla-OL6sTwYaiDIAmg-1; Fri, 04 Jun 2021 14:44:37 -0400 X-MC-Unique: JOKNpla-OL6sTwYaiDIAmg-1 Received: by mail-pf1-f200.google.com with SMTP id j206-20020a6280d70000b02902e9e02e1654so5853135pfd.6 for ; Fri, 04 Jun 2021 11:44:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=JPz8rMGDO/UPLJeZdXMGWiTu0gbM7UTM4H4OPwPllTU=; b=rdJxxt2PZ/tht0/4e6nNCz50Ks6OuzzZNGfFPwzEM2OXNV709Myx/LhekvETBBUuK5 KAqZZ0dCA5fhbID7nbVFXpfd7iZma11v56cpjXKwkQ0fzn9uch8/LtDOLbs9f5f3mQyO cIkqiR6vbEcaMTH+eEmIm1mziQb7kVJcShmuSyWTDqH98TJtZurlPrSI5q4v6lntOQBQ i/JS5oIvT0jNOTpMXnD8lKd8+0OTovoaYKPtS2F9GDARsfC+SFtPVrdoKvk0QANzX8gv APj0RKAjoj3XcpeEJLnfZwzLg3NVPZxZQFPvxwnsZuA/KGwc2DwrP8Rp6XbihEhiz2xz ERBQ== X-Gm-Message-State: AOAM532oOjC++ZlELyb8foKIqYZtKFzRd3DqEyIC0N/IJvSIC78fZmvD Y+CejjaXUNGRShaWlvk0oFDq0/oZJmDK0kn+N2if2Wytf5u84nLPK0kr1mF/D560+YLt/FdZ0TE YV66/Cwd7Y3Wdl7dNRL+0IWLF4Xk6HBs= X-Received: by 2002:a17:902:8484:b029:101:7016:fb7b with SMTP id c4-20020a1709028484b02901017016fb7bmr5529797plo.23.1622832275968; Fri, 04 Jun 2021 11:44:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJywQlXljQNlZkkMod7GDS4ueSg/HkvWQKAKbs9irOAepQHL37NwcqsXq05vGO5j9BywVF3+MQ1hFrUlZF5jQlI= X-Received: by 2002:a17:902:8484:b029:101:7016:fb7b with SMTP id c4-20020a1709028484b02901017016fb7bmr5529774plo.23.1622832275696; Fri, 04 Jun 2021 11:44:35 -0700 (PDT) MIME-Version: 1.0 References: <2f8796f4-f164-5734-16ca-9a392e788beb@gmail.com> In-Reply-To: From: Jeff Johnston Date: Fri, 4 Jun 2021 14:44:24 -0400 Message-ID: Subject: Re: incorrectly rounded square root To: Paul Zimmermann Cc: joel@rtems.org, Newlib Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jjohnstn@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-4.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, HTML_MESSAGE, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Newlib mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Jun 2021 18:44:49 -0000 Ok, I now know exactly what is happening. The compiler is optimizing out the rounding check in ef_sqrt.c, probably due to the operation using two constants. 86 ix += (m <<23); (gdb) list 81 else 82 q += (q&1); When I debug, it always does the else at line 81 without performing the one-tiny operation. The difference in the mxcsr register is the PE bit which I believe gets set when you do the one-tiny operation. Since we aren't doing it, it never gets set on and the difference of 0x20 in the mxcsr register is explained. By making the constants volatile, I am able to get the code working as it should. I have pushed a patch for this. -- Jeff J. On Fri, Jun 4, 2021 at 3:14 AM Paul Zimmermann wrote: > Hi Jeff, > > > I figured the values were off when I had to hard-code them in my own > > test_sqrt.c but forgot to include that info in my note. > > > > Now, that said, using the code I attached earlier, I am seeing the exact > > values you are quoting above for glibc for the mxcsr register and the > round > > is working. Have your > > tried running that code? > > yes it works as expected, but it doesn't work with Newlib's fenv.h and > libm.a > (see below). > > > The mxcsr values you are seeing that are different are not due to the > > fesetround code. The code is shifting the round value 13 bits > > and for 3, that ends up being 0x6000. It is masking mxcsr with > 0xffff9fff > > first so when you start with 0x1fxx and end up with 0x7fxx, the code is > > doing what is supposed to do. > > The difference in values above is 0x20 (e.g. 0x7fa0 vs 0x7f80) which is a > > bit in the last 2 hex digits which isn't touched by the code logic. > > here is how to reproduce the issue: > > tar xf newlib-4.1.0.tar.gz > cd newlib-4.1.0 > mkdir build > cd build > ../configure --prefix=/tmp --disable-multilib --target=x86_64 > make -j4 > make install > > $ cat test_sqrt_2.c > #include > #include > #include > > #ifdef NEWLIB > /* RedHat's libm claims: > undefined reference to `__errno' in j1f/y1f */ > int errno; > int* __errno () { return &errno; } > #endif > > int main() > { > int rnd[4] = { FE_TONEAREST, FE_TOWARDZERO, FE_UPWARD, FE_DOWNWARD }; > char Rnd[4] = "NZUD"; > float x = 0x1.ff07fep+127f; > float y; > for (int i = 0; i < 4; i++) > { > unsigned short cw; > unsigned int mxcsr = 0; > fesetround (rnd[i]); > __asm__ volatile ("fnstcw %0" : "=m" (cw) : ); > __asm__ volatile ("stmxcsr %0" : "=m" (mxcsr) : ); > y = sqrtf (x); > printf ("RND%c: %a cw=%u mxcsr=%u\n", Rnd[i], y, cw, mxcsr); > } > } > > With GNU libc: > $ gcc -fno-builtin test_sqrt_2.c -lm > $ ./a.out > RNDN: 0x1.ff83fp+63 cw=895 mxcsr=8064 > RNDZ: 0x1.ff83eep+63 cw=3967 mxcsr=32672 > RNDU: 0x1.ff83fp+63 cw=2943 mxcsr=24480 > RNDD: 0x1.ff83eep+63 cw=1919 mxcsr=16288 > > With Newlib: > $ gcc -I/tmp/x86_64/include -DNEWLIB -fno-builtin test_sqrt_2.c /tmp/libm.a > $ ./a.out > RNDN: 0x1.ff83fp+63 cw=895 mxcsr=8064 > RNDZ: 0x1.ff83fp+63 cw=3967 mxcsr=32640 > RNDU: 0x1.ff83fp+63 cw=2943 mxcsr=24448 > RNDD: 0x1.ff83fp+63 cw=1919 mxcsr=16256 > > Can you reproduce that on x86_64 Linux? > > Paul > >