/* * GENERATED FILE, DO NOT EDIT. Generated by mkcast.py */ #include "cast.h" #include #include #include #include unsigned cast_float_unsigned(float x) { cast_assert(!isnan(x), "Tried casting %f to unsigned. It's nan", x); cast_assert(x >= 0.0f, "Tried casting %f to unsigned. It's not positive", x); // Due to rounding errors, avoid comparing close to the edge. cast_assert(x <= (float)(UINT_MAX - 1000), "Tried casting %f to unsigned. It's too big", x); return (unsigned)x; } uint32_t cast_double_uint32(double x) { cast_assert(!isnan(x), "Tried casting %f to uint32_t. It's nan", x); cast_assert(x >= 0.0, "Tried casting %f to uint32_t. It's not positive", x); // Due to rounding errors, avoid comparing close to the edge. cast_assert(x <= (double)(UINT32_MAX - 1000), "Tried casting %f to uint32_t. It's too big", x); return (uint32_t)x; } void cast_assert(int must, const char* fmt, ...) { va_list ap; va_start(ap, fmt); if (!must) { fprintf(stderr, "arping: runtime check: "); vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); exit(1); } va_end(ap); } uint16_t cast_int16_uint16(int16_t from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); cast_assert(from >= 0, "cast_int16_uint16(%"PRIdMAX"): %s", (intmax_t)from, "need >= 0"); const uint16_t to = (uint16_t)from; if (from != (int16_t)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in uint16_t"); } else { fprintf(stderr, "cast_int16_uint16(%"PRIdMAX"): %s", (intmax_t)from, "value won't fit in uint16_t\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } size_t cast_ssize_size(ssize_t from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); cast_assert(from >= 0, "cast_ssize_size(%"PRIdMAX"): %s", (intmax_t)from, "need >= 0"); const size_t to = (size_t)from; if (from != (ssize_t)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in size_t"); } else { fprintf(stderr, "cast_ssize_size(%"PRIdMAX"): %s", (intmax_t)from, "value won't fit in size_t\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } ssize_t cast_size_ssize(size_t from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); #pragma GCC diagnostic push // If the `from` type cannot represent `dst_max`, then GCC warns as // it always being true. So suppressing that warning. // // Silence both GCC and clang for the same error. #pragma GCC diagnostic ignored "-Wpragmas" #pragma GCC diagnostic ignored "-Wtype-limits" #pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare" cast_assert(from <= SSIZE_MAX, "cast_size_ssize(%"PRIuMAX"): %s", (uintmax_t)from, "value won't fit in ssize_t"); #pragma GCC diagnostic pop const ssize_t to = (ssize_t)from; cast_assert(to >= 0, "cast_size_ssize(%"PRIuMAX"): %s", (uintmax_t)from, "wrapped after casting"); if (from != (size_t)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in ssize_t"); } else { fprintf(stderr, "cast_size_ssize(%"PRIuMAX"): %s", (uintmax_t)from, "value won't fit in ssize_t\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } uint16_t cast_size_uint16(size_t from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); const uint16_t to = (uint16_t)from; if (from != (size_t)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in uint16_t"); } else { fprintf(stderr, "cast_size_uint16(%"PRIuMAX"): %s", (uintmax_t)from, "value won't fit in uint16_t\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } uint32_t cast_size_uint32(size_t from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); const uint32_t to = (uint32_t)from; if (from != (size_t)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in uint32_t"); } else { fprintf(stderr, "cast_size_uint32(%"PRIuMAX"): %s", (uintmax_t)from, "value won't fit in uint32_t\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } unsigned int cast_ulong_uint(unsigned long from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); const unsigned int to = (unsigned int)from; if (from != (unsigned long)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in unsigned int"); } else { fprintf(stderr, "cast_ulong_uint(%"PRIuMAX"): %s", (uintmax_t)from, "value won't fit in unsigned int\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } int16_t cast_long_int16(long from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); const int16_t to = (int16_t)from; if (from != (long)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in int16_t"); } else { fprintf(stderr, "cast_long_int16(%"PRIdMAX"): %s", (intmax_t)from, "value won't fit in int16_t\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } uint8_t cast_int16_uint8(int16_t from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); cast_assert(from >= 0, "cast_int16_uint8(%"PRIdMAX"): %s", (intmax_t)from, "need >= 0"); const uint8_t to = (uint8_t)from; if (from != (int16_t)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in uint8_t"); } else { fprintf(stderr, "cast_int16_uint8(%"PRIdMAX"): %s", (intmax_t)from, "value won't fit in uint8_t\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } unsigned int cast_int_uint(int from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); cast_assert(from >= 0, "cast_int_uint(%"PRIdMAX"): %s", (intmax_t)from, "need >= 0"); const unsigned int to = (unsigned int)from; if (from != (int)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in unsigned int"); } else { fprintf(stderr, "cast_int_uint(%"PRIdMAX"): %s", (intmax_t)from, "value won't fit in unsigned int\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } uint16_t cast_int_uint16(int from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); cast_assert(from >= 0, "cast_int_uint16(%"PRIdMAX"): %s", (intmax_t)from, "need >= 0"); const uint16_t to = (uint16_t)from; if (from != (int)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in uint16_t"); } else { fprintf(stderr, "cast_int_uint16(%"PRIdMAX"): %s", (intmax_t)from, "value won't fit in uint16_t\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } size_t cast_int_size(int from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); cast_assert(from >= 0, "cast_int_size(%"PRIdMAX"): %s", (intmax_t)from, "need >= 0"); const size_t to = (size_t)from; if (from != (int)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in size_t"); } else { fprintf(stderr, "cast_int_size(%"PRIdMAX"): %s", (intmax_t)from, "value won't fit in size_t\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } int cast_long_int(long from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); const int to = (int)from; if (from != (long)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in int"); } else { fprintf(stderr, "cast_long_int(%"PRIdMAX"): %s", (intmax_t)from, "value won't fit in int\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } uint8_t cast_long_uint8(long from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); cast_assert(from >= 0, "cast_long_uint8(%"PRIdMAX"): %s", (intmax_t)from, "need >= 0"); const uint8_t to = (uint8_t)from; if (from != (long)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in uint8_t"); } else { fprintf(stderr, "cast_long_uint8(%"PRIdMAX"): %s", (intmax_t)from, "value won't fit in uint8_t\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } gid_t cast_ulong_gid(unsigned long from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); const gid_t to = (gid_t)from; if (from != (unsigned long)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in gid_t"); } else { fprintf(stderr, "cast_ulong_gid(%"PRIuMAX"): %s", (uintmax_t)from, "value won't fit in gid_t\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } long long cast_gid_longlong(gid_t from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); #pragma GCC diagnostic push // If the `from` type cannot represent `dst_max`, then GCC warns as // it always being true. So suppressing that warning. // // Silence both GCC and clang for the same error. #pragma GCC diagnostic ignored "-Wpragmas" #pragma GCC diagnostic ignored "-Wtype-limits" #pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare" cast_assert(from <= LLONG_MAX, "cast_gid_longlong(%"PRIuMAX"): %s", (uintmax_t)from, "value won't fit in long long"); #pragma GCC diagnostic pop const long long to = (long long)from; cast_assert(to >= 0, "cast_gid_longlong(%"PRIuMAX"): %s", (uintmax_t)from, "wrapped after casting"); if (from != (gid_t)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in long long"); } else { fprintf(stderr, "cast_gid_longlong(%"PRIuMAX"): %s", (uintmax_t)from, "value won't fit in long long\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } long long cast_uid_longlong(uid_t from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); #pragma GCC diagnostic push // If the `from` type cannot represent `dst_max`, then GCC warns as // it always being true. So suppressing that warning. // // Silence both GCC and clang for the same error. #pragma GCC diagnostic ignored "-Wpragmas" #pragma GCC diagnostic ignored "-Wtype-limits" #pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare" cast_assert(from <= LLONG_MAX, "cast_uid_longlong(%"PRIuMAX"): %s", (uintmax_t)from, "value won't fit in long long"); #pragma GCC diagnostic pop const long long to = (long long)from; cast_assert(to >= 0, "cast_uid_longlong(%"PRIuMAX"): %s", (uintmax_t)from, "wrapped after casting"); if (from != (uid_t)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in long long"); } else { fprintf(stderr, "cast_uid_longlong(%"PRIuMAX"): %s", (uintmax_t)from, "value won't fit in long long\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } unsigned long long cast_longlong_ulonglong(long long from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); cast_assert(from >= 0, "cast_longlong_ulonglong(%"PRIdMAX"): %s", (intmax_t)from, "need >= 0"); const unsigned long long to = (unsigned long long)from; if (from != (long long)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in unsigned long long"); } else { fprintf(stderr, "cast_longlong_ulonglong(%"PRIdMAX"): %s", (intmax_t)from, "value won't fit in unsigned long long\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } unsigned int cast_longlong_uint(long long from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); cast_assert(from >= 0, "cast_longlong_uint(%"PRIdMAX"): %s", (intmax_t)from, "need >= 0"); const unsigned int to = (unsigned int)from; if (from != (long long)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in unsigned int"); } else { fprintf(stderr, "cast_longlong_uint(%"PRIdMAX"): %s", (intmax_t)from, "value won't fit in unsigned int\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } int16_t cast_int_int16(int from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); const int16_t to = (int16_t)from; if (from != (int)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in int16_t"); } else { fprintf(stderr, "cast_int_int16(%"PRIdMAX"): %s", (intmax_t)from, "value won't fit in int16_t\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } unsigned long cast_long_ulong(long from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); cast_assert(from >= 0, "cast_long_ulong(%"PRIdMAX"): %s", (intmax_t)from, "need >= 0"); const unsigned long to = (unsigned long)from; if (from != (long)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in unsigned long"); } else { fprintf(stderr, "cast_long_ulong(%"PRIdMAX"): %s", (intmax_t)from, "value won't fit in unsigned long\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } unsigned int cast_long_uint(long from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); cast_assert(from >= 0, "cast_long_uint(%"PRIdMAX"): %s", (intmax_t)from, "need >= 0"); const unsigned int to = (unsigned int)from; if (from != (long)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in unsigned int"); } else { fprintf(stderr, "cast_long_uint(%"PRIdMAX"): %s", (intmax_t)from, "value won't fit in unsigned int\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; } unsigned long cast_int_ulong(int from, const char* fmt, ...) { va_list ap; va_start(ap, fmt); cast_assert(from >= 0, "cast_int_ulong(%"PRIdMAX"): %s", (intmax_t)from, "need >= 0"); const unsigned long to = (unsigned long)from; if (from != (int)to) { fprintf(stderr, "arping: "); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": value won't fit in unsigned long"); } else { fprintf(stderr, "cast_int_ulong(%"PRIdMAX"): %s", (intmax_t)from, "value won't fit in unsigned long\n"); } fprintf(stderr, "\n"); exit(1); } va_end(ap); return to; }