From 516d0d2c6c5a680a16d8a3dc04906bd20e2c726a Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Tue, 7 Jan 2025 22:29:00 +0800 Subject: [PATCH] refactor dword: use pointer to return result Signed-off-by: John Sanpe --- include/bfdev/asm-generic/dword.h | 60 +++++++++++++++---------------- src/dword.c | 16 ++++----- src/levenshtein.c | 11 +++--- src/mpi.c | 8 ++--- 4 files changed, 46 insertions(+), 49 deletions(-) diff --git a/include/bfdev/asm-generic/dword.h b/include/bfdev/asm-generic/dword.h index a0b2de5f..2a793267 100644 --- a/include/bfdev/asm-generic/dword.h +++ b/include/bfdev/asm-generic/dword.h @@ -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; \ @@ -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--; \ @@ -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--; \ @@ -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 diff --git a/src/dword.c b/src/dword.c index dad90244..bce28d71 100644 --- a/src/dword.c +++ b/src/dword.c @@ -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 */ @@ -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) { @@ -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; @@ -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; } diff --git a/src/levenshtein.c b/src/levenshtein.c index 6755f823..324e0337 100644 --- a/src/levenshtein.c +++ b/src/levenshtein.c @@ -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; @@ -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; diff --git a/src/mpi.c b/src/mpi.c index 5695cb94..4240f36c 100644 --- a/src/mpi.c +++ b/src/mpi.c @@ -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; @@ -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; @@ -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; @@ -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--;