--- libecb/ecb.pod 2015/01/26 12:04:56 1.59 +++ libecb/ecb.pod 2021/06/21 21:26:48 1.87 @@ -12,14 +12,14 @@ http://software.schmorp.de/pkg/libecb -It mainly provides a number of wrappers around GCC built-ins, together -with replacement functions for other compilers. In addition to this, -it provides a number of other lowlevel C utilities, such as endianness -detection, byte swapping or bit rotations. - -Or in other words, things that should be built into any standard C system, -but aren't, implemented as efficient as possible with GCC, and still -correct with other compilers. +It mainly provides a number of wrappers around many compiler built-ins, +together with replacement functions for other compilers. In addition +to this, it provides a number of other lowlevel C utilities, such as +endianness detection, byte swapping or bit rotations. + +Or in other words, things that should be built into any standard C +system, but aren't, implemented as efficient as possible with GCC (clang, +msvc...), and still correct with other compilers. More might come. @@ -60,17 +60,23 @@ 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 + int8_t uint8_ + int16_t uint16_t + int32_t uint32_ + int64_t uint64_t + int_fast8_t uint_fast8_t + int_fast16_t uint_fast16_t + int_fast32_t uint_fast32_t + int_fast64_t uint_fast64_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. +For C and C use C/C. -=head2 LANGUAGE/COMPILER VERSIONS +=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 @@ -81,7 +87,7 @@ =item ECB_C True if the implementation defines the C<__STDC__> macro to a true value, -while not claiming to be C++. +while not claiming to be C++, i..e C, but not C++. =item ECB_C99 @@ -91,26 +97,34 @@ Note that later versions (ECB_C11) remove core features again (for example, variable length arrays). -=item ECB_C11 +=item ECB_C11, ECB_C17 -True if the implementation claims to be compliant to C11 (ISO/IEC -9899:2011) or any later version, while not claiming to be C++. +True if the implementation claims to be compliant to C11/C17 (ISO/IEC +9899:2011, :20187) 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 +=item ECB_CPP11, ECB_CPP14, ECB_CPP17 -True if the implementation claims to be compliant to ISO/IEC 14882:2011 -(C++11) or any later version. +True if the implementation claims to be compliant to C++11/C++14/C++17 +(ISO/IEC 14882:2011, :2014, :2017) or any later version. + +Note that many C++20 features will likely have their own feature test +macros (see e.g. L). + +=item ECB_OPTIMIZE_SIZE + +Is C<1> when the compiler optimizes for size, C<0> otherwise. This symbol +can also be defined before including F, in which case it will be +unchanged. =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. +Expands to a true value (suitable for testing 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. @@ -139,7 +153,7 @@ =item ECB_STDFP -If this evaluates to a true value (suitable for testing in by the +If this evaluates to a true value (suitable for testing 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. @@ -152,6 +166,14 @@ not be able to deduce this correctly everywhere and might err on the safe side. +=item ECB_64BIT_NATIVE + +Evaluates to a true value (suitable for both preprocessor and C code +testing) if 64 bit integer types on this architecture are evaluated +"natively", that is, with similar speeds as 32 bit integerss. While 64 bit +integer support is very common (and in fatc required by libecb), 32 bit +cpus have to emulate operations on them, so you might want to avoid them. + =item ECB_AMD64, ECB_AMD64_X32 These two macros are defined to C<1> on the x86_64/amd64 ABI and the X32 @@ -165,45 +187,69 @@ =back -=head2 GCC ATTRIBUTES +=head2 MACRO TRICKERY -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: +=over 4 - ecb_const int mysqrt (int a); - ecb_unused int i; +=item ECB_CONCAT (a, b) -For variables, it is often nicer to put the attribute after the name, and -avoid multiple declarations using commas: +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.: - int i ecb_unused; + #define S1 str + #define S2 cpy -=over 4 + ECB_CONCAT (S1, S2)(dst, src); // == strcpy (dst, src); + +=item ECB_STRINGIFY (arg) -=item ecb_attribute ((attrs...)) +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.: -A simple wrapper that expands to C<__attribute__((attrs))> on GCC 3.1+ and -Clang 2.8+, and to nothing on other compilers, so the effect is that only -GCC and Clang see these. + #define SQL_LIMIT 100 + sql_exec ("select * from table limit " ECB_STRINGIFY (SQL_LIMIT)); -Example: use the C attribute on a function. +=item ECB_STRINGIFY_EXPR (expr) - ecb_attribute((__deprecated__)) void - do_not_use_me_anymore (void); +Like C, but additionally evaluates C to make sure it +is a valid expression. This is useful to catch typos or cases where the +macro isn't available: + + #include + + ECB_STRINGIFY (EDOM); // "33" (on my system at least) + ECB_STRINGIFY_EXPR (EDOM); // "33" + + // now imagine we had a typo: + + ECB_STRINGIFY (EDAM); // "EDAM" + ECB_STRINGIFY_EXPR (EDAM); // error: EDAM undefined + +=back + +=head2 ATTRIBUTES + +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 Marks a function or a variable as "unused", which simply suppresses a -warning by GCC when it detects it as unused. This is useful when you e.g. -declare a variable but do not always use it: +warning by the compiler when it detects it as unused. This is useful when +you e.g. declare a variable but do not always use it: { - int var ecb_unused; + ecb_unused int var; #ifdef SOMECONDITION var = ...; @@ -218,12 +264,17 @@ 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, the specified diagnostic 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 (a compiler-specific equivalent of) 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. @@ -235,7 +286,7 @@ =item ecb_noinline -Prevent a function from being inlined - it might be optimised away, but +Prevents a function from being inlined - it might be optimised away, but not inlined into other functions. This is useful if you know your function is rarely called and large enough for inlining not to be helpful. @@ -266,8 +317,8 @@ loop, because it knows it doesn't overwrite input values. void - multiply (float *ecb_restrict src, - float *ecb_restrict dst, + multiply (ecb_restrict float *src, + ecb_restrict float *dst, int len, float factor) { int i; @@ -403,7 +454,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 @@ -460,10 +511,11 @@ 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. +Tries to tell the compiler that some condition is true, even if it's not +obvious. This is not a function, but a statement: it cannot be used in +another expression. This can be used to teach the compiler about invariants or other conditions that might improve code generation, but which are impossible to @@ -490,13 +542,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. +function can be used to implement C or similar functionality. -=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 @@ -506,6 +558,9 @@ need to be accessible (it could be a null pointer for example), but C and C must be compile-time constants. +This is a statement, not a function: you cannot use it as part of an +expression. + An obvious way to use this is to prefetch some data far away, in a big array you loop over. This prefetches memory some 128 array elements later, in the hope that it will be ready when the CPU arrives at that location. @@ -552,12 +607,17 @@ =item int ecb_ctz64 (uint64_t x) +=item int ecb_ctz (T x) [C++] + 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 smaller types than C you can safely use C. +The overloaded C++ C function supports C, C, +C and C types. + For example: ecb_ctz32 (3) = 0 @@ -567,14 +627,21 @@ =item bool ecb_is_pot64 (uint32_t x) -Return true iff C is a power of two or C. +=item bool ecb_is_pot (T x) [C++] -For smaller types then C you can safely use C. +Returns true iff C is a power of two or C. + +For smaller types than C you can safely use C. + +The overloaded C++ C function supports C, C, +C and C types. =item int ecb_ld32 (uint32_t x) =item int ecb_ld64 (uint64_t x) +=item int ecb_ld64 (T x) [C++] + 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 @@ -588,14 +655,22 @@ For smaller types than C you can safely use C. +The overloaded C++ C function supports C, C, +C and C types. + =item int ecb_popcount32 (uint32_t x) =item int ecb_popcount64 (uint64_t x) +=item int ecb_popcount (T x) [C++] + Returns the number of bits set to 1 in C. For smaller types than C you can safely use C. +The overloaded C++ C function supports C, C, +C and C types. + For example: ecb_popcount32 (7) = 3 @@ -607,24 +682,39 @@ =item uint32_t ecb_bitrev32 (uint32_t x) +=item T ecb_bitrev (T x) [C++] + Reverses the bits in x, i.e. the MSB becomes the LSB, MSB-1 becomes LSB+1 and so on. +The overloaded C++ C function supports C, C and C types. + Example: ecb_bitrev8 (0xa7) = 0xea ecb_bitrev32 (0xffcc4411) = 0x882233ff +=item T ecb_bitrev (T x) [C++] + +Overloaded C++ bitrev function. + +C must be one of C, C or C. + =item uint32_t ecb_bswap16 (uint32_t x) =item uint32_t ecb_bswap32 (uint32_t x) =item uint64_t ecb_bswap64 (uint64_t x) +=item T ecb_bswap (T x) + 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). +The overloaded C++ C function supports C, C, +C and C types. + =item uint8_t ecb_rotl8 (uint8_t x, unsigned int count) =item uint16_t ecb_rotl16 (uint16_t x, unsigned int count) @@ -645,9 +735,186 @@ 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 or a combination of C on -x86). +Current GCC/clang versions understand these functions and usually compile +them to "optimal" code (e.g. a single C or a combination of C +on x86). + +=item T ecb_rotl (T x, unsigned int count) [C++] + +=item T ecb_rotr (T x, unsigned int count) [C++] + +Overloaded C++ rotl/rotr functions. + +C must be one of C, C, C or C. + +=back + +=head2 HOST ENDIANNESS CONVERSION + +=over 4 + +=item uint_fast16_t ecb_be_u16_to_host (uint_fast16_t v) + +=item uint_fast32_t ecb_be_u32_to_host (uint_fast32_t v) + +=item uint_fast64_t ecb_be_u64_to_host (uint_fast64_t v) + +=item uint_fast16_t ecb_le_u16_to_host (uint_fast16_t v) + +=item uint_fast32_t ecb_le_u32_to_host (uint_fast32_t v) + +=item uint_fast64_t ecb_le_u64_to_host (uint_fast64_t v) + +Convert an unsigned 16, 32 or 64 bit value from big or little endian to host byte order. + +The naming convention is C(C|C)C<_u>C<16|32|64>C<_to_host>, +where C and C stand for big endian and little endian, respectively. + +=item uint_fast16_t ecb_host_to_be_u16 (uint_fast16_t v) + +=item uint_fast32_t ecb_host_to_be_u32 (uint_fast32_t v) + +=item uint_fast64_t ecb_host_to_be_u64 (uint_fast64_t v) + +=item uint_fast16_t ecb_host_to_le_u16 (uint_fast16_t v) + +=item uint_fast32_t ecb_host_to_le_u32 (uint_fast32_t v) + +=item uint_fast64_t ecb_host_to_le_u64 (uint_fast64_t v) + +Like above, but converts I host byte order to the specified +endianness. + +=back + +In C++ the following additional template functions are supported: + +=over 4 + +=item T ecb_be_to_host (T v) + +=item T ecb_le_to_host (T v) + +=item T ecb_host_to_be (T v) + +=item T ecb_host_to_le (T v) + +=back + +These functions work like their C counterparts, above, but use templates, +which make them useful in generic code. + +C must be one of C, C, C or C +(so unlike their C counterparts, there is a version for C, which +again can be useful in generic code). + +=head2 UNALIGNED LOAD/STORE + +These function load or store unaligned multi-byte values. + +=over 4 + +=item uint_fast16_t ecb_peek_u16_u (const void *ptr) + +=item uint_fast32_t ecb_peek_u32_u (const void *ptr) + +=item uint_fast64_t ecb_peek_u64_u (const void *ptr) + +These functions load an unaligned, unsigned 16, 32 or 64 bit value from +memory. + +=item uint_fast16_t ecb_peek_be_u16_u (const void *ptr) + +=item uint_fast32_t ecb_peek_be_u32_u (const void *ptr) + +=item uint_fast64_t ecb_peek_be_u64_u (const void *ptr) + +=item uint_fast16_t ecb_peek_le_u16_u (const void *ptr) + +=item uint_fast32_t ecb_peek_le_u32_u (const void *ptr) + +=item uint_fast64_t ecb_peek_le_u64_u (const void *ptr) + +Like above, but additionally convert from big endian (C) or little +endian (C) byte order to host byte order while doing so. + +=item ecb_poke_u16_u (void *ptr, uint16_t v) + +=item ecb_poke_u32_u (void *ptr, uint32_t v) + +=item ecb_poke_u64_u (void *ptr, uint64_t v) + +These functions store an unaligned, unsigned 16, 32 or 64 bit value to +memory. + +=item ecb_poke_be_u16_u (void *ptr, uint_fast16_t v) + +=item ecb_poke_be_u32_u (void *ptr, uint_fast32_t v) + +=item ecb_poke_be_u64_u (void *ptr, uint_fast64_t v) + +=item ecb_poke_le_u16_u (void *ptr, uint_fast16_t v) + +=item ecb_poke_le_u32_u (void *ptr, uint_fast32_t v) + +=item ecb_poke_le_u64_u (void *ptr, uint_fast64_t v) + +Like above, but additionally convert from host byte order to big endian +(C) or little endian (C) byte order while doing so. + +=back + +In C++ the following additional template functions are supported: + +=over 4 + +=item T ecb_peek (const void *ptr) + +=item T ecb_peek_be (const void *ptr) + +=item T ecb_peek_le (const void *ptr) + +=item T ecb_peek_u (const void *ptr) + +=item T ecb_peek_be_u (const void *ptr) + +=item T ecb_peek_le_u (const void *ptr) + +Similarly to their C counterparts, these functions load an unsigned 8, 16, +32 or 64 bit value from memory, with optional conversion from big/little +endian. + +Since the type cannot be deduced, it has to be specified explicitly, e.g. + + uint_fast16_t v = ecb_peek (ptr); + +C must be one of C, C, C or C. + +Unlike their C counterparts, these functions support 8 bit quantities +(C) and also have an aligned version (without the C<_u> prefix), +all of which hopefully makes them more useful in generic code. + +=item ecb_poke (void *ptr, T v) + +=item ecb_poke_be (void *ptr, T v) + +=item ecb_poke_le (void *ptr, T v) + +=item ecb_poke_u (void *ptr, T v) + +=item ecb_poke_be_u (void *ptr, T v) + +=item ecb_poke_le_u (void *ptr, T v) + +Again, similarly to their C counterparts, these functions store an +unsigned 8, 16, 32 or z64 bit value to memory, with optional conversion to +big/little endian. + +C must be one of C, C, C or C. + +Unlike their C counterparts, these functions support 8 bit quantities +(C) and also have an aligned version (without the C<_u> prefix), +all of which hopefully makes them more useful in generic code. =back @@ -655,12 +922,29 @@ =over 4 +=item ECB_INFINITY [-UECB_NO_LIBM] + +Evaluates to positive infinity if supported by the platform, otherwise to +a truly huge number. + +=item ECB_NAN [-UECB_NO_LIBM] + +Evaluates to a quiet NAN if supported by the platform, otherwise to +C. + +=item float ecb_ldexpf (float x, int exp) [-UECB_NO_LIBM] + +Same as C, but always available. + +=item uint32_t ecb_float_to_binary16 (float x) [-UECB_NO_LIBM] + =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. +type and return the IEEE 754 bit representation of it (binary16/half, +binary32/single or binary64/double precision). 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. @@ -674,7 +958,7 @@ 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. +can serialise the return value like a normal uint16_t/uint32_t/uint64_t. Another use for these functions is to manipulate floating point values directly. @@ -691,11 +975,12 @@ =item float ecb_binary32_to_float (uint32_t x) [-UECB_NO_LIBM] -=item double ecb_binary32_to_double (uint64_t x) [-UECB_NO_LIBM] +=item double ecb_binary64_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. +representation of an IEEE binary16, binary32 or binary64 number (half, +single or double precision) 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 @@ -706,6 +991,19 @@ On all modern platforms (where C is true), the compiler should be able to optimise away this function completely. +=item uint16_t ecb_binary32_to_binary16 (uint32_t x) + +=item uint32_t ecb_binary16_to_binary32 (uint16_t x) + +Convert a IEEE binary32/single precision to binary16/half format, and vice +versa, handling all details (round-to-nearest-even, subnormals, infinity +and NaNs) correctly. + +These are functions are available under C<-DECB_NO_LIBM>, since +they do not rely on the platform floating point format. The +C and C functions are +usually what you want. + =back =head2 ARITHMETIC @@ -727,8 +1025,8 @@ type (this typically excludes the minimum signed integer value, the same limitation as for C and C<%> in C). -Current GCC versions compile this into an efficient branchless sequence on -almost all CPUs. +Current GCC/clang versions compile this into an efficient branchless +sequence on almost all CPUs. For example, when you want to rotate forward through the members of an array for increasing C (which might be negative), then you should use @@ -795,4 +1093,23 @@ =back +=head1 UNDOCUMENTED FUNCTIONALITY + +F is full of undocumented functionality as well, some of which is +intended to be internal-use only, some of which we forgot to document, and +some of which we hide because we are not sure we will keep the interface +stable. + +While you are welcome to rummage around and use whatever you find useful +(we can't stop you), keep in mind that we will change undocumented +functionality in incompatible ways without thinking twice, while we are +considerably more conservative with documented things. + +=head1 AUTHORS + +C is designed and maintained by: + + Emanuele Giaquinta + Marc Alexander Lehmann +