simh-testsetgenerator/sim_printf_fmts.h
B. Scott Michel a275c71170 SCP: Reduce compiler warnings on LP64 platforms
(Note: Reducing compiler warnings across all, but primarily LP64
platforms, is a long term objective.)

Reduce compiler warnings on LP64 platforms (macOS, Windows) and 32-bit
builds (Win32). Prefer 'size_t' for pointer arithmetic, array indexing
and extents; 'int' hasn't been used for these purposes for many years
and across many ANSI standards. N.B. that conversions from int or int32
to size_t cause the compiler to zero-extend the value, which is
inefficient.

Refactor printf() format modifiers into sim_printf_fmts.h. Add the
SIZE_T_FMT modifier for better portability, especially on LP64 platforms
where size_t is unsigned long and sizeof(size_t) > sizeof(int).

3B2: Fix known size_t printf() format.
2024-02-01 12:51:32 -05:00

94 lines
2.6 KiB
C

/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
* sim_printf_fmts.h
*
* Cross-platform printf() formats for simh data types. Refactored out to
* this header so that these formats are avaiable to more than SCP.
*
* Author: B. Scott Michel
*
* "scooter me fecit"
*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~*/
#pragma once
#if !defined(SIM_PRINTF_H)
/* cross-platform printf() format specifiers:
*
* Note: MS apparently does recognize "ll" as "l" in its printf() routines, but "I64" is
* preferred for 64-bit types.
*
* MinGW note: __MINGW64__ and __MINGW32__ are both defined by 64-bit gcc. Check
* for __MINGW64__ before __MINGW32__.
*
*
* LL_FMT: long long format modifier, e.g. "%016" LL_FMT "x"
* SIZE_T: size_t format modifier, e.g., "%" SIZE_T_FMT "u" (can use "d", but you will
* probably get a warning.)
* T_UINT64_FMT: t_uint64 format modifier, e.g. "%016" T_UINT64_FMT "x"
* T_INT64_FMT: t_int64 format modifier, e.g., "%" T_INT64_FMT "d"
* POINTER_FMT: Format modifier for pointers, e.g. "%08" POINTER_FMT "X"
*/
#if defined (_WIN32) || defined(_WIN64)
# if defined(__MINGW64__)
# define LL_FMT "I64"
# define SIZE_T_FMT "I64"
# elif defined(_MSC_VER) || defined(__MINGW32__)
# define LL_FMT "ll"
# define SIZE_T_FMT "z"
# else
/* Graceful fail -- shouldn't ever default to this on a Windows platform. */
# define LL_FMT "ll"
# define SIZE_T_FMT "I32"
# endif
# define T_UINT64_FMT "I64"
# define T_INT64_FMT "I64"
# define POINTER_FMT "p"
#elif defined(__GNU_LIBRARY__) || defined(__GLIBC__) || defined(__GLIBC_MINOR__) || \
defined(__APPLE__)
/* GNU libc (Linux) and macOS */
# define LL_FMT "ll"
# define SIZE_T_FMT "z"
# define T_UINT64_FMT "ll"
# define T_INT64_FMT "ll"
# define POINTER_FMT "p"
#elif defined(__VAX)
/* No 64 bit ints on VAX, nothing special about size_t */
# define LL_FMT "l"
# define SIZE_T_FMT ""
# define T_UINT64_FMT ""
# define T_INT64_FMT ""
# define POINTER_FMT ""
#else
/* Defaults. */
# define LL_FMT "ll"
# define SIZE_T_FMT ""
# define T_UINT64_FMT ""
# define T_INT64_FMT ""
# define POINTER_FMT ""
#endif
#if defined (USE_INT64) && defined (USE_ADDR64)
# define T_ADDR_FMT T_UINT64_FMT
#else
# define T_ADDR_FMT ""
#endif
#if defined (USE_INT64)
# define T_VALUE_FMT T_UINT64_FMT
# define T_SVALUE_FMT T_INT64_FMT
#else
# define T_VALUE_FMT ""
# define T_SVALUE_FMT ""
#endif
#define SIM_PRINTF_H
#endif