1
0
Fork 0
mirror of https://codeberg.org/noisytoot/notnotdnethack.git synced 2025-04-28 11:25:11 +01:00
notnotdnethack/include/tradstdc.h
Ron Nazarov 3f74c9f174 Fix altar display in mapseen
Previously, #overview would show the altar's god as being to your god
if it's of your alignment (regardless of the altar's actual god), and
doesn't show anything about the altar's god/alignment otherwise.  This
commit makes it always show the altar's correct god if all altars on
the level are to the same god.

mapseen-related stuff is moved from dungeon.h to mapseen.h, to avoid a
circular makedefs -> dungeon.h -> gnames.h -> makedefs dependency.
gnames.h needs to be included for MAX_GOD.

# Conflicts:
#	include/tradstdc.h
2025-01-19 19:47:09 -05:00

141 lines
5.1 KiB
C

/* SCCS Id: @(#)tradstdc.h 3.4 1993/05/30 */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* NetHack may be freely redistributed. See license for details. */
#ifndef TRADSTDC_H
#define TRADSTDC_H
/*
* ANSI X3J11 detection.
* Makes substitutes for compatibility with the old C standard.
*/
#ifdef NEED_VARARGS /* only define these if necessary */
/*
* These have changed since 3.4.3. VA_END() now provides an explicit
* closing brace to complement VA_DECL()'s hidden opening brace, so code
* started with VA_DECL() needs an extra opening brace to complement
* the explicit final closing brace. This was done so that the source
* would look less strange, where VA_DECL() appeared to introduce a
* function whose opening brace was missing; there are now visible and
* invisible braces at beginning and end. Sample usage:
void foo VA_DECL(int, arg) --macro expansion has a hidden opening brace
{ --new, explicit opening brace (actually introduces a nested block)
VA_START(bar);
...code for foo...
VA_END(); --expansion now provides a closing brace for the nested block
} --existing closing brace, still pairs with the hidden one in VA_DECL()
* Reading the code--or using source browsing tools which match braces--
* results in seeing a matched set of braces. Usage of VA_END() is
* potentially trickier, but nethack uses it in a straightforward manner.
*/
#include <stdarg.h>
#define VA_DECL(typ1,var1) (typ1 var1, ...) { va_list the_args;
#define VA_DECL2(typ1,var1,typ2,var2) \
(typ1 var1, typ2 var2, ...) { va_list the_args;
#define VA_INIT(var1,typ1)
#define VA_NEXT(var1,typ1) var1 = va_arg(the_args, typ1)
#define VA_ARGS the_args
#define VA_START(x) va_start(the_args, x)
#define VA_END() va_end(the_args); }
#endif /* NEED_VARARGS */
/*
* Allow gcc2 to check parameters of printf-like calls with -Wformat;
* append this to a prototype declaration (see pline() in extern.h).
*/
#ifdef __GNUC__
# if __GNUC__ >= 2
#define PRINTF_F(f,v) __attribute__ ((format (printf, f, v)))
# endif
#endif
#ifndef PRINTF_F
#define PRINTF_F(f,v)
#endif
/*
* If we don't have C23 <stdckdint.h>, fall back to GCC
* __builtin_*_overflow functions. If we don't have those either,
* fall back to implementation using C11 _Generic.
*/
#if __has_include(<stdckdint.h>)
# include <stdckdint.h>
#else
# ifdef __GNUC__
# define ckd_add(R, A, B) __builtin_add_overflow ((A), (B), (R))
# define ckd_sub(R, A, B) __builtin_sub_overflow ((A), (B), (R))
# define ckd_mul(R, A, B) __builtin_mul_overflow ((A), (B), (R))
# else
# define max_int_value(I) \
_Generic((I), \
signed char: SCHAR_MAX, \
short: SHRT_MAX, \
int: INT_MAX, \
long: LONG_MAX, \
long long: LLONG_MAX, \
unsigned char: UCHAR_MAX, \
unsigned short: USHRT_MAX, \
unsigned int: UINT_MAX, \
unsigned long: ULONG_MAX, \
unsigned long long: ULLONG_MAX, \
default: LARGEST_INT)
# define min_int_value(I) \
_Generic((I), \
signed char: SCHAR_MIN, \
short: SHRT_MIN, \
int: INT_MIN, \
long: LONG_MIN, \
long long: LLONG_MIN, \
default: 0)
# define ckd_add(R, A, B) \
((((A) > 0) && ((B) > max_int_value(*(R)) - (A))) \
? (*(R) = ((A) + min_int_value(*(R))) + ((B) + min_int_value(*(R))), TRUE) \
: ((((A) < 0) && ((B) < min_int_value(*(R)) - (A))) \
? (*(R) = ((A) - min_int_value(*(R))) + ((B) - min_int_value(*(R))), TRUE) \
: (*(R) = ((A) + (B)), FALSE)))
# define ckd_sub(R, A, B) ckd_add((R), (A), -(B))
/* Lazy implementation: only works for 32-bit types */
# define ckd_mul(R, A, B) (*(R) = (long long)(A) * (B), ((long long)(A) * (B)) > max_int_value(*(R)))
# endif /* __GNUC__ */
#endif /* __has_include(<stdckdint.h>) */
/*
* If we don't have C23 <stdbit.h>, fall back to GCC
* __builtin_popcount and C11 _Generic. If we don't have that either,
* fall back to Brian Kernighan's algorithm and C11 _Generic.
*/
#if __has_include(<stdbit.h>)
# include <stdbit.h>
#else
# ifdef __GNUC__
# define stdc_count_ones_uc(n) (unsigned char)__builtin_popcount((unsigned char)(n))
# define stdc_count_ones_us(n) (unsigned short)__builtin_popcount((unsigned short)(n))
# define stdc_count_ones_ui(n) __builtin_popcount((n))
# define stdc_count_ones_ul(n) __builtin_popcountl((n))
# define stdc_count_ones_ull(n) __builtin_popcountll((n))
# else
# define DEFINE_STDC_COUNT_ONES(suffix, type) \
static inline type stdc_count_ones_##suffix(type n) { \
int count = 0; \
while (n) { n &= n-1; count++; } \
return count; \
}
DEFINE_STDC_COUNT_ONES(uc, unsigned char)
DEFINE_STDC_COUNT_ONES(us, unsigned short)
DEFINE_STDC_COUNT_ONES(ui, unsigned int)
DEFINE_STDC_COUNT_ONES(ul, unsigned long)
DEFINE_STDC_COUNT_ONES(ull, unsigned long long)
# undef DEFINE_STDC_COUNT_ONES
# endif /* __GNUC__ */
#define stdc_count_ones(n) \
_Generic((n), \
unsigned char: stdc_count_ones_uc((n)), \
unsigned short: stdc_count_ones_us((n)), \
unsigned int: stdc_count_ones_ui((n)), \
unsigned long: stdc_count_ones_ul((n)), \
unsigned long long: stdc_count_ones_ull((n)))
#endif /* __has_include(<stdbit.h>) */
#endif /* TRADSTDC_H */