Does type-casting each element of an array takes less space than copying the array into a new one?

  Kiến thức lập trình

I am currently writing a program where given two input matrices (int8_t and float respectively) I compute the multiplication of the two.

For memory reasons, I do not want the entire int8 matrix to be converted to a floating type (or any type that occupies more than 8bits in memory). I believe that in C, when multiplying int with float, there is an implicit type casting that is done to convert int to float, in order to do the operation.

My question now is, if I cast my int8 input as a float, then do the computation, what is actually happening in the memory ?
Is it over-writing other useless memory spaces when the cast is done, or is it taking additional place as if I created a float array in which I copied my data ?

for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
        float sum = 0;
        for (int l = 0; l < k; ++l) {
            sum += (float) input_a[i*k + l] * input_b[l*m + j];
        }
        output[i*m + j] = sum;
    }
} 

I am not very knowledgeable about the way C works, any kind of help is greatly appreciated.

New contributor

Elyon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT