Sam Skuce
Sam Skuce

Reputation: 1694

Where Is gcvt or gcvtf Defined in gcc Source Code?

I'm working on some old source code for an embedded system on an m68k target, and I'm seeing massive memory allocation requests sometimes when calling gcvtf to format a floating point number for display. I can probably work around this by writing my own substitute routine, but the nature of the error has me very curious, because it only occurs when the heap starts at or above a certain address, and it goes away if I hack the .ld linker script or remove any set of global variables (which are placed before the heap in my memory map) that add up to enough byte size so that the heap starts below the mysterious critical address.

So, I thought I'd look in the gcc source code for the compiler version I'm using (m68k-elf-gcc 3.3.2). I downloaded what appears to be the source for this version at http://gcc.petsads.us/releases/gcc-3.3.2/, but I can't find the definition for gcvt or gcvtf anywhere in there. When I search for it, grep only finds some documentation and .h references, but not the definition:

$ find | xargs grep gcvt
./gcc/doc/gcc.info:     C library functions `ecvt', `fcvt' and `gcvt'.  Given va
lid
./gcc/doc/trouble.texi:library functions @code{ecvt}, @code{fcvt} and @code{gcvt
}.  Given valid
./gcc/sys-protos.h:extern char *                 gcvt(double, int, char *);

So, where is this function actually defined in the source code? Or did I download the entirely wrong thing?

I don't want to change this project to use the most recent gcc, due to project stability and testing considerations, and like I said, I can work around this by writing my own formatting routine, but this behavior is very confusing to me, and it will grind my brain if I don't find out why it's acting so weird.

Upvotes: 3

Views: 2471

Answers (2)

Brooks Moses
Brooks Moses

Reputation: 9527

Wallyk is correct that this is defined in the C library rather than the compiler. However, the GNU C library is (nearly always) only used with Linux compilers and distributions. Your compiler, being a "bare-metal" compiler, almost certainly uses the Newlib C library instead.

The main website for Newlib is here: http://sourceware.org/newlib/, and this particular function is defined in the newlib/libc/stdlib/efgcvt.c file. The sources have been quite stable for a long time, so (unless this is a result of a bug) chances are pretty good that the current sources are not too different from what your compiler is using.

As with the GNU C source, I don't see anything in there that would obviously cause this weirdness that you're seeing, but it's all eventually a bunch of wrappers around the basic sprintf routines.

Upvotes: 3

wallyk
wallyk

Reputation: 57774

It is in the GNU C library as glibc/misc/efgcvt.c. To save you some trouble, the code for the function is:

char *
__APPEND (FUNC_PREFIX, gcvt) (value, ndigit, buf)
     FLOAT_TYPE value;
     int ndigit;
     char *buf;
{
  sprintf (buf, "%.*" FLOAT_FMT_FLAG "g", MIN (ndigit, NDIGIT_MAX), value);
  return buf;
}

The directions for obtain glibc are here.

Upvotes: 2

Related Questions