summaryrefslogtreecommitdiffstats
path: root/src/misc/extra/extraUtilMacc.c
blob: 67515aa718753d489abf0e1482847b021add2555 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**CFile****************************************************************

  FileName    [extraUtilMacc.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [extra]

  Synopsis    [Generates multiplier accumulators.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: extraUtilMacc.c,v 1.0 2003/02/01 00:00:00 alanmi Exp $]

***********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "misc/vec/vec.h"
#include "misc/vec/vecMem.h"
#include "misc/extra/extra.h"
#include "misc/util/utilTruth.h"

ABC_NAMESPACE_IMPL_START

////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Macc_ConstMultSpecOne2( FILE * pFile, int n, int nBits, int nWidth )
{
    int nTotal = nWidth+nBits;
    int Bound  = 1 << (nBits-1);
    assert( -Bound <= n && n < Bound );
    fprintf( pFile, "// %d-bit multiplier-accumulator with constant %d generated by ABC on %s\n", nTotal, n, Extra_TimeStamp() );
    fprintf( pFile, "module mulacc%03d%s (\n", Abc_AbsInt(n), n < 0 ? "_neg" : "_pos" );
    fprintf( pFile, "    input  [%d:0] i,\n", nTotal-1 );
    fprintf( pFile, "    input  [%d:0] s,\n", nTotal-1 );
    fprintf( pFile, "    output [%d:0] o\n",  nTotal-1 );
    fprintf( pFile, ");\n" );
    fprintf( pFile, "    wire [%d:0] c = %d\'h%x;\n", nTotal-1, nTotal, Abc_AbsInt(n) );
    fprintf( pFile, "    wire [%d:0] m = i * c;\n",   nTotal-1 );
    fprintf( pFile, "    assign o = s %c m;\n",       n < 0 ? '-' : '+' );
    fprintf( pFile, "endmodule\n\n" );
}
void Macc_ConstMultSpecOne( FILE * pFile, int n, int nBits, int nWidth )
{
    int nTotal = nWidth+nBits;
    int Bound  = 1 << (nBits-1);
    assert( -Bound <= n && n < Bound );
    fprintf( pFile, "// %d-bit multiplier by %d-bit constant %d generated by ABC\n", nWidth, nBits, n );
    fprintf( pFile, "module mul%03d%s (\n", Abc_AbsInt(n), n < 0 ? "_neg" : "_pos" );
    fprintf( pFile, "    input  [%d:0] i,\n", nWidth-1 );
    fprintf( pFile, "    output [%d:0] o\n", nWidth-1 );
    fprintf( pFile, ");\n" );
    fprintf( pFile, "    wire [%d:0] c = %d\'h%x;\n",          nBits-1,  nBits, Abc_AbsInt(n) );
    fprintf( pFile, "    wire [%d:0] I = {{%d{i[%d]}}, i};\n", nTotal-1, nBits, nWidth-1 );
    fprintf( pFile, "    wire [%d:0] m = I * c;\n",            nTotal-1 );
    fprintf( pFile, "    wire [%d:0] t = %cm;\n",              nTotal-1, n < 0 ? '-' : ' ' );
    fprintf( pFile, "    assign o = t[%d:%d];\n",              nTotal-1, nBits );
    fprintf( pFile, "endmodule\n\n" );
}
void Macc_ConstMultSpecTest()
{
    int nBits  =  8;
    int nWidth = 16;
    int Bound  =  1 << (nBits-1);
    int i;
    char Buffer[100];
    FILE * pFile;
    for ( i = -Bound; i < Bound; i++ )
    {
        sprintf( Buffer, "const_mul//macc_spec_%03d.v", 0xFF & i );
        pFile = fopen( Buffer, "wb" );
        Macc_ConstMultSpecOne2( pFile, i, nBits, nWidth );
        fclose( pFile );
    }
}

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned * Macc_ConstMultGenerate( int nBits )
{
    unsigned Mask = Abc_InfoMask( nBits );
    Vec_Wec_t * vDivs = Vec_WecStart( 2*nBits );
    Vec_Int_t * vOne, * vTwo, * vThree;
    unsigned * pPlace = ABC_CALLOC( unsigned, (1 << nBits) );
    int n, a, b, One, Two, i, k, New, Bound = 1 << (nBits-1);
    // skip trivial
    pPlace[0] = ~0;
    pPlace[1] = ~0;
    pPlace[Mask & (-1)] = ~0;
    // skip div by 2
    for ( i = 2; i < (1 << nBits); i++ )
        if ( (i & 1) == 0 )
            pPlace[i] = ~0;
    // collect trivial
    printf( "Generating numbers using %d adders:\n", 0 );
    for ( i = 0; i < nBits; i++ )
    {
        Vec_WecPush( vDivs, 0, 1<<i );
        printf( "%d = %d << %d\n", 1<<i, 1, i );
    }
    // generate for each adder count
    for ( n = 1; n < nBits; n++ )
    {
        // quit if all of them are finished
        for ( a = 1; a < (1 << nBits); a++ )
            if ( pPlace[a] == 0 )
                break;
        if ( a == (1 << nBits) )
            break;

        printf( "Generating numbers using %d adders:\n", n );
        vThree = Vec_WecEntry( vDivs, n );
        for ( a = 0; a < nBits; a++ )
        for ( b = 0; b < nBits; b++ )
        {
            if ( a + b != n-1 )
                continue;
            vOne = Vec_WecEntry( vDivs, a );
            vTwo = Vec_WecEntry( vDivs, b );
            Vec_IntForEachEntry( vOne, One, i )
            Vec_IntForEachEntry( vTwo, Two, k )
            {
                New = One + Two;
                if ( New >= -Bound && New < Bound && pPlace[Mask & New] == 0 )
                {
                    if ( New > 0 )
                        Vec_IntPush( vThree, New );

                    pPlace[Mask & New] = (One << 16) | Two;
                    printf( "%d = %d + %d\n", New, One, Two );
                }
                New = One - Two;
                if ( New >= -Bound && New < Bound && pPlace[Mask & New] == 0 )
                {
                    if ( New > 0 )
                        Vec_IntPush( vThree, New );

                    pPlace[Mask & New] = (One << 16) | Two | (1 << 15);
                    printf( "%d = %d - %d\n", New, One, Two );
                }
                New = Two - One;
                if ( New >= -Bound && New < Bound && pPlace[Mask & New] == 0 )
                {
                    if ( New > 0 )
                        Vec_IntPush( vThree, New );

                    pPlace[Mask & New] = (Two << 16) | One | (1 << 15);
                    printf( "%d = %d - %d\n", New, Two, One );
                }
            }
            printf( "Adding one incrementor:\n" );
            Vec_IntForEachEntry( vThree, One, i )
            {
                if ( One < 0 )
                    continue;
                New = -One;
                if ( pPlace[Mask & New] == 0 )
                {
                    pPlace[Mask & New] = One << 16;
                    printf( "-%d = ~%d + 1\n", One, One );
                }
            }
        }
    }
    Vec_WecPrint( vDivs, 0 );
    Vec_WecFree( vDivs );
    //ABC_FREE( pPlace );
    return pPlace;
}
void Macc_ConstMultGenTest0()
{
    int nBits  =  8;
    unsigned * p = Macc_ConstMultGenerate( nBits );
    ABC_FREE( p );
}

void Macc_ConstMultGenOne_rec( FILE * pFile, unsigned * p, int n, int nBits, int nWidth )
{
    unsigned Mask = Abc_InfoMask( nBits );
    unsigned New  = Mask & (unsigned)n;
    int nTotal    = nWidth+nBits;
    int One = p[New] >> 16;
    int Two = p[New] & 0x7FFF;
    char Sign  = n < 0 ? 'N' : 'n';
    char Oper  = ((p[New] >> 15) & 1) ? '-' : '+';
    if ( p[New] == ~0 )
    {
        // count trailing zeros
        int nn, nZeros;
        for ( nZeros = 0; nZeros < nBits; nZeros++ )
            if ( (n >> nZeros) & 1 )
                break;
        nn = n >> nZeros;
        if ( nn == -1 )
            fprintf( pFile, "    wire [%d:0] N1 = -n1;\n", nTotal-1 );
        if ( Abc_AbsInt(nn) != 1 )
            Macc_ConstMultGenOne_rec( pFile, p, nn, nBits, nWidth );
        if ( nZeros > 0 )
            fprintf( pFile, "    wire [%d:0] %c%d = %c%d << %d;\n", nTotal-1, Sign, Abc_AbsInt(n), Sign, Abc_AbsInt(nn), nZeros );
    }
    else if ( One && Two ) // add/sub
    {
        Macc_ConstMultGenOne_rec( pFile, p, One, nBits, nWidth );
        Macc_ConstMultGenOne_rec( pFile, p, Two, nBits, nWidth );
        fprintf( pFile, "    wire [%d:0] %c%d = n%d %c n%d;\n", nTotal-1, Sign, Abc_AbsInt(n), One, Oper, Two );
    }
    else if ( Two == 0 ) // minus
    {
        Macc_ConstMultGenOne_rec( pFile, p, One, nBits, nWidth );
        fprintf( pFile, "    wire [%d:0] N%d = -n%d;\n", nTotal-1, One, One );
    }
}
void Macc_ConstMultGenMult( FILE * pFile, unsigned * p, int n, int nBits, int nWidth )
{
    int nTotal = nWidth+nBits;
    int Bound  = 1 << (nBits-1);
    char Sign  = n < 0 ? 'N' : 'n';
    assert( -Bound <= n && n < Bound );
    fprintf( pFile, "// %d-bit multiplier by %d-bit constant %d generated by ABC\n", nWidth, nBits, n );
    fprintf( pFile, "module mul%03d%s (\n", Abc_AbsInt(n), n < 0 ? "_neg" : "_pos" );
    fprintf( pFile, "    input  [%d:0] i,\n", nWidth-1 );
    fprintf( pFile, "    output [%d:0] o\n", nWidth-1 );
    fprintf( pFile, ");\n" );
    if ( n == 0 )
        fprintf( pFile, "    assign o = %d\'h0;\n", nWidth );
    else 
    {
        fprintf( pFile, "    wire [%d:0] n1 = {{%d{i[%d]}}, i};\n", nTotal-1, nBits, nWidth-1 );
        Macc_ConstMultGenOne_rec( pFile, p, n, nBits, nWidth );
        fprintf( pFile, "    assign o = %c%d[%d:%d];\n", Sign, Abc_AbsInt(n), nTotal-1, nBits );
    }
    fprintf( pFile, "endmodule\n\n" );
}
void Macc_ConstMultGenMacc( FILE * pFile, unsigned * p, int n, int nBits, int nWidth )
{
    int nTotal = nWidth+nBits;
    int Bound  = 1 << (nBits-1);
    char Sign  = n < 0 ? 'N' : 'n';
    assert( -Bound <= n && n < Bound );
    fprintf( pFile, "// %d-bit multiplier-accumulator by %d-bit constant %d generated by ABC\n", nWidth, nBits, n );
    fprintf( pFile, "module macc%03d%s (\n", Abc_AbsInt(n), n < 0 ? "_neg" : "_pos" );
    fprintf( pFile, "    input  [%d:0] i,\n", nWidth-1 );
    fprintf( pFile, "    input  [%d:0] c,\n", nWidth-1 );
    fprintf( pFile, "    output [%d:0] o\n", nWidth-1 );
    fprintf( pFile, ");\n" );
    if ( n == 0 )
        fprintf( pFile, "    assign o = c;\n" );
    else 
    {
        fprintf( pFile, "    wire [%d:0] n1 = {{%d{i[%d]}}, i};\n", nTotal-1, nBits, nWidth-1 );
        Macc_ConstMultGenOne_rec( pFile, p, n, nBits, nWidth );
        fprintf( pFile, "    wire [%d:0] s = %c%d[%d:%d];\n", nWidth-1, Sign, Abc_AbsInt(n), nTotal-1, nBits );
        fprintf( pFile, "    assign o = s + c;\n" );
    }
    fprintf( pFile, "endmodule\n\n" );
}
void Macc_ConstMultGenMacc2( FILE * pFile, unsigned * p, int n, int nBits, int nWidth )
{
    int nTotal = nWidth+nBits;
    int Bound  = 1 << (nBits-1);
    char Sign  = n < 0 ? 'N' : 'n';
    assert( -Bound <= n && n < Bound );
    fprintf( pFile, "// %d-bit multiplier-accumulator by constant %d generated by ABC on %s\n", nTotal, n, Extra_TimeStamp() );
    fprintf( pFile, "module macc%03d%s (\n", Abc_AbsInt(n), n < 0 ? "_neg" : "_pos" );
    fprintf( pFile, "    input  [%d:0] i,\n", nTotal-1 );
    fprintf( pFile, "    input  [%d:0] s,\n", nTotal-1 );
    fprintf( pFile, "    output [%d:0] o\n", nTotal-1 );
    fprintf( pFile, ");\n" );
    if ( n == 0 )
        fprintf( pFile, "    assign o = s;\n" );
    else 
    {
        fprintf( pFile, "    wire [%d:0] n1 = i;\n", nTotal-1 );
        Macc_ConstMultGenOne_rec( pFile, p, n, nBits, nWidth );
        fprintf( pFile, "    assign o = s + %c%d;\n", Sign, Abc_AbsInt(n) );
    }
    fprintf( pFile, "endmodule\n\n" );
}
void Macc_ConstMultGenTest()
{
    int nBits  =  8;
    int nWidth = 16;
    int Bound  =  1 << (nBits-1);
    unsigned * p = Macc_ConstMultGenerate( nBits );
    int i;
    char Buffer[100];
    FILE * pFile;
    for ( i = -Bound; i < Bound; i++ )
    {
        sprintf( Buffer, "const_mul//macc%03d.v", 0xFF & i );
        pFile = fopen( Buffer, "wb" );
        Macc_ConstMultGenMacc2( pFile, p, i, nBits, nWidth );
        fclose( pFile );
    }
    ABC_FREE( p );
}

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END