diff options
| author | Cliff Click <cliffc@acm.org> | 2026-11-15 16:11:39 -0800 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-11-15 16:11:39 -0800 |
| commit | 9fe7c6f9697b5bbf3041261fdc60040e75cc674f (patch) | |
| tree | d92a8ff20ed984938d90ad8917b4d67fba9fb745 /chapter32/src/test/java/com/seaofnodes/simple/progs | |
| parent | f0a6c6757a795b8e186de0076836a87acef59fa7 (diff) | |
| download | Simple-ch32.tar.gz Simple-ch32.zip | |
Split off chapter 31ch32
Ch 32 will be focused on compiler performance issues.
Diffstat (limited to 'chapter32/src/test/java/com/seaofnodes/simple/progs')
21 files changed, 449 insertions, 0 deletions
diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/arg_count.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/arg_count.c new file mode 100644 index 00000000..e247e28a --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/arg_count.c @@ -0,0 +1,10 @@ +#include <stdio.h> + +__attribute__ ((CALL_CONV)) +extern double addAll( long long i0, double f1, long long i2, double f3, long long i4, double f5, long long i6, double f7, long long i8, double f9, long long i10, double f11, long long i12, double f13, long long i14, double f15, long long i16, double f17, long long i18, double f19 ); + +int main() { + double result = addAll( 0, 1.1, 2, 3.1, 4, 5.1, 6, 7.1, 8, 9.1, 10, 11.1, 12, 13.1, 14, 15.1, 16, 17.1, 18, 19.1 ); + printf("%f\n", result); + return 0; +} diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/brain_fuck.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/brain_fuck.c new file mode 100644 index 00000000..127cbe73 --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/brain_fuck.c @@ -0,0 +1,18 @@ +#include <stdio.h> +#include <stdbool.h> + +typedef struct AryInt { + unsigned int len; + unsigned char data[]; +} AryInt; + +__attribute__ ((CALL_CONV)) +extern AryInt* brain_fuck(); + +int main() { + AryInt* ps = brain_fuck(); + + printf("%.*s", ps->len, ps->data); + + return 0; +} diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/c_returns.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/c_returns.c new file mode 100644 index 00000000..6d0ff2fe --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/c_returns.c @@ -0,0 +1,28 @@ +#include <stdint.h> +#include <stdio.h> + +uint64_t c_bits; +// Legal narrow C returns: unused RAX bits deliberately contain garbage. +#define RET(name) __asm__(".text\n.globl " #name "\n" #name ":\nmovq c_bits(%rip), %rax\nret\n"); +RET(c_i8) RET(c_u8) RET(c_i16) RET(c_u16) RET(c_i32) RET(c_u32) RET(c_i64) +#define DECL(name) extern int64_t __attribute__((CALL_CONV)) name(void); +DECL(test_i8) DECL(test_u8) DECL(test_i16) DECL(test_u16) +DECL(test_i32) DECL(test_u32) DECL(test_i64) DECL(test_cmp) + +int main(void) { + uint64_t values[] = {0xa5a5a5a500000000ULL, 0xa5a5a5a50000007fULL, + 0xa5a5a5a500000080ULL, 0xa5a5a5a500007fffULL, 0xa5a5a5a500008000ULL, + 0xa5a5a5a57fffffffULL, 0xa5a5a5a580000000ULL, 0xa5a5a5a5ffffffffULL}; + for(unsigned i=0; i<sizeof(values)/sizeof(values[0]); i++) { + c_bits=values[i]; + int64_t actual[]={test_i8(),test_u8(),test_i16(),test_u16(),test_i32(),test_u32(),test_i64(),test_cmp()}; + int64_t expected[]={(int8_t)c_bits,(uint8_t)c_bits,(int16_t)c_bits,(uint16_t)c_bits, + (int32_t)c_bits,(uint32_t)c_bits,(int64_t)c_bits,(int32_t)c_bits==-1}; + for(unsigned j=0; j<8; j++) + if(actual[j]!=expected[j]) { + printf("case %u type %u: %lld != %lld\n",i,j,(long long)actual[j],(long long)expected[j]); + return 1; + } + } + return 0; +} diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/extern_data.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/extern_data.c new file mode 100644 index 00000000..e8156d0f --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/extern_data.c @@ -0,0 +1,29 @@ +#include <stdint.h> +#include <stdio.h> + +int32_t counter=7; +int8_t small=-3; +double fraction=1.25; + +int64_t bump(void) { counter+=5; return counter; } +__attribute__((CALL_CONV)) extern int64_t test_global(void); +__attribute__((CALL_CONV)) extern int64_t extern_matches(int32_t); +__attribute__((CALL_CONV)) extern int64_t extern_less(int32_t); +__attribute__((CALL_CONV)) extern int64_t extern_seven(void); + +int main(void) { + if (!extern_matches(7) || extern_matches(8) || !extern_seven() || + !extern_less(6) || extern_less(7) || extern_less(8)) return 2; + int64_t result=test_global(); + if(result!=71515 || counter!=15 || small!=-2 || fraction!=1.75) { + printf("result=%lld counter=%d small=%d fraction=%g\n", + (long long)result,counter,small,fraction); + return 1; + } + // A second call must observe C's new values, not a Simple initialization copy. + counter=-2; + if (!extern_matches(-2) || extern_matches(7) || extern_seven() || + !extern_less(-3) || extern_less(-2)) return 3; + result=test_global(); + return result!=-19394 || counter!=6 || small!=-1 || fraction!=2.25; +} diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/extern_data_import.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/extern_data_import.c new file mode 100644 index 00000000..f65e5c84 --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/extern_data_import.c @@ -0,0 +1,14 @@ +#include <stdint.h> +#include <stdio.h> + +int32_t counter=7; +__attribute__((CALL_CONV)) extern int64_t test_global(void); + +int main(void) { + int64_t result=test_global(); + if(result!=710 || counter!=10) { + printf("result=%lld counter=%d\n",(long long)result,counter); + return 1; + } + return 0; +} diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/fib.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/fib.c new file mode 100644 index 00000000..263d2f13 --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/fib.c @@ -0,0 +1,16 @@ +#include <stdio.h> +#include <stdbool.h> + + +__attribute__ ((CALL_CONV)) +extern int fib(int x); + +int main() { + printf("["); + for (int i = 0; i < 10; i++) { + printf("%d", fib(i)); + if (i < 9) printf(", "); + } + printf("]"); + return 0; +}
\ No newline at end of file diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/floatToInteger.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/floatToInteger.c new file mode 100644 index 00000000..cf9390d8 --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/floatToInteger.c @@ -0,0 +1,25 @@ +#include <stdint.h> +#include <math.h> +#include <stdio.h> + +extern int64_t float_to_int(double); +extern int64_t float_to_u8(double); + +int main(void) { + double values[] = {0.0,-0.0,6.92,-6.92,0.99,-0.99,255.75,-257.75, + 0x1p63,0x1.fffffffffffffp62,-0x1p63,-0x1.0000000000001p63, + INFINITY,-INFINITY,NAN,-NAN}; + for( unsigned i=0; i<sizeof(values)/sizeof(values[0]); i++ ) { + double x=values[i]; + int64_t expected=isnan(x) ? 0 : x>=0x1p63 ? INT64_MAX + : x<=-0x1p63 ? INT64_MIN : (int64_t)x; + int64_t actual=float_to_int(x), narrow=float_to_u8(x); + if( actual!=expected || narrow!=(expected&255) ) { + fprintf(stderr,"conversion of %g: got %lld / %lld, expected %lld / %lld\n", + x,(long long)actual,(long long)narrow, + (long long)expected,(long long)(expected&255)); + return 1; + } + } + return 0; +} diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/int32.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/int32.c new file mode 100644 index 00000000..9694c47b --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/int32.c @@ -0,0 +1,26 @@ +#include <stdint.h> +#include <stdio.h> + +typedef struct { int32_t x, y; } Pair; +typedef struct { uint32_t len; int32_t values[]; } Ints; + +__attribute__((CALL_CONV)) extern int64_t int_echo(int32_t); +__attribute__((CALL_CONV)) extern int64_t int_wrap(int32_t); +__attribute__((CALL_CONV)) extern int64_t wide_echo(int64_t); +__attribute__((CALL_CONV)) extern int64_t narrow_sum(int8_t,uint8_t,int16_t,uint16_t,int32_t,uint32_t,int32_t,uint32_t); +__attribute__((CALL_CONV)) extern Pair *int_pair(void); +__attribute__((CALL_CONV)) extern Ints *int_array(void); + +int main(void) { + if (int_echo(-1) != -1 || int_echo(INT32_MIN) != INT32_MIN) return 1; + if (int_wrap(INT32_MAX) != INT32_MIN) return 2; + if (wide_echo(INT64_MAX) != INT64_MAX || wide_echo(INT64_MIN) != INT64_MIN) return 3; + int64_t expected = -128LL + 255 - 32768 + 65535 + INT32_MIN + UINT32_MAX - 7 + UINT32_MAX; + if (narrow_sum(-128,255,-32768,65535,INT32_MIN,UINT32_MAX,-7,UINT32_MAX) != expected) return 4; + Pair *p = int_pair(); + if (p->x != -1 || p->y != INT32_MAX) return 5; + Ints *a = int_array(); + if (a->len != 3 || a->values[0] != -1 || a->values[1] != INT32_MAX || a->values[2] != INT32_MIN) return 6; + puts("ok"); + return 0; +} diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/loop2.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/loop2.c new file mode 100644 index 00000000..934f2d89 --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/loop2.c @@ -0,0 +1,7 @@ + +__attribute__ ((CALL_CONV)) +extern int loop2(); + +int main() { + return loop2(); +} diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/newtonFloat.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/newtonFloat.c new file mode 100644 index 00000000..7e7e796d --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/newtonFloat.c @@ -0,0 +1,16 @@ +#include <stdio.h> +#include <math.h> +#include <stdlib.h> + +extern double test_sqrt(double); + +int main( int argc, char** argv ) { + for( int i=0; i<10; i++ ) { + double d = test_sqrt((double)i), expect = sqrt((double)i); + double delta = fabs(d-expect); + printf("%d %f (%g)\n",i,d,delta); + if( delta > 1e-15 ) + return 1; + } + return 0; +} diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/person.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/person.c new file mode 100644 index 00000000..df95e1ca --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/person.c @@ -0,0 +1,34 @@ +#include <stdio.h> +#include <stdbool.h> +#include <stdlib.h> + +typedef struct Person { + int age; +} Person; + +typedef struct PersonArray { + unsigned int length; + int pad; + Person* data[]; +} PersonArray; + +__attribute__ ((CALL_CONV)) +extern void fcn(PersonArray* people, int index); + +int main() { + Person base[5] = {{25},{1},{18},{45},{60}}; + unsigned char buf[8+8*5]; + PersonArray* people_array = (PersonArray*)buf; + people_array->length=5; + for (int i=0; i<people_array->length; i++) people_array->data[i] = &base[i]; + + fcn(people_array, 1); + fcn(people_array, 1); + fcn(people_array, 1); + fcn(people_array, 1); + fcn(people_array, 1); + + printf("%d\n", people_array->data[1]->age); // expected: 6 + + return 0; +}
\ No newline at end of file diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/runtime_args.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/runtime_args.c new file mode 100644 index 00000000..cd676006 --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/runtime_args.c @@ -0,0 +1,35 @@ +#include <stdint.h> +#include <stdio.h> +#include <string.h> + +typedef struct { uint32_t len; unsigned char bytes[]; } Bytes; +typedef struct { uint32_t len, pad; Bytes *args[]; } Args; + +__attribute__((CALL_CONV)) +extern Args *simple_args(int64_t argc, char **argv); + +const char *simple_argv(char **argv, int64_t index) { return argv[index]; } + +int main(void) { + Args *empty = simple_args(0, NULL); + if (!empty || empty->len != 0) return 1; + if (simple_args(-1, NULL) != NULL) return 2; + if (simple_args(4294967296LL, NULL) != NULL) return 6; + + char name[] = "program", blank[] = "", utf8[] = "\xc3\xa9\xcf\x80"; + char *argv[] = { name, blank, utf8, name }; + Args *args = simple_args(4, argv); + if (!args || args->len != 4) return 3; + for (int i = 0; i < 4; i++) { + if (!args->args[i] || args->args[i]->len != strlen(argv[i]) || + memcmp(args->args[i]->bytes, argv[i], args->args[i]->len)) return 4; + } + // Copies survive changes to the native buffers, and repeated argv pointers + // still produce independent Simple arrays. + name[0] = 'X'; + utf8[0] = 0; + args->args[0]->bytes[0] = 'Y'; + if (args->args[3]->bytes[0] != 'p' || args->args[2]->bytes[0] != 0xc3) return 5; + puts("ok"); + return 0; +} diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/sieve.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/sieve.c new file mode 100644 index 00000000..cac4cfa3 --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/sieve.c @@ -0,0 +1,19 @@ +#include <stdio.h> +#include <stdbool.h> + +typedef struct AryInt { + unsigned int len; + unsigned int data[]; +} AryInt; + +__attribute__ ((CALL_CONV)) +extern AryInt *sieve(int N); + +int main() { + AryInt* ps = sieve(100); + printf("%d[",ps->len); + for( int i=0; i<ps->len; i++ ) + printf("%d, ",ps->data[i]); + printf("]"); + return 0; +} diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/sort.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/sort.c new file mode 100644 index 00000000..08acf2da --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/sort.c @@ -0,0 +1,28 @@ +#include <stdio.h> +#include <stdlib.h> + +// Test wrapper to call `sort.smp` + +typedef struct AryInt { + unsigned int len; + int data[]; +} AryInt; + +__attribute__ ((CALL_CONV)) +extern AryInt* merge_sort(AryInt*); + + +int primes[] = { 25, 97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2 }; + +int main() { + AryInt *as = (AryInt*)primes; + + merge_sort(as); + + printf("%d[",as->len); + for( int i=0; i<as->len; i++ ) + printf("%d, ",as->data[i]); + printf("]"); + + return 0; +}
\ No newline at end of file diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/stacked_r_10x.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/stacked_r_10x.c new file mode 100644 index 00000000..d44d5387 --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/stacked_r_10x.c @@ -0,0 +1,13 @@ +#include <stdio.h> +#include <stdbool.h> +#include <stdlib.h> + +__attribute__ ((CALL_CONV)) +extern int stack10(int x); + +int main() { +int result = stack10(0); + +printf("%d", result); +return 0; +}
\ No newline at end of file diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/stacked_r_11x.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/stacked_r_11x.c new file mode 100644 index 00000000..d71979d1 --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/stacked_r_11x.c @@ -0,0 +1,13 @@ +#include <stdio.h> +#include <stdbool.h> +#include <stdlib.h> + +__attribute__ ((CALL_CONV)) +extern int stack11(int x); + +int main() { +int result = stack11(1); + +printf("%d", result); +return 0; +}
\ No newline at end of file diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/stacked_r_12x.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/stacked_r_12x.c new file mode 100644 index 00000000..a4e425cb --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/stacked_r_12x.c @@ -0,0 +1,13 @@ +#include <stdio.h> +#include <stdbool.h> +#include <stdlib.h> + +__attribute__ ((CALL_CONV)) +extern int stack12(int x); + +int main() { +int result = stack12(0); + +printf("%d", result); +return 0; +}
\ No newline at end of file diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/stacked_r_13x.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/stacked_r_13x.c new file mode 100644 index 00000000..a3a51d0e --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/stacked_r_13x.c @@ -0,0 +1,13 @@ +#include <stdio.h> +#include <stdbool.h> +#include <stdlib.h> + +__attribute__ ((CALL_CONV)) +extern int stack13(int x); + +int main() { +int result = stack13(0); + +printf("%d", result); +return 0; +}
\ No newline at end of file diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/stacked_r_9x.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/stacked_r_9x.c new file mode 100644 index 00000000..4f93ba24 --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/stacked_r_9x.c @@ -0,0 +1,13 @@ +#include <stdio.h> +#include <stdbool.h> +#include <stdlib.h> + +__attribute__ ((CALL_CONV)) +extern int stack9(int x); + +int main() { +int result = stack9(0); + +printf("%d", result); +return 0; +}
\ No newline at end of file diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/stream_io.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/stream_io.c new file mode 100644 index 00000000..5cd1096a --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/stream_io.c @@ -0,0 +1,49 @@ +#include <errno.h> +#include <stdint.h> +#include <stddef.h> +#include <unistd.h> + +// Deterministic short I/O and failures; ordinary descriptors still use libc. +extern ssize_t __real_read(int, void *, size_t); +extern ssize_t __real_write(int, const void *, size_t); +static int mode, calls, bad; +static int64_t count, offset; + +int32_t test_io_mode(int32_t next, int64_t start) { + mode = next; + calls = bad = 0; + count = 0; + offset = start; + return 0; +} +int64_t test_io_count(void) { return count; } +int32_t test_io_status(void) { + return bad || (mode == 5 && (errno != EAGAIN || calls != 1)); +} + +ssize_t __wrap_read(int fd, void *buffer, size_t size) { + if (fd != 700) return __real_read(fd, buffer, size); + ++calls; + if (mode == 1 && calls == 1) { errno = EINTR; return -1; } + if (mode == 2 && calls > 1) { errno = EIO; return -1; } + if (count == 24593) return 0; + size_t n = size < 137 ? size : 137; + if (n > (size_t)(24593-count)) n = (size_t)(24593-count); + for (size_t i=0; i<n; ++i) ((unsigned char *)buffer)[i] = (count+i)%251; + count += n; + return (ssize_t)n; +} + +ssize_t __wrap_write(int fd, const void *buffer, size_t size) { + if (fd != 701) return __real_write(fd, buffer, size); + ++calls; + if (mode == 1 && calls == 1) { errno = EINTR; return -1; } + if (mode == 3 && calls > 1) { errno = EIO; return -1; } + if (mode == 4) return 0; + if (mode == 5) { errno = EAGAIN; return -1; } + size_t n = size < 7 ? size : 7; + for (size_t i=0; i<n; ++i) + if (((const unsigned char *)buffer)[i] != (offset+count+i)%251) bad = 1; + count += n; + return (ssize_t)n; +} diff --git a/chapter32/src/test/java/com/seaofnodes/simple/progs/stringHash.c b/chapter32/src/test/java/com/seaofnodes/simple/progs/stringHash.c new file mode 100644 index 00000000..6578c852 --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/progs/stringHash.c @@ -0,0 +1,30 @@ +#include <stdio.h> +#include <stdbool.h> + +typedef struct ArrayU8 { + unsigned int length; + unsigned char data[]; +} ArrayU8; +typedef struct String { + ArrayU8 * cs; + int _hashCode; +} String; + + +__attribute__ ((CALL_CONV)) +extern bool equals(String*st1, String*st2); + +int main() { + String test1 = {.cs=(ArrayU8*)"\x04\x00\x00\x00test"}; + String test2 = {.cs=(ArrayU8*)"\x05\x00\x00\x00test1"}; + + if( equals(&test1, &test2) ) { + printf("Expected test1 and test2 unequal"); + return 1; + } + if( !equals(&test2, &test2) ) { + printf("Expected test2 and test2 equal"); + return 1; + } + return 0; +} |
