public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: marxin <mliska@suse.cz>
To: gcc-patches@gcc.gnu.org
Cc: dvlasenk@redhat.com
Subject: [PATCH 1/3] Add vec::reverse.
Date: Fri, 25 May 2018 11:04:00 -0000	[thread overview]
Message-ID: <8332734ea696b10f03be75eda7fc854174800794.1527245880.git.mliska@suse.cz> (raw)
In-Reply-To: <cover.1527245880.git.mliska@suse.cz>

[-- Attachment #1: Type: text/plain, Size: 332 bytes --]


gcc/ChangeLog:

2018-05-25  Martin Liska  <mliska@suse.cz>
	    David Malcolm  <dmalcolm@redhat.com>

	* vec.c (test_reverse): New.
	(vec_c_tests): Add new test.
	* vec.h (vl_ptr>::reverse): New function.
---
 gcc/vec.c | 38 ++++++++++++++++++++++++++++++++++++++
 gcc/vec.h | 14 ++++++++++++++
 2 files changed, 52 insertions(+)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-vec-reverse.patch --]
[-- Type: text/x-patch; name="0001-Add-vec-reverse.patch", Size: 2047 bytes --]

diff --git a/gcc/vec.c b/gcc/vec.c
index 2941715a34a..beb857fd838 100644
--- a/gcc/vec.c
+++ b/gcc/vec.c
@@ -476,6 +476,43 @@ test_qsort ()
   ASSERT_EQ (10, v.length ());
 }
 
+/* Verify that vec::reverse works correctly.  */
+
+static void
+test_reverse ()
+{
+  /* Reversing an empty vec ought to be a no-op.  */
+  {
+    auto_vec <int> v;
+    ASSERT_EQ (0, v.length ());
+    v.reverse ();
+    ASSERT_EQ (0, v.length ());
+  }
+
+  /* Verify reversing a vec with even length.  */
+  {
+    auto_vec <int> v;
+    safe_push_range (v, 0, 4);
+    v.reverse ();
+    ASSERT_EQ (3, v[0]);
+    ASSERT_EQ (2, v[1]);
+    ASSERT_EQ (1, v[2]);
+    ASSERT_EQ (0, v[3]);
+    ASSERT_EQ (4, v.length ());
+  }
+
+  /* Verify reversing a vec with odd length.  */
+  {
+    auto_vec <int> v;
+    safe_push_range (v, 0, 3);
+    v.reverse ();
+    ASSERT_EQ (2, v[0]);
+    ASSERT_EQ (1, v[1]);
+    ASSERT_EQ (0, v[2]);
+    ASSERT_EQ (3, v.length ());
+  }
+}
+
 /* Run all of the selftests within this file.  */
 
 void
@@ -492,6 +529,7 @@ vec_c_tests ()
   test_unordered_remove ();
   test_block_remove ();
   test_qsort ();
+  test_reverse ();
 }
 
 } // namespace selftest
diff --git a/gcc/vec.h b/gcc/vec.h
index 2d1f468ca1c..a9f3bcf09eb 100644
--- a/gcc/vec.h
+++ b/gcc/vec.h
@@ -1389,6 +1389,7 @@ public:
   T *bsearch (const void *key, int (*compar)(const void *, const void *));
   unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
   bool contains (const T &search) const;
+  void reverse (void);
 
   bool using_auto_storage () const;
 
@@ -1900,6 +1901,19 @@ vec<T, va_heap, vl_ptr>::contains (const T &search) const
   return m_vec ? m_vec->contains (search) : false;
 }
 
+/* Reverse content of the vector.  */
+
+template<typename T>
+inline void
+vec<T, va_heap, vl_ptr>::reverse (void)
+{
+  unsigned l = length ();
+  T *ptr = address ();
+
+  for (unsigned i = 0; i < l / 2; i++)
+    std::swap (ptr[i], ptr[l - i - 1]);
+}
+
 template<typename T>
 inline bool
 vec<T, va_heap, vl_ptr>::using_auto_storage () const

  reply	other threads:[~2018-05-25 11:04 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-25 11:04 [PATCH 0/3] Extend -falign-FOO=N to N[,M[,N2[,M2]]] version 9 marxin
2018-05-25 11:04 ` marxin [this message]
2018-05-28 12:13   ` [PATCH 1/3] Add vec::reverse Richard Biener
2018-06-13  1:54   ` Jeff Law
2018-05-25 11:04 ` [PATCH 3/3] Extend -falign-FOO=N to N[:M[:N2[:M2]]] marxin
2018-06-29 19:05   ` Jeff Law
2018-07-03  8:53     ` Martin Liška
2018-07-03  9:55       ` Segher Boessenkool
2018-07-03 10:16         ` Martin Liška
2018-07-03 10:58           ` Segher Boessenkool
2018-07-03 12:51             ` Martin Liška
2018-07-03 13:23               ` Segher Boessenkool
2018-07-03 19:12       ` Martin Liška
2018-07-04  0:20         ` Jeff Law
2018-05-25 11:08 ` [PATCH 2/3] Temporary remove "at least 8 byte alignment" code from x86 marxin
2018-06-13  2:02   ` Jeff Law
2018-07-03 19:20     ` Martin Liška

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=8332734ea696b10f03be75eda7fc854174800794.1527245880.git.mliska@suse.cz \
    --to=mliska@suse.cz \
    --cc=dvlasenk@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).