--- libecb/ecb.pod 2011/06/17 15:55:41 1.32 +++ libecb/ecb.pod 2014/01/09 05:16:12 1.55 @@ -56,6 +56,115 @@ is usually implemented as a macro. Specifically, a "bool" in this manual refers to any kind of boolean value, not a specific type. +=head2 TYPES / TYPE SUPPORT + +ecb.h makes sure that the following types are defined (in the expected way): + + int8_t uint8_t int16_t uint16_t + int32_t uint32_t int64_t uint64_t + intptr_t uintptr_t + +The macro C is defined to the size of a pointer on this +platform (currently C<4> or C<8>) and can be used in preprocessor +expressions. + +For C and C use C. + +=head2 LANGUAGE/COMPILER VERSIONS + +All the following symbols expand to an expression that can be tested in +preprocessor instructions as well as treated as a boolean (use C to +ensure it's either C<0> or C<1> if you need that). + +=over 4 + +=item ECB_C + +True if the implementation defines the C<__STDC__> macro to a true value, +while not claiming to be C++. + +=item ECB_C99 + +True if the implementation claims to be compliant to C99 (ISO/IEC +9899:1999) or any later version, while not claiming to be C++. + +Note that later versions (ECB_C11) remove core features again (for +example, variable length arrays). + +=item ECB_C11 + +True if the implementation claims to be compliant to C11 (ISO/IEC +9899:2011) or any later version, while not claiming to be C++. + +=item ECB_CPP + +True if the implementation defines the C<__cplusplus__> macro to a true +value, which is typically true for C++ compilers. + +=item ECB_CPP11 + +True if the implementation claims to be compliant to ISO/IEC 14882:2011 +(C++11) or any later version. + +=item ECB_GCC_VERSION(major,minor) + +Expands to a true value (suitable for testing in by the preprocessor) +if the compiler used is GNU C and the version is the given version, or +higher. + +This macro tries to return false on compilers that claim to be GCC +compatible but aren't. + +=item ECB_EXTERN_C + +Expands to C in C++, and a simple C in C. + +This can be used to declare a single external C function: + + ECB_EXTERN_C int printf (const char *format, ...); + +=item ECB_EXTERN_C_BEG / ECB_EXTERN_C_END + +These two macros can be used to wrap multiple C definitions - +they expand to nothing in C. + +They are most useful in header files: + + ECB_EXTERN_C_BEG + + int mycfun1 (int x); + int mycfun2 (int x); + + ECB_EXTERN_C_END + +=item ECB_STDFP + +If this evaluates to a true value (suitable for testing in by the +preprocessor), then C and C use IEEE 754 single/binary32 +and double/binary64 representations internally I the endianness of +both types match the endianness of C and C. + +This means you can just copy the bits of a C (or C) to an +C (or C) and get the raw IEEE 754 bit representation +without having to think about format or endianness. + +This is true for basically all modern platforms, although F might +not be able to deduce this correctly everywhere and might err on the safe +side. + +=item ECB_AMD64, ECB_AMD64_X32 + +These two macros are defined to C<1> on the x86_64/amd64 ABI and the X32 +ABI, respectively, and undefined elsewhere. + +The designers of the new X32 ABI for some inexplicable reason decided to +make it look exactly like amd64, even though it's completely incompatible +to that ABI, breaking about every piece of software that assumed that +C<__x86_64> stands for, well, the x86-64 ABI, making these macros +necessary. + +=back + =head2 GCC ATTRIBUTES A major part of libecb deals with GCC attributes. These are additional @@ -140,6 +249,27 @@ In this case, the compiler would probably be smart enough to deduce it on its own, so this is mainly useful for declarations. +=item ecb_restrict + +Expands to the C keyword or equivalent on compilers that support +them, and to nothing on others. Must be specified on a pointer type or +an array index to indicate that the memory doesn't alias with any other +restricted pointer in the same scope. + +Example: multiply a vector, and allow the compiler to parallelise the +loop, because it knows it doesn't overwrite input values. + + void + multiply (float *ecb_restrict src, + float *ecb_restrict dst, + int len, float factor) + { + int i; + + for (i = 0; i < len; ++i) + dst [i] = src [i] * factor; + } + =item ecb_const Declares that the function only depends on the values of its arguments, @@ -209,7 +339,7 @@ =item ecb_artificial Declares the function as "artificial", in this case meaning that this -function is not really mean to be a function, but more like an accessor +function is not really meant to be a function, but more like an accessor - many methods in C++ classes are mere accessor functions, and having a crash reported in such a method, or single-stepping through them, is not usually so helpful, especially when it's inlined to just a few instructions. @@ -398,7 +528,7 @@ =back -=head2 BIT FIDDLING / BITSTUFFS +=head2 BIT FIDDLING / BIT WIZARDRY =over 4 @@ -414,36 +544,159 @@ =item int ecb_ctz32 (uint32_t x) +=item int ecb_ctz64 (uint64_t x) + Returns the index of the least significant bit set in C (or equivalently the number of bits set to 0 before the least significant bit -set), starting from 0. If C is 0 the result is undefined. For example: +set), starting from 0. If C is 0 the result is undefined. + +For smaller types than C you can safely use C. + +For example: ecb_ctz32 (3) = 0 ecb_ctz32 (6) = 1 +=item bool ecb_is_pot32 (uint32_t x) + +=item bool ecb_is_pot64 (uint32_t x) + +Return true iff C is a power of two or C. + +For smaller types then C you can safely use C. + +=item int ecb_ld32 (uint32_t x) + +=item int ecb_ld64 (uint64_t x) + +Returns the index of the most significant bit set in C, or the number +of digits the number requires in binary (so that C<< 2**ld <= x < +2**(ld+1) >>). If C is 0 the result is undefined. A common use case is +to compute the integer binary logarithm, i.e. C, for +example to see how many bits a certain number requires to be encoded. + +This function is similar to the "count leading zero bits" function, except +that that one returns how many zero bits are "in front" of the number (in +the given data type), while C returns how many bits the number +itself requires. + +For smaller types than C you can safely use C. + =item int ecb_popcount32 (uint32_t x) -Returns the number of bits set to 1 in C. For example: +=item int ecb_popcount64 (uint64_t x) + +Returns the number of bits set to 1 in C. + +For smaller types than C you can safely use C. + +For example: ecb_popcount32 (7) = 3 ecb_popcount32 (255) = 8 +=item uint8_t ecb_bitrev8 (uint8_t x) + +=item uint16_t ecb_bitrev16 (uint16_t x) + +=item uint32_t ecb_bitrev32 (uint32_t x) + +Reverses the bits in x, i.e. the MSB becomes the LSB, MSB-1 becomes LSB+1 +and so on. + +Example: + + ecb_bitrev8 (0xa7) = 0xea + ecb_bitrev32 (0xffcc4411) = 0x882233ff + =item uint32_t ecb_bswap16 (uint32_t x) =item uint32_t ecb_bswap32 (uint32_t x) -These two functions return the value of the 16-bit (32-bit) value C -after reversing the order of bytes (0x11223344 becomes 0x44332211). +=item uint64_t ecb_bswap64 (uint64_t x) -=item uint32_t ecb_rotr32 (uint32_t x, unsigned int count) +These functions return the value of the 16-bit (32-bit, 64-bit) value +C after reversing the order of bytes (0x11223344 becomes 0x44332211 in +C). + +=item uint8_t ecb_rotl8 (uint8_t x, unsigned int count) + +=item uint16_t ecb_rotl16 (uint16_t x, unsigned int count) =item uint32_t ecb_rotl32 (uint32_t x, unsigned int count) -These two functions return the value of C after rotating all the bits -by C positions to the right or left respectively. +=item uint64_t ecb_rotl64 (uint64_t x, unsigned int count) + +=item uint8_t ecb_rotr8 (uint8_t x, unsigned int count) + +=item uint16_t ecb_rotr16 (uint16_t x, unsigned int count) + +=item uint32_t ecb_rotr32 (uint32_t x, unsigned int count) + +=item uint64_t ecb_rotr64 (uint64_t x, unsigned int count) + +These two families of functions return the value of C after rotating +all the bits by C positions to the right (C) or left +(C). Current GCC versions understand these functions and usually compile them -to "optimal" code (e.g. a single C on x86). +to "optimal" code (e.g. a single C or a combination of C on +x86). + +=back + +=head2 FLOATING POINT FIDDLING + +=over 4 + +=item uint32_t ecb_float_to_binary32 (float x) [-UECB_NO_LIBM] + +=item uint64_t ecb_double_to_binary64 (double x) [-UECB_NO_LIBM] + +These functions each take an argument in the native C or C +type and return the IEEE 754 bit representation of it. + +The bit representation is just as IEEE 754 defines it, i.e. the sign bit +will be the most significant bit, followed by exponent and mantissa. + +This function should work even when the native floating point format isn't +IEEE compliant, of course at a speed and code size penalty, and of course +also within reasonable limits (it tries to convert NaNs, infinities and +denormals, but will likely convert negative zero to positive zero). + +On all modern platforms (where C is true), the compiler should +be able to optimise away this function completely. + +These functions can be helpful when serialising floats to the network - you +can serialise the return value like a normal uint32_t/uint64_t. + +Another use for these functions is to manipulate floating point values +directly. + +Silly example: toggle the sign bit of a float. + + /* On gcc-4.7 on amd64, */ + /* this results in a single add instruction to toggle the bit, and 4 extra */ + /* instructions to move the float value to an integer register and back. */ + + x = ecb_binary32_to_float (ecb_float_to_binary32 (x) ^ 0x80000000U) + +=item float ecb_binary32_to_float (uint32_t x) [-UECB_NO_LIBM] + +=item double ecb_binary32_to_double (uint64_t x) [-UECB_NO_LIBM] + +The reverse operation of the previos function - takes the bit representation +of an IEEE binary32 or binary64 number and converts it to the native C +or C format. + +This function should work even when the native floating point format isn't +IEEE compliant, of course at a speed and code size penalty, and of course +also within reasonable limits (it tries to convert normals and denormals, +and might be lucky for infinities, and with extraordinary luck, also for +negative zero). + +On all modern platforms (where C is true), the compiler should +be able to optimise away this function completely. =back @@ -477,6 +730,15 @@ for (m = -100; m <= 100; ++m) int elem = myarray [ecb_mod (m, ecb_array_length (myarray))]; +=item x = ecb_div_rd (val, div) + +=item x = ecb_div_ru (val, div) + +Returns C divided by C
rounded down or up, respectively. +C and C
must have integer types and C
must be strictly +positive. Note that these functions are implemented with macros in C +and with function templates in C++. + =back =head2 UTILITY @@ -495,4 +757,34 @@ =back +=head2 SYMBOLS GOVERNING COMPILATION OF ECB.H ITSELF + +These symbols need to be defined before including F the first time. + +=over 4 + +=item ECB_NO_THREADS + +If F is never used from multiple threads, then this symbol can +be defined, in which case memory fences (and similar constructs) are +completely removed, leading to more efficient code and fewer dependencies. + +Setting this symbol to a true value implies C. + +=item ECB_NO_SMP + +The weaker version of C - if F is used from +multiple threads, but never concurrently (e.g. if the system the program +runs on has only a single CPU with a single core, no hyperthreading and so +on), then this symbol can be defined, leading to more efficient code and +fewer dependencies. + +=item ECB_NO_LIBM + +When defined to C<1>, do not export any functions that might introduce +dependencies on the math library (usually called F<-lm>) - these are +marked with [-UECB_NO_LIBM]. + +=back +