/* * Utilities for calculating index for vector and matrix * * Copyright (c) 2005 Shinji Tamura */ #ifndef FARRAY_INDEX_H #define FARRAY_INDEX_H #ifndef FARRAY_OFFSET #ifdef FARRAY_INIT0 #define FARRAY_OFFSET 0 #else #define FARRAY_OFFSET 1 #endif /* FARRAY_INIT0 */ #endif /* FARRAY_OFFSET */ #ifdef FARRAY_DEBUG #define FARRAY_ERROR_MSG "farray_index.h: subscript out of range\n" #ifdef __cplusplus #include #include #else #include #include #endif /* __cplusplus */ #endif /* FARRAY_DEBUG */ #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L #define FARRAY_C99 #endif /* C99 */ #if defined __cplusplus || defined FARRAY_C99 || defined USE_FARRAY_INLINE #define FARRAY_INLINE 1 #ifdef __cplusplus #define INLINE inline #else #define INLINE static inline #endif /* __cplusplus */ INLINE int idx1(int i) { #ifdef FARRAY_DEBUG if (i < FARRAY_OFFSET) { fprintf(stderr, FARRAY_ERROR_MSG); raise(SIGSEGV); } #endif /* FARRAY_DEBUG */ return (i - FARRAY_OFFSET); }; INLINE int idx2(int i, int j, int LDI) { #ifdef FARRAY_DEBUG if (i < FARRAY_OFFSET || i >= LDI + FARRAY_OFFSET || j < FARRAY_OFFSET) { fprintf(stderr, FARRAY_ERROR_MSG); raise(SIGSEGV); } #endif /* FARRAY_DEBUG */ return (i - FARRAY_OFFSET + (j - FARRAY_OFFSET) * LDI); }; #else /* can't use inline function */ #define FARRAY_INLINE 0 #ifdef FARRAY_DEBUG #define idx1(i) (((i) < FARRAY_OFFSET) ? (fprintf(stderr, FARRAY_ERROR_MSG), raise(SIGSEGV)) : ((i) - FARRAY_OFFSET)) #define idx2(i, j, LDI) (((i) < FARRAY_OFFSET || (i) >= (LDI) + FARRAY_OFFSET || (j) < FARRAY_OFFSET) ? (fprintf(stderr, FARRAY_ERROR_MSG), raise(SIGSEGV)) : ((i) - FARRAY_OFFSET + ((j) - FARRAY_OFFSET) * (LDI))) #else #define idx1(i) ((i) - FARRAY_OFFSET) #define idx2(i, j, LDI) ((i) - FARRAY_OFFSET + ((j) - FARRAY_OFFSET) * (LDI)) #endif /* FARRAY_DEBUG */ #endif /* __cplusplus || FARRAY_C99 || USE_FARRAY_INLINE */ #endif /* FARRAY_INDEX_H */