Skip to content

Commit c979866

Browse files
committed
convert divcoeff.c to D
1 parent 9d7403f commit c979866

File tree

5 files changed

+102
-91
lines changed

5 files changed

+102
-91
lines changed

.circleci/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -uexo pipefail
44

5-
HOST_DMD_VER=2.072.2 # same as in dmd/src/posix.mak
5+
HOST_DMD_VER=2.074.1 # same as in dmd/src/posix.mak
66
CURL_USER_AGENT="CirleCI $(curl --version | head -n 1)"
77
N=4
88
CIRCLE_NODE_INDEX=${CIRCLE_NODE_INDEX:-0}

src/dmd/backend/cod2.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <stdio.h>
1616
#include <string.h>
17+
#include <stdint.h>
1718
#include <time.h>
1819
#include "cc.h"
1920
#include "oper.h"
@@ -32,9 +33,11 @@ int cdcmp_flag;
3233
extern signed char regtorm[8];
3334

3435
// from divcoeff.c
35-
extern bool choose_multiplier(int N, targ_ullong d, int prec, targ_ullong *pm, int *pshpost);
36-
extern bool udiv_coefficients(int N, targ_ullong d, int *pshpre, targ_ullong *pm, int *pshpost);
37-
36+
extern "C"
37+
{
38+
extern bool choose_multiplier(int N, uint64_t d, int prec, uint64_t *pm, int *pshpost);
39+
extern bool udiv_coefficients(int N, uint64_t d, int *pshpre, uint64_t *pm, int *pshpost);
40+
}
3841

3942
/*******************************
4043
* Swap two integers.
@@ -1090,7 +1093,7 @@ void cdmul(CodeBuilder& cdb,elem *e,regm_t *pretregs)
10901093

10911094
unsigned r3;
10921095

1093-
targ_ullong m;
1096+
uint64_t m;
10941097
int shpost;
10951098
int N = sz * 8;
10961099
bool mhighbit = choose_multiplier(N, d, N - 1, &m, &shpost);
@@ -1191,7 +1194,7 @@ void cdmul(CodeBuilder& cdb,elem *e,regm_t *pretregs)
11911194
unsigned r3;
11921195
regm_t regm;
11931196
unsigned reg;
1194-
targ_ullong m;
1197+
uint64_t m;
11951198
int shpre;
11961199
int shpost;
11971200
if (udiv_coefficients(sz * 8, e2factor, &shpre, &m, &shpost))

src/dmd/backend/divcoeff.c renamed to src/dmd/backend/divcoeff.d

Lines changed: 74 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,47 @@
22
* Compiler implementation of the
33
* $(LINK2 http://www.dlang.org, D programming language).
44
*
5-
* Copyright: Copyright (C) 2013-2018 by The D Language Foundation, All Rights Reserved
5+
* Copyright: Copyright (c) 2013-2018 by The D Language Foundation, All Rights Reserved
66
* Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
77
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
8-
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/backend/divcoeff.c, backend/divcoeff.c)
8+
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/backend/divcoeff.d, backend/divcoeff.d)
99
*/
1010

1111
/***************************************************
1212
* Algorithms from "Division by Invariant Integers using Multiplication"
1313
* by Torbjoern Granlund and Peter L. Montgomery
1414
*/
1515

16-
#include <stdio.h>
17-
#include <assert.h>
16+
import core.stdc.stdio;
1817

19-
// This MUST MATCH typedef targ_ullong in cdef.h.
20-
#if defined(__UINT64_TYPE__) && !defined(__APPLE__)
21-
typedef __UINT64_TYPE__ ullong;
22-
#elif defined(__UINTMAX_TYPE__)
23-
typedef __UINTMAX_TYPE__ ullong;
24-
#else
25-
typedef unsigned long long ullong;
26-
#endif
18+
extern (C):
2719

28-
void test_udiv_coefficients();
20+
alias ullong = ulong;
2921

3022
/* unsigned 128 bit math
3123
*/
3224

33-
#define SIGN64(x) ((long long)(x) < 0)
34-
#define SHL128(dh,dl, xh,xl) ((dh = (xh << 1) | SIGN64(xl)), (dl = xl << 1))
35-
#define SHR128(dh,dl, xh,xl) ((dl = (xl >> 1) | ((xh & 1) << 63)),(dh = xh >> 1))
36-
#define XltY128(xh,xl,yh,yl) (xh < yh || (xh == yh && xl < yl))
25+
bool SIGN64(ullong x)
26+
{
27+
return cast(long)x < 0;
28+
}
29+
30+
void SHL128(out ullong dh, out ullong dl, ullong xh,ullong xl)
31+
{
32+
dh = (xh << 1) | SIGN64(xl);
33+
dl = xl << 1;
34+
}
35+
36+
void SHR128(out ullong dh, out ullong dl, ullong xh,ullong xl)
37+
{
38+
dl = (xl >> 1) | ((xh & 1) << 63);
39+
dh = xh >> 1;
40+
}
41+
42+
bool XltY128(ullong xh, ullong xl, ullong yh, ullong yl)
43+
{
44+
return xh < yh || (xh == yh && xl < yl);
45+
}
3746

3847
void u128Div(ullong xh, ullong xl, ullong yh, ullong yl, ullong *pqh, ullong *pql)
3948
{
@@ -46,7 +55,7 @@ void u128Div(ullong xh, ullong xl, ullong yh, ullong yl, ullong *pqh, ullong *pq
4655
assert(yh || yl); // no div-by-0 bugs
4756

4857
// left justify y
49-
unsigned shiftcount = 1;
58+
uint shiftcount = 1;
5059
if (!yh)
5160
{ yh = yl;
5261
yl = 0;
@@ -85,27 +94,28 @@ void u128Div(ullong xh, ullong xl, ullong yh, ullong yl, ullong *pqh, ullong *pq
8594

8695
// Remainder is xh,xl
8796

88-
#if 0
89-
printf("%016llx_%016llx / %016llx_%016llx = %016llx_%016llx\n", xxh,xxl,yyh,yyl,qh,ql);
90-
if (xxh == 0 && yyh == 0)
91-
printf("should be %llx\n", xxl / yyl);
92-
#endif
97+
version (none)
98+
{
99+
printf("%016llx_%016llx / %016llx_%016llx = %016llx_%016llx\n", xxh,xxl,yyh,yyl,qh,ql);
100+
if (xxh == 0 && yyh == 0)
101+
printf("should be %llx\n", xxl / yyl);
102+
}
93103
}
94104

95105
/************************************
96106
* Implement Algorithm 6.2: Selection of multiplier and shift count
97-
* Input:
98-
* N 32 or 64
99-
* d divisor (must not be 0 or a power of 2)
100-
* prec bits of precision desired
107+
* Params:
108+
* N = 32 or 64
109+
* d = divisor (must not be 0 or a power of 2)
110+
* prec = bits of precision desired
101111
* Output:
102-
* *pm factor
103-
* *pshpost post shift
112+
* *pm = factor
113+
* *pshpost = post shift
104114
* Returns:
105115
* true m >= 2**N
106116
*/
107117

108-
bool choose_multiplier(int N, ullong d, int prec, ullong *pm, int *pshpost)
118+
extern (C) bool choose_multiplier(int N, ullong d, int prec, ullong *pm, int *pshpost)
109119
{
110120
assert(N == 32 || N == 64);
111121
assert(prec <= N);
@@ -127,10 +137,10 @@ bool choose_multiplier(int N, ullong d, int prec, ullong *pm, int *pshpost)
127137
if (N == 32)
128138
{
129139
// mlow = (2**(N + b)) / d
130-
ullong mlow = (1ULL << (N + b)) / d;
140+
ullong mlow = (1UL << (N + b)) / d;
131141

132142
// uhigh = (2**(N + b) + 2**(N + b - prec)) / d
133-
ullong mhigh = ((1ULL << (N + b)) + (1ULL << (N + b - prec))) / d;
143+
ullong mhigh = ((1UL << (N + b)) + (1UL << (N + b - prec))) / d;
134144

135145
while (mlow/2 < mhigh/2 && shpost)
136146
{
@@ -140,27 +150,27 @@ bool choose_multiplier(int N, ullong d, int prec, ullong *pm, int *pshpost)
140150
}
141151

142152
*pm = mhigh & 0xFFFFFFFF;
143-
mhighbit = mhigh >> N;
153+
mhighbit = (mhigh >> N) != 0;
144154
}
145155
else if (N == 64)
146156
{
147157
// Same as for N==32, but use 128 bit unsigned arithmetic
148158

149159
// mlow = (2**(N + b)) / d
150160
ullong mlowl = 0;
151-
ullong mlowh = 1ULL << b;
161+
ullong mlowh = 1UL << b;
152162

153163
// mlow /= d
154164
u128Div(mlowh, mlowl, 0, d, &mlowh, &mlowl);
155165

156166
// mhigh = (2**(N + b) + 2**(N + b - prec)) / d
157167
ullong mhighl = 0;
158-
ullong mhighh = 1ULL << b;
168+
ullong mhighh = 1UL << b;
159169
int e = N + b - prec;
160170
if (e < 64)
161-
mhighl = 1ULL << e;
171+
mhighl = 1UL << e;
162172
else
163-
mhighh |= 1ULL << (e - 64);
173+
mhighh |= 1UL << (e - 64);
164174

165175
// mhigh /= d
166176
u128Div(mhighh, mhighl, 0, d, &mhighh, &mhighl);
@@ -221,12 +231,8 @@ bool choose_multiplier(int N, ullong d, int prec, ullong *pm, int *pshpost)
221231
* q = SRL(MULUH(m, SRL(n, shpre)), shpost)
222232
*/
223233

224-
bool udiv_coefficients(int N, ullong d, int *pshpre, ullong *pm, int *pshpost)
234+
extern (C) bool udiv_coefficients(int N, ullong d, int *pshpre, ullong *pm, int *pshpost)
225235
{
226-
#ifdef DEBUG
227-
test_udiv_coefficients();
228-
#endif
229-
230236
bool mhighbit = choose_multiplier(N, d, N, pm, pshpost);
231237
if (mhighbit && (d & 1) == 0)
232238
{
@@ -244,14 +250,8 @@ bool udiv_coefficients(int N, ullong d, int *pshpre, ullong *pm, int *pshpost)
244250
return mhighbit;
245251
}
246252

247-
#ifdef DEBUG
248-
void test_udiv_coefficients()
253+
unittest
249254
{
250-
static bool tested = false;
251-
if (tested)
252-
return;
253-
tested = true;
254-
255255
struct S
256256
{
257257
int N;
@@ -260,10 +260,10 @@ void test_udiv_coefficients()
260260
int highbit;
261261
ullong m;
262262
int shpost;
263-
};
263+
}
264264

265-
static S table[] =
266-
{
265+
static immutable S[14] table =
266+
[
267267
{ 32, 10, 0, 0, 0xCCCCCCCD, 3 },
268268
{ 32, 13, 0, 0, 0x4EC4EC4F, 2 },
269269
{ 32, 14, 1, 0, 0x92492493, 2 },
@@ -279,39 +279,40 @@ void test_udiv_coefficients()
279279
{ 64, 17, 0, 0, 0xF0F0F0F0F0F0F0F1, 4 },
280280
{ 64, 100, 2, 0, 0x28F5C28F5C28F5C3, 2 },
281281
{ 64, 14007, 0, 1, 0x2B71840C5ADF02C3, 14 },
282-
};
282+
];
283283

284-
for (int i = 0; i < sizeof(table)/sizeof(table[0]); i++)
285-
{ S *ps = &table[i];
284+
for (int i = 0; i < table.length; i++)
285+
{ const ps = &table[i];
286286

287287
ullong m;
288288
int shpre;
289289
int shpost;
290-
bool mhighbit = udiv_coefficients(ps->N, ps->d, &shpre, &m, &shpost);
290+
bool mhighbit = udiv_coefficients(ps.N, ps.d, &shpre, &m, &shpost);
291291

292292
//printf("[%d] %d %d %llx %d\n", i, shpre, mhighbit, m, shpost);
293-
assert(shpre == ps->shpre);
294-
assert(mhighbit == (bool)ps->highbit);
295-
assert(m == ps->m);
296-
assert(shpost == ps->shpost);
293+
assert(shpre == ps.shpre);
294+
assert(mhighbit == ps.highbit);
295+
assert(m == ps.m);
296+
assert(shpost == ps.shpost);
297297
}
298298
}
299-
#endif
300-
301-
#if 0
302-
#include <stdlib.h>
303299

304-
void main(int argc, char **argv)
300+
version (none)
305301
{
306-
if (argc == 2)
302+
import core.stdc.stdlib;
303+
304+
extern (D) int main(string[] args)
307305
{
308-
ullong d = atoi(argv[1]);
309-
ullong m;
310-
int shpre;
311-
int shpost;
312-
bool mhighbit = udiv_coefficients(64, d, &shpre, &m, &shpost);
306+
if (args.length == 2)
307+
{
308+
ullong d = atoi(args[1].ptr);
309+
ullong m;
310+
int shpre;
311+
int shpost;
312+
bool mhighbit = udiv_coefficients(64, d, &shpre, &m, &shpost);
313313

314-
printf("%d %d %llx, %d\n", shpre, mhighbit, m, shpost);
314+
printf("%d %d %llx, %d\n", shpre, mhighbit, m, shpost);
315+
}
316+
return 0;
315317
}
316318
}
317-
#endif

src/posix.mak

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ ifeq (,$(AUTO_BOOTSTRAP))
136136
else
137137
# Auto-bootstrapping, will download dmd automatically
138138
# Keep var below in sync with other occurrences of that variable, e.g. in circleci.sh
139-
HOST_DMD_VER=2.072.2
139+
HOST_DMD_VER=2.074.1
140140
HOST_DMD_ROOT=$(TMP)/.host_dmd-$(HOST_DMD_VER)
141141
# dmd.2.072.2.osx.zip or dmd.2.072.2.linux.tar.xz
142142
HOST_DMD_BASENAME=dmd.$(HOST_DMD_VER).$(OS)$(if $(filter $(OS),freebsd),-$(MODEL),)
@@ -340,11 +340,14 @@ BACK_OBJS = go.o gdag.o gother.o gflow.o gloop.o gsroa.o var.o el.o \
340340
cgcod.o cod5.o outbuf.o compress.o \
341341
bcomplex.o aa.o ti_achar.o \
342342
ti_pvoid.o pdata.o cv8.o backconfig.o \
343-
divcoeff.o dwarf.o dwarfeh.o varstats.o \
343+
dwarf.o dwarfeh.o varstats.o \
344344
ph2.o util2.o tk.o strtold.o md5.o \
345345
$(TARGET_OBJS)
346346

347-
G_OBJS = $(addprefix $G/, $(BACK_OBJS))
347+
BACK_DOBJS = divcoeff.o
348+
349+
G_OBJS = $(addprefix $G/, $(BACK_OBJS))
350+
G_DOBJS = $(addprefix $G/, $(BACK_DOBJS))
348351
#$(info $$G_OBJS is [${G_OBJS}])
349352

350353
ifeq (osx,$(OS))
@@ -384,7 +387,7 @@ BACK_SRC = \
384387
$C/dwarf.c $C/dwarf.h $C/aa.h $C/aa.c $C/tinfo.h $C/ti_achar.c \
385388
$C/ti_pvoid.c $C/platform_stub.c $C/code_x86.h $C/code_stub.h \
386389
$C/machobj.c $C/mscoffobj.c \
387-
$C/xmm.h $C/obj.h $C/pdata.c $C/cv8.c $C/backconfig.c $C/divcoeff.c \
390+
$C/xmm.h $C/obj.h $C/pdata.c $C/cv8.c $C/backconfig.c $C/divcoeff.d \
388391
$C/varstats.c $C/varstats.h \
389392
$C/md5.c $C/md5.h \
390393
$C/ph2.c $C/util2.c $C/dwarfeh.c \
@@ -415,7 +418,7 @@ SRC_MAKE = posix.mak osmodel.mak
415418

416419
STRING_IMPORT_FILES = $G/VERSION $G/SYSCONFDIR.imp ../res/default_ddoc_theme.ddoc
417420

418-
DEPS = $(patsubst %.o,%.deps,$(DMD_OBJS) $(GLUE_OBJS) $(BACK_OBJS))
421+
DEPS = $(patsubst %.o,%.deps,$(DMD_OBJS) $(GLUE_OBJS) $(BACK_OBJS) $(BACK_DOBJS))
419422

420423
######## Begin build targets
421424

@@ -440,8 +443,8 @@ toolchain-info:
440443
$G/glue.a: $(G_GLUE_OBJS) $(SRC_MAKE)
441444
$(AR) rcs $@ $(G_GLUE_OBJS)
442445

443-
$G/backend.a: $(G_OBJS) $(SRC_MAKE)
444-
$(AR) rcs $@ $(G_OBJS)
446+
$G/backend.a: $(G_OBJS) $(G_DOBJS) $(SRC_MAKE)
447+
$(AR) rcs $@ $(G_OBJS) $(G_DOBJS)
445448

446449
$G/lexer.a: $(LEXER_SRCS) $(LEXER_ROOT) $(HOST_DMD_PATH) $(SRC_MAKE)
447450
CC="$(HOST_CXX)" $(HOST_DMD_RUN) -lib -of$@ $(MODEL_FLAG) -J$G -L-lstdc++ $(DFLAGS) $(LEXER_SRCS) $(LEXER_ROOT)
@@ -450,14 +453,14 @@ $G/dmd_frontend: $(FRONT_SRCS) $D/gluelayer.d $(ROOT_SRCS) $G/newdelete.o $G/lex
450453
CC="$(HOST_CXX)" $(HOST_DMD_RUN) -of$@ $(MODEL_FLAG) -vtls -J$G -J../res -L-lstdc++ $(DFLAGS) $(filter-out $(STRING_IMPORT_FILES) $(HOST_DMD_PATH),$^) -version=NoBackend
451454

452455
ifdef ENABLE_LTO
453-
$G/dmd: $(DMD_SRCS) $(ROOT_SRCS) $G/newdelete.o $G/lexer.a $(G_GLUE_OBJS) $(G_OBJS) $(STRING_IMPORT_FILES) $(HOST_DMD_PATH) $G/dmd.conf
456+
$G/dmd: $(DMD_SRCS) $(ROOT_SRCS) $G/newdelete.o $G/lexer.a $(G_GLUE_OBJS) $(G_OBJS) $(G_DOBJS) $(STRING_IMPORT_FILES) $(HOST_DMD_PATH) $G/dmd.conf
454457
CC="$(HOST_CXX)" $(HOST_DMD_RUN) -of$@ $(MODEL_FLAG) -vtls -J$G -J../res -L-lstdc++ $(DFLAGS) $(filter-out $(STRING_IMPORT_FILES) $(HOST_DMD_PATH) $G/dmd.conf,$^)
455458
else
456459
$G/dmd: $(DMD_SRCS) $(ROOT_SRCS) $G/newdelete.o $G/backend.a $G/lexer.a $(STRING_IMPORT_FILES) $(HOST_DMD_PATH) $G/dmd.conf
457460
CC="$(HOST_CXX)" $(HOST_DMD_RUN) -of$@ $(MODEL_FLAG) -vtls -J$G -J../res -L-lstdc++ $(DFLAGS) $(filter-out $(STRING_IMPORT_FILES) $(HOST_DMD_PATH) $(LEXER_ROOT) $G/dmd.conf,$^)
458461
endif
459462

460-
$G/dmd-unittest: $(DMD_SRCS) $(ROOT_SRCS) $G/newdelete.o $G/lexer.a $(G_GLUE_OBJS) $(G_OBJS) $(STRING_IMPORT_FILES) $(HOST_DMD_PATH)
463+
$G/dmd-unittest: $(DMD_SRCS) $(ROOT_SRCS) $G/newdelete.o $G/lexer.a $(G_GLUE_OBJS) $(G_OBJS) $(G_DOBJS) $(STRING_IMPORT_FILES) $(HOST_DMD_PATH)
461464
CC=$(HOST_CXX) $(HOST_DMD_RUN) -of$@ $(MODEL_FLAG) -vtls -J$G -J../res -L-lstdc++ $(DFLAGS) -g -unittest -main -version=NoMain $(filter-out $(STRING_IMPORT_FILES) $(HOST_DMD_PATH),$^)
462465

463466
unittest: $G/dmd-unittest
@@ -540,6 +543,10 @@ $(G_OBJS): $G/%.o: $C/%.c $(optabgen_files) $(SRC_MAKE)
540543
@echo " (CC) BACK_OBJS $<"
541544
$(CXX) -c -o$@ $(CXXFLAGS) $(BACK_FLAGS) $(MMD) $<
542545

546+
$(G_DOBJS): $G/%.o: $C/%.d posix.mak
547+
@echo " (CC) BACK_DOBJS $<"
548+
$(HOST_DMD_RUN) -c -of$@ $(DFLAGS) $(MODEL_FLAG) -betterC $<
549+
543550
$(G_GLUE_OBJS): $G/%.o: $D/%.c $(optabgen_files) $(SRC_MAKE)
544551
@echo " (CC) GLUE_OBJS $<"
545552
$(CXX) -c -o$@ $(CXXFLAGS) $(GLUE_FLAGS) $(MMD) $<

0 commit comments

Comments
 (0)