all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Arsen Arsenović" <arsen@aarsen.me>
To: Eli Zaretskii <eliz@gnu.org>
Cc: Po Lu <luangruo@yahoo.com>,
	mattias.engdegard@gmail.com, vibhavp@gmail.com, rpluim@gmail.com,
	emacs-devel@gnu.org
Subject: Re: HAVE_FAST_UNALIGNED_ACCESS
Date: Sat, 01 Apr 2023 14:59:53 +0200	[thread overview]
Message-ID: <87lejbpxkm.fsf@aarsen.me> (raw)
In-Reply-To: <83bkk7zvxl.fsf@gnu.org>


[-- Attachment #1.1: Type: text/plain, Size: 733 bytes --]


Eli Zaretskii <eliz@gnu.org> writes:

> I'm still unconvinced, and I said already what will have a chance of
> convincing me: a specific report about a problem this particular code
> causes on a specific existing platform we support in Emacs 29 and with
> a specific compiler.

Similar (but not exactly the same) loops as this one have been shown to
generate incorrect code in this thread.  It's not a large leap for it to
happen to this one, introducing subtle errors for a bit of code that is
completely unnecessary (as demonstrated by it being optional),
especially at higher optimization levels, where the compiler could
easily produce better code than the assumption of a 'mov' would.

Is the following trivial enough for 29?


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: [PATCH] Remove aliasing violation in Fstring_lessp --]
[-- Type: text/x-patch, Size: 1226 bytes --]

From 96d75e78358d6c2643bfb7cc65744b8a6178c9d2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Arsen=20Arsenovi=C4=87?= <arsen@aarsen.me>
Date: Sat, 1 Apr 2023 14:25:12 +0200
Subject: [PATCH] Remove aliasing violation in Fstring_lessp

* src/fns.c (Fstring_lessp) <HAVE_FAST_UNALIGNED_ACCESS>: Remove
strict aliasing violation.
---
 src/fns.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/fns.c b/src/fns.c
index ff364c6..e3e11e2 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -499,10 +499,16 @@ DEFUN ("string-lessp", Fstring_lessp, Sstring_lessp, 2, 2, 0,
 	  /* First compare entire machine words.  */
 	  typedef size_t word_t;
 	  int ws = sizeof (word_t);
-	  const word_t *w1 = (const word_t *) SDATA (string1);
-	  const word_t *w2 = (const word_t *) SDATA (string2);
-	  while (b < nb - ws + 1 && w1[b / ws] == w2[b / ws])
-	    b += ws;
+	  while (b < nb - ws + 1)
+	    {
+	      word_t w1;
+	      word_t w2;
+	      memcpy (&w1, SDATA (string1) + b, sizeof (w1));
+	      memcpy (&w2, SDATA (string2) + b, sizeof (w2));
+	      if (w1 != w2)
+		break;
+	      b += ws;
+	    }
 	}
 
       /* Scan forward to the differing byte.  */
-- 
2.40.0


[-- Attachment #1.3: Type: text/plain, Size: 492 bytes --]


.. or something similar to it, assuming I made an error, which is likely
given the circumstances.  This does pass the testsuite, anyway.  It
should just expand deferences into explicit memcpys.

No actual memcpy calls are produced, and this is at least functional on
a superset of compilers, and I suspect replacing the whole thing with a
naive-looking while (*(w1++) != *(w2++)); loop would be even better (but
I can settle for that being too experimental).
-- 
Arsen Arsenović

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 251 bytes --]

  reply	other threads:[~2023-04-01 12:59 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-30  9:34 HAVE_FAST_UNALIGNED_ACCESS Robert Pluim
2023-03-30 10:26 ` HAVE_FAST_UNALIGNED_ACCESS Eli Zaretskii
2023-03-30 11:09   ` HAVE_FAST_UNALIGNED_ACCESS Sam James
2023-03-30 12:18   ` HAVE_FAST_UNALIGNED_ACCESS Arsen Arsenović
     [not found]     ` <87v8ihu3t8.fsf@yahoo.com>
2023-03-31  7:15       ` HAVE_FAST_UNALIGNED_ACCESS Robert Pluim
2023-03-31  7:45       ` HAVE_FAST_UNALIGNED_ACCESS Arsen Arsenović
2023-03-31 17:29     ` HAVE_FAST_UNALIGNED_ACCESS Mattias Engdegård
2023-03-31 20:13       ` HAVE_FAST_UNALIGNED_ACCESS Arsen Arsenović
2023-03-30 10:28 ` HAVE_FAST_UNALIGNED_ACCESS Mattias Engdegård
2023-03-30 11:38 ` HAVE_FAST_UNALIGNED_ACCESS Vibhav Pant
2023-03-31 16:57   ` HAVE_FAST_UNALIGNED_ACCESS Mattias Engdegård
2023-03-31 17:59     ` HAVE_FAST_UNALIGNED_ACCESS Eli Zaretskii
2023-03-31 18:03       ` HAVE_FAST_UNALIGNED_ACCESS Mattias Engdegård
2023-03-31 18:12         ` HAVE_FAST_UNALIGNED_ACCESS Eli Zaretskii
2023-04-01  0:45         ` HAVE_FAST_UNALIGNED_ACCESS Po Lu
2023-04-01  5:43           ` HAVE_FAST_UNALIGNED_ACCESS Eli Zaretskii
2023-04-01  6:31             ` HAVE_FAST_UNALIGNED_ACCESS Po Lu
2023-04-01  6:39               ` HAVE_FAST_UNALIGNED_ACCESS Eli Zaretskii
2023-04-01  7:42                 ` HAVE_FAST_UNALIGNED_ACCESS Mattias Engdegård
2023-04-01  8:19                   ` HAVE_FAST_UNALIGNED_ACCESS Eli Zaretskii
2023-04-01  9:17                     ` HAVE_FAST_UNALIGNED_ACCESS Po Lu
2023-04-01 11:25                       ` HAVE_FAST_UNALIGNED_ACCESS Eli Zaretskii
2023-04-01 12:59                         ` Arsen Arsenović [this message]
2023-04-01 13:33                           ` HAVE_FAST_UNALIGNED_ACCESS Eli Zaretskii
2023-04-01 15:22                             ` HAVE_FAST_UNALIGNED_ACCESS Arsen Arsenović
2023-04-01 16:22                               ` HAVE_FAST_UNALIGNED_ACCESS Eli Zaretskii
2023-04-02  0:50                                 ` HAVE_FAST_UNALIGNED_ACCESS Po Lu
2023-04-02  0:48                             ` HAVE_FAST_UNALIGNED_ACCESS Po Lu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87lejbpxkm.fsf@aarsen.me \
    --to=arsen@aarsen.me \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=luangruo@yahoo.com \
    --cc=mattias.engdegard@gmail.com \
    --cc=rpluim@gmail.com \
    --cc=vibhavp@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.