00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef YA_UTILS_H
00026 #define YA_UTILS_H
00027
00028 #define _GNU_SOURCE
00029 #include <stdlib.h>
00030 #include <stdint.h>
00031 #include <yyast/types.h>
00032 #include <yyast/config.h>
00033
00034 #ifndef HAVE___BUILTIN_BSWAP64
00035 #ifdef __linux
00036 #include <byteswap.h>
00037 #define __builtin_bswap64(x) __bswap_64(x)
00038 #else
00039 #error "Do not know how to replace __builtin_bswap64 on this platform."
00040 #endif
00041 #endif
00042
00043 typedef union {
00044 uint128_t v;
00045 struct {
00046 uint64_t v[2];
00047 } in;
00048 } bswap128_t;
00049
00050 static inline uint128_t bswap128(uint128_t x)
00051 {
00052 bswap128_t tmp128;
00053 uint64_t tmp64;
00054
00055 tmp128.v = x;
00056
00057 tmp64 = __builtin_bswap64(tmp128.in.v[0]);
00058 tmp128.in.v[0] = __builtin_bswap64(tmp128.in.v[1]);
00059 tmp128.in.v[1] = tmp64;
00060
00061 return tmp128.v;
00062 }
00063
00066 static inline uint128_t htonlll(uint128_t x)
00067 {
00068 #ifdef WORDS_BIGENDIAN
00069 return x;
00070 #else
00071 return bswap128(x);
00072 #endif
00073 }
00074
00077 static inline unsigned long long htonll(unsigned long long x)
00078 {
00079 #ifdef WORDS_BIGENDIAN
00080 return x;
00081 #else
00082 return __builtin_bswap64(x);
00083 #endif
00084 }
00085
00088 static inline unsigned long long ntohll(unsigned long long x)
00089 {
00090 #ifdef WORDS_BIGENDIAN
00091 return x;
00092 #else
00093 return __builtin_bswap64(x);
00094 #endif
00095 }
00096
00101 static inline size_t ya_align64(size_t x)
00102 {
00103 if (sizeof (x) == 4) {
00104 return (x + 7) & 0xfffffff8;
00105 } else {
00106 return (x + 7) & 0xfffffffffffffff8;
00107 }
00108 }
00109
00120 size_t ya_string_escape(uint8_t *string, size_t string_size, int raw);
00121
00128 char *ya_new_extension(char *filename, char *new_extension);
00129
00130 #endif