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
3847void  u128Div (ullong xh, ullong xl, ullong yh, ullong yl, ullong * pqh, ullong * pql)
3948{
@@ -43,10 +52,10 @@ void u128Div(ullong xh, ullong xl, ullong yh, ullong yl, ullong *pqh, ullong *pq
4352
4453    // ullong xxh = xh, xxl = xl, yyh = yh, yyl = yl;
4554
46-     assert (yh  ||  yl );           // no div-by-0 bugs 
55+ //     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,31 +94,32 @@ 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 " 
100+         if  (xxh ==  0  &&  yyh ==  0 )
101+             printf(" should be %llx\n " /  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{
110-     assert (N  ==  32  ||  N  ==  64 );
111-     assert (prec  <= N );
112-     assert (d  >  1  &&  (d  &  (d  -  1 )));
120+ //     assert(N == 32 || N == 64);
121+ //     assert(prec <= N);
122+ //     assert(d > 1 && (d & (d - 1)));
113123
114124    //  Compute b such that 2**(b-1) < d <= 2**b
115125    //  which is the number of significant bits in d
@@ -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);
@@ -195,8 +205,8 @@ bool choose_multiplier(int N, ullong d, int prec, ullong *pm, int *pshpost)
195205        * pm = mhighl;
196206        mhighbit = mhighh & 1 ;
197207    }
198-     else 
199-         assert (0 );
208+ //     else
209+ //         assert(0);
200210
201211    * pshpost = shpost;
202212    return  mhighbit;
@@ -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    {
@@ -237,21 +243,15 @@ bool udiv_coefficients(int N, ullong d, int *pshpre, ullong *pm, int *pshpost)
237243        }
238244        * pshpre = e;
239245        mhighbit = choose_multiplier(N, d, N -  e, pm, pshpost);
240-         assert (mhighbit  ==  false);
246+ //         assert(mhighbit == false);
241247    }
242248    else 
243249        * pshpre = 0 ;
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 " 
315+         }
316+         return  0 ;
315317    }
316318}
317- #endif 
0 commit comments