… | |
… | |
168 | |
168 | |
169 | =item ECB_64BIT_NATIVE |
169 | =item ECB_64BIT_NATIVE |
170 | |
170 | |
171 | Evaluates to a true value (suitable for both preprocessor and C code |
171 | Evaluates to a true value (suitable for both preprocessor and C code |
172 | testing) if 64 bit integer types on this architecture are evaluated |
172 | testing) if 64 bit integer types on this architecture are evaluated |
173 | "natively", that is, with similar speeds as 32 bit integerss. While 64 bit |
173 | "natively", that is, with similar speeds as 32 bit integers. While 64 bit |
174 | integer support is very common (and in fatc required by libecb), 32 bit |
174 | integer support is very common (and in fact required by libecb), 32 bit |
175 | cpus have to emulate operations on them, so you might want to avoid them. |
175 | cpus have to emulate operations on them, so you might want to avoid them. |
176 | |
176 | |
177 | =item ECB_AMD64, ECB_AMD64_X32 |
177 | =item ECB_AMD64, ECB_AMD64_X32 |
178 | |
178 | |
179 | These two macros are defined to C<1> on the x86_64/amd64 ABI and the X32 |
179 | These two macros are defined to C<1> on the x86_64/amd64 ABI and the X32 |
… | |
… | |
731 | |
731 | |
732 | =item uint64_t ecb_rotr64 (uint64_t x, unsigned int count) |
732 | =item uint64_t ecb_rotr64 (uint64_t x, unsigned int count) |
733 | |
733 | |
734 | These two families of functions return the value of C<x> after rotating |
734 | These two families of functions return the value of C<x> after rotating |
735 | all the bits by C<count> positions to the right (C<ecb_rotr>) or left |
735 | all the bits by C<count> positions to the right (C<ecb_rotr>) or left |
736 | (C<ecb_rotl>). |
736 | (C<ecb_rotl>). There are no restrictions on the value C<count>, i.e. both |
|
|
737 | zero and values equal or larger than the word width work correctly. |
737 | |
738 | |
738 | Current GCC/clang versions understand these functions and usually compile |
739 | Current GCC/clang versions understand these functions and usually compile |
739 | them to "optimal" code (e.g. a single C<rol> or a combination of C<shld> |
740 | them to "optimal" code (e.g. a single C<rol> or a combination of C<shld> |
740 | on x86). |
741 | on x86). |
741 | |
742 | |
… | |
… | |
934 | =head3 HIGH LEVEL API |
935 | =head3 HIGH LEVEL API |
935 | |
936 | |
936 | The high level API consists of four functions, one each for C<int32_t>, |
937 | The high level API consists of four functions, one each for C<int32_t>, |
937 | C<uint32_t>, C<int64_t> and C<uint64_t>: |
938 | C<uint32_t>, C<int64_t> and C<uint64_t>: |
938 | |
939 | |
|
|
940 | Example: |
|
|
941 | |
|
|
942 | char buf[ECB_I2A_MAX_DIGITS + 1]; |
|
|
943 | char *end = ecb_i2a_i32 (buf, 17262); |
|
|
944 | *end = 0; |
|
|
945 | // buf now contains "17262" |
|
|
946 | |
939 | =over |
947 | =over |
940 | |
948 | |
941 | =item ECB_I2A_I32_DIGITS (=11) |
949 | =item ECB_I2A_I32_DIGITS (=11) |
942 | |
950 | |
943 | =item char *ecb_i2a_u32 (char *ptr, uint32_t value) |
951 | =item char *ecb_i2a_u32 (char *ptr, uint32_t value) |
944 | |
952 | |
945 | Takes an C<uint32_t> I<value> and formats it as a decimal number starting |
953 | Takes an C<uint32_t> I<value> and formats it as a decimal number starting |
946 | at I<ptr>, using at most C<ECB_I2A_I32_DIGITS> characters. Returns a |
954 | at I<ptr>, using at most C<ECB_I2A_I32_DIGITS> characters. Returns a |
947 | pointer to just after the generated string, where you would normally put |
955 | pointer to just after the generated string, where you would normally put |
948 | the temrinating C<0> character. This function outputs the minimum number |
956 | the terminating C<0> character. This function outputs the minimum number |
949 | of digits. |
957 | of digits. |
950 | |
958 | |
951 | =item ECB_I2A_U32_DIGITS (=10) |
959 | =item ECB_I2A_U32_DIGITS (=10) |
952 | |
960 | |
953 | =item char *ecb_i2a_i32 (char *ptr, int32_t value) |
961 | =item char *ecb_i2a_i32 (char *ptr, int32_t value) |
… | |
… | |
973 | =back |
981 | =back |
974 | |
982 | |
975 | =head3 LOW-LEVEL API |
983 | =head3 LOW-LEVEL API |
976 | |
984 | |
977 | The functions above use a number of low-level APIs which have some strict |
985 | The functions above use a number of low-level APIs which have some strict |
978 | limitaitons, but cna be used as building blocks (study of C<ecb_i2a_i32> |
986 | limitations, but can be used as building blocks (study of C<ecb_i2a_i32> |
979 | and related cunctions is recommended). |
987 | and related functions is recommended). |
980 | |
988 | |
981 | There are three families of functions: functions that convert a number |
989 | There are three families of functions: functions that convert a number |
982 | to a fixed number of digits with leading zeroes (C<ecb_i2a_0N>, C<0> |
990 | to a fixed number of digits with leading zeroes (C<ecb_i2a_0N>, C<0> |
983 | for "leading zeroes"), functions that generate up to N digits, skipping |
991 | for "leading zeroes"), functions that generate up to N digits, skipping |
984 | leading zeroes (C<_N>), and functions that can generate more digits, but |
992 | leading zeroes (C<_N>), and functions that can generate more digits, but |
985 | the leading digit has limited range (C<_xN>). |
993 | the leading digit has limited range (C<_xN>). |
986 | |
994 | |
987 | None of the functions deal with negative numbera. |
995 | None of the functions deal with negative numbers. |
|
|
996 | |
|
|
997 | Example: convert an IP address in an u32 into dotted-quad: |
|
|
998 | |
|
|
999 | uint32_t ip = 0x0a000164; // 10.0.1.100 |
|
|
1000 | char ips[3 * 4 + 3 + 1]; |
|
|
1001 | char *ptr = ips; |
|
|
1002 | ptr = ecb_i2a_3 (ptr, ip >> 24 ); *ptr++ = '.'; |
|
|
1003 | ptr = ecb_i2a_3 (ptr, (ip >> 16) & 0xff); *ptr++ = '.'; |
|
|
1004 | ptr = ecb_i2a_3 (ptr, (ip >> 8) & 0xff); *ptr++ = '.'; |
|
|
1005 | ptr = ecb_i2a_3 (ptr, ip & 0xff); *ptr++ = 0; |
|
|
1006 | printf ("ip: %s\n", ips); // prints "ip: 10.0.1.100" |
988 | |
1007 | |
989 | =over |
1008 | =over |
990 | |
1009 | |
991 | =item char *ecb_i2a_02 (char *ptr, uint32_t value) // 32 bit |
1010 | =item char *ecb_i2a_02 (char *ptr, uint32_t value) // 32 bit |
992 | |
1011 | |
… | |
… | |
1044 | functions, but they can generate one digit more, as long as the number |
1063 | functions, but they can generate one digit more, as long as the number |
1045 | is within range, which is given by the symbols C<ECB_I2A_MAX_X5> (almost |
1064 | is within range, which is given by the symbols C<ECB_I2A_MAX_X5> (almost |
1046 | 16 bit range) and C<ECB_I2A_MAX_X10> (a bit more than 31 bit range), |
1065 | 16 bit range) and C<ECB_I2A_MAX_X10> (a bit more than 31 bit range), |
1047 | respectively. |
1066 | respectively. |
1048 | |
1067 | |
1049 | For example, the sigit part of a 32 bit signed integer just fits into the |
1068 | For example, the digit part of a 32 bit signed integer just fits into the |
1050 | C<ECB_I2A_MAX_X10> range, so while C<ecb_i2a_x10> cannot convert a 10 |
1069 | C<ECB_I2A_MAX_X10> range, so while C<ecb_i2a_x10> cannot convert a 10 |
1051 | digit number, it can convert all 32 bit signed numbers. Sadly, it's not |
1070 | digit number, it can convert all 32 bit signed numbers. Sadly, it's not |
1052 | good enough for 32 bit unsigned numbers. |
1071 | good enough for 32 bit unsigned numbers. |
1053 | |
1072 | |
1054 | =back |
1073 | =back |