5 ms·
I would do it like this: #if SIZEOF_SIZE_T == 8 typedef int64_t ssize_t #elif SIZEOF_SIZE_T == 4 typedef int32_t ssize_t #elif SIZEOF_SIZE_T == 2 //
by kazinator 4y ago
I would do it like this:
#if SIZEOF_SIZE_T == 8
typedef int64_t ssize_t
#elif SIZEOF_SIZE_T == 4
typedef int32_t ssize_t
#elif SIZEOF_SIZE_T == 2 // LOL
typedef int16_t ssize_t
#else
#error port me!
#endif
SIZEOF_SIZE_T can be obtained using a script which compiles a test program without executing it.
Over they years I used more than one approach, settling on this one:
https://www.kylheku.com/cgit/txr/tree/configure?id=1f902ca63eba9071da5d1da2e861fe49028d32b4#n1487 https://www.kylheku.com/cgit/txr/tree/configure?id=1f902ca63...
Here, in the test program, a DEC macro has been defined which given a constant expression, produces two decimal digits as the initializer for a two-character array. For instance:
DEC(42) -> { '4', '2' } // not exactly: character constants are not used
With this trick we can use DEC(sizeof (size_t)) to get a value like { ' ', '8' } into the portion of some character data, which we can prefix with an identifying string we can look for, like:
SIZEOF_SIZE_T= 8
That we can basically grep out. In my configure script, this data is extracted, spaces are removed from it, and it's evaluated directly as shell assignments, so then the values are available in shell variables.
- skissane 4y agoRather than a bunch of #if blocks, one could instead do: #define SIZE_T_BITS 64 #define PASTE3(a,b,c) a##b##c #define TYPEDEF_UINT(bits,name) typedef PASTE3(uint,bits,_t) name #define TYPEDEF_INT(bits,name) typedef PASTE3(int,bits,_t) name TYPEDEF_UINT(SIZE_T_BITS,size_t); TYPEDEF_INT(SIZE_T_BITS,ssize_t); Should work for any value of SIZE_T_BITS, even something weird like 128 or 36 (PDP-10 port?), so long as you already have [u]intN_t defined. It does require SIZE_T_BITS to be in bits rather than bytes, as your SIZEOF_SIZE_T is. But surely if your script can calculate SIZEOF_SIZE_T, it can multiply the answer by 8? (Or by CHAR_BIT, if we want to support weird platforms without 8-bit bytes – Lars Brinkhoff's PDP-10 port of GCC 3.2 has CHAR_BIT==9.)
- kazinator 4y agoThe main thing in my post is how we get that input value, in your case SIZE_T_BITS, at the preprocessing level where we can then use it to select alternative pieces of code. After that it's cosmetic. The simple, dumb #if blocks have the virtue is that they are not hostile to simple tooling, like Exuberant Ctags, Cscope, and whatnot. When we ask the editor to jump to the definition of ssize_t, it knows the three possible places where it is defined and serves them up. This alternative is possible: #define PASTE3(a,b,c) a##b##c #define UINT_TYPE(bits) PASTE3(uint,bits,_t) #define INT_TYPE(bits,name) PASTE3(int,bits,_t) typedef UINT_TYPE(SIZE_T_BITS) usize_t; typedef INT_TYPE(SIZE_T_BITS) ssize_t; I think in this form, there is a good chance the tools will grok the typedefs and index them, because we have not disguised the basic phrase structure.
- rkangel 4y ago> #elif SIZEOF_SIZE_T == 2 // LOL Fun fact - for a while, this was true for the third most popular processor architecture in the world, one that most people haven't heard of. CSR (the bluetooth chip maker) was originally a spin-out from Cambridge Consultants, and their chips were for years based on a Cambridge Consultants processer design called XAP. As CSR had made billions of devices, XAP was likely right up there in terms of number of processors made. XAP was unusual in a couple of ways (at least unusual compared to the fairly universal modern design). Here's a fun set of statements that all evaluate to true: sizeof(uint8_t) == 1 // So far so normal, this is required by the C standard sizeof(uint16_t) == 1 // Surprise! BTW, uint16_t is defined as "unsigned int" sizeof(void *) == 1 // Weird sizeof(void (*)(void)) == 2 // Yes, function pointers are not the same size as data pointers This is all completely legal according to the C standard. Fun fact, the size of a byte isn't "8 bits", it's technically "the minimum addressable size on your platform"[1]. On every modern processor design I know of, that is an 8 bit quantity, but the XAP only allowed you to address 16 bits at a time. Presumably this was for efficiency - the XAP was very low gate count for a fairly capable processor (at the time). 16 bits was also the size that the instruction set operated on, the "natural size for calculation" as described in the C spec, so that's the size of an int. This means that sizeof(int) == 1, which is a surprise to most people. It also means that you can't do tricks like "cast a uint16_t to an array of 2 uint8_ts" (which is non-portable also for endian reasons anyway). The other unusual aspect is that it's a Harvard architecture, which separates the address space for code and data. Code size being large, this meant function pointers were 24 bit, whereas data pointers were 16. Code that assumed it could cast a function pointer to "void *" to put it in callback context could get a nasty surprise (usually a subtle one, because it only broke if the function was late in the address space). This was what I worked on early in my career, it was a great education in writing C that was actually portable. If it ran on PC and XAP, then it would likely run on anything! [1] This is why picky/anal engineers will refer to "octets" in a network protocol rather than bytes. Bytes don't have a meaning except in the context of execution, which obviously doesn't apply to a network protocol.
- vlovich123 4y agoWhat’s also interesting was that the processor was actually a 32 bit processor iirc (at least the later ones). The 16 bit variant is purely the VM mode they run “application” code on which emulates the environment of an old CPU (my guess anyway - not sure why the VM was limited to 16bits). The Kalimba was equally weird with sizeof(int) = 1 being 24 bit and sizeof (long) was 48bit. I ported an ECDSA library to it for Pixel Buds 1 because the XAP was too slow. It was really really hard to get that to work properly and writing a simulated environment to make sure I emulated the math correctly with masking and whatnot. It was such an annoying architecture to work with that all the senior SW leads for Pixel Buds fought extra hard to avoid Qualcomm’s solution. Their evolution for the next set of chips to compete with Apple’s W1 saw them go down a weird path where they doubled down on getting rid of GCC and instead using their home grown compiler (none of their shit ran natively on Linux not Mac) and similarly weird architecture decisions (forget all the details now). Our job was made easier in that they couldn’t actually deliver a W1 competitor. Their best was exposing each bud as a separate device which would have been a terrible experience and they could only improve that experience for Android if we mainlined their weird decisions into Android (sorry - no). By comparison BESTech delivered a proper competitor design to the W1 (transparent sniffing and hand off) and their SW architecture was totally sane (ARM chip + gcc for sure + FreeRTOS if I recall correctly). Much better partners than Qualcomm.