I need to write my own optimised math library for embedded processor. How can I do that?
Three answers:
2007-05-15 21:26:19 UTC
You need to write a class......you are prolly better off using C++ because it is more object oriented than C.....I haven't worked with C a lot but in C++ you create a .h file, in your case math.h, and you include it like #include
but, the syntax is quotes like
#include
#include"math.h"
NOTE:
#include, is legal with out a source file (a .cpp file) , and will include the standard math libary .cpp (source file) that was already created by the standard, so you might wanna use a different name I dont know if it will confuse the compiler or not...
the .h file is your decleration, it is where you would put things such as (this is my class to handle big integers
#ifndef _BIG_INT_H_
#define _BIG_INT_H_
class big_int
{
public:
big_int::big_int(); // default constructor
big_int big_int::operator+(big_int); // operator overloading for addition
// add_result has a differnt value at end of loop, and sum of values is result
//however i do not know why it is not working, and the error is not in the addition
// it outputs numbers whoes sum is the multiplication result
std::cout << '\n';
std::cout << "ADD ";
add_result.output();
result = (result + add_result);
std::cout << '\n';
result.output();
// makes add_result have a zero value
add_result.num_array[0] = 0;
add_result.set_array_size(1);
// decriment index
--biggest_index;
};
//decriment index
--smallest_index;
};
;
return result;
};
/*
big_int big_int::fact(const int in_fact){
big_int result;
result.set_array_size(1);
result.num_array[0] = 1;
//big int with value of one to add to incriment
big_int one;
one.set_array_size(1);
one.num_array[0] = 1;
big_int incriment; // big int that will be incriment up to the value of fact
incriment.set_array_size(1);
incriment.num_array[0] = 0;
//calculates the factoral by multiplying result *= incriment
// then incrimenting incriment by one
for(int i = 0; i < fact; ++i){
result = result * incriment;
incriment = incriment + one;
};
return result;
};
*/
// outputs the big_int stored in the array
void big_int::output(){
for(int i=0; i < size(); ++i){
std::cout << num_array[i];
if(0 == (i % 50) && i != 0){
std::cout << '\n';
};
};
};
// sets the size of the private member array_size
void big_int::set_array_size(int size){
array_size = size;
};
// returns the size of the array
int big_int::size(){
return array_size;
};
classes are pretty confusing, that code I showed you compiles, but doesn't completely work like it should and is done in C++ lol......
classes will allow you in your main program to do things such as
x.add(1), vector.push_back(5), if you are familiar with vectors, they are sort of like complex functions...you can also overload operators, (operator is a key word in C++) i dont know about C.....if you search online you can prolly find a source code to a math library.....librarys are classes....
You also need to compile your classes, with your main program, it is the same as compiling a function with a header file......
sorry to post all this code on here lol
good luck!
griz803
2007-05-16 04:29:00 UTC
Assuming that you have a C compiler with other utility libraries for the processor already, then you should be able to write your code without the standard entry point function (main) and compile it to an object. You'll likely need to use a switch to tell the compiler that you want no entry point unless the one you have has an IDE that supports library creation as an automatic option. Most compilers, though not all, include a library tool that will take this compiled object code and create a standard library that the compiler and linker understand. The documentation for the librarian utility is usually included with it. Don't forget to write the header file separately and include it in the source for the library.
When you place your library in as a link option and include your new header file (using #include "MyHeaders.h" ) your project should access it all as needed.
Happy new library!
charlyvvvvv
2007-05-16 04:29:01 UTC
First of all, what is the processor, and what embedded operating environment are you using? That kind of enables someone to help you.
Second, writing your own optimized math library is a science in itself. There is so much open source software out there for importing a math library into your project... why try and re-invent the wheel?
Get a handle on using tools available before considering anything else. There are people out there that have devoted considerable parts of their careers writing all of these math API's and libraries. Take advantage of that and concentrate on your application.
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Search results for 'Writing compiled library .lib in C.....?' (Questions and Answers)