OpenCores
URL https://opencores.org/ocsvn/raytrac/raytrac/trunk

Subversion Repositories raytrac

[/] [raytrac/] [trunk/] [utils/] [memMaker.c] - Blame information for rev 195

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 33 jguarin200
/*
2
 *  memMaker.c
3
 *  memoryMaker
4
 *
5
 *  Created by julian on 23/02/11.
6
 *  GPL LICENSED
7
 *  The goal of this peace of code is to create a memory initialization file of random fixed point numbers
8
 *  in order to simulate RtEngine.
9
 *  Usage is
10
 */
11
 
12
#include <stdio.h>
13
#include <stdlib.h>
14
#include <unistd.h>
15
#include <math.h>
16
#include <time.h>
17
#include <string.h>
18
 
19 35 jguarin200
#ifdef __MINGW32__
20
#define random() ((((long int)rand())<<17)|rand())
21
#define srandom srand
22
#endif
23
 
24
 
25 33 jguarin200
char australia[]="DEPTH = %03d;\nWIDTH = %02d;\nADDRESS_RADIX=HEX;\nDATA_RADIX=HEX;\nCONTENT\nBEGIN\n\n\0";
26
char canada[]="END;\n\0";
27
struct {
28
        int depth;
29
        int width;
30
        int dec;
31 67 jguarin200
 
32 33 jguarin200
        char *initialheader;
33
        char *end;
34 61 jguarin200
        int R;
35 33 jguarin200
 
36 67 jguarin200
        int offset;
37 101 jguarin200
        int addressoffset;
38 67 jguarin200
 
39 79 jguarin200
 
40 101 jguarin200
}memparam={0,0,0,australia,canada,0,1,0};
41 33 jguarin200
 
42
//mpx memparam={0,0,australia};
43
 
44
void optParser(int argc, char ** argv){
45
 
46
        char a=0;
47 61 jguarin200
        int e=0,d=0,t=0,s=0,i=0;
48 33 jguarin200
        /*memparam.initialheader=australia;
49
        memparam.width=0;
50
        memparam.depth=0;*/
51 101 jguarin200
        while ((a=getopt(argc,argv,"a:o:t:e:d:Rr"))!=-1){
52 33 jguarin200
                switch(a){
53 101 jguarin200
                        case 'a':
54
                                memparam.addressoffset=atoi(optarg);
55
                                break;
56 67 jguarin200
                        case 'o':
57
                                memparam.offset=atoi(optarg);
58
                                break;
59 61 jguarin200
                        case 'R': //Raiz Cuadrada
60
                                memparam.R=1;
61
                                break;
62
                        case 'r': //random
63
                                memparam.R=2;
64
                                break;
65 33 jguarin200
                        case 't':
66
                                if (t){
67 35 jguarin200
                                        fprintf (stdout, "error:Doble parametro t...\n");
68 33 jguarin200
                                        exit(-1);
69
                                }
70
                                t++;
71
                                memparam.depth=atoi(optarg);
72
                                break;
73
                        case 'e':
74
                                if (e){
75 35 jguarin200
                                        fprintf (stdout, "error:Doble parametro e...\n");
76 33 jguarin200
                                        exit(-1);
77
                                }
78
                                e++;
79
                                memparam.width+=atoi(optarg);
80
                                break;
81
                        case 'd':
82
                                if (d){
83 35 jguarin200
                                        fprintf (stdout,"error:Doble parametro d...\n");
84 33 jguarin200
                                        exit(-1);
85
                                }
86
                                d++;
87
                                memparam.dec=atoi(optarg);
88
                                memparam.width+=memparam.dec;
89
 
90
                                break;
91
                        case '?':
92 35 jguarin200
                                fprintf(stdout,"error: WTF! %c !?\n",optopt);
93 33 jguarin200
                                exit(-1);
94
                                break;
95
                }
96
        }
97
        if (!e || !d || !t){
98 35 jguarin200
                fprintf(stdout,"uso: memMaker -t numeroDePosicionesDeMemoria -e numeroDeBitsParaLaRepresentacionEntera -d numeroDeBitsParaLaRepresentacionDecimal\n");
99 33 jguarin200
                exit(-1);
100
        }
101
        if ((e+d)>31){
102 35 jguarin200
                fprintf(stdout,"enteros + decimales no puede ser mayor a 31 bits!\n");
103 33 jguarin200
                exit(-1);
104
        }
105
}
106
 
107
int hexreq(long int x){
108
        return ((int)(log2(x)/4))+1;
109
}
110 61 jguarin200
int f0inv(float x){
111
        int I;
112
        float fI;
113
        fI=(1/x);
114 62 jguarin200
        //fprintf (stdout," %f %f ", x, fI);
115 79 jguarin200
        fI*=pow(2,memparam.dec);
116 61 jguarin200
        I=fI;
117 75 jguarin200
        I&=0x3ffff;
118 61 jguarin200
        return I;
119
}
120 33 jguarin200
 
121 61 jguarin200
int f1sqrt(float x){
122
        int S;
123
        float fS;
124 79 jguarin200
        fS=(sqrt(x)*pow(2,memparam.dec));
125 61 jguarin200
        S=fS;
126 75 jguarin200
        S&=0x3ffff;
127 61 jguarin200
        return S;
128
}
129 33 jguarin200
 
130 61 jguarin200
int f2random(float x){
131
        int mask=pow(2,memparam.width+1)-1;
132
        return random()&mask;
133
}
134
typedef int (*ff2i)(float);
135 33 jguarin200
void generatenums(void){
136
 
137
        int index;
138
        unsigned long int factor;
139 61 jguarin200
        float ffactor,epsilon;
140 33 jguarin200
        char buff[1024],sign;
141 61 jguarin200
        ff2i xf[]={f0inv,f1sqrt,f2random};
142
        int depthpfw=hexreq(memparam.depth);
143
        int widthpfw=((int)(memparam.width/4))+(memparam.width%4?1:0);
144 33 jguarin200
        srandom(time(0));
145 61 jguarin200
        epsilon=1/(float)memparam.depth;
146
 
147
        fprintf(stdout,"-- epsilon: %f\n",epsilon);
148 101 jguarin200
        for(index=memparam.addressoffset;index<memparam.depth+memparam.addressoffset;index++){
149
                factor=xf[memparam.R](memparam.offset*(1+(index-memparam.addressoffset)*epsilon));
150 61 jguarin200
                sign=memparam.R==2?((factor&(1<<memparam.width))?'-':'+'):'+';
151 33 jguarin200
                ffactor=(factor&(1<<memparam.width))?(factor^(int)(pow(2,memparam.width+1)-1))+1:factor;
152
                ffactor/=pow(2,memparam.dec);
153
                memset(buff,0,1024);
154 43 jguarin200
                sprintf(buff,"%c0%dx : %c0%dx; -- FIXED => %x . %x (%d . %d) FLOAT %c%f\n",
155 33 jguarin200
                                '%',
156
                                depthpfw,
157
                                '%',
158
                                widthpfw,
159
                                factor>>memparam.dec,
160
                                factor&(int)(pow(2,memparam.dec)-1),
161
                                factor>>memparam.dec,
162
                                factor&(int)(pow(2,memparam.dec)-1),
163
                                sign,
164
                                ffactor);
165 35 jguarin200
                fprintf(stdout,buff,index,factor);
166 33 jguarin200
        }
167
 
168
}
169
void printmem(void){
170 43 jguarin200
        fprintf (stdout,memparam.initialheader,memparam.depth,memparam.width+1);
171 33 jguarin200
        generatenums();
172 35 jguarin200
        fprintf (stdout,memparam.end);
173 33 jguarin200
 
174
}
175
 
176
int main (int argc, char **argv){
177 35 jguarin200
 
178
        fprintf (stdout,"--RAND MAX: 0x%x\n", RAND_MAX);
179
#ifdef __MINGW32__
180
        fprintf (stdout,"--MINGW32 VERSION\n");
181
#else 
182
        fprintf (stdout,"--UNIX BASED VERSION\n");
183
#endif
184
 
185 33 jguarin200
        optParser(argc,argv);
186
        printmem();
187
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.