Skip to content

Commit 9c4a6e6

Browse files
authored
Merge pull request #647 from sanpeqf/refactor-dword
refactor dword: use pointer to return result
2 parents 97282f4 + 516d0d2 commit 9c4a6e6

4 files changed

Lines changed: 46 additions & 49 deletions

File tree

include/bfdev/asm-generic/dword.h

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,29 @@ BFDEV_BEGIN_DECLS
3535
__bl = (bl); \
3636
\
3737
__x = __al + __bl; \
38-
(sh) = __ah + __bh + (__x < __al); \
39-
(sl) = __x; \
38+
*(sh) = __ah + __bh + (__x < __al); \
39+
*(sl) = __x; \
4040
} while (0)
4141
#endif
4242

43-
#ifndef bfdev_sub_ddmmss
44-
# define bfdev_sub_ddmmss(sh, sl, ah, al, bh, bl) do { \
45-
bfdev_uw_t __ah, __al, __bh, __bl; \
46-
bfdev_uw_t __x; \
47-
\
48-
__ah = (ah); \
49-
__al = (al); \
50-
__bh = (bh); \
51-
__bl = (bl); \
52-
\
53-
__x = __al - __bl; \
54-
(sh) = __ah - __bh - (__x > __al); \
55-
(sl) = __x; \
43+
#ifndef bfdev_dword_sub_ddmmss
44+
# define bfdev_dword_sub_ddmmss(sh, sl, ah, al, bh, bl) do { \
45+
bfdev_uw_t __ah, __al, __bh, __bl; \
46+
bfdev_uw_t __x; \
47+
\
48+
__ah = (ah); \
49+
__al = (al); \
50+
__bh = (bh); \
51+
__bl = (bl); \
52+
\
53+
__x = __al - __bl; \
54+
*(sh) = __ah - __bh - (__x > __al); \
55+
*(sl) = __x; \
5656
} while (0)
5757
#endif
5858

59-
#ifndef bfdev_umul_ppmm
60-
# define bfdev_umul_ppmm(dh, dl, va, vb) do { \
59+
#ifndef bfdev_dword_umul_ppmm
60+
# define bfdev_dword_umul_ppmm(dh, dl, va, vb) do { \
6161
bfdev_uhw_t __ul, __vl, __uh, __vh; \
6262
bfdev_uw_t __x0, __x1, __x2, __x3; \
6363
bfdev_uw_t __va, __vb; \
@@ -81,28 +81,28 @@ BFDEV_BEGIN_DECLS
8181
__x3 += BFDEV_DWORD_SIZE; \
8282
\
8383
__x2 = BFDEV_DWORD_LOWER(__x1) << BFDEV_DWORD_BITS; \
84-
(dh) = __x3 + BFDEV_DWORD_HIGHER(__x1); \
85-
(dl) = __x2 + BFDEV_DWORD_LOWER(__x0); \
84+
*(dh) = __x3 + BFDEV_DWORD_HIGHER(__x1); \
85+
*(dl) = __x2 + BFDEV_DWORD_LOWER(__x0); \
8686
} while (0)
8787
#endif
8888

89-
#ifndef bfdev_udiv_qrnnd
90-
# define bfdev_udiv_qrnnd(quot, rem, sh, sl, div) do { \
89+
#ifndef bfdev_dword_udiv_qrnnd
90+
# define bfdev_dword_udiv_qrnnd(quot, rem, vh, vl, div) do { \
9191
bfdev_uw_t __d1, __d0, __q1, __q0; \
9292
bfdev_uw_t __r1, __r0, __m; \
93-
bfdev_uw_t __sh, __sl, __div; \
93+
bfdev_uw_t __vh, __vl, __div; \
9494
\
95-
__sh = (sh); \
96-
__sl = (sl); \
95+
__vh = (vh); \
96+
__vl = (vl); \
9797
__div = (div); \
9898
\
9999
__d1 = BFDEV_DWORD_HIGHER(__div); \
100100
__d0 = BFDEV_DWORD_LOWER(__div); \
101101
\
102-
__r1 = __sh % __d1; \
103-
__q1 = __sh / __d1; \
102+
__r1 = __vh % __d1; \
103+
__q1 = __vh / __d1; \
104104
__m = (bfdev_uw_t)__q1 * __d0; \
105-
__r1 = __r1 * BFDEV_DWORD_SIZE | BFDEV_DWORD_HIGHER(__sl); \
105+
__r1 = __r1 * BFDEV_DWORD_SIZE | BFDEV_DWORD_HIGHER(__vl); \
106106
\
107107
if (__r1 < __m) { \
108108
__q1--; \
@@ -120,7 +120,7 @@ BFDEV_BEGIN_DECLS
120120
__r0 = __r1 % __d1; \
121121
__q0 = __r1 / __d1; \
122122
__m = (bfdev_uw_t)__q0 * __d0; \
123-
__r0 = __r0 * BFDEV_DWORD_SIZE | BFDEV_DWORD_LOWER(__sl); \
123+
__r0 = __r0 * BFDEV_DWORD_SIZE | BFDEV_DWORD_LOWER(__vl); \
124124
\
125125
if (__r0 < __m) { \
126126
__q0--; \
@@ -135,8 +135,8 @@ BFDEV_BEGIN_DECLS
135135
} \
136136
\
137137
__r0 -= __m; \
138-
(quot) = (bfdev_uw_t)__q1 * BFDEV_DWORD_SIZE | __q0; \
139-
(rem) = __r0; \
138+
*(quot) = (bfdev_uw_t)__q1 * BFDEV_DWORD_SIZE | __q0; \
139+
*(rem) = __r0; \
140140
} while (0)
141141
#endif
142142

src/dword.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ bfdev_dword_generic_udiv(bfdev_uw_t *quot, bfdev_uw_t *rem,
3232
n0 = n0 << bm;
3333
}
3434

35-
bfdev_udiv_qrnnd(q0, n0, n1, n0, div);
35+
bfdev_dword_udiv_qrnnd(&q0, &n0, n1, n0, div);
3636
q1 = 0;
3737
} else {
3838
/* divide by zero */
@@ -48,10 +48,10 @@ bfdev_dword_generic_udiv(bfdev_uw_t *quot, bfdev_uw_t *rem,
4848
n2 = n1 >> b;
4949
n1 = (n1 << bm) | (n0 >> b);
5050
n0 = n0 << bm;
51-
bfdev_udiv_qrnnd(q1, n1, n2, n1, div);
51+
bfdev_dword_udiv_qrnnd(&q1, &n1, n2, n1, div);
5252
}
5353

54-
bfdev_udiv_qrnnd(q0, n0, n1, n0, div);
54+
bfdev_dword_udiv_qrnnd(&q0, &n0, n1, n0, div);
5555
}
5656

5757
if (quot) {
@@ -89,7 +89,7 @@ bfdev_dword_generic_udivd(bfdev_uw_t *quot, bfdev_uw_t *rem,
8989
q0 = 0;
9090
else {
9191
q0 = 1;
92-
bfdev_sub_ddmmss(n1, n0, n1, n0, d1, d0);
92+
bfdev_dword_sub_ddmmss(&n1, &n0, n1, n0, d1, d0);
9393
}
9494

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

108-
bfdev_udiv_qrnnd(q0, n1, n2, n1, d1);
109-
bfdev_umul_ppmm(m1, m0, q0, d0);
108+
bfdev_dword_udiv_qrnnd(&q0, &n1, n2, n1, d1);
109+
bfdev_dword_umul_ppmm(&m1, &m0, q0, d0);
110110

111111
if (m1 > n1 || (m1 == n1 && m0 > n0)) {
112112
q0--;
113-
bfdev_sub_ddmmss(m1, m0, m1, m0, d1, d0);
113+
bfdev_dword_sub_ddmmss(&m1, &m0, m1, m0, d1, d0);
114114
}
115115

116116
q1 = 0;
117117
if (rem) {
118-
bfdev_sub_ddmmss(n1, n0, n1, n0, m1, m0);
118+
bfdev_dword_sub_ddmmss(&n1, &n0, n1, n0, m1, m0);
119119
rem[0] = (n1 << b) | (n0 >> bm);
120120
rem[1] = n1 >> bm;
121121
}

src/levenshtein.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99

1010
export unsigned int
1111
bfdev_levenshtein_len(const bfdev_alloc_t *alloc,
12-
const char *str1, const char *str2,
13-
size_t len1, size_t len2,
14-
unsigned int s, unsigned int w,
15-
unsigned int a, unsigned int d)
12+
const char *str1, const char *str2, size_t len1, size_t len2,
13+
unsigned int s, unsigned int w, unsigned int a, unsigned int d)
1614
{
1715
unsigned int *row1, *row2, *row3;
1816
unsigned int distance, *cache;
@@ -68,9 +66,8 @@ bfdev_levenshtein_len(const bfdev_alloc_t *alloc,
6866

6967
export unsigned int
7068
bfdev_levenshtein(const bfdev_alloc_t *alloc,
71-
const char *str1, const char *str2,
72-
unsigned int s, unsigned int w,
73-
unsigned int a, unsigned int d)
69+
const char *str1, const char *str2,
70+
unsigned int s, unsigned int w, unsigned int a, unsigned int d)
7471
{
7572
size_t len1, len2;
7673

src/mpi.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ mpa_muli(BFDEV_MPI_TYPE *ptrs,
179179
BFDEV_MPI_TYPE vhigh, vlow;
180180

181181
while (length--) {
182-
bfdev_umul_ppmm(vhigh, vlow, *ptra++, vi);
182+
bfdev_dword_umul_ppmm(&vhigh, &vlow, *ptra++, vi);
183183
vlow += carry;
184184
carry = (vlow < carry) + vhigh;
185185
*ptrs++ = vlow;
@@ -196,7 +196,7 @@ mpa_maci(BFDEV_MPI_TYPE *ptrs,
196196
BFDEV_MPI_TYPE vhigh, vlow;
197197

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

@@ -216,7 +216,7 @@ mpa_msui(BFDEV_MPI_TYPE *ptrs,
216216
BFDEV_MPI_TYPE vhigh, vlow;
217217

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

@@ -384,7 +384,7 @@ mpa_divrem(BFDEV_MPI_TYPE *ptrs,
384384
bfdev_dword_udiv(result, &rem, dword, dhigh);
385385

386386
quot = result[0];
387-
bfdev_umul_ppmm(v1, value, dlow, quot);
387+
bfdev_dword_umul_ppmm(&v1, &value, dlow, quot);
388388

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

0 commit comments

Comments
 (0)