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

Subversion Repositories raytrac

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

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
 
38
}memparam={0,0,0,australia,canada,0,1};
39 33 jguarin200
 
40
//mpx memparam={0,0,australia};
41
 
42
void optParser(int argc, char ** argv){
43
 
44
        char a=0;
45 61 jguarin200
        int e=0,d=0,t=0,s=0,i=0;
46 33 jguarin200
        /*memparam.initialheader=australia;
47
        memparam.width=0;
48
        memparam.depth=0;*/
49 67 jguarin200
        while ((a=getopt(argc,argv,"o:t:e:d:Rr"))!=-1){
50 33 jguarin200
                switch(a){
51 67 jguarin200
                        case 'o':
52
                                memparam.offset=atoi(optarg);
53
                                break;
54 61 jguarin200
                        case 'R': //Raiz Cuadrada
55
                                memparam.R=1;
56
                                break;
57
                        case 'r': //random
58
                                memparam.R=2;
59
                                break;
60 33 jguarin200
                        case 't':
61
                                if (t){
62 35 jguarin200
                                        fprintf (stdout, "error:Doble parametro t...\n");
63 33 jguarin200
                                        exit(-1);
64
                                }
65
                                t++;
66
                                memparam.depth=atoi(optarg);
67
                                break;
68
                        case 'e':
69
                                if (e){
70 35 jguarin200
                                        fprintf (stdout, "error:Doble parametro e...\n");
71 33 jguarin200
                                        exit(-1);
72
                                }
73
                                e++;
74
                                memparam.width+=atoi(optarg);
75
                                break;
76
                        case 'd':
77
                                if (d){
78 35 jguarin200
                                        fprintf (stdout,"error:Doble parametro d...\n");
79 33 jguarin200
                                        exit(-1);
80
                                }
81
                                d++;
82
                                memparam.dec=atoi(optarg);
83
                                memparam.width+=memparam.dec;
84
 
85
                                break;
86
                        case '?':
87 35 jguarin200
                                fprintf(stdout,"error: WTF! %c !?\n",optopt);
88 33 jguarin200
                                exit(-1);
89
                                break;
90
                }
91
        }
92
        if (!e || !d || !t){
93 35 jguarin200
                fprintf(stdout,"uso: memMaker -t numeroDePosicionesDeMemoria -e numeroDeBitsParaLaRepresentacionEntera -d numeroDeBitsParaLaRepresentacionDecimal\n");
94 33 jguarin200
                exit(-1);
95
        }
96
        if ((e+d)>31){
97 35 jguarin200
                fprintf(stdout,"enteros + decimales no puede ser mayor a 31 bits!\n");
98 33 jguarin200
                exit(-1);
99
        }
100
}
101
 
102
int hexreq(long int x){
103
        return ((int)(log2(x)/4))+1;
104
}
105 61 jguarin200
int f0inv(float x){
106
        int I;
107
        float fI;
108
        fI=(1/x);
109 62 jguarin200
        //fprintf (stdout," %f %f ", x, fI);
110 75 jguarin200
        fI*=pow(2,memparam.dec+2);
111 61 jguarin200
        I=fI;
112 75 jguarin200
        I&=0x3ffff;
113 61 jguarin200
        return I;
114
}
115 33 jguarin200
 
116 61 jguarin200
int f1sqrt(float x){
117
        int S;
118
        float fS;
119 75 jguarin200
        fS=(sqrt(x)*pow(2,memparam.dec+2));
120 61 jguarin200
        S=fS;
121 75 jguarin200
        S&=0x3ffff;
122 61 jguarin200
        return S;
123
}
124 33 jguarin200
 
125 61 jguarin200
int f2random(float x){
126
        int mask=pow(2,memparam.width+1)-1;
127
        return random()&mask;
128
}
129
typedef int (*ff2i)(float);
130 33 jguarin200
void generatenums(void){
131
 
132
        int index;
133
        unsigned long int factor;
134 61 jguarin200
        float ffactor,epsilon;
135 33 jguarin200
        char buff[1024],sign;
136 61 jguarin200
        ff2i xf[]={f0inv,f1sqrt,f2random};
137
        int depthpfw=hexreq(memparam.depth);
138
        int widthpfw=((int)(memparam.width/4))+(memparam.width%4?1:0);
139 33 jguarin200
        srandom(time(0));
140 61 jguarin200
        epsilon=1/(float)memparam.depth;
141
 
142
        fprintf(stdout,"-- epsilon: %f\n",epsilon);
143 33 jguarin200
        for(index=0;index<memparam.depth ;index++){
144 67 jguarin200
                factor=xf[memparam.R](memparam.offset*(1+index*epsilon));
145 61 jguarin200
                sign=memparam.R==2?((factor&(1<<memparam.width))?'-':'+'):'+';
146 33 jguarin200
                ffactor=(factor&(1<<memparam.width))?(factor^(int)(pow(2,memparam.width+1)-1))+1:factor;
147
                ffactor/=pow(2,memparam.dec);
148
                memset(buff,0,1024);
149 43 jguarin200
                sprintf(buff,"%c0%dx : %c0%dx; -- FIXED => %x . %x (%d . %d) FLOAT %c%f\n",
150 33 jguarin200
                                '%',
151
                                depthpfw,
152
                                '%',
153
                                widthpfw,
154
                                factor>>memparam.dec,
155
                                factor&(int)(pow(2,memparam.dec)-1),
156
                                factor>>memparam.dec,
157
                                factor&(int)(pow(2,memparam.dec)-1),
158
                                sign,
159
                                ffactor);
160 35 jguarin200
                fprintf(stdout,buff,index,factor);
161 33 jguarin200
        }
162
 
163
}
164
void printmem(void){
165 43 jguarin200
        fprintf (stdout,memparam.initialheader,memparam.depth,memparam.width+1);
166 33 jguarin200
        generatenums();
167 35 jguarin200
        fprintf (stdout,memparam.end);
168 33 jguarin200
 
169
}
170
 
171
int main (int argc, char **argv){
172 35 jguarin200
 
173
        fprintf (stdout,"--RAND MAX: 0x%x\n", RAND_MAX);
174
#ifdef __MINGW32__
175
        fprintf (stdout,"--MINGW32 VERSION\n");
176
#else 
177
        fprintf (stdout,"--UNIX BASED VERSION\n");
178
#endif
179
 
180 33 jguarin200
        optParser(argc,argv);
181
        printmem();
182
}

powered by: WebSVN 2.1.0

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