0

I have big structure with few arrays. For simplicity let's consider structure like this:

typedef struct {
    uint32_t data_count;
    uint8_t data[512];
    uint32_t error_count;
    uint8_t errors[512];
} measurements_t;

How many microcontroller CPU cycles does it take to initialize ~1kB structure with zeros?

Does

I'm wondering if I should initialize whole structure with zeroes, or not (assign required data after declaration)?

Does the Cortex-M7 core have any assembly language instruction to fill memory with zeros? I'm not assembly guy, I can't find anything in this manual and I have no access to proper compiler at this moment to check assembly that compiler produces.

measurements_t measurement_results = {0};    // initialization with all zeroes

// or

measurements_t measurement_results;          // no initialization
measurement_results.data_count = 0;
measurement_results.error_count = 0;

In fact my struct is more complicated and has few levels and probably more data.

It will be easier to manage code later if I just initialize it with {0}, but if that is cycle-expensive on STM32F7 - I will have to create fast constructor that puts zeroes only where it is really needed for structures like this.

Kamil
  • 13,363
  • 24
  • 88
  • 183
  • How many and how often are you creating this? – stark May 25 '22 at 20:46
  • @stark I think up to 100 structures per second (when counting all types). But some structures are bigger, like 4kB (but these will be initialized about 10 times per second). Well... actually I could skip initialization of biggest structures... But I'm curious how much cycles it cost per 1kB. – Kamil May 25 '22 at 20:54
  • If `data_count` & `error_count` holds the size of actual data in respective data-variables, then zero initialisation it superfluous. – जलजनक May 25 '22 at 21:46

1 Answers1

0

From my experience the fastest way is to have long list of 64 bit store instructions one after another.

typedef struct {
    uint32_t data_count;
    uint8_t data[512];
    uint32_t error_count;
    uint8_t errors[512];
} measurements_t;


void zoo(measurements_t *m)
{
    uint64_t *x = (uint64_t *)m;
    #pragma GCC unroll sizeof(*m) / sizeof(*x)
    for(int i = 0; i < sizeof(*m) / sizeof(*x); i++)
        x[i] = 0;
}

https://godbolt.org/z/6Wv1dYxfP

to make it faster place code into ITCM memory and if possible structures into DTCM memory. It separates address and data busses.

0___________
  • 60,014
  • 4
  • 34
  • 74