This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: ARM-only [Patch] Allow 4 byte enum size (-fno-short-enums) | Remove hard coded short enum flag from the build scripts?


Hello Corinna and all others,

On Tuesday 30 of August 2016 20:27:57 Corinna Vinschen wrote:
> Given that, per Richard, the enum usage in the affected files is
> internal a change here is apparently not ABI-relevant.  From my
> rather neutral stance it looks like a pretty safe change so far...

When I look to

  newlib/libc/stdlib/mbtowc_r.c

then it seems that tables using enums as base type can have considerable
impact on size for tiny/nano targets.

typedef enum { ESCAPE, DOLLAR, BRACKET, AT, B, J,
               NUL, JIS_CHAR, OTHER, JIS_C_NUM } JIS_CHAR_TYPE;
typedef enum { ASCII, JIS, A_ESC, A_ESC_DL, JIS_1, J_ESC, J_ESC_BR,
               INV, JIS_S_NUM } JIS_STATE;
typedef enum { COPY_A, COPY_J1, COPY_J2, MAKE_A, NOOP, EMPTY, ERROR } JIS_ACTION;

But I am not sure if using -fshort-enums is the right way to solve that.
If we can agree that enums in C are mainly usesfull for unique values
in the set assignment but in the fact there is no check that enum
values are assigned to the variables of same type, then much better
solution is to count cases by hand and decide about type.

In this particular case, I expect that next is most space conserving

enum { ESCAPE, DOLLAR, BRACKET, AT, B, J, NUL, JIS_CHAR, OTHER, JIS_C_NUM };
typedef int_least8_t JIS_CHAR_TYPE;

enum { ASCII, JIS, A_ESC, A_ESC_DL, JIS_1, J_ESC, J_ESC_BR,
     INV, JIS_S_NUM };
typedef int_least8_t JIS_STATE;

enum { COPY_A, COPY_J1, COPY_J2, MAKE_A, NOOP, EMPTY, ERROR };
typedef int_least8_t JIS_ACTION;

What makes me worry for waste of memory in this particular file is
that tables go to data section

#ifndef  __CYGWIN__
static JIS_STATE JIS_state_table[JIS_S_NUM][JIS_C_NUM] = {

I think, that they should be
static const JIS_STATE JIS_state_table[JIS_S_NUM][JIS_C_NUM] = {

Same for

static mbtowc_p __cp_xxx_mbtowc[26] = {

For newlib/libc/stdio/vfprintf.c, it looks like enums are used only as
variables inside functions. The size is not big deal for local variables.
If registers halves, quarters etc. can used for multiple variables
on some targets then space for enum types can be defined as uint_fast8_t .
But generally difference is minimal.

Best wishes,

                Pavel




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]