I need to write my own optimised math library for embedded processor. How can I do that?
Writing compiled library .lib in C.....?
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?
For starters, check out:
http://www.thefreecountry.com/sourcecode...
and
http://opensource.hp.com/opensource_proj...
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.
Reply: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!
Reply: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%26lt;iostream%26gt;
but, the syntax is quotes like
#include%26lt;iostream%26gt;
#include"math.h"
NOTE:
#include%26lt;math.h%26gt;, 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
big_int big_int::operator*(big_int);// calculates multiplication
big_int big_int::fact(const int); //calculates factoral
big_int%26amp; big_int::operator=(big_int);
void big_int::set_array_size(int);
void big_int::output();
int big_int::size();
int num_array[500];
private:
int array_size;
friend void set_array_size(int);
};
#endif
the #ifndef _BIG_INT_H_ and #define _BIG_INT_H_, are used by the compiler
then you need a source file, my source file for this was:
/*
big_int.cpp defination file
A class that can compute mathmatical opperations of large integers by using arrays.
*/
#include"big_int.h"
#include%26lt;iostream%26gt;
big_int::big_int(){
};
big_int%26amp; big_int::operator=(big_int rhs){
for(int i =0; i %26lt; rhs.size(); ++i){
num_array[i] = rhs.num_array[i];
};
set_array_size(rhs.size());
return *this;
};
big_int big_int::operator+(big_int rhs){
// variables that are going to have the reversed data
big_int reversed_lhs , reversed_rhs;
int rhs_index = rhs.size() - 1; // value of the highest index in rhs array
// reverse the rhs array
for(int i = 0; i %26lt; rhs.size(); ++i){
reversed_rhs.num_array[i] = rhs.num_array[rhs_index];
--rhs_index;
};
// set the value of the size of the reversed rhs array
reversed_rhs.set_array_size(rhs.size());
int lhs_index = size() - 1; // value of the highest index in the lhs array
// reverse the lhs array and store it in reversed_lhs
for(int i = 0; i %26lt; size(); ++i){
reversed_lhs.num_array[i] = num_array[lhs_index];
--lhs_index;
};
// et the value of the size of the reversed lhs array
reversed_lhs.set_array_size(size());
// variable to store the reversed result
big_int reversed_result;
int max_size;
// calculate the biggest array and store it in max_size
if(reversed_lhs.size() %26gt; reversed_rhs.size()){
max_size = reversed_lhs.size();
}else{
max_size = reversed_rhs.size();
};
// variables needed for addition
int index = 0;
int carry = 0;
while(index %26lt; max_size+1){
// store the values of the added indexs in temp
int temp = reversed_rhs.num_array[index] + reversed_lhs.num_array[index] + carry;
// assures that there is only a single digit in the array
if(temp %26gt; 9){
reversed_result.num_array[index] = temp % 10; // sets value of big int if it is 2 digits
carry = (temp / 10); //sets carry if the temp is greater than 10
}else{
reversed_result.num_array[index] = temp;//sets single digit value
carry = 0;// no carry
};
++index;//incriment index
};
reversed_result.set_array_size(index); //sets size of the big_int
// put the array in the correct order and filter out possible 0's that were added on the front and store in result
big_int result;
bool check = true;
int reversed_result_index = reversed_result.size() - 1;
int result_index = 0;
int times_to_execute = reversed_result.size();
int count = 0;
while(count %26lt; times_to_execute){
// makes sure that there was not an extra 0 added on front, and if so runs loop one less time and does not copy 0
if(reversed_result.num_array[reversed_re... - 1] == 0 %26amp;%26amp; check == true){
-- reversed_result_index;
check = false; // prevents output of leading 0's
times_to_execute = times_to_execute -1;
};
// copies the arrays
result.num_array[result_index] = reversed_result.num_array[reversed_resul...
++result_index;
--reversed_result_index;
++count;
};
// sets the size of result
result.set_array_size(count);
//returns a big_int with the addition
return result;
};
//operator overloading for multiplication
// uses place values and adds up a series of numbers with place holding values
// to reach result, does not work, but does output series of numbers whoes sum
//is the correct result, and error is not in addition
big_int big_int::operator*(big_int rhs){
// variables that are going to have the reversed data
big_int biggest , smallest;
// if statement to figure out the array with the most digits, then revereses arrays to the correct reveresed greatest or reversed least
if(rhs.size() %26gt; size()){
// reverse the rhs array
for(int i = 0; i %26lt; rhs.size(); ++i){
biggest.num_array[i] = rhs.num_array[i];
};
// set the value of the size of the reversed rhs array
biggest.set_array_size(rhs.size());
// reverse the lhs array and store it in reversed_lhs
for(int i = 0; i %26lt; size(); ++i){
smallest.num_array[i] = num_array[i];
};
//s et the value of the size of the reversed lhs array
smallest.set_array_size(size());
}else{
// reverse the rhs array
for(int i = 0; i %26lt; rhs.size(); ++i){
smallest.num_array[i] = rhs.num_array[i];
};
// set the value of the size of the reversed rhs array
smallest.set_array_size(rhs.size());
int lhs_index = size() - 1; // value of the highest index in the lhs array
// reverse the lhs array and store it in reversed_lhs
for(int i = 0; i %26lt; size(); ++i){
biggest.num_array[i] = num_array[i];
};
// set the value of the size of the reversed lhs array
biggest.set_array_size(size());
};
// variables that will be used in multiplication
int smallest_index = smallest.size() - 1; //index to start on right side
int carry = 0;
big_int add_result;
int add_result_size = 0;
add_result.set_array_size(1);//add_resul is initalized with one digit that is a zero;
int add_result_index; //index of result
big_int result;//stores result
result.set_array_size(1);//sets size of result, only needs to be set once
// because addition process returns a big int
// with the size already set
// loop that multiplies each digit in the big int arrays
while(smallest_index %26gt;= 0){
int biggest_index = biggest.size() - 1; // index to start on right side, resets
while(biggest_index %26gt;= 0){
// set the front of the array to 0
add_result.num_array[0] = 0;
int add_size = 0;
int temp;
// temp value that stores the mutliplication
temp = (smallest.num_array[smallest_index]* biggest.num_array[biggest_index]) + carry;
// if temp is greater than 9 it sets the carry value and the number to be added
if(temp %26gt; 10){
add_result.num_array[0] = (temp%10);
carry = temp / 10;
}else{
// sets number to be added and carry value if temp is a single digit
add_result.num_array[0] = temp;
carry = 0;
};
//calculates the place (number of 0's) to put after the value to be added
add_size= ((biggest.size() - biggest_index) + (smallest.size() - smallest_index)-1);
//sets the size of the add array
add_result.set_array_size(add_size);
// 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 %26lt;%26lt; '\n';
std::cout %26lt;%26lt; "ADD ";
add_result.output();
result = (result + add_result);
std::cout %26lt;%26lt; '\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 %26lt; 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 %26lt; size(); ++i){
std::cout %26lt;%26lt; num_array[i];
if(0 == (i % 50) %26amp;%26amp; i != 0){
std::cout %26lt;%26lt; '\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!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment