+/*
+ * qfarith.h: utilities for quick field arithmetic
+ */
+/*
+ * This file is Free Software. It has been modified to as part of its
+ * incorporation into secnet.
+ *
+ * Copyright 2017 Mark Wooding
+ *
+ * You may redistribute this file and/or modify it under the terms of
+ * the permissive licence shown below.
+ *
+ * You may redistribute secnet as a whole and/or modify it under the
+ * terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3, or (at your option) any
+ * later version.
+ *
+ * This program 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, see
+ * https://www.gnu.org/licenses/gpl.html.
+ */
+/*
+ * Imported from Catacomb, and modified for Secnet (2017-04-30):
+ *
+ * * Use `fake-mLib-bits.h' in place of the real <mLib/bits.h>.
+ *
+ * * Use C99 signed integer types instead of our own local probing.
+ *
+ * * Remove the support for non-two's-complement targets.
+ *
+ * The file's original comment headers are preserved below.
+ */
/* -*-c-*-
*
* Utilities for quick field arithmetic
/*----- Header files ------------------------------------------------------*/
#include <limits.h>
+#include <stdint.h>
-#include <mLib/bits.h>
+#include "fake-mLib-bits.h"
/*----- Signed integer types ----------------------------------------------*/
-/* See if we can find a suitable 64-bit or wider type. Don't bother if we
- * don't have a corresponding unsigned type, because we really need both.
- */
-#ifdef HAVE_UINT64
-# if INT_MAX >> 31 == 0xffffffff
-# define HAVE_INT64
- typedef int int64;
-# elif LONG_MAX >> 31 == 0xffffffff
-# define HAVE_INT64
- typedef long int64;
-# elif defined(LLONG_MAX)
-# define HAVE_INT64
- MLIB_BITS_EXTENSION typedef long long int64;
-# endif
-#endif
-
-/* Choose suitable 32- and 16-bit types. */
-#if INT_MAX >= 0x7fffffff
- typedef int int32;
-#else
- typedef long int32;
-#endif
-
-typedef short int16;
+typedef int_fast32_t int32;
+typedef int_fast64_t int64;
+#define HAVE_INT64 1
/*----- General bit-hacking utilities -------------------------------------*/
# define CONDSWAP(x, y, m) \
do { mask32 t_ = ((x) ^ (y))&(m); (x) ^= t_; (y) ^= t_; } while (0)
#else
-
- /* We don't have two's complement arithmetic. We can't use bithacking at
- * all: if we try to hack on the bits of signed numbers we'll come unstuck
- * when we hit the other representation of zero; and if we switch to
- * unsigned arithmetic then we'll have overflow when we try to convert a
- * negative number back. So fall back to simple arithmetic.
- */
- typedef uint32 mask32;
-
- /* Convert an unsigned mask M into a `mask32'. Our masks are unsigned, so
- * this does nothing.
- */
-# define FIX_MASK32(m) ((mask32)(m))
-
- /* If Z is zero and M has its low 32 bits set, then copy (at least) the low
- * 32 bits of X to Z; if M is zero, do nothing. Otherwise, scramble Z
- * unhelpfully.
- */
-# define CONDPICK(z, x, m) \
- do { (z) += (x)*(int)((unsigned)(m)&1u); } while (0)
-
- /* If M has its low 32 bits set, then return (at least) the low 32 bits of
- * X in Z; if M is zero, then return (at least) the low 32 bits of Y in Z.
- * Otherwise, return an unhelful result.
- */
-# define PICK2(x, y, m) \
- ((x)*(int)((unsigned)(m)&1u) + (y)*(int)(1 - ((unsigned)(m)&1u)))
-
- /* If M has its low 32 bits set then swap (at least) the low 32 bits of X
- * and Y; if M is zero, then do nothing. Otherwise, scramble X and Y
- * unhelpfully.
- */
-# define CONDSWAP(x, y, m) do { \
- int32 x_ = PICK2((y), (x), (m)), y_ = PICK2((x), (y), (m)); \
- (x) = x_; (y) = y_; \
- } while (0)
+# error "Targets without two's complement arithmetic aren't supported"
#endif
/* Return zero if bit 31 of X is clear, or a mask with (at least) the low 32