Overhaul `math' representation machinery.
[u/mdw/catacomb] / m4 / mdw-probe-constant.m4
diff --git a/m4/mdw-probe-constant.m4 b/m4/mdw-probe-constant.m4
new file mode 100644 (file)
index 0000000..3916582
--- /dev/null
@@ -0,0 +1,183 @@
+dnl -*-autoconf-*-
+
+### SYNOPSIS
+###
+###   mdw_PROBE_CONSTANT(VAR, EXPR, [PREAMBLE], [IF-FAILED])
+###
+### DESCRIPTION
+###
+###   Extracts the value of a a constant integer expression from the
+###   compiler.  This works even if the compiler in question doesn't target
+###   the current architecture.  The value must be in the range -10^244 < x <
+###   10^244; this is probably fair enough.  In the extraordinarily unliklely
+###   event that the constant value is outside these bounds, the macro will
+###   fail rather than silently giving a wrong answer.
+###
+###   The result of the macro is that the shell variable VAR has the value of
+###   the expression EXPR, in decimal.  The PREAMBLE, if given, is inserted
+###   before EXPR is evaluated; it should contain #include and #define
+###   directives which are used to compute the value of the expression.
+###
+###   The idea for this macro came from the AC_C_COMPILE_VALUE macro by
+###   Ilguiz Latypov; this implementation has a number of advantages:
+###
+###     * it has an immense range of representable values, notably including
+###       negative numbers; and
+###
+###     * it returns the value directly in a shell variable rather than
+###       inventing an AC_DEFINE for it.
+###
+### LICENSE
+###
+###   Copyright (c) 2013 Mark Wooding <mdw@distorted.org.uk>
+###
+###   This program is free software: you can redistribute it and/or modify it
+###   under the terms of the GNU General Public License as published by the
+###   Free Software Foundation, either version 2 of the License, 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 <http://www.gnu.org/licenses/>.
+###
+###   In particular, no exception to the GPL is granted regarding generated
+###   `configure' scripts which are the output of Autoconf.
+
+# Serial 1
+AC_COPYRIGHT([
+Portions copyright (c) 2013 Mark Wooding.
+
+This configure script is free software: you can redistribute it and/or
+modify it under he terms of the GNU General Public License as published
+by the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.])
+
+AC_DEFUN([mdw__PROBE_CONSTANT_SETUP],
+  [mdw__probe_constant_body="[
+
+/* The following program is copyright (c) 2013 Mark Wooding.  It is free
+ * software: you can redistribute it and/or modify it under the terms of the
+ * GNU General Public License as published by the Free Software Foundation,
+ * either version 2 of the License, 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 <http://www.gnu.org/licenses/>.
+ */
+
+/* The constant: 1 billion.  We'll pull digits out in groups of nine, since
+ * we can work with constants of at least the size of a C \`long'.
+ */
+#define MDW__G 1000000000
+
+/* An empty macro, used as an argument sometimes. */
+#define MDW__E
+
+/* A cheesy compile-time assertion.  If X is zero, then we try to declare
+ * an array with a negative number of elements.  Wrap this up in an anonymous
+ * struct so that we don't have to worry about naming things if we make
+ * more than one assertion.
+ */
+#define MDW__ASSERT(x) struct { int v[1 - 2*!(x)]; }
+
+/* Return the value of X/DIV, with further divisions D applied, truncating
+ * towards zero.  DIV must be greater than one.  This works even if X is
+ * negative, never tries to divide negative numbers, and doesn't try to
+ * negate the most-negative value.  There are three cases: if X <= -DIV then
+ * X/DIV = -(X + DIV)/DIV - 1, and X + DIV is less negative than X so this is
+ * a safe negation; if -DIV < X < 0 then the result is zero; otherwise, X
+ * is nonnegative so the straightforward division is safe.  Because DIV > 1,
+ * X/DIV is safe to negate, and we can apply the remaining divisions to it.
+ */
+#define MDW__SHIFT(x, div, d) \\
+  ((x) >= 0 ? ((x)/div d) : \\
+   (x) <= -(div) ? -((-((x) + (div))/(div) + 1) d) : 0)
+
+/* Extract the bottommost digit of X, as an integer: i.e., the value of
+ * abs(X) mod 10.  This works even if X is negative, never tries to divide
+ * negative numbers, and doesn't try to divide the most-negative value.
+ */
+#define MDW__RAW_DIGIT(x) (((x) < 0 ? -((x) + 1) % 10 + 1 : (x)) % 10)
+
+/* Extract the bottommost digit of X, as a character; if X is zero, then
+ * produce a space instead.  This avoids leading zeroes which can be
+ * misinterpreted by callers.
+ */
+#define MDW__TEXT_DIGIT(x) ((x) ? '0' + MDW__RAW_DIGIT(x) : ' ')
+
+/* Extract the bottommost digit of the probe value, after dividing by DIV
+ * and applying the divisons D.
+ */
+#define MDW__DIGIT(div, d) \\
+  MDW__TEXT_DIGIT(MDW__SHIFT(MDW__PROBE_EXPR, div, d))
+
+/* Extract the bottommost six digits of the probe value after dividing by 10
+ * and then applying the divisions D.
+ */
+#define MDW__9DIGITS(d) \\
+  MDW__DIGIT(1000000000, d), \\
+  MDW__DIGIT( 100000000, d), \\
+  MDW__DIGIT(  10000000, d), \\
+  MDW__DIGIT(   1000000, d), \\
+  MDW__DIGIT(    100000, d), \\
+  MDW__DIGIT(     10000, d), \\
+  MDW__DIGIT(      1000, d), \\
+  MDW__DIGIT(       100, d), \\
+  MDW__DIGIT(        10, d)
+
+/* Increasingly huge divisions.  PN divides by 10^(9*2^N). */
+#define MDW__P0 /MDW__G
+#define MDW__P1 MDW__P0 MDW__P0
+#define MDW__P2 MDW__P1 MDW__P1
+#define MDW__P3 MDW__P2 MDW__P2
+#define MDW__P4 MDW__P3 MDW__P3
+#define MDW__P5 MDW__P4 MDW__P4
+
+/* Increasingly long sequences of digits.  DN(P) produces the 9 * 2^N
+ * digits after applying divisions P.
+ */
+#define MDW__D0(p) MDW__9DIGITS(p MDW__P0), MDW__9DIGITS(p MDW__E)
+#define MDW__D1(p) MDW__D0(p MDW__P1), MDW__D0(p)
+#define MDW__D2(p) MDW__D1(p MDW__P2), MDW__D1(p)
+#define MDW__D3(p) MDW__D2(p MDW__P3), MDW__D2(p)
+#define MDW__D4(p) MDW__D3(p MDW__P4), MDW__D3(p)
+
+/* Ensure that our exponential cascade is sufficient to represent the
+ * expression.
+ */
+MDW__ASSERT(MDW__SHIFT(MDW__PROBE_EXPR, 10, MDW__P5) == 0);
+
+/* Format the output.  Everything is taken care of except the bottommost
+ * digit, which we handle seaprately because we actually want a \`leading'
+ * zero here if the constant value is actually zero.  Format it so that
+ * we can extract it from the object file.
+ */
+const char mdw__probe_output[] = {
+  '\\n',
+  'm', 'd', 'w', '-',
+  'p', 'r', 'o', 'b', 'e', '-',
+  'v', 'a', 'l', 'u', 'e', '=', '\"',
+  (MDW__PROBE_EXPR < 0 ? '-' : ' '),
+  MDW__D4(MDW__E),
+  '0' + MDW__RAW_DIGIT(MDW__PROBE_EXPR),
+  '\"', '\\n'
+};]"])
+
+AC_DEFUN([mdw_PROBE_CONSTANT],
+  [AC_REQUIRE([mdw__PROBE_CONSTANT_SETUP])
+   AC_COMPILE_IFELSE([AC_LANG_SOURCE([[$3
+#define MDW__PROBE_EXPR ($2)
+$mdw__probe_constant_body]])],
+     [$1=$(sed -n \
+         's:^mdw-probe-value="\(-\|\) *\([[0-9]]*\)"$:\1\2:p' conftest.o)],
+     [m4_if([$4], [],
+           [AC_MSG_FAILURE([failed to evaluate expression])],
+           [$4])])])