From 771addd8b749f86ba576cc3ec2b96e5c2fb34de5 Mon Sep 17 00:00:00 2001 From: Jordy Aaldering Date: Thu, 22 Jan 2026 13:20:53 +0100 Subject: [PATCH 1/3] ArrayIO index types --- src/stdio/src/ArrayIO/ArrayIO.h | 28 ++ src/stdio/src/ArrayIO/PrintArray.c | 414 +++++++++++++---------------- src/stdio/src/ArrayIO/ShowArray.c | 103 ++++--- 3 files changed, 263 insertions(+), 282 deletions(-) create mode 100644 src/stdio/src/ArrayIO/ArrayIO.h diff --git a/src/stdio/src/ArrayIO/ArrayIO.h b/src/stdio/src/ArrayIO/ArrayIO.h new file mode 100644 index 00000000..5f7c5824 --- /dev/null +++ b/src/stdio/src/ArrayIO/ArrayIO.h @@ -0,0 +1,28 @@ +#ifndef _SAC_ARRAYIO_H_ +#define _SAC_ARRAYIO_H_ + +#include +#include +#include + +#include "sac.h" +#include "sacinterface.h" + +#define INT 1 +#define FLOAT 2 +#define DOUBLE 3 +#define CHAR 4 +#define BOOL 5 +#define BYTE 6 +#define SHORT 7 +#define LONG 8 +#define LONGLONG 9 +#define UBYTE 10 +#define USHORT 11 +#define UINT 12 +#define ULONG 13 +#define ULONGLONG 14 + +typedef char* string; + +#endif /* _SAC_ARRAYIO_H_ */ diff --git a/src/stdio/src/ArrayIO/PrintArray.c b/src/stdio/src/ArrayIO/PrintArray.c index 9ccad082..5b615bc0 100644 --- a/src/stdio/src/ArrayIO/PrintArray.c +++ b/src/stdio/src/ArrayIO/PrintArray.c @@ -1,249 +1,211 @@ /* - * Implementation of printing functions for Arrays as used in ArrayIO + * Implementation of printing functions for Arrays as used in ArrayIO. */ -#include -#include -#include - -#include "sac.h" - -#define INT 1 -#define FLOAT 2 -#define DOUBLE 3 -#define CHAR 4 -#define BOOL 5 -#define BYTE 6 -#define SHORT 7 -#define LONG 8 -#define LONGLONG 9 -#define UBYTE 10 -#define USHORT 11 -#define UINT 12 -#define ULONG 13 -#define ULONGLONG 14 +#include "ArrayIO.h" -/* - * Code generation macros - */ - -#define PRINT_CASE( constant, ctype, ftype) \ - case constant: \ - fprintf(stream, format, \ - (ftype)((ctype *)a)[Index2Offset( dim, shp, index)]); \ +#define PRINT_CASE(constant, ctype, ftype) \ +case constant: \ + fprintf(stream, format, (ftype)((ctype *)a)[Index2Offset(dim, shp, index)]);\ break; -#define PRINT_FUNS( name_prefix, ctype, constant, default_format) \ -void ARRAYIO__Print##name_prefix##Array( FILE *stream, int dim, \ - int * shp, ctype * a) \ -{ \ - PrintArr(stream, constant, default_format, dim, shp, a); \ -} \ - \ -void ARRAYIO__Print##name_prefix##ArrayFormat( FILE *stream, string format, int dim, \ - int * shp, ctype * a) \ -{ \ - PrintArr(stream, constant, format, dim, shp, a); \ +#define PRINT_FUNS(name_prefix, ctype, constant, default_format) \ +void ARRAYIO__Print##name_prefix##Array(FILE *stream, sac_int dim, \ + sac_int *shp, ctype *a) \ +{ \ + PrintArr(stream, constant, default_format, dim, shp, a); \ +} \ + \ +void ARRAYIO__Print##name_prefix##ArrayFormat(FILE *stream, string format, \ + sac_int dim, sac_int *shp, ctype *a)\ +{ \ + PrintArr(stream, constant, format, dim, shp, a); \ } -typedef char* string; - static -int Index2Offset( int dim, int *shp, int *index) +sac_int Index2Offset(sac_int dim, sac_int *shp, sac_int *index) { - int i,n; - int offset=0; - int fact; - - for (i=0; i\n"); - - /* - * Now, element_count carries the total number of elements - * to be expected behind *a! - */ - - if( dim == 0) { - switch(typeflag) { - case BOOL: - fprintf(stream, format , ((bool *)a)[0]); - break; - case BYTE: - fprintf(stream, format , ((char *)a)[0]); - break; - case SHORT: - fprintf(stream, format , ((short *)a)[0]); - break; - case INT: - fprintf(stream, format , ((int *)a)[0]); - break; - case LONG: - fprintf(stream, format , ((long *)a)[0]); - break; - case LONGLONG: - fprintf(stream, format , ((long long *)a)[0]); - break; - case UBYTE: - fprintf(stream, format , ((unsigned char *)a)[0]); - break; - case USHORT: - fprintf(stream, format , ((unsigned short *)a)[0]); - break; - case UINT: - fprintf(stream, format , ((unsigned int *)a)[0]); - break; - case ULONG: - fprintf(stream, format , ((unsigned long *)a)[0]); - break; - case ULONGLONG: - fprintf(stream, format , ((unsigned long long *)a)[0]); - break; - case FLOAT: - fprintf(stream, format, (double)((float *)a)[0]); - break; - case DOUBLE: - fprintf(stream, format, ((double *)a)[0]); - break; - case CHAR: - fprintf(stream, format, ((char *)a)[0]); /* TODO: Isn't CHAR compiled to unsigned char? */ - break; - default: - SAC_RuntimeError ("illegal typeflag %d", typeflag); - } - fprintf(stream,"\n"); - - } else if (dim > 0) { - if (element_count == 0) { - fprintf(stream, "<>\n"); - } else { + fprintf(stream, "Dimension: %2"PRIisac"\n", dim); + fprintf(stream, "Shape : <"); - index = SAC_MALLOC (dim * sizeof (int)); - for (i=0; i "); - } - else { - fprintf(stream, "| "); + fprintf(stream, ">\n"); + + /* + * Now, element_count carries the total number of elements + * to be expected behind *a! + */ + if(dim == 0) { + switch(typeflag) { + case BOOL: + fprintf(stream, format, ((bool *)a)[0]); + break; + case BYTE: + fprintf(stream, format, ((char *)a)[0]); + break; + case SHORT: + fprintf(stream, format, ((short *)a)[0]); + break; + case INT: + fprintf(stream, format, ((sac_int *)a)[0]); + break; + case LONG: + fprintf(stream, format, ((long *)a)[0]); + break; + case LONGLONG: + fprintf(stream, format, ((long long *)a)[0]); + break; + case UBYTE: + fprintf(stream, format, ((unsigned char *)a)[0]); + break; + case USHORT: + fprintf(stream, format, ((unsigned short *)a)[0]); + break; + case UINT: + fprintf(stream, format, ((unsigned int *)a)[0]); + break; + case ULONG: + fprintf(stream, format, ((unsigned long *)a)[0]); + break; + case ULONGLONG: + fprintf(stream, format, ((unsigned long long *)a)[0]); + break; + case FLOAT: + fprintf(stream, format, (double)((float *)a)[0]); + break; + case DOUBLE: + fprintf(stream, format, ((double *)a)[0]); + break; + case CHAR: + // TODO: Isn't CHAR compiled to unsigned char? + fprintf(stream, format, ((char *)a)[0]); + break; + default: + SAC_RuntimeError("illegal typeflag %d", typeflag); } - while(( n>0) && (index[n]>=(shp[n]-1))) { - index[n]=0; - n -= 2; - if(n<0) { - space="\n"; - n=((dim-2)/2)*2; - } - fprintf(stream, "%s",space); - } - if(( n>=0) && (index[n]<(shp[n]-1))) { - index[n]++; - n=dim-1; - space=" "; + fprintf(stream, "\n"); + } else if (dim > 0) { + if (element_count == 0) { + fprintf(stream, "<>\n"); + } else { + sac_int *index = SAC_MALLOC((size_t)dim * sizeof(sac_int)); + for (sac_int i = 0; i < dim; i++) { + index[i] = 0; + } + + sac_int n = dim - 1; + + /* + * Now, we have: + * index == [0,...,0] + * 0,...,n + */ + do { + if ((dim % 2) == 1) { + fprintf(stream, "<"); + } else { + fprintf(stream, "|"); + } + + while (index[n] < shp[dim - 1]) { + switch(typeflag) { + PRINT_CASE(BOOL, bool, bool) + PRINT_CASE(BYTE, char, char) + PRINT_CASE(SHORT, short, short) + PRINT_CASE(INT, sac_int, sac_int) + PRINT_CASE(LONG, long, long) + PRINT_CASE(LONGLONG, long long, long long) + PRINT_CASE(UBYTE, unsigned char, unsigned char) + PRINT_CASE(USHORT, unsigned short, unsigned short) + PRINT_CASE(UINT, unsigned int, unsigned int) + PRINT_CASE(ULONG, unsigned long, unsigned long) + PRINT_CASE(ULONGLONG, unsigned long long, unsigned long long) + PRINT_CASE(FLOAT, float, double) + PRINT_CASE(DOUBLE, double, double) + PRINT_CASE(CHAR, char, char) /* TODO: Isn't CHAR compiled to unsigned char? */ + default: + SAC_RuntimeError ("illegal typeflag %d", typeflag); + } + + index[n] += 1; + } + + if ((dim % 2) == 1) { + index[n] = 0; + fprintf(stream, "> "); + n -= 1; + } else { + fprintf(stream, "| "); + } + + char *space = " "; + while((n > 0) && (index[n] >= (shp[n] - 1))) { + index[n] = 0; + n -= 2; + + if (n < 0) { + space = "\n"; + n = ((dim - 2) / 2) * 2; + } + + fprintf(stream, "%s", space); + } + + if((n >= 0) && (index[n] < (shp[n] - 1))) { + index[n] += 1; + n = dim - 1; + space = " "; + } + } while(n > 0); + + fprintf(stream, "\n"); + SAC_FREE(index); } - } while( n>0); - - fprintf(stream, "\n"); - - SAC_FREE(index); - } /* if (element_count == 0) */ - } else { - SAC_RuntimeError ("Dimension is less than 0, aborting!"); - } + } else { + SAC_RuntimeError ("Dimension is less than 0, aborting!"); + } } - -PRINT_FUNS( Bool, bool, BOOL, "%2i ") - -PRINT_FUNS( Double, double, DOUBLE, "%e ") - -PRINT_FUNS( Float, float, FLOAT, "%4f ") - -PRINT_FUNS( Byte, char, BYTE, "%2hd ") - -PRINT_FUNS( Short, short, SHORT, "%2hd ") - -PRINT_FUNS( Int, int, INT, "%2i ") - -PRINT_FUNS( Long, long, LONG, "%2ld ") - -PRINT_FUNS( Longlong, long long, LONGLONG, "%2lld ") - -PRINT_FUNS( Ubyte, unsigned char, UBYTE, "%2hu ") - -PRINT_FUNS( Ushort, unsigned short, USHORT, "%2hu ") - -PRINT_FUNS( Uint, unsigned int, UINT, "%2u ") - -PRINT_FUNS( Ulong, unsigned long, ULONG, "%2lu ") - -PRINT_FUNS( Ulonglong, unsigned long long, ULONGLONG, "%2llu ") - -PRINT_FUNS( Char, char, CHAR, "%c") - +PRINT_FUNS(Bool, bool, BOOL, "%2i ") +PRINT_FUNS(Double, double, DOUBLE, "%e ") +PRINT_FUNS(Float, float, FLOAT, "%4f ") +PRINT_FUNS(Byte, char, BYTE, "%2hd ") +PRINT_FUNS(Short, short, SHORT, "%2hd ") +PRINT_FUNS(Int, sac_int, INT, "%2"PRIisac" ") +PRINT_FUNS(Long, long, LONG, "%2ld ") +PRINT_FUNS(Longlong, long long, LONGLONG, "%2lld ") +PRINT_FUNS(Ubyte, unsigned char, UBYTE, "%2hu ") +PRINT_FUNS(Ushort, unsigned short, USHORT, "%2hu ") +PRINT_FUNS(Uint, unsigned int, UINT, "%2u ") +PRINT_FUNS(Ulong, unsigned long, ULONG, "%2lu ") +PRINT_FUNS(Ulonglong, unsigned long long, ULONGLONG, "%2llu ") +PRINT_FUNS(Char, char, CHAR, "%c") diff --git a/src/stdio/src/ArrayIO/ShowArray.c b/src/stdio/src/ArrayIO/ShowArray.c index 76791a26..0f28facd 100644 --- a/src/stdio/src/ArrayIO/ShowArray.c +++ b/src/stdio/src/ArrayIO/ShowArray.c @@ -1,75 +1,66 @@ -/* $Id$ */ - /* - * Implementation of printing functions for Arrays as used in ArrayIO, - * conforming with ISO Standard APL N8485. - * The show() functions print data only - no rank, shape, or decorators - * + * Implementation of printing functions for Arrays as used in ArrayIO, + * conforming with ISO Standard APL N8485. + * The show() functions print data only - no rank, shape, or decorators. */ - -#include -#include -#include - -#include "sac.h" - -#define INT 1 -#define FLOAT 2 -#define DOUBLE 3 -#define CHAR 4 -#define BOOL 5 - -typedef char* string; +#include "ArrayIO.h" static -void ShowArr(FILE *stream, int typeflag, string fmt, int dim, int * shp, char *a) +void ShowArr(FILE *stream, string fmt, sac_int dim, sac_int *shp, char *a) { - // Introduce new lines at end of ultimate and penultimate dimensions - int i, element_count; - int rownum, colnum, planenum; - int rownums, colnums, planenums; - int rowoffset = 0; - - planenums = 1; - for (i=0;i<(dim-2);i++) { - planenums = planenums * shp[i]; - } + // Introduce new lines at end of ultimate and penultimate dimensions + sac_int element_count; + sac_int rownums, colnums, planenums; + sac_int rowoffset = 0; + planenums = 1; + for (sac_int i = 0; i < (dim - 2); i++) { + planenums = planenums * shp[i]; + } - if( 0 == dim) { // show scalar + if (dim == 0) { + // Show scalar fprintf(stream, fmt, a[0]); - fprintf(stream, "\n"); // End row with new line - } else { - colnums = shp[dim-1]; - if (dim >= 2) { // row count for matrix and tensor - rownums = shp[dim-2]; + // End row with new line + fprintf(stream, "\n"); } else { - rownums = 1; // vector row count - } + colnums = shp[dim - 1]; + if (dim >= 2) { + // Row count for matrix and tensor + rownums = shp[dim - 2]; + } else { + // Vector row count + rownums = 1; + } - element_count = planenums * colnums * rownums; + element_count = planenums * colnums * rownums; - if (element_count != 0) { // no output for empty array - for ( planenum=0; planenum Date: Thu, 22 Jan 2026 13:59:58 +0100 Subject: [PATCH 2/3] fix argument mismatch --- src/stdio/src/ArrayIO/ShowArray.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stdio/src/ArrayIO/ShowArray.c b/src/stdio/src/ArrayIO/ShowArray.c index 0f28facd..8944e650 100644 --- a/src/stdio/src/ArrayIO/ShowArray.c +++ b/src/stdio/src/ArrayIO/ShowArray.c @@ -62,5 +62,5 @@ void ShowArr(FILE *stream, string fmt, sac_int dim, sac_int *shp, char *a) void ARRAYIO__ShowCharArray(FILE *stream, sac_int dim, sac_int *shp, char *a) { - ShowArr(stream, CHAR, "%c", dim, shp, a); + ShowArr(stream, "%c", dim, shp, a); } From 9f4887fbd0c8675c2f9c507a560542151da512b4 Mon Sep 17 00:00:00 2001 From: Jordy Aaldering Date: Thu, 22 Jan 2026 14:00:40 +0100 Subject: [PATCH 3/3] typeflag is int --- src/stdio/src/ArrayIO/PrintArray.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stdio/src/ArrayIO/PrintArray.c b/src/stdio/src/ArrayIO/PrintArray.c index 5b615bc0..0aa74ec2 100644 --- a/src/stdio/src/ArrayIO/PrintArray.c +++ b/src/stdio/src/ArrayIO/PrintArray.c @@ -40,7 +40,7 @@ sac_int Index2Offset(sac_int dim, sac_int *shp, sac_int *index) return offset; } -static void PrintArr(FILE *stream, sac_int typeflag, string format, +static void PrintArr(FILE *stream, int typeflag, string format, sac_int dim, sac_int *shp, void *a) { fprintf(stream, "Dimension: %2"PRIisac"\n", dim);