--- cvsroot/libecb/ecb.pod 2012/05/29 14:35:43 1.45 +++ cvsroot/libecb/ecb.pod 2015/02/18 20:29:27 1.62 @@ -62,15 +62,17 @@ int8_t uint8_t int16_t uint16_t int32_t uint32_t int64_t uint64_t - intptr_t uintptr_t ptrdiff_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. -=head2 LANGUAGE/COMPILER VERSIONS +For C and C use C. -All the following symbols expand to an expressionb that cna be tested in +=head2 LANGUAGE/ENVIRONMENT/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). @@ -79,72 +81,128 @@ =item ECB_C True if the implementation defines the C<__STDC__> macro to a true value, -which is typically true for both C and C++ compilers. +while not claiming to be C++. =item ECB_C99 -True if the implementation claims to be C99 compliant. +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 C11 compliant. +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_CPP98 - -True if the implementation claims to be compliant to ISO/IEC 14882:1998 -(the first C++ ISO standard) or any later vwersion. Typically true for all -C++ compilers. - =item ECB_CPP11 True if the implementation claims to be compliant to ISO/IEC 14882:2011 -(C++11) or any later vwersion. +(C++11) or any later version. -=item ECB_GCC_VERSION(major,minor) +=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 givne version, or +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. -=back +=item ECB_EXTERN_C -=head2 GCC ATTRIBUTES +Expands to C in C++, and a simple C in C. -A major part of libecb deals with GCC attributes. These are additional -attributes that you can assign to functions, variables and sometimes even -types - much like C or C in C. - -While GCC allows declarations to show up in many surprising places, -but not in many expected places, the safest way is to put attribute -declarations before the whole declaration: +This can be used to declare a single external C function: - ecb_const int mysqrt (int a); - ecb_unused int i; + 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 -For variables, it is often nicer to put the attribute after the name, and -avoid multiple declarations using commas: +These two macros are defined to C<1> on the x86_64/amd64 ABI and the X32 +ABI, respectively, and undefined elsewhere. - int i ecb_unused; +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 MACRO TRICKERY =over 4 -=item ecb_attribute ((attrs...)) +=item ECB_CONCAT (a, b) + +Expands any macros in C and C, then concatenates the result to form +a single token. This is mainly useful to form identifiers from components, +e.g.: + + #define S1 str + #define S2 cpy + + ECB_CONCAT (S1, S2)(dst, src); // == strcpy (dst, src); + +=item ECB_STRINGIFY (arg) + +Expands any macros in C and returns the stringified version of +it. This is mainly useful to get the contents of a macro in string form, +e.g.: + + #define SQL_LIMIT 100 + sql_exec ("select * from table limit " ECB_STRINGIFY (SQL_LIMIT)); -A simple wrapper that expands to C<__attribute__((attrs))> on GCC, and to -nothing on other compilers, so the effect is that only GCC sees these. +=back -Example: use the C attribute on a function. +=head2 ATTRIBUTES - ecb_attribute((__deprecated__)) void - do_not_use_me_anymore (void); +A major part of libecb deals with additional attributes that can be +assigned to functions, variables and sometimes even types - much like +C or C in C. They are implemented using either GCC +attributes or other compiler/language specific features. Attributes +declarations must be put before the whole declaration: + + ecb_const int mysqrt (int a); + ecb_unused int i; + +=over 4 =item ecb_unused @@ -153,7 +211,7 @@ declare a variable but do not always use it: { - int var ecb_unused; + ecb_unused int var; #ifdef SOMECONDITION var = ...; @@ -163,12 +221,22 @@ #endif } +=item ecb_deprecated + +Similar to C, but marks a function, variable or type as +deprecated. This makes some compilers warn when the type is used. + +=item ecb_deprecated_message (message) + +Same as C, but if possible, supply a diagnostic that is +used instead of a generic depreciation message when the object is being +used. + =item ecb_inline -This is not actually an attribute, but you use it like one. It expands -either to C or to just C, if inline isn't -supported. It should be used to declare functions that should be inlined, -for code size or speed reasons. +Expands either to C or to just C, if inline +isn't supported. It should be used to declare functions that should be +inlined, for code size or speed reasons. Example: inline this function, it surely will reduce codesize. @@ -200,6 +268,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 (ecb_restrict float *src, + ecb_restrict float *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, @@ -269,7 +358,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. @@ -299,7 +388,7 @@ =over 4 -=item bool ecb_is_constant(expr) +=item bool ecb_is_constant (expr) Returns true iff the expression can be deduced to be a compile-time constant, and false otherwise. @@ -327,7 +416,7 @@ : (n * (uint32_t)rndm16 ()) >> 16; } -=item bool ecb_expect (expr, value) +=item ecb_expect (expr, value) Evaluates C and returns it. In addition, it tells the compiler that the C evaluates to C a lot, which can be used for static @@ -384,7 +473,7 @@ real_reserve_method (size); /* presumably noinline */ } -=item bool ecb_assume (cond) +=item ecb_assume (cond) Try to tell the compiler that some condition is true, even if it's not obvious. @@ -414,13 +503,13 @@ completely, as it knows that C<< current + 1 > end >> is false and the call will never be executed. -=item bool ecb_unreachable () +=item ecb_unreachable () This function does nothing itself, except tell the compiler that it will never be executed. Apart from suppressing a warning in some cases, this function can be used to implement C or similar functions. -=item bool ecb_prefetch (addr, rw, locality) +=item ecb_prefetch (addr, rw, locality) Tells the compiler to try to prefetch memory at the given Cess for either reading (C = 0) or writing (C = 1). A C of @@ -575,6 +664,77 @@ =back +=head2 FLOATING POINT FIDDLING + +=over 4 + +=item ECB_INFINITY + +Evaluates to positive infinity if supported by the platform, otherwise to +a truly huge number. + +=item ECB_NON + +Evaluates to a quiet NAN if supported by the platform, otherwise to +C. + +=item float ecb_ldexpf (float x, int exp) + +Same as C, but always available. + +=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_binary16_to_float (uint16_t x) [-UECB_NO_LIBM] + +=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 previous function - takes the bit +representation of an IEEE binary16, 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 + =head2 ARITHMETIC =over 4 @@ -638,7 +798,7 @@ =over 4 -=item ECB_NO_THRADS +=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 @@ -654,6 +814,12 @@ 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