Skip to content

Commit d0c966a

Browse files
committed
dtoa.c: Extract macro to update 5powers Bigint cache atomically
1 parent 0887d82 commit d0c966a

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

missing/dtoa.c

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -815,29 +815,30 @@ static Bigint *
815815
pow5mult(Bigint *b, int k)
816816
{
817817
Bigint *b1, *p5, *p51;
818-
Bigint *p5tmp;
819818
int i;
820819
static const int p05[3] = { 5, 25, 125 };
821820

822821
if ((i = k & 3) != 0)
823822
b = multadd(b, p05[i-1], 0);
823+
#define b_cache(var, addr, new_expr) \
824+
if ((var = addr) != 0) {} else { \
825+
Bigint *tmp = 0; \
826+
ACQUIRE_DTOA_LOCK(1); \
827+
if (!(var = addr) && (var = (new_expr)) != 0) { \
828+
var->next = 0; \
829+
tmp = ATOMIC_PTR_CAS(addr, NULL, var); \
830+
} \
831+
FREE_DTOA_LOCK(1); \
832+
if (UNLIKELY(tmp)) { \
833+
Bfree(var); \
834+
var = tmp; \
835+
} \
836+
}
824837

825838
if (!(k >>= 2))
826839
return b;
827-
if (!(p5 = p5s)) {
828-
/* first time */
829-
ACQUIRE_DTOA_LOCK(1);
830-
if (!(p5 = p5s)) {
831-
p5 = i2b(625);
832-
p5->next = 0;
833-
p5tmp = ATOMIC_PTR_CAS(p5s, NULL, p5);
834-
if (UNLIKELY(p5tmp)) {
835-
Bfree(p5);
836-
p5 = p5tmp;
837-
}
838-
}
839-
FREE_DTOA_LOCK(1);
840-
}
840+
/* first time */
841+
b_cache(p5, p5s, i2b(625));
841842
for (;;) {
842843
if (k & 1) {
843844
b1 = mult(b, p5);
@@ -846,19 +847,7 @@ pow5mult(Bigint *b, int k)
846847
}
847848
if (!(k >>= 1))
848849
break;
849-
if (!(p51 = p5->next)) {
850-
ACQUIRE_DTOA_LOCK(1);
851-
if (!(p51 = p5->next)) {
852-
p51 = mult(p5,p5);
853-
p51->next = 0;
854-
p5tmp = ATOMIC_PTR_CAS(p5->next, NULL, p51);
855-
if (UNLIKELY(p5tmp)) {
856-
Bfree(p51);
857-
p51 = p5tmp;
858-
}
859-
}
860-
FREE_DTOA_LOCK(1);
861-
}
850+
b_cache(p51, p5->next, mult(p5, p5));
862851
p5 = p51;
863852
}
864853
return b;

0 commit comments

Comments
 (0)