Skip to content

Commit

Permalink
refactor dword: use pointer to return result
Browse files Browse the repository at this point in the history
Signed-off-by: John Sanpe <[email protected]>
  • Loading branch information
sanpeqf committed Jan 7, 2025
1 parent 97282f4 commit 516d0d2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 49 deletions.
60 changes: 30 additions & 30 deletions include/bfdev/asm-generic/dword.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,29 @@ BFDEV_BEGIN_DECLS
__bl = (bl); \
\
__x = __al + __bl; \
(sh) = __ah + __bh + (__x < __al); \
(sl) = __x; \
*(sh) = __ah + __bh + (__x < __al); \
*(sl) = __x; \
} while (0)
#endif

#ifndef bfdev_sub_ddmmss
# define bfdev_sub_ddmmss(sh, sl, ah, al, bh, bl) do { \
bfdev_uw_t __ah, __al, __bh, __bl; \
bfdev_uw_t __x; \
\
__ah = (ah); \
__al = (al); \
__bh = (bh); \
__bl = (bl); \
\
__x = __al - __bl; \
(sh) = __ah - __bh - (__x > __al); \
(sl) = __x; \
#ifndef bfdev_dword_sub_ddmmss
# define bfdev_dword_sub_ddmmss(sh, sl, ah, al, bh, bl) do { \
bfdev_uw_t __ah, __al, __bh, __bl; \
bfdev_uw_t __x; \
\
__ah = (ah); \
__al = (al); \
__bh = (bh); \
__bl = (bl); \
\
__x = __al - __bl; \
*(sh) = __ah - __bh - (__x > __al); \
*(sl) = __x; \
} while (0)
#endif

#ifndef bfdev_umul_ppmm
# define bfdev_umul_ppmm(dh, dl, va, vb) do { \
#ifndef bfdev_dword_umul_ppmm
# define bfdev_dword_umul_ppmm(dh, dl, va, vb) do { \
bfdev_uhw_t __ul, __vl, __uh, __vh; \
bfdev_uw_t __x0, __x1, __x2, __x3; \
bfdev_uw_t __va, __vb; \
Expand All @@ -81,28 +81,28 @@ BFDEV_BEGIN_DECLS
__x3 += BFDEV_DWORD_SIZE; \
\
__x2 = BFDEV_DWORD_LOWER(__x1) << BFDEV_DWORD_BITS; \
(dh) = __x3 + BFDEV_DWORD_HIGHER(__x1); \
(dl) = __x2 + BFDEV_DWORD_LOWER(__x0); \
*(dh) = __x3 + BFDEV_DWORD_HIGHER(__x1); \
*(dl) = __x2 + BFDEV_DWORD_LOWER(__x0); \
} while (0)
#endif

#ifndef bfdev_udiv_qrnnd
# define bfdev_udiv_qrnnd(quot, rem, sh, sl, div) do { \
#ifndef bfdev_dword_udiv_qrnnd
# define bfdev_dword_udiv_qrnnd(quot, rem, vh, vl, div) do { \
bfdev_uw_t __d1, __d0, __q1, __q0; \
bfdev_uw_t __r1, __r0, __m; \
bfdev_uw_t __sh, __sl, __div; \
bfdev_uw_t __vh, __vl, __div; \
\
__sh = (sh); \
__sl = (sl); \
__vh = (vh); \
__vl = (vl); \
__div = (div); \
\
__d1 = BFDEV_DWORD_HIGHER(__div); \
__d0 = BFDEV_DWORD_LOWER(__div); \
\
__r1 = __sh % __d1; \
__q1 = __sh / __d1; \
__r1 = __vh % __d1; \
__q1 = __vh / __d1; \
__m = (bfdev_uw_t)__q1 * __d0; \
__r1 = __r1 * BFDEV_DWORD_SIZE | BFDEV_DWORD_HIGHER(__sl); \
__r1 = __r1 * BFDEV_DWORD_SIZE | BFDEV_DWORD_HIGHER(__vl); \
\
if (__r1 < __m) { \
__q1--; \
Expand All @@ -120,7 +120,7 @@ BFDEV_BEGIN_DECLS
__r0 = __r1 % __d1; \
__q0 = __r1 / __d1; \
__m = (bfdev_uw_t)__q0 * __d0; \
__r0 = __r0 * BFDEV_DWORD_SIZE | BFDEV_DWORD_LOWER(__sl); \
__r0 = __r0 * BFDEV_DWORD_SIZE | BFDEV_DWORD_LOWER(__vl); \
\
if (__r0 < __m) { \
__q0--; \
Expand All @@ -135,8 +135,8 @@ BFDEV_BEGIN_DECLS
} \
\
__r0 -= __m; \
(quot) = (bfdev_uw_t)__q1 * BFDEV_DWORD_SIZE | __q0; \
(rem) = __r0; \
*(quot) = (bfdev_uw_t)__q1 * BFDEV_DWORD_SIZE | __q0; \
*(rem) = __r0; \
} while (0)
#endif

Expand Down
16 changes: 8 additions & 8 deletions src/dword.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bfdev_dword_generic_udiv(bfdev_uw_t *quot, bfdev_uw_t *rem,
n0 = n0 << bm;
}

bfdev_udiv_qrnnd(q0, n0, n1, n0, div);
bfdev_dword_udiv_qrnnd(&q0, &n0, n1, n0, div);
q1 = 0;
} else {
/* divide by zero */
Expand All @@ -48,10 +48,10 @@ bfdev_dword_generic_udiv(bfdev_uw_t *quot, bfdev_uw_t *rem,
n2 = n1 >> b;
n1 = (n1 << bm) | (n0 >> b);
n0 = n0 << bm;
bfdev_udiv_qrnnd(q1, n1, n2, n1, div);
bfdev_dword_udiv_qrnnd(&q1, &n1, n2, n1, div);
}

bfdev_udiv_qrnnd(q0, n0, n1, n0, div);
bfdev_dword_udiv_qrnnd(&q0, &n0, n1, n0, div);
}

if (quot) {
Expand Down Expand Up @@ -89,7 +89,7 @@ bfdev_dword_generic_udivd(bfdev_uw_t *quot, bfdev_uw_t *rem,
q0 = 0;
else {
q0 = 1;
bfdev_sub_ddmmss(n1, n0, n1, n0, d1, d0);
bfdev_dword_sub_ddmmss(&n1, &n0, n1, n0, d1, d0);
}

q1 = 0;
Expand All @@ -105,17 +105,17 @@ bfdev_dword_generic_udivd(bfdev_uw_t *quot, bfdev_uw_t *rem,
n1 = (n1 << bm) | (n0 >> b);
n0 = n0 << bm;

bfdev_udiv_qrnnd(q0, n1, n2, n1, d1);
bfdev_umul_ppmm(m1, m0, q0, d0);
bfdev_dword_udiv_qrnnd(&q0, &n1, n2, n1, d1);
bfdev_dword_umul_ppmm(&m1, &m0, q0, d0);

if (m1 > n1 || (m1 == n1 && m0 > n0)) {
q0--;
bfdev_sub_ddmmss(m1, m0, m1, m0, d1, d0);
bfdev_dword_sub_ddmmss(&m1, &m0, m1, m0, d1, d0);
}

q1 = 0;
if (rem) {
bfdev_sub_ddmmss(n1, n0, n1, n0, m1, m0);
bfdev_dword_sub_ddmmss(&n1, &n0, n1, n0, m1, m0);
rem[0] = (n1 << b) | (n0 >> bm);
rem[1] = n1 >> bm;
}
Expand Down
11 changes: 4 additions & 7 deletions src/levenshtein.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

export unsigned int
bfdev_levenshtein_len(const bfdev_alloc_t *alloc,
const char *str1, const char *str2,
size_t len1, size_t len2,
unsigned int s, unsigned int w,
unsigned int a, unsigned int d)
const char *str1, const char *str2, size_t len1, size_t len2,
unsigned int s, unsigned int w, unsigned int a, unsigned int d)
{
unsigned int *row1, *row2, *row3;
unsigned int distance, *cache;
Expand Down Expand Up @@ -68,9 +66,8 @@ bfdev_levenshtein_len(const bfdev_alloc_t *alloc,

export unsigned int
bfdev_levenshtein(const bfdev_alloc_t *alloc,
const char *str1, const char *str2,
unsigned int s, unsigned int w,
unsigned int a, unsigned int d)
const char *str1, const char *str2,
unsigned int s, unsigned int w, unsigned int a, unsigned int d)
{
size_t len1, len2;

Expand Down
8 changes: 4 additions & 4 deletions src/mpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ mpa_muli(BFDEV_MPI_TYPE *ptrs,
BFDEV_MPI_TYPE vhigh, vlow;

while (length--) {
bfdev_umul_ppmm(vhigh, vlow, *ptra++, vi);
bfdev_dword_umul_ppmm(&vhigh, &vlow, *ptra++, vi);
vlow += carry;
carry = (vlow < carry) + vhigh;
*ptrs++ = vlow;
Expand All @@ -196,7 +196,7 @@ mpa_maci(BFDEV_MPI_TYPE *ptrs,
BFDEV_MPI_TYPE vhigh, vlow;

while (length--) {
bfdev_umul_ppmm(vhigh, vlow, *ptra++, vi);
bfdev_dword_umul_ppmm(&vhigh, &vlow, *ptra++, vi);
vlow += carry;
carry = (vlow < carry) + vhigh;

Expand All @@ -216,7 +216,7 @@ mpa_msui(BFDEV_MPI_TYPE *ptrs,
BFDEV_MPI_TYPE vhigh, vlow;

while (length--) {
bfdev_umul_ppmm(vhigh, vlow, *ptra++, vi);
bfdev_dword_umul_ppmm(&vhigh, &vlow, *ptra++, vi);
vlow += carry;
carry = (vlow < carry) + vhigh;

Expand Down Expand Up @@ -384,7 +384,7 @@ mpa_divrem(BFDEV_MPI_TYPE *ptrs,
bfdev_dword_udiv(result, &rem, dword, dhigh);

quot = result[0];
bfdev_umul_ppmm(v1, value, dlow, quot);
bfdev_dword_umul_ppmm(&v1, &value, dlow, quot);

while (v1 > rem || (v1 == rem && value > ptra[cntb - 2])) {
quot--;
Expand Down

0 comments on commit 516d0d2

Please sign in to comment.