aboutsummaryrefslogtreecommitdiff
path: root/paludis/util/byte_swap.hh
diff options
context:
space:
mode:
authorAvatar David Leverton <levertond@googlemail.com> 2008-02-04 03:44:44 +0000
committerAvatar David Leverton <levertond@googlemail.com> 2008-02-04 03:44:44 +0000
commit5ea3259964cf5639c1b1cc8e8189473eb23857d7 (patch)
treeba0f54342a2e6628a1d2d9d38a8aa2dc3bd136f8 /paludis/util/byte_swap.hh
parentb6f7ab0efa52e66ab9989d1cad5ae9380893e551 (diff)
downloadpaludis-5ea3259964cf5639c1b1cc8e8189473eb23857d7.tar.gz
paludis-5ea3259964cf5639c1b1cc8e8189473eb23857d7.tar.xz
Move byte_swap into paludis/util/.
Diffstat (limited to 'paludis/util/byte_swap.hh')
-rw-r--r--paludis/util/byte_swap.hh80
1 files changed, 80 insertions, 0 deletions
diff --git a/paludis/util/byte_swap.hh b/paludis/util/byte_swap.hh
new file mode 100644
index 000000000..b19fce38e
--- /dev/null
+++ b/paludis/util/byte_swap.hh
@@ -0,0 +1,80 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2007, 2008 David Leverton
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef PALUDIS_GUARD_PALUDIS_UTIL_BYTE_SWAP_HH
+#define PALUDIS_GUARD_PALUDIS_UTIL_BYTE_SWAP_HH
+
+#include <cstddef>
+
+namespace paludis
+{
+ namespace byte_swap_internals
+ {
+ template <std::size_t, typename> struct ByteSwap;
+
+ template <typename T_>
+ struct ByteSwap<1, T_>
+ {
+ static T_ swap(T_ x)
+ {
+ return x;
+ }
+ };
+
+ template <typename T_>
+ struct ByteSwap<2, T_>
+ {
+ static T_ swap(T_ x)
+ {
+ return ((x << 8) & (T_(0xFF) << 8)) | ((x >> 8) & T_(0xFF));
+ }
+ };
+
+ template <typename T_>
+ struct ByteSwap<4, T_>
+ {
+ static T_ swap(T_ x)
+ {
+ return ((x << 24) & (T_(0xFF) << 24)) | ((x << 8) & (T_(0xFF) << 16))
+ | ((x >> 8) & (T_(0xFF) << 8)) | ((x >> 24) & T_(0xFF) );
+ }
+ };
+
+ template <typename T_>
+ struct ByteSwap<8, T_>
+ {
+ static T_ swap(T_ x)
+ {
+ return ((x << 56) & (T_(0xFF) << 56)) | ((x << 40) & (T_(0xFF) << 48))
+ | ((x << 24) & (T_(0xFF) << 40)) | ((x << 8) & (T_(0xFF) << 32))
+ | ((x >> 8) & (T_(0xFF) << 24)) | ((x >> 24) & (T_(0xFF) << 16))
+ | ((x >> 40) & (T_(0xFF) << 8)) | ((x >> 56) & T_(0xFF) );
+ }
+ };
+ }
+
+ template <typename T_>
+ inline T_
+ byte_swap(T_ x)
+ {
+ return byte_swap_internals::ByteSwap<sizeof(T_), T_>::swap(x);
+ }
+}
+
+#endif