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

Subversion Repositories raytrac

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

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

powered by: WebSVN 2.1.0

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