samedi 9 mai 2020

Optimization: making a struct that uses different data types

Newbie C programmer here. I'm working on a math library in C and implementing some linear algebra stuff at the moment. The struct Matrix is defined as follows:

typedef struct {
    double** mat;
    int nRows;
    int nCols;
} Matrix ;

I'm trying to optimize this part for an atmel ATMega16A deployment. This has around 1kB of SRAM and with a double array, I would only be able to store an 8x8 matrix at max in memory (or around 10 3x3 matrices with some space left over for other calculations). I realized that for most use cases, I don't need the double precision and can get by with using float.

What is the right way of implementing a similar struct with a float array that occupies less space in memory?

In an object-oriented environment, the right way of doing this would have been to create an abstract superclass Matrix and define various implementations (eg MatrixDouble and MatrixFloat). The same set of methods would then be able to do operations on both implementations. In this library, all the methods are currently returning double or Matrix* and accepting Matrix* arguments, and I don't want to do a complete rewrite of these methods and implement two methods for each procedure eg double determinantDouble( MatrixDouble* mat ) and float determinantFloat( MatrixFloat* mat ).

Aucun commentaire:

Enregistrer un commentaire