ref: 80771da12a271c80a9786f299ec464a7dbd11800
parent: 35424e72a0a8b9069b3f24bb33b62892ca4b66d1
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Sat Apr 5 01:39:29 EDT 2025
pass-by-value C integer types: simplify Assume all of them to be represented as signed to reduce branching.
--- a/src/builtins.c
+++ b/src/builtins.c
@@ -274,10 +274,8 @@
sl_v v = args[0];
if(isfixnum(v))
return v;
- if(isubnumu(v))
- return fixnum(ubnumuval(v));
- if(isubnums(v))
- return fixnum(ubnumsval(v));
+ if(isubnum(v))
+ return fixnum(ubnumval(v));
if(iscprim(v)){
void *p = ptr(v);
return fixnum(conv_to_s64(cp_data(p), cp_numtype(p)));
@@ -356,10 +354,8 @@
{
if(isfixnum(a))
return numval(a);
- if(isubnumu(a))
- return ubnumuval(a);
- if(isubnums(a))
- return ubnumsval(a);
+ if(isubnum(a))
+ return ubnumval(a);
if(iscprim(a)){
sl_cprim *cp = ptr(a);
sl_numtype nt = cp_numtype(cp);
--- a/src/cvalues.c
+++ b/src/cvalues.c
@@ -209,12 +209,9 @@
r = numval(a);
}else if(isrune(a)){
r = torune(a);
- }else if(isubnumu(a)){
- uintptr v = ubnumuval(a);
+ }else if(isubnum(a)){
+ sl_fx v = ubnumval(a);
r = conv_to_u32(&v, ubnumtype(a));
- }else if(isubnums(a)){
- intptr v = ubnumsval(a);
- r = conv_to_u32(&v, ubnumtype(a));
}else if(iscprim(a)){
sl_cprim *cp = ptr(a);
r = conv_to_u32(cp_data(cp), cp_numtype(cp));
@@ -233,12 +230,9 @@
USED(type); \
if(isfixnum(a)){ \
n = (ctype)numval(a); \
- }else if(isubnumu(a)){ \
- uintptr v = ubnumuval(a); \
+ }else if(isubnum(a)){ \
+ sl_fx v = ubnumval(a); \
n = (ctype)conv_to_##cnvt(&v, ubnumtype(a)); \
- }else if(isubnums(a)){ \
- intptr v = ubnumsval(a); \
- n = (ctype)conv_to_##cnvt(&v, ubnumtype(a)); \
}else if(iscprim(a)){ \
sl_cprim *cp = ptr(a); \
n = (ctype)conv_to_##cnvt(cp_data(cp), cp_numtype(cp)); \
@@ -297,7 +291,7 @@
sizeof(ctype) < sizeof(sl_v)){ \
ctype u; \
cvalue_##ctype##_init(sl_##typenam##type, args[0], &u); \
- v = (sl_v)u<<TAG_EXT_BITS | tag<<4 | TAG_UNBOXED; \
+ v = (sl_fx)u<<TAG_EXT_BITS | tag<<4 | TAG_UNBOXED; \
}else{ \
v = cprim(sl_##typenam##type, sizeof(ctype)); \
cvalue_##ctype##_init(sl_##typenam##type, args[0], cp_data(ptr(v))); \
@@ -321,8 +315,8 @@
{ \
sl_v v; \
if(tag < T_UNBOXED_NUM && \
- (sizeof(n) < sizeof(sl_v) || fits_bits(n, UNBOXED_BITS))){ \
- v = n<<TAG_EXT_BITS | tag<<4 | TAG_UNBOXED; \
+ (sizeof(n) < sizeof(sl_v) || fits_bits(n, UNBOXED_BITS-1))){ \
+ v = (sl_fx)n<<TAG_EXT_BITS | tag<<4 | TAG_UNBOXED; \
}else{ \
v = cprim(sl_##typenam##type, sizeof(n)); \
*(ctype*)cp_data(ptr(v)) = n; \
@@ -358,13 +352,8 @@
if(isfixnum(a))
n = vtomp(numval(a), nil);
else if(isubnum(a)){
- uintptr v;
- void *p = &v;
- if(isubnumu(a))
- v = ubnumuval(a);
- else
- v = ubnumsval(a);
- n = conv_to_mp(p, ubnumtype(a));
+ uintptr v = ubnumval(a);
+ n = conv_to_mp(&v, ubnumtype(a));
}else if(iscvalue(a)){
sl_cv *cv = ptr(a);
void *p = cv_data(cv);
@@ -422,12 +411,12 @@
tosize(sl_v n)
{
if(isfixnum(n))
- return (usize)numval(n);
+ return numval(n);
if(isubnum(n))
- return (usize)ubnumuval(n);
+ return ubnumval(n);
if(iscprim(n)){
sl_cprim *cp = ptr(n);
- if(sizeof(usize) == 8)
+ if(sizeof(usize) > 4)
return conv_to_u64(cp_data(cp), cp_numtype(cp));
return conv_to_u32(cp_data(cp), cp_numtype(cp));
}
@@ -439,13 +428,13 @@
{
if(isfixnum(n))
return numval(n);
- if(isubnumu(n))
- return ubnumuval(n);
- if(isubnums(n))
- return ubnumsval(n);
+ if(isubnum(n))
+ return ubnumval(n);
if(iscprim(n)){
sl_cprim *cp = ptr(n);
- return conv_to_s64(cp_data(cp), cp_numtype(cp));
+ if(sizeof(usize) > 4)
+ return conv_to_s64(cp_data(cp), cp_numtype(cp));
+ return conv_to_s32(cp_data(cp), cp_numtype(cp));
}
type_error("num", n);
}
@@ -625,18 +614,12 @@
void
to_sized_ptr(sl_v v, u8int **pdata, usize *psz, uintptr *u)
{
- if(isubnumu(v)){
- *u = ubnumuval(v);
+ if(isubnum(v)){
+ *u = ubnumval(v);
*pdata = (u8int*)u;
*psz = unboxedtypes[ubnumtype(v)]->size;
return;
}
- if(isubnums(v)){
- *u = ubnumsval(v);
- *pdata = (u8int*)u;
- *psz = unboxedtypes[ubnumtype(v)]->size;
- return;
- }
if(isrune(v)){
Rune r = torune(v);
*pdata = (u8int*)u;
@@ -1058,10 +1041,7 @@
*pt = T_FIXNUM;
return true;
}else if(isubnum(a)){
- if(isubnumu(a))
- *pi = ubnumuval(a);
- else
- *pi = ubnumsval(a);
+ *pi = ubnumval(a);
*pp = pi;
*pt = ubnumtype(a);
return true;
@@ -1358,10 +1338,8 @@
if(isfixnum(a))
return fixnum(~numval(a));
- if(isubnumu(a))
- return (~ubnumuval(a) & ~0xff) | (a & 0xff);
- if(isubnums(a))
- return (~ubnumsval(a) & ~0xff) | (a & 0xff);
+ if(isubnum(a))
+ return (~ubnumval(a) & ~0xff) | (a & 0xff);
if(iscprim(a)){
cp = ptr(a);
ta = cp_numtype(cp);
@@ -1391,7 +1369,6 @@
{
sl_fx n;
s64int accum;
- u64int u;
sl_cprim *cp;
int ta;
mpint *mp;
@@ -1403,8 +1380,8 @@
if(n == 0)
return a;
mp = nil;
- if(isfixnum(a) || isubnums(a)){
- accum = isfixnum(a) ? numval(a) : ubnumsval(a);
+ if(isfixnum(a) || isubnum(a)){
+ accum = isfixnum(a) ? numval(a) : ubnumval(a);
if(n > -64 && n < 0)
return fixnum(accum>>(-n));
if(n < 0 || n >= 64 || sash_overflow_64(accum, n, &accum)){
@@ -1412,15 +1389,6 @@
mpleft(mp, n, mp);
}else
return fits_fixnum(accum) ? fixnum(accum) : return_from_s64(accum);
- }
- if(isubnumu(a)){
- u = ubnumuval(a);
- if(n > 0)
- u = n < 64 ? u<<n : 0;
- else
- u = n > -64 ? u>>-n : 0;
- accum = u;
- return (accum >= 0 && fits_fixnum(accum)) ? fixnum(accum) : return_from_u64(u);
}
if(iscprim(a)){
cp = ptr(a);
--- a/src/equal.c
+++ b/src/equal.c
@@ -83,13 +83,13 @@
if(isubnum(b) || iscprim(b) || (iscvalue(b) && (cv = ptr(b), valid_numtype(cv_numtype(cv)))))
return fixnum(numeric_compare(a, b, eq, true, false));
if(isrune(b))
- return fixnum(1);
+ return fixnum(1);
return fixnum(-1);
case TAG_UNBOXED:
if(isrune(a))
return fixnum(isrune(b) && a == b ? 0 : -1);
if(isrune(b))
- return fixnum(1);
+ return fixnum(1);
if(isfixnum(b) || isubnum(b) || iscprim(b) || (iscvalue(b) && (cv = ptr(b), valid_numtype(cv_numtype(cv)))))
return fixnum(numeric_compare(a, b, eq, true, false));
return fixnum(-1);
@@ -329,10 +329,8 @@
u.d = (double)numval(a);
return doublehash(u.i64);
case TAG_UNBOXED:
- if(isubnumu(a))
- u.d = ubnumuval(a);
- else if(isubnums(a))
- u.d = ubnumsval(a);
+ if(isubnum(a))
+ u.d = ubnumval(a);
else if(isrune(a))
return inthash(torune(a));
else
--- a/src/print.c
+++ b/src/print.c
@@ -864,17 +864,12 @@
static void
unboxed_print(sl_ios *f, sl_v v)
{
- uintptr u;
-
- if(isubnumu(v))
- u = ubnumuval(v);
- else if(isubnums(v))
- u = ubnumsval(v);
- else if(isrune(v)){
+ if(isrune(v)){
rune_print(f, torune(v));
return;
- }else
- abort();
+ }
+ assert(isubnum(v));
+ sl_fx u = ubnumval(v);
int numtype = ubnumtype(v);
sl_v typesym = unboxedtypesyms[numtype];
sl_type *type = unboxedtypes[numtype];
@@ -913,10 +908,8 @@
sl_v v = sym_value(sl_printwidthsym);
if(isfixnum(v))
sl.scr_width = numval(v);
- else if(isubnumu(v))
- sl.scr_width = ubnumuval(v);
- else if(isubnums(v))
- sl.scr_width = ubnumsval(v);
+ else if(isubnum(v))
+ sl.scr_width = ubnumval(v);
}
void
@@ -927,9 +920,9 @@
set_print_width();
sl.print_princ = sym_value(sl_printreadablysym) == sl_nil;
sl_v pl = sym_value(sl_printlengthsym);
- sl.print_length = sl_isnum(pl) ? numtofx(pl) : -1;
+ sl.print_length = sl_isnum(pl) ? tooffset(pl) : -1;
pl = sym_value(sl_printlevelsym);
- sl.print_level = sl_isnum(pl) ? numtofx(pl) : -1;
+ sl.print_level = sl_isnum(pl) ? tooffset(pl) : -1;
sl.p_level = 0;
sl.printlabel = 0;
--- a/src/sl.h
+++ b/src/sl.h
@@ -110,8 +110,7 @@
#define tagptr(p, t) ((sl_v)(p) | (t))
#define fixnum(x) ((sl_v)(x)<<TAG_BITS)
#define numval(x) ((sl_fx)(x)>>TAG_BITS)
-#define ubnumsval(x) ((intptr)(x)>>(TAG_BITS+1+4))
-#define ubnumuval(x) ((uintptr)(x)>>(TAG_BITS+1+4))
+#define ubnumval(x) ((sl_fx)(x)>>(TAG_BITS+1+4))
#define uintval(x) (((unsigned int)(x))>>TAG_BITS)
#define builtin(n) tagptr(((sl_v)n<<TAG_BITS), TAG_FN)
#define iscons(x) (tag(x) == TAG_CONS)
@@ -131,10 +130,7 @@
*/
#define TAG_UBNUM_SHIFT (TAG_BITS+1)
#define isubnum(x) ((tagext(x) & ((1<<TAG_UBNUM_SHIFT)-1)) == (0<<TAG_BITS | TAG_UNBOXED))
-#define isubnums(x) ((tagext(x) & ((2<<TAG_UBNUM_SHIFT)-1)) == (0<<TAG_BITS | TAG_UNBOXED))
-#define isubnumu(x) ((tagext(x) & ((2<<TAG_UBNUM_SHIFT)-1)) == (2<<TAG_BITS | TAG_UNBOXED))
#define ubnumtype(x) ((tagext(x)>>TAG_UBNUM_SHIFT) & 0xf)
-#define numtofx(v) (sl_fx)tooffset(v)
extern sl_type *unboxedtypes[T_UNBOXED_NUM];
extern sl_v unboxedtypesyms[T_UNBOXED_NUM];
--- a/src/sl_arith_any.h
+++ b/src/sl_arith_any.h
@@ -20,12 +20,8 @@
if(isfixnum(arg))
x = numval(arg);
else{
- if(isubnumu(arg)){
- u64 = ubnumuval(arg);
- a = &u64;
- pt = ubnumtype(arg);
- }else if(isubnums(arg)){
- u64 = ubnumsval(arg);
+ if(isubnum(arg)){
+ u64 = ubnumval(arg);
a = &u64;
pt = ubnumtype(arg);
}else if(iscprim(arg)){
--- a/src/str.c
+++ b/src/str.c
@@ -348,10 +348,8 @@
radix = get_radix_arg(args[1]);
if(isfixnum(n))
num = numval(n);
- else if(isubnumu(n))
- num = ubnumuval(n);
- else if(isubnums(n))
- num = ubnumsval(n);
+ else if(isubnum(n))
+ num = ubnumval(n);
else if(iscprim(n)){
void *data = ptr(n);
if(cp_numtype(data) < T_FLOAT)
--
⑨