summaryrefslogtreecommitdiff
path: root/src/main/smp/sys/ary.smp
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-08-10 11:11:18 -0700
committerCliff Click <cliffc@acm.org>2026-10-19 14:56:14 -0700
commit495c3fb44ff91c75f9258e5a34a8fbe5dcdbff9c (patch)
tree8085242d90310d32e1dc34d5e5b8fbfb9c81d439 /src/main/smp/sys/ary.smp
parentf613348651956d68e225ffc184dd6eabaea5326a (diff)
downloadSimple-495c3fb44ff91c75f9258e5a34a8fbe5dcdbff9c.tar.gz
Simple-495c3fb44ff91c75f9258e5a34a8fbe5dcdbff9c.zip
Chapter 25: Modules, Separate Compilation, and SSA Constructionlinear
Co-authored-by: Hsiang-Ying Fu <66062782+fuhsnn@users.noreply.github.com> Co-authored-by: fuhsnn <66062782+fuhsnn@users.noreply.github.com> Co-authored-by: Symious <14933944+symious@users.noreply.github.com> Co-authored-by: Jun Kawasaki <jun784@gmail.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Diffstat (limited to 'src/main/smp/sys/ary.smp')
-rw-r--r--src/main/smp/sys/ary.smp24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/main/smp/sys/ary.smp b/src/main/smp/sys/ary.smp
new file mode 100644
index 00000000..8312e94c
--- /dev/null
+++ b/src/main/smp/sys/ary.smp
@@ -0,0 +1,24 @@
+
+// Array utilities
+
+// Copy byte array contents. Copies the shorter of the buffers and len
+val copy = { u8[] dst, int doff, u8[~] src, int soff, int len ->
+ if( len > dst# - doff ) len = dst# - doff;
+ if( len > src# - soff ) len = src# - soff;
+ for( int i=0; i<len; i++ )
+ dst[i+doff] = src[i+soff];
+ return dst;
+};
+
+// Compare lexigraphically array contents. Compares up to shorter array or
+// len. Returns <0 or >0 for first non-matching byte. Returns <0 or >0 for
+// the larger array after that, or 0 if both arrays are fully equal.
+val cmp = { u8[~] src1, u32 off1, u8[~] src2, u32 off2, u32 len ->
+ u32 len1 = len > src1# - off1 ? src1# - off1 : len;
+ u32 len2 = len > src2# - off2 ? src2# - off2 : len;
+ u32 max = len1 > len2 ? len2 : len1;
+ for( int i=0; i<max; i++ )
+ if( src1[off1+i] != src2[off2+i] )
+ return src1[off1+i] - src2[off2+i]; // Bytes determine compare
+ return len1 - len2; // Length determines compare
+};