add int.h

Added int.h as a centeral place for calc integer types and integer macros.

Currently zmath.c includes int.h but does not yet use it.

Added missing ptr type checks to chk_c.c.
This commit is contained in:
Landon Curt Noll
2023-08-22 22:37:08 -07:00
parent 2d5339fc51
commit 0353aba275
5 changed files with 239 additions and 3 deletions

View File

@@ -72,6 +72,9 @@ The following are the changes from calc version 2.14.3.5 to date:
appear to meet calc requirements, or undefines CHK_C appear to meet calc requirements, or undefines CHK_C
when it does not. when it does not.
Added int.h as a centeral place for calc integer types and
integer macros.
The following are the changes from calc version 2.14.3.4 to 2.14.3.5: The following are the changes from calc version 2.14.3.4 to 2.14.3.5:

View File

@@ -164,7 +164,7 @@ CALCOBJS= calc.o
# #
LIB_H_SRC= alloc.h banned.h blkcpy.h block.h bool.h byteswap.h calc.h \ LIB_H_SRC= alloc.h banned.h blkcpy.h block.h bool.h byteswap.h calc.h \
cmath.h config.h custom.h decl.h file.h func.h hash.h hist.h \ cmath.h config.h custom.h decl.h file.h func.h hash.h hist.h \
jump.h label.h lib_util.h lib_calc.h nametype.h \ int.h jump.h label.h lib_util.h lib_calc.h nametype.h \
opcodes.h prime.h qmath.h sha1.h str.h strl.h \ opcodes.h prime.h qmath.h sha1.h str.h strl.h \
symbol.h token.h value.h zmath.h zrand.h zrandom.h attribute.h symbol.h token.h value.h zmath.h zrand.h zrandom.h attribute.h
@@ -5570,9 +5570,12 @@ zmath.o: have_limits.h
zmath.o: have_memmv.h zmath.o: have_memmv.h
zmath.o: have_newstr.h zmath.o: have_newstr.h
zmath.o: have_stdbool.h zmath.o: have_stdbool.h
zmath.o: have_stdint.h
zmath.o: have_stdlib.h zmath.o: have_stdlib.h
zmath.o: have_string.h zmath.o: have_string.h
zmath.o: int.h
zmath.o: longbits.h zmath.o: longbits.h
zmath.o: status.chk_c.h
zmath.o: zmath.c zmath.o: zmath.c
zmath.o: zmath.h zmath.o: zmath.h
zmod.o: alloc.h zmod.o: alloc.h

20
chk_c.c
View File

@@ -12,6 +12,7 @@
* int16_t uint16_t * int16_t uint16_t
* int32_t uint32_t * int32_t uint32_t
* int64_t uint64_t * int64_t uint64_t
* intptr_t uintptr_t
* intmax_t uintmax_t * intmax_t uintmax_t
* *
* be able to sum values from all of those files, * be able to sum values from all of those files,
@@ -119,6 +120,8 @@ main(int argc, char *argv[])
uint32_t ui32 = UINT32_C(32); /* unsigned 32-bit test value */ uint32_t ui32 = UINT32_C(32); /* unsigned 32-bit test value */
int64_t i64 = INT64_C(-64); /* signed 64-bit test value */ int64_t i64 = INT64_C(-64); /* signed 64-bit test value */
uint64_t ui64 = UINT64_C(64); /* unsigned 64-bit test value */ uint64_t ui64 = UINT64_C(64); /* unsigned 64-bit test value */
intptr_t iptr = -128; /* integer type capable of holding a pointer */
intptr_t uiptr = 128; /* unsigned integer type capable of holding a pointer */
intmax_t imax = INTMAX_C(-256); /* maximum sized signed int test value */ intmax_t imax = INTMAX_C(-256); /* maximum sized signed int test value */
uintmax_t uimax = UINTMAX_C(256); /* maximum sized unsigned int test value */ uintmax_t uimax = UINTMAX_C(256); /* maximum sized unsigned int test value */
uintmax_t isum = 0; /* sum of all test values as signed value */ uintmax_t isum = 0; /* sum of all test values as signed value */
@@ -218,7 +221,7 @@ main(int argc, char *argv[])
/* /*
* calculate sum of all test values as signed value * calculate sum of all test values as signed value
*/ */
isum = i + ui + l + ul + ll + ull + i8 + ui8 + i16 + ui16 + i32 + ui32 + i64 + ui64 + imax + uimax; isum = i + ui + l + ul + ll + ull + i8 + ui8 + i16 + ui16 + i32 + ui32 + i64 + ui64 + iptr + uiptr + imax + uimax;
if (c_flag == true) { if (c_flag == true) {
printf("\nzero valued isum: %jd\n", isum); printf("\nzero valued isum: %jd\n", isum);
} }
@@ -230,7 +233,7 @@ main(int argc, char *argv[])
/* /*
* calculate sum of all test values as unsigned value * calculate sum of all test values as unsigned value
*/ */
uisum = i + ui + l + ul + ll + ull + i8 + ui8 + i16 + ui16 + i32 + ui32 + i64 + ui64 + imax + uimax; uisum = i + ui + l + ul + ll + ull + i8 + ui8 + i16 + ui16 + i32 + ui32 + i64 + ui64 + iptr + uiptr + imax + uimax;
if (c_flag == true) { if (c_flag == true) {
printf("zero valued uisum: %ju\n", uisum); printf("zero valued uisum: %ju\n", uisum);
} }
@@ -252,6 +255,8 @@ main(int argc, char *argv[])
printf("INT32_MAX: %+"PRId32"\n", (int32_t)INT32_MAX); printf("INT32_MAX: %+"PRId32"\n", (int32_t)INT32_MAX);
printf("INT64_MIN: %+"PRId64"\n", (int64_t)INT64_MIN); printf("INT64_MIN: %+"PRId64"\n", (int64_t)INT64_MIN);
printf("INT64_MAX: %+"PRId64"\n", (int64_t)INT64_MAX); printf("INT64_MAX: %+"PRId64"\n", (int64_t)INT64_MAX);
printf("INTPTR_MIN: %+"PRIdMAX"\n", (intptr_t)INTPTR_MIN);
printf("INTPTR_MAX: %+"PRIdMAX"\n", (intptr_t)INTPTR_MAX);
printf("INTMAX_MIN: %+"PRIdMAX"\n", (intmax_t)INTMAX_MIN); printf("INTMAX_MIN: %+"PRIdMAX"\n", (intmax_t)INTMAX_MIN);
printf("INTMAX_MAX: %+"PRIdMAX"\n", (intmax_t)INTMAX_MAX); printf("INTMAX_MAX: %+"PRIdMAX"\n", (intmax_t)INTMAX_MAX);
@@ -260,6 +265,7 @@ main(int argc, char *argv[])
printf("UINT16_MAX: %"PRIu16"\n", (uint16_t)UINT16_MAX); printf("UINT16_MAX: %"PRIu16"\n", (uint16_t)UINT16_MAX);
printf("UINT32_MAX: %"PRIu32"\n", (uint32_t)UINT32_MAX); printf("UINT32_MAX: %"PRIu32"\n", (uint32_t)UINT32_MAX);
printf("UINT64_MAX: %"PRIu64"\n", (uint64_t)UINT64_MAX); printf("UINT64_MAX: %"PRIu64"\n", (uint64_t)UINT64_MAX);
printf("UINTPTR_MAX: %"PRIuMAX"\n", (uintptr_t)UINTPTR_MAX);
printf("UINTMAX_MAX: %"PRIuMAX"\n", (uintmax_t)UINTMAX_MAX); printf("UINTMAX_MAX: %"PRIuMAX"\n", (uintmax_t)UINTMAX_MAX);
printf("\ninteger MIN and MAX\n"); printf("\ninteger MIN and MAX\n");
@@ -271,6 +277,8 @@ main(int argc, char *argv[])
printf("INT32_MAX: %"PRIi32"\n", (int32_t)INT32_MAX); printf("INT32_MAX: %"PRIi32"\n", (int32_t)INT32_MAX);
printf("INT64_MIN: %"PRIi64"\n", (int64_t)INT64_MIN); printf("INT64_MIN: %"PRIi64"\n", (int64_t)INT64_MIN);
printf("INT64_MAX: %"PRIi64"\n", (int64_t)INT64_MAX); printf("INT64_MAX: %"PRIi64"\n", (int64_t)INT64_MAX);
printf("INTPTR_MIN: %"PRIiMAX"\n", (intptr_t)INTPTR_MIN);
printf("INTPTR_MAX: %"PRIiMAX"\n", (intptr_t)INTPTR_MAX);
printf("INTMAX_MIN: %"PRIiMAX"\n", (intmax_t)INTMAX_MIN); printf("INTMAX_MIN: %"PRIiMAX"\n", (intmax_t)INTMAX_MIN);
printf("INTMAX_MAX: %"PRIiMAX"\n", (intmax_t)INTMAX_MAX); printf("INTMAX_MAX: %"PRIiMAX"\n", (intmax_t)INTMAX_MAX);
@@ -283,6 +291,8 @@ main(int argc, char *argv[])
printf("INT32_MAX: %"PRIo32"\n", (int32_t)INT32_MAX); printf("INT32_MAX: %"PRIo32"\n", (int32_t)INT32_MAX);
printf("INT64_MIN: %"PRIo64"\n", (int64_t)INT64_MIN); printf("INT64_MIN: %"PRIo64"\n", (int64_t)INT64_MIN);
printf("INT64_MAX: %"PRIo64"\n", (int64_t)INT64_MAX); printf("INT64_MAX: %"PRIo64"\n", (int64_t)INT64_MAX);
printf("INTPTR_MIN: %"PRIoMAX"\n", (intptr_t)INTPTR_MIN);
printf("INTPTR_MAX: %"PRIoMAX"\n", (intptr_t)INTPTR_MAX);
printf("INTMAX_MIN: %"PRIoMAX"\n", (intmax_t)INTMAX_MIN); printf("INTMAX_MIN: %"PRIoMAX"\n", (intmax_t)INTMAX_MIN);
printf("INTMAX_MAX: %"PRIoMAX"\n", (intmax_t)INTMAX_MAX); printf("INTMAX_MAX: %"PRIoMAX"\n", (intmax_t)INTMAX_MAX);
@@ -295,6 +305,8 @@ main(int argc, char *argv[])
printf("INT32_MAX: %"PRIx32"\n", (int32_t)INT32_MAX); printf("INT32_MAX: %"PRIx32"\n", (int32_t)INT32_MAX);
printf("INT64_MIN: %"PRIx64"\n", (int64_t)INT64_MIN); printf("INT64_MIN: %"PRIx64"\n", (int64_t)INT64_MIN);
printf("INT64_MAX: %"PRIx64"\n", (int64_t)INT64_MAX); printf("INT64_MAX: %"PRIx64"\n", (int64_t)INT64_MAX);
printf("INTPTR_MIN: %"PRIxMAX"\n", (intptr_t)INTPTR_MIN);
printf("INTPTR_MAX: %"PRIxMAX"\n", (intptr_t)INTPTR_MAX);
printf("INTMAX_MIN: %"PRIxMAX"\n", (intmax_t)INTMAX_MIN); printf("INTMAX_MIN: %"PRIxMAX"\n", (intmax_t)INTMAX_MIN);
printf("INTMAX_MAX: %"PRIxMAX"\n", (intmax_t)INTMAX_MAX); printf("INTMAX_MAX: %"PRIxMAX"\n", (intmax_t)INTMAX_MAX);
@@ -307,6 +319,8 @@ main(int argc, char *argv[])
printf("INT32_MAX: %"PRIX32"\n", (int32_t)INT32_MAX); printf("INT32_MAX: %"PRIX32"\n", (int32_t)INT32_MAX);
printf("INT64_MIN: %"PRIX64"\n", (int64_t)INT64_MIN); printf("INT64_MIN: %"PRIX64"\n", (int64_t)INT64_MIN);
printf("INT64_MAX: %"PRIX64"\n", (int64_t)INT64_MAX); printf("INT64_MAX: %"PRIX64"\n", (int64_t)INT64_MAX);
printf("INTPTR_MIN: %"PRIXMAX"\n", (intptr_t)INTPTR_MIN);
printf("INTPTR_MAX: %"PRIXMAX"\n", (intptr_t)INTPTR_MAX);
printf("INTMAX_MIN: %"PRIXMAX"\n", (intmax_t)INTMAX_MIN); printf("INTMAX_MIN: %"PRIXMAX"\n", (intmax_t)INTMAX_MIN);
printf("INTMAX_MAX: %"PRIXMAX"\n", (intmax_t)INTMAX_MAX); printf("INTMAX_MAX: %"PRIXMAX"\n", (intmax_t)INTMAX_MAX);
@@ -319,6 +333,8 @@ main(int argc, char *argv[])
printf("sizeof(uint32_t): %lu\n", sizeof(uint32_t)); printf("sizeof(uint32_t): %lu\n", sizeof(uint32_t));
printf("sizeof(int64_t): %lu\n", sizeof(int64_t)); printf("sizeof(int64_t): %lu\n", sizeof(int64_t));
printf("sizeof(uint64_t): %lu\n", sizeof(uint64_t)); printf("sizeof(uint64_t): %lu\n", sizeof(uint64_t));
printf("sizeof(intptr_t): %lu\n", sizeof(intptr_t));
printf("sizeof(uintptr_t): %lu\n", sizeof(uintptr_t));
printf("sizeof(intmax_t): %lu\n", sizeof(intmax_t)); printf("sizeof(intmax_t): %lu\n", sizeof(intmax_t));
printf("sizeof(uintmax_t): %lu\n", sizeof(uintmax_t)); printf("sizeof(uintmax_t): %lu\n", sizeof(uintmax_t));
} }

213
int.h Normal file
View File

@@ -0,0 +1,213 @@
/*
* int - integer types and integer macros used in calc
*
* Copyright (C) 2023 Landon Curt Noll
*
* Primary author: Landon Curt Noll
*
* Calc is open software; you can redistribute it and/or modify it under
* the terms of the version 2.1 of the GNU Lesser General Public License
* as published by the Free Software Foundation.
*
* Calc 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 Lesser General
* Public License for more details.
*
* A copy of version 2.1 of the GNU Lesser General Public License is
* distributed with calc under the filename COPYING-LGPL. You should have
* received a copy with calc; if not, write to Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Under source code control: 2023/08/22 21:14:25
* File existed as early as: 2023
*
* Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
*/
#if !defined(INCLUDE_INT_H)
#define INCLUDE_INT_H
/*
* important include files that have an impact in integer types used in calc
*/
#include "have_stdint.h"
#if defined(HAVE_STDINT_H)
#include <stdint.h>
#endif /* HAVE_STDINT_H */
#include "have_inttypes.h"
#if defined(HAVE_INTTYPES_H)
#include <inttypes.h>
#endif /* HAVE_INTTYPES_H */
#include "status.chk_c.h"
#include "bool.h"
#include "charbit.h"
#include "version.h"
/*
* case: C compiler and select include files appear to meet calc requirements
*/
#if defined(CHK_C)
/*
* bit width of various signed integers
*
* These are standard for c23 or later.
* Compute them for earlier compiler standards.
*/
#if !defined(INT8_WIDTH)
#define INT8_WIDTH (sizeof(int8_t) * CALC_CHARBIT)
#endif /* INT8_WIDTH */
#if !defined(INT16_WIDTH)
#define INT16_WIDTH (sizeof(int16_t) * CALC_CHARBIT)
#endif /* INT16_WIDTH */
#if !defined(INT32_WIDTH)
#define INT32_WIDTH (sizeof(int32_t) * CALC_CHARBIT)
#endif /* INT32_WIDTH */
#if !defined(INT64_WIDTH)
#define INT64_WIDTH (sizeof(int64_t) * CALC_CHARBIT)
#endif /* INT64_WIDTH */
#if !defined(INTPTR_WIDTH)
#define INTPTR_WIDTH (sizeof(intptr_t) * CALC_CHARBIT)
#endif /* INTPTR_WIDTH */
#if !defined(INTMAX_WIDTH)
#define INTMAX_WIDTH (sizeof(intmax_t) * CALC_CHARBIT)
#endif /* INTMAX_WIDTH */
/*
* bit width of various unsigned integers
*
* These are standard for c23 or later.
* Compute them for earlier compiler standards.
*/
#if !defined(UINT8_WIDTH)
#define UINT8_WIDTH (sizeof(uint8_t) * CALC_CHARBIT)
#endif /* UINT8_WIDTH */
#if !defined(UINT16_WIDTH)
#define UINT16_WIDTH (sizeof(uint16_t) * CALC_CHARBIT)
#endif /* UINT16_WIDTH */
#if !defined(UINT32_WIDTH)
#define UINT32_WIDTH (sizeof(uint32_t) * CALC_CHARBIT)
#endif /* UINT32_WIDTH */
#if !defined(UINT64_WIDTH)
#define UINT64_WIDTH (sizeof(uint64_t) * CALC_CHARBIT)
#endif /* UINT64_WIDTH */
#if !defined(UINTPTR_WIDTH)
#define UINTPTR_WIDTH (sizeof(uintptr_t) * CALC_CHARBIT)
#endif /* UINTPTR_WIDTH */
#if !defined(UINTMAX_WIDTH)
#define UINTMAX_WIDTH (sizeof(uintmax_t) * CALC_CHARBIT)
#endif /* UINTMAX_WIDTH */
/*
* case: C compiler and/or select include files do not meet calc requirements
*/
#else /* CHK_C */
/*
* calc v2 is the last version where one might be able to use an old C compiler
* and/or tolerate missing include files
*/
#if defined(CALC2_COMPAT)
/*
* bit width of various signed integers
*
* These are standard for c23 or later.
* Compute them for earlier compiler standards.
*/
#if !defined(INT8_WIDTH)
#define INT8_WIDTH (8)
#endif /* INT8_WIDTH */
#if !defined(INT16_WIDTH)
#define INT16_WIDTH (16)
#endif /* INT16_WIDTH */
#if !defined(INT32_WIDTH)
#define INT32_WIDTH (32)
#endif /* INT32_WIDTH */
#if !defined(INT64_WIDTH)
#define INT64_WIDTH (64)
#endif /* INT64_WIDTH */
#if !defined(INTPTR_WIDTH)
#define INTPTR_WIDTH (64)
#endif /* INTPTR_WIDTH */
#if !defined(INTMAX_WIDTH)
#define INTMAX_WIDTH (64)
#endif /* INTMAX_WIDTH */
/*
* bit width of various unsigned integers
*
* These are standard for c23 or later.
* Compute them for earlier compiler standards.
*/
#if !defined(UINT8_WIDTH)
#define UINT8_WIDTH (8)
#endif /* UINT8_WIDTH */
#if !defined(UINT16_WIDTH)
#define UINT16_WIDTH (16)
#endif /* UINT16_WIDTH */
#if !defined(UINT32_WIDTH)
#define UINT32_WIDTH (32)
#endif /* UINT32_WIDTH */
#if !defined(UINT64_WIDTH)
#define UINT64_WIDTH (64)
#endif /* UINT64_WIDTH */
#if !defined(UINTPTR_WIDTH)
#define UINTPTR_WIDTH (64)
#endif /* UINTPTR_WIDTH */
#if !defined(UINTMAX_WIDTH)
#define UINTMAX_WIDTH (64)
#endif /* UINTMAX_WIDTH */
#else /* CALC2_COMPAT */
/*
* CHK_C undefuned and not calc v2 is an error
*/
#pragma GCC error "CHK_C undefined: C compiler and/or select include files do not meet calc requirements"
#pragma GCC error "CALC2_COMPAT undefined: calc v3 and later requires CHK_C"
#endif /* CALC2_COMPAT */
#endif /* CHK_C */
#endif /* !INCLUDE_INT_H */

View File

@@ -27,6 +27,7 @@
#include <stdio.h> #include <stdio.h>
#include "int.h"
#include "alloc.h" #include "alloc.h"
#include "zmath.h" #include "zmath.h"