Home > Archive > C > March 2004 > Statically create a table of data in C: How?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Statically create a table of data in C: How?
|
|
| robin.pain@tesco.net 2004-03-26, 11:08 pm |
| Is it possible to get a C compiler to compute a table?
I *don't* want to e.g. use Excel and have to copy it in by hand!
Robin
| |
| Joona I Palaste 2004-03-26, 11:08 pm |
| robin.pain@tesco.net <robin.pain@tesco.net> scribbled the following:
> Is it possible to get a C compiler to compute a table?
> I *don't* want to e.g. use Excel and have to copy it in by hand!
C compilers very rarely compute tables, it's not very related to the
job of compiling a C program. But you could write a C program to
compute a table and compile it with a C compiler.
It is impossible to give a specific answer to your question, because
you haven't told us what "compute a table" means. I am pretty sure you
want a two-dimensional array of numbers, but then what? What do you
want to do with the numbers?
Depending on whether the answer to that question is more like "I want
cell A347 to be the sum of cells A1 through Z346" or "I want to be able
to move my mouse on a cell and open a pop-up menu with the right mouse
button, preferably in 10-point Tacoma font", the answer to your
original question is either "definitely yes" or "not in ISO standard C,
but ask in a platform-specific newsgroup".
--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"We sorcerers don't like to eat our words, so to say."
- Sparrowhawk
| |
| Mike Wahler 2004-03-26, 11:08 pm |
|
<robin.pain@tesco.net> wrote in message
news:bd24a397.0403250815.597f27fa@posting.google.com...
> Is it possible to get a C compiler to compute a table?
>
> I *don't* want to e.g. use Excel and have to copy it in by hand!
Write a program to generate the code with the static data.
Use your table data for the input.
-Mike
| |
| Bernhard Holzmayer 2004-03-26, 11:08 pm |
| robin.pain@tesco.net wrote:
> Is it possible to get a C compiler to compute a table?
>
> I *don't* want to e.g. use Excel and have to copy it in by hand!
>
> Robin
I guess you have a table, maybe in Excel, and you want to insert its
contents to a C header file. Is this, what you mean?
If so, one possible way (which I took some time ago):
1) export the table in CSV mode with the table separator set to ','
2) define a header file which defines a field or struct type which
fits to the table's content, end it with a =
and an #include "table"
3) there you are.
If you need an example, let me know.
Bernhard
| |
| robin.pain@tesco.net 2004-03-26, 11:08 pm |
| Joona I Palaste <palaste@cc.helsinki.fi> wrote in message news:<c3v10o$bui$1@oravannahka.helsinki.fi>...
> robin.pain@tesco.net <robin.pain@tesco.net> scribbled the following:
>
>
> C compilers very rarely compute tables, it's not very related to the
> job of compiling a C program. But you could write a C program to
> compute a table and compile it with a C compiler.
> It is impossible to give a specific answer to your question, because
> you haven't told us what "compute a table" means. I am pretty sure you
> want a two-dimensional array of numbers, but then what? What do you
> want to do with the numbers?
<snip>
Thanks Joona, it is the table of pre-computed "divisions" needed for
the table-driven CRC.
I think I know how to do it. This compiler also has an assembler and
the assembler has "WHILE" and "IF" directives. I can use these to
write a macro to pre-compute the table values and then have it insert
each result "inline".
The C code can reference the table in the Assembler code and the
linker can connect the two. Mind you, I have not tried it yet!
Cheers
Robin
| |
| Kevin Bracey 2004-03-26, 11:08 pm |
| In message <bd24a397.0403260045.6b537cc2@posting.google.com>
robin.pain@tesco.net (robin.pain@tesco.net) wrote:
> Thanks Joona, it is the table of pre-computed "divisions" needed for
> the table-driven CRC.
>
> I think I know how to do it. This compiler also has an assembler and
> the assembler has "WHILE" and "IF" directives. I can use these to
> write a macro to pre-compute the table values and then have it insert
> each result "inline".
>
> The C code can reference the table in the Assembler code and the
> linker can connect the two. Mind you, I have not tried it yet!
A somewhat more portable way of doing it is to write a program that creates a
C source file.
For example:
/* Generate a table of 1 / n for n in the range [1..255], *
* giving the results to 28 binary places. */
#include <stdio.h>
int main(void)
{
printf("/* Reciprocal table generated on " __DATE__ " */\n"
"\n"
"static const unsigned long reciptable[256] =\n"
"{\n"
" 0xFFFFFFFF,"); // dummy entry for 1/0
for (unsigned i = 1; i < 256; i++)
printf("%s0x%.8lX,", i % 4 == 0 ? "\n " : " ",
(0x10000000UL+i/2) / i);
printf("\n}\n");
}
[ Insert standard "don't use C99" replies here ]
Then you just include the generated output in the part of your main program
that needs it:
/* myprogram.c */
....
#include "table.c"
....
Then if you're using a conventional make system, you just need to add the
dependencies. Something along the lines of:
myprogram.o: table.c
table.c: maketable
./maketable > table.c
maketable: maketable.c
cc maketable
This method has the advantage that you can precompute anything at
compile-time that you could compute at run-time. I suspect your assembler's
set of mathematical functions is somewhat more limited, and wouldn't extend
to, say, generating a precomputed cosine table.
--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
| |
| CBFalconer 2004-03-26, 11:08 pm |
| "robin.pain@tesco.net" wrote:
> Joona I Palaste <palaste@cc.helsinki.fi> wrote in message
>
>
> <snip>
>
> Thanks Joona, it is the table of pre-computed "divisions" needed
> for the table-driven CRC.
>
> I think I know how to do it. This compiler also has an assembler
> and the assembler has "WHILE" and "IF" directives. I can use these
> to write a macro to pre-compute the table values and then have it
> insert each result "inline".
>
> The C code can reference the table in the Assembler code and the
> linker can connect the two. Mind you, I have not tried it yet!
Simply declare the table without any initialization, and write a
routine to initialize it. That routine will be relatively slow,
but will be executed only once at start-up, rather than per byte
of the data to be CRCd. The result is easily modified to handle
different polynomials, requires no tricks, etc. etc.
You can probably automate the initialization by testing a static
array.
static int crctable[CHAR_BIT];
....
if (0 == crctable[1]) init(crctable);
but that may involve excess running time for the test.
--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
| |
| robin.pain@tesco.net 2004-03-29, 4:35 am |
| Kevin Bracey <kevin.bracey@tematic.com> wrote in message news:<fe26b3954c.kbracey@tematic.com>...
> In message <bd24a397.0403260045.6b537cc2@posting.google.com>
> robin.pain@tesco.net (robin.pain@tesco.net) wrote:
>
>
> A somewhat more portable way of doing it is to write a program that creates a
> C source file.
>
> For example:
>
> /* Generate a table of 1 / n for n in the range [1..255], *
> * giving the results to 28 binary places. */
> #include <stdio.h>
>
> int main(void)
> {
> printf("/* Reciprocal table generated on " __DATE__ " */\n"
> "\n"
> "static const unsigned long reciptable[256] =\n"
> "{\n"
> " 0xFFFFFFFF,"); // dummy entry for 1/0
>
> for (unsigned i = 1; i < 256; i++)
> printf("%s0x%.8lX,", i % 4 == 0 ? "\n " : " ",
> (0x10000000UL+i/2) / i);
>
> printf("\n}\n");
> }
>
> [ Insert standard "don't use C99" replies here ]
>
> Then you just include the generated output in the part of your main program
> that needs it:
>
> /* myprogram.c */
> ....
> #include "table.c"
> ....
>
> Then if you're using a conventional make system, you just need to add the
> dependencies. Something along the lines of:
>
> myprogram.o: table.c
>
> table.c: maketable
> ./maketable > table.c
>
> maketable: maketable.c
> cc maketable
>
> This method has the advantage that you can precompute anything at
> compile-time that you could compute at run-time. I suspect your assembler's
> set of mathematical functions is somewhat more limited, and wouldn't extend
> to, say, generating a precomputed cosine table.
You don't realise how helpful this is, in one hit I find out about
__data__, printf and makefile (otherwise <help> was useless because I
didn't know what the question was:)
Many thanks, extremely grateful
Robin Pain
I am in Mercer's Row, just off Newmarket Rd
|
|
|
|
|