ref: 628cc6785b2cfa3d4ee6101667e4bb9e11095eec
parent: c48a9f44ffdd2bd2055a5296c84874716dd303b9
author: Simon Tatham <anakin@pobox.com>
date: Mon May 1 09:49:54 EDT 2023
Untangle: replace manual int64 bodging with int64_t. Where possible, that is. If our compilation environment has provided int64_t, we can just make our int64 type be that, and not have to mess around with multi-word arithmetic at all.
--- a/untangle.c
+++ b/untangle.c
@@ -20,10 +20,6 @@
* requirements are adequately expressed by a single scalar tile
* size), and probably complicate the rest of the puzzles' API as a
* result. So I'm not sure I really want to do it.
- *
- * - It would be nice if we could somehow auto-detect a real `long
- * long' type on the host platform and use it in place of my
- * hand-hacked int64s. It'd be faster and more reliable.
*/
#include <stdio.h>
@@ -37,6 +33,9 @@
#else
# include <tgmath.h>
#endif
+#if HAVE_STDINT_H
+# include <stdint.h>
+#endif
#include "puzzles.h"
#include "tree234.h"
@@ -221,6 +220,9 @@
* integer overflow at the very core of cross().
*/
+#if !HAVE_STDINT_H
+/* For prehistoric C implementations, do this the hard way */
+
typedef struct {
long hi;
unsigned long lo;
@@ -299,6 +301,21 @@
ab.hi++;
return ab;
}
+
+#else /* HAVE_STDINT_H */
+
+typedef int64_t int64;
+#define greater64(i,j) ((i) > (j))
+#define sign64(i) ((i) < 0 ? -1 : (i)==0 ? 0 : +1)
+#define mulu32to64(x,y) ((int64_t)(unsigned long)(x) * (unsigned long)(y))
+#define mul32to64(x,y) ((int64_t)(long)(x) * (long)(y))
+
+static int64 dotprod64(long a, long b, long p, long q)
+{
+ return (int64)a * b + (int64)p * q;
+}
+
+#endif /* HAVE_STDINT_H */
/*
* Determine whether the line segments between a1 and a2, and