Commit 356ab202 authored by Raphael Rieu-Helft's avatar Raphael Rieu-Helft

Use structs to extract functions that return multiple values

parent f7b732b8
...@@ -50,39 +50,74 @@ module mach.int.UInt32 ...@@ -50,39 +50,74 @@ module mach.int.UInt32
" "
#define LOW_MASK 0x00000000FFFFFFFFULL #define LOW_MASK 0x00000000FFFFFFFFULL
void add32_with_carry(uint32_t * res, uint32_t * carry, uint32_t x, uint32_t y, uint32_t c) struct __add32_with_carry_result
{ uint32_t __field_0;
uint32_t __field_1;
};
struct __add32_with_carry_result add32_with_carry(uint32_t x, uint32_t y, uint32_t c)
{ {
struct __add32_with_carry_result result;
uint64_t r = (uint64_t)x + (uint64_t)y + (uint64_t) c; uint64_t r = (uint64_t)x + (uint64_t)y + (uint64_t) c;
*res = (uint32_t)(r & LOW_MASK); result.__field_0 = (uint32_t)(r & LOW_MASK);
*carry = (uint32_t)(r >> 32); result.__field_1 = (uint32_t)(r >> 32);
return result;
} }
void sub32_with_borrow(uint32_t * res, uint32_t * borrow, uint32_t x, uint32_t y, uint32_t b) struct __sub32_with_borrow_result
{ uint32_t __field_0;
uint32_t __field_1;
};
struct __sub32_with_borrow_result sub32_with_borrow(uint32_t x, uint32_t y, uint32_t b)
{ {
struct __sub32_with_borrow_result result;
uint64_t r = (uint64_t)x - (uint64_t)y - (uint64_t) b; uint64_t r = (uint64_t)x - (uint64_t)y - (uint64_t) b;
*res = (uint32_t)(r & LOW_MASK); result.__field_0 = (uint32_t)(r & LOW_MASK);
*borrow = (uint32_t)(r >> 63); result.__field_1 = (uint32_t)(r >> 63);
return result;
} }
void mul32_double(uint32_t * low, uint32_t * high, uint32_t x, uint32_t y) struct __mul32_double_result
{ uint32_t __field_0;
uint32_t __field_1;
};
struct __mul32_double_result mul32_double(uint32_t x, uint32_t y)
{ {
struct __mul32_double_result result;
uint64_t r = (uint64_t)x * (uint64_t)y; uint64_t r = (uint64_t)x * (uint64_t)y;
*low = (uint32_t)(r & LOW_MASK); result.__field_0 = (uint32_t)(r & LOW_MASK);
*high = (uint32_t)(r >> 32); result.__field_1 = (uint32_t)(r >> 32);
return result;
} }
void add32_3(uint32_t * low, uint32_t * high, uint32_t x, uint32_t y, uint32_t z) struct __add32_3_result
{ uint32_t __field_0;
uint32_t __field_1;
};
struct __add32_3_result add32_3(uint32_t x, uint32_t y, uint32_t z)
{ {
struct __add32_3_result result;
uint64_t r = (uint64_t)x + (uint64_t)y + (uint64_t) z; uint64_t r = (uint64_t)x + (uint64_t)y + (uint64_t) z;
*low = (uint32_t)(r & LOW_MASK); result.__field_0 = (uint32_t)(r & LOW_MASK);
*high = (uint32_t)(r >> 32); result.__field_1 = (uint32_t)(r >> 32);
return result;
} }
void lsld32(uint32_t * low, uint32_t * high, uint32_t x, uint32_t cnt) struct __lsld32_result
{ uint32_t __field_0;
uint32_t __field_1;
};
struct __lsld32_result lsld32(uint32_t x, uint32_t cnt)
{ {
struct __lsld32_result result;
uint64_t r = (uint64_t)x << cnt; uint64_t r = (uint64_t)x << cnt;
*low = (uint32_t)(r & LOW_MASK); result.__field_0 = (uint32_t)(r & LOW_MASK);
*high = (uint32_t)(r >> 32); result.__field_1 = (uint32_t)(r >> 32);
return result;
} }
" "
...@@ -141,28 +176,47 @@ module mach.int.UInt64 ...@@ -141,28 +176,47 @@ module mach.int.UInt64
#undef invert_limb #undef invert_limb
void add64_with_carry(uint64_t * res, uint64_t * carry, uint64_t x, uint64_t y, uint64_t c) struct __add64_with_carry_result
{ uint64_t __field_0;
uint64_t __field_1;
};
struct __add64_with_carry_result add64_with_carry(uint64_t x, uint64_t y, uint64_t c)
{ {
struct __add64_with_carry_result result;
uint64_t r = x + y + c; uint64_t r = x + y + c;
*res = r; result.__field_0 = r;
if (r == x) *carry = c; if (r == x) result.__field_1 = c;
else *carry = (r < x); else result.__field_1 = (r < x);
//if (r < x) *carry = 1; return result;
//else *carry = 0;
} }
void sub64_with_borrow(uint64_t * res, uint64_t * borrow, uint64_t x, uint64_t y, uint64_t b) struct __sub64_with_borrow_result
{ uint64_t __field_0;
uint64_t __field_1;
};
struct __sub64_with_borrow_result sub64_with_borrow(uint64_t x, uint64_t y, uint64_t b)
{ {
struct __sub64_with_borrow_result result;
uint64_t r = x - y - b; uint64_t r = x - y - b;
*res = r; result.__field_0 = r;
if (r > x) *borrow = 1; if (r > x) result.__field_1 = 1;
else if (r == x) *borrow = b; else if (r == x) result.__field_1 = b;
else *borrow = 0; else result.__field_1 = 0;
return result;
} }
void mul64_double(uint64_t * low, uint64_t * high, uint64_t x, uint64_t y) struct __mul64_double_result
{ uint64_t __field_0;
uint64_t __field_1;
};
struct __mul64_double_result mul64_double(uint64_t x, uint64_t y)
{ {
umul_ppmm(*high,*low,x,y); struct __mul64_double_result result;
umul_ppmm(result.__field_1,result.__field_0,x,y);
return result;
} }
uint64_t div64_2by1(uint64_t ul, uint64_t uh, uint64_t d) uint64_t div64_2by1(uint64_t ul, uint64_t uh, uint64_t d)
...@@ -172,42 +226,37 @@ uint64_t div64_2by1(uint64_t ul, uint64_t uh, uint64_t d) ...@@ -172,42 +226,37 @@ uint64_t div64_2by1(uint64_t ul, uint64_t uh, uint64_t d)
return q; return q;
} }
void add64_3(uint64_t * low, uint64_t * high, uint64_t x, uint64_t y, uint64_t z) struct __add64_3_result
{ uint64_t __field_0;
uint64_t __field_1;
};
struct __add64_3_result add64_3(uint64_t x, uint64_t y, uint64_t z)
{ {
struct __add64_3_result result;
uint64_t r, c1, c2; uint64_t r, c1, c2;
r = x + y; r = x + y;
c1 = r < y; c1 = r < y;
r += z; r += z;
c2 = r < z; c2 = r < z;
//add64_with_carry(&r, &c1, x, y, 0); result.__field_1 = c1 + c2;
//add64_with_carry(&r, &c2, r, z, 0); result.__field_0 = r;
*high = c1 + c2; return result;
*low = r;
} }
void lsld64(uint64_t * low, uint64_t * high, uint64_t x, uint64_t cnt) struct __lsld64_result
{ { uint64_t __field_0;
*high = x >> (64 - cnt); uint64_t __field_1;
*low = x << cnt; };
}
"
(*
void mul64_double(uint64_t * low, uint64_t * high, uint64_t x, uint64_t y)
{
uint64_t h, l;
asm(\"mulq %3\" : \"=a\"(l),\"=d\"(h) : \"a\"(x), \"rm\"(y));
*high = h;
*low = l;
}
uint64_t div64_2by1(uint64_t ul, uint64_t uh, uint64_t d) struct __lsld64_result lsld64(uint64_t x, uint64_t cnt)
{ {
uint64_t q,r; struct __lsld64_result result;
asm(\"divq %4\" : \"=a\"(q),\"=d\"(r) : \"a\"(ul), \"d\"(uh), \"rm\"(d)); result.__field_1 = x >> (64 - cnt);
return q; result.__field_0 = x << cnt;
return result;
} }
*) "
syntax converter of_int "%1ULL" syntax converter of_int "%1ULL"
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment