]> granicus.if.org Git - nethack/commitdiff
un-macro some of display.h
authorPatR <rankin@nethack.org>
Thu, 12 May 2022 21:42:20 +0000 (14:42 -0700)
committerPatR <rankin@nethack.org>
Thu, 12 May 2022 21:42:20 +0000 (14:42 -0700)
I recently captured preprocessor output for a file and the amount of
code being expanded--and subsequently compiled--for canspotmon() was
quite an eye opener.  This converts most of the macros it uses into
function calls.  The resulting executable generated for OSX (built
for x86_64 and containing four interfaces) is about 5.5% smaller! and
there wasn't any difference in speed that I could notice.

The knowninvisible() macro has been in error for as far back as the
git logs go (which include those for the second cvs repository, so
over 20 years now).

include/display.h
include/extern.h
src/display.c

index 1c7bd972d9910aa0612ca80850bac0ced024ad65..36c4115afd74408877ec45db1883d32e81b4b19b 100644 (file)
@@ -1,4 +1,4 @@
-/* NetHack 3.7 display.h       $NHDT-Date: 1651099381 2022/04/27 22:43:01 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.67 $ */
+/* NetHack 3.7 display.h       $NHDT-Date: 1652391718 2022/05/12 21:41:58 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.70 $ */
 /* Copyright (c) Dean Luick, with acknowledgements to Kevin Darcy */
 /* and Dave Cohrs, 1990.                                          */
 /* NetHack may be freely redistributed.  See license for details. */
  * (or display non-adjacent, non-submerged ones when hero is underwater),
  * so treat those situations as blocking telepathy, detection, and warning
  * even though conceptually they shouldn't do so.
+ *
+ * [3.7 also] The macros whose name begins with an understore have been
+ * converted to functions in order to have compilers generate smaller code.
+ * The retained underscore versions are still used in display.c but should
+ * only be used in other situations if the function calls actually produce
+ * noticeably slower processing.
  */
-#define tp_sensemon(mon) \
+#define _tp_sensemon(mon) \
     (/* The hero can always sense a monster IF:        */  \
      /* 1. the monster has a brain to sense            */  \
      (!mindless(mon->data))                                \
           /*        object and in range                */  \
           || (Unblind_telepat                              \
               && (distu(mon->mx, mon->my) <= (BOLT_LIM * BOLT_LIM)))))
+
 /* organized to perform cheaper tests first;
    is_pool() vs is_pool_or_lava(): hero who is underwater can see adjacent
    lava, but presumeably any monster there is on top so not sensed */
-#define sensemon(mon) \
+#define _sensemon(mon) \
     (   (!u.uswallow || (mon) == u.ustuck)                                 \
      && (!Underwater || (distu((mon)->mx, (mon)->my) <= 2                  \
                          && is_pool((mon)->mx, (mon)->my)))                \
@@ -55,7 +62,7 @@
  * mon_warning() is used to warn of any dangerous monsters in your
  * vicinity, and a glyph representing the warning level is displayed.
  */
-#define mon_warning(mon)                                                 \
+#define _mon_warning(mon) \
     (Warning && !(mon)->mpeaceful && (distu((mon)->mx, (mon)->my) < 100) \
      && (((int) ((mon)->m_lev / 4)) >= g.context.warnlevel))
 
  * reviewing all those instances and also existing mundetected instances.]
  */
 #if 0
-#define mon_visible(mon) \
+#define _mon_visible(mon) \
     (/* The hero can see the monster IF the monster                     */ \
      (!mon->minvis || See_invisible)  /*     1. is not invisible        */ \
      && !mon->mundetected             /* AND 2. not an undetected hider */ \
      && !(mon->mburied || u.uburied)) /* AND 3. neither you nor it is buried */
 #else   /* without 'mburied' and 'uburied' */
-#define mon_visible(mon) \
+#define _mon_visible(mon) \
     (/* The hero can see the monster IF the monster                     */ \
      (!mon->minvis || See_invisible)  /*     1. is not invisible        */ \
      && !mon->mundetected)            /* AND 2. not an undetected hider */
  * invisible to infravision), because this is usually called from within
  * canseemon() or canspotmon() which already check that.
  */
-#define see_with_infrared(mon)                        \
+#define _see_with_infrared(mon) \
     (!Blind && Infravision && mon && infravisible(mon->data) \
      && couldsee(mon->mx, mon->my))
 
  * routines.  Like mon_visible(), but it checks to see if the hero sees the
  * location instead of assuming it.  (And also considers worms.)
  */
-#define canseemon(mon)                                                    \
+#define _canseemon(mon) \
     ((mon->wormno ? worm_known(mon)                                       \
                   : (cansee(mon->mx, mon->my) || see_with_infrared(mon))) \
      && mon_visible(mon))
  * creature in an apparently empty spot.
  * Infravision is not relevant; we assume that invisible monsters are also
  * invisible to infravision.
+ * [3.7: the macro definition erroneously started with 'mtmp->minvis' for
+ * over 20 years.  The one place it's used called it as knowninvisible(mtmp)
+ * so worked by coincidence when there was no argument expansion involved
+ * on the first line.]
  */
-#define knowninvisible(mon)                                               \
-    (mtmp->minvis                                                         \
-     && ((cansee(mon->mx, mon->my) && (See_invisible || Detect_monsters)) \
-         || (!Blind && (HTelepat & ~INTRINSIC)                            \
-             && distu(mon->mx, mon->my) <= (BOLT_LIM * BOLT_LIM))))
+#define _knowninvisible(mon) \
+    ((mon)->minvis                                                      \
+     && ((cansee((mon)->mx, (mon)->my)                                  \
+          && (See_invisible || Detect_monsters))                        \
+         || (!Blind && (HTelepat & ~INTRINSIC)                          \
+             && distu((mon)->mx, (mon)->my) <= (BOLT_LIM * BOLT_LIM))))
 
 /*
  * is_safemon(mon)
  * A special case check used in attack() and domove().  Placing the
  * definition here is convenient.  No longer limited to pets.
  */
-#define is_safemon(mon) \
+#define _is_safemon(mon) \
     (flags.safe_dog && (mon) && (mon)->mpeaceful && canspotmon(mon)     \
      && !Confusion && !Hallucination && !Stunned)
 
index 93217baa29634e2229fcd208c579e417fa3e28ca..5eb65674f694a2c58a03c5b13f35b38991ad27d1 100644 (file)
@@ -1,4 +1,4 @@
-/* NetHack 3.7 extern.h        $NHDT-Date: 1652299940 2022/05/11 20:12:20 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.1114 $ */
+/* NetHack 3.7 extern.h        $NHDT-Date: 1652391728 2022/05/12 21:42:08 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.1115 $ */
 /* Copyright (c) Steve Creps, 1988.                              */
 /* NetHack may be freely redistributed.  See license for details. */
 
@@ -393,6 +393,14 @@ extern int wiz_debug_cmd_bury(void);
 
 /* ### display.c ### */
 
+extern int tp_sensemon(struct monst *);
+extern int sensemon(struct monst *);
+extern int mon_warning(struct monst *);
+extern int mon_visible(struct monst *);
+extern int see_with_infrared(struct monst *);
+extern int canseemon(struct monst *);
+extern int knowninvisible(struct monst *);
+extern int is_safemon(struct monst *);
 extern void magic_map_background(xchar, xchar, int);
 extern void map_background(xchar, xchar, int);
 extern void map_trap(struct trap *, int);
index 2978e3fc4d94896e7048cc37b3648618a9bf094d..839d99c1a9a0dada9fe827c049f40f037fe6d2cc 100644 (file)
@@ -1,4 +1,4 @@
-/* NetHack 3.7 display.c       $NHDT-Date: 1644440006 2022/02/09 20:53:26 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.177 $ */
+/* NetHack 3.7 display.c       $NHDT-Date: 1652391730 2022/05/12 21:42:10 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.183 $ */
 /* Copyright (c) Dean Luick, with acknowledgements to Kevin Darcy */
 /* and Dave Cohrs, 1990.                                          */
 /* NetHack may be freely redistributed.  See license for details. */
@@ -151,6 +151,77 @@ static int wall_angle(struct rm *);
 
 #define remember_topology(x, y) (g.lastseentyp[x][y] = levl[x][y].typ)
 
+/*
+ *      See display.h for descriptions of tp_sensemon() through
+ *      is_safemon().  Some of these were generating an awful lot of
+ *      code "behind the curtain", particularly canspotmon() (which is
+ *      still a macro but one that now expands to a pair of function
+ *      calls rather than to a ton of special case checks).  Return
+ *      values are all int 0 or 1, not boolean.
+ *
+ *      They're still implemented as macros within this file.
+ */
+int
+tp_sensemon(struct monst *mon)
+{
+    return _tp_sensemon(mon);
+}
+#define tp_sensemon(mon) _tp_sensemon(mon)
+
+int
+sensemon(struct monst *mon)
+{
+    return _sensemon(mon);
+}
+#define sensemon(mon) _sensemon(mon)
+
+int
+mon_warning(struct monst *mon)
+{
+    return _mon_warning(mon);
+}
+#define mon_warning(mon) _mon_warning(mon)
+
+int
+mon_visible(struct monst *mon)
+{
+    return _mon_visible(mon);
+}
+#define mon_visible(mon) _mon_visible(mon)
+
+int
+see_with_infrared(struct monst *mon)
+{
+    return _see_with_infrared(mon);
+}
+#define see_with_infrared(mon) _see_with_infrared(mon)
+
+int
+canseemon(struct monst *mon)
+{
+    return _canseemon(mon);
+}
+#define canseemon(mon) _canseemon(mon)
+
+int
+knowninvisible(struct monst *mon)
+{
+    return _knowninvisible(mon);
+}
+/* #define knowninvisible() isn't useful here */
+
+int
+is_safemon(struct monst *mon)
+{
+    return _is_safemon(mon);
+}
+/* #define is_safemon() isn't useful here */
+
+/*
+ *      End of former macro-only vision related (mostly) routines
+ *      converted to functions.
+ */
+
 /*
  * magic_map_background()
  *