From 4d8ae44794a4d2c56fe8eb1c40e1516266d62089 Mon Sep 17 00:00:00 2001 From: Kp Date: Wed, 29 Jul 2015 03:05:28 +0000 Subject: [PATCH] Add helper macro DXX_CONSTANT_TRUE --- SConstruct | 1 + common/include/3d.h | 4 ++-- common/include/ntstring.h | 8 ++++---- common/include/ogl_init.h | 4 ++-- common/include/partial_range.h | 4 ++-- common/include/physfsx.h | 2 +- common/include/vecmat.h | 8 +++----- common/main/multiinternal.h | 11 ++++------- similar/main/multi.cpp | 8 ++++---- 9 files changed, 23 insertions(+), 27 deletions(-) diff --git a/SConstruct b/SConstruct index 2050e9b90..13afecae4 100644 --- a/SConstruct +++ b/SConstruct @@ -484,6 +484,7 @@ static int a(int b){ if self.Link(context, text=f % 'c(b)', main=main, msg='whether compiler optimizes __builtin_constant_p'): context.sconf.Define('DXX_HAVE_BUILTIN_CONSTANT_P') context.sconf.Define('dxx_builtin_constant_p(A)', '__builtin_constant_p(A)') + context.sconf.Define('DXX_CONSTANT_TRUE(E)', '(__builtin_constant_p((E)) && (E))') else: self.Compile(context, text=f % '2', main=main, msg='whether compiler accepts __builtin_constant_p') context.sconf.Define('dxx_builtin_constant_p(A)', '((void)(A),0)') diff --git a/common/include/3d.h b/common/include/3d.h index 4a0c4c9f5..f0c09fadd 100644 --- a/common/include/3d.h +++ b/common/include/3d.h @@ -210,8 +210,8 @@ template static inline void g3_draw_tmap(unsigned nv, const array &pointlist, const array &uvl_list, const array &light_rgb, grs_bitmap &bm) { static_assert(N <= MAX_POINTS_PER_POLY, "too many points in tmap"); -#ifdef DXX_HAVE_BUILTIN_CONSTANT_P - if (__builtin_constant_p(nv > N) && nv > N) +#ifdef DXX_CONSTANT_TRUE + if (DXX_CONSTANT_TRUE(nv > N)) DXX_ALWAYS_ERROR_FUNCTION(dxx_trap_tmap_overread, "reading beyond array"); #endif _g3_draw_tmap(nv, &pointlist[0], &uvl_list[0], &light_rgb[0], bm); diff --git a/common/include/ntstring.h b/common/include/ntstring.h index 0d0dc5740..0b53665b3 100644 --- a/common/include/ntstring.h +++ b/common/include/ntstring.h @@ -60,8 +60,8 @@ public: template std::size_t copy_if(std::size_t out_offset, const array &i, std::size_t n = N) { -#ifdef DXX_HAVE_BUILTIN_CONSTANT_P - if (__builtin_constant_p(n > N) && n > N) +#ifdef DXX_CONSTANT_TRUE + if (DXX_CONSTANT_TRUE(n > N)) DXX_ALWAYS_ERROR_FUNCTION(dxx_trap_overread, "read size exceeds array size"); #endif return copy_if(out_offset, i.data(), n); @@ -76,8 +76,8 @@ public: std::size_t copy_if(std::size_t offset, const char *i, std::size_t N) { const std::size_t d = -#ifdef DXX_HAVE_BUILTIN_CONSTANT_P - (__builtin_constant_p(i[N - 1]) && !i[N - 1]) ? N - 1 : +#ifdef DXX_CONSTANT_TRUE + (DXX_CONSTANT_TRUE(!i[N - 1])) ? N - 1 : #endif std::distance(i, std::find(i, i + N, terminator())); return _copy_n(offset, i, d); diff --git a/common/include/ogl_init.h b/common/include/ogl_init.h index e66bbc6f9..dee5048a3 100644 --- a/common/include/ogl_init.h +++ b/common/include/ogl_init.h @@ -109,8 +109,8 @@ template static inline void g3_draw_tmap_2(unsigned nv, const array &pointlist, const array &uvl_list, const array &light_rgb, grs_bitmap *bmbot, grs_bitmap *bm, int orient) { static_assert(N <= MAX_POINTS_PER_POLY, "too many points in tmap"); -#ifdef DXX_HAVE_BUILTIN_CONSTANT_P - if (__builtin_constant_p(nv) && nv > N) +#ifdef DXX_CONSTANT_TRUE + if (DXX_CONSTANT_TRUE(nv > N)) DXX_ALWAYS_ERROR_FUNCTION(dxx_trap_tmap_overread, "reading beyond array"); #endif _g3_draw_tmap_2(nv, &pointlist[0], &uvl_list[0], &light_rgb[0], bmbot, bm, orient); diff --git a/common/include/partial_range.h b/common/include/partial_range.h index febd95dd8..4c39d8c1a 100644 --- a/common/include/partial_range.h +++ b/common/include/partial_range.h @@ -163,7 +163,7 @@ static inline void check_range_bounds(const char (&file)[NF], unsigned line, con * !DXX_HAVE_BUILTIN_CONSTANT_P and the macro expands to nothing. */ #define PARTIAL_RANGE_COMPILE_CHECK_BOUND(EXPR,S) \ - (__builtin_constant_p(EXPR > d) && (EXPR > d) && (DXX_ALWAYS_ERROR_FUNCTION(partial_range_will_always_throw_##S, #S " will always throw"), 0)) + (DXX_CONSTANT_TRUE(EXPR > d) && (DXX_ALWAYS_ERROR_FUNCTION(partial_range_will_always_throw_##S, #S " will always throw"), 0)) #else #define PARTIAL_RANGE_COMPILE_CHECK_BOUND(EXPR,S) static_cast(0) #endif @@ -207,7 +207,7 @@ static inline partial_range_t (unchecked_partial_range)(const char (&file)[NF * happen, then the range is always empty, which likely indicates a * bug. */ - if (__builtin_constant_p(!(o < l)) && !(o < l)) + if (DXX_CONSTANT_TRUE(!(o < l))) DXX_ALWAYS_ERROR_FUNCTION(partial_range_is_always_empty, "offset never less than length"); #endif #ifdef DXX_HAVE_BUILTIN_OBJECT_SIZE diff --git a/common/include/physfsx.h b/common/include/physfsx.h index 7b32a22ad..7f9546caa 100644 --- a/common/include/physfsx.h +++ b/common/include/physfsx.h @@ -42,7 +42,7 @@ #include "compiler-type_traits.h" #ifdef DXX_HAVE_BUILTIN_CONSTANT_P -#define _DXX_PHYSFS_CHECK_SIZE_CONSTANT(S,v) (__builtin_constant_p((S) > v) && (S) > v) +#define _DXX_PHYSFS_CHECK_SIZE_CONSTANT(S,v) DXX_CONSTANT_TRUE((S) > (v)) #define _DXX_PHYSFS_CHECK_SIZE(S,C,v) _DXX_PHYSFS_CHECK_SIZE_CONSTANT(static_cast(S) * static_cast(C), v) #define DXX_PHYSFS_CHECK_READ_SIZE_OBJECT_SIZE(S,C,v) \ (void)(__builtin_object_size(v, 1) != static_cast(-1) && _DXX_PHYSFS_CHECK_SIZE(S,C,__builtin_object_size(v, 1)) && (DXX_ALWAYS_ERROR_FUNCTION(dxx_trap_overwrite, "read size exceeds element size"), 0)) diff --git a/common/include/vecmat.h b/common/include/vecmat.h index eb9efeaa7..95afedb55 100644 --- a/common/include/vecmat.h +++ b/common/include/vecmat.h @@ -268,13 +268,11 @@ static inline vms_vector vm_vec_add (const vms_vector &src0, const vms_vector &s vms_vector &_vm_vec_sub(vms_vector &dest, const vms_vector &src0, const vms_vector &src1); static inline vms_vector &vm_vec_sub(vms_vector &dest, const vms_vector &src0, const vms_vector &src1) { -#ifdef DXX_HAVE_BUILTIN_CONSTANT_P -#define vec_sub_constant_true(A) (__builtin_constant_p(A) && (A)) - if (vec_sub_constant_true(&src0 == &src1)) +#ifdef DXX_CONSTANT_TRUE + if (DXX_CONSTANT_TRUE(&src0 == &src1)) DXX_ALWAYS_ERROR_FUNCTION(vm_vec_sub_same_op, "vm_vec_sub with &src0 == &src1"); - else if (vec_sub_constant_true(src0.x == src1.x && src0.y == src1.y && src0.z == src1.z)) + else if (DXX_CONSTANT_TRUE(src0.x == src1.x && src0.y == src1.y && src0.z == src1.z)) DXX_ALWAYS_ERROR_FUNCTION(vm_vec_sub_same_values, "vm_vec_sub with equal value inputs"); -#undef vec_sub_constant_true #endif return _vm_vec_sub(dest, src0, src1); } diff --git a/common/main/multiinternal.h b/common/main/multiinternal.h index 631045f52..ce40fc733 100644 --- a/common/main/multiinternal.h +++ b/common/main/multiinternal.h @@ -119,15 +119,12 @@ static inline void multi_send_data(ubyte *buf, unsigned len, int priority) { buf[0] = C; unsigned expected = command_length::value; +#ifdef DXX_CONSTANT_TRUE + if (DXX_CONSTANT_TRUE(len != expected)) + DXX_ALWAYS_ERROR_FUNCTION(dxx_trap_multi_send_data, "wrong packet size"); +#endif if (len != expected) { -#ifdef DXX_HAVE_BUILTIN_CONSTANT_P - /* Restate (len != expected) for ::value; +#ifdef DXX_CONSTANT_TRUE + if (DXX_CONSTANT_TRUE(len != expected)) + DXX_ALWAYS_ERROR_FUNCTION(dxx_trap_multi_send_data, "wrong packet size"); +#endif if (len != expected) { -#ifdef DXX_HAVE_BUILTIN_CONSTANT_P - if (__builtin_constant_p(len) && __builtin_constant_p(len != expected)) - DXX_ALWAYS_ERROR_FUNCTION(dxx_trap_multi_send_data, "wrong packet size"); -#endif Error("multi_send_data_direct: Packet type %i length: %i, expected: %i\n", C, len, expected); } _multi_send_data_direct(buf, len, pnum, priority);