From 5ab6b53062018f7a75595aceef5a5910f8f1ae50 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sat, 11 Nov 2006 05:47:09 +0000 Subject: [PATCH] Make life a little easier by adding an is_exp2() macro. svn path=/trunk/yasm/; revision=1682 --- modules/objfmts/bin/bin-objfmt.c | 4 ++-- modules/objfmts/coff/coff-objfmt.c | 4 ++-- modules/objfmts/elf/elf-objfmt.c | 10 +++++----- modules/objfmts/rdf/rdf-objfmt.c | 4 ++-- modules/objfmts/xdf/xdf-objfmt.c | 4 ++-- util.h | 7 +++++++ 6 files changed, 20 insertions(+), 13 deletions(-) diff --git a/modules/objfmts/bin/bin-objfmt.c b/modules/objfmts/bin/bin-objfmt.c index 4ed8cde6..5ebea8c6 100644 --- a/modules/objfmts/bin/bin-objfmt.c +++ b/modules/objfmts/bin/bin-objfmt.c @@ -433,14 +433,14 @@ bin_objfmt_section_switch(yasm_objfmt *objfmt, yasm_valparamhead *valparams, align_expr = yasm_expr_get_intnum(&vp->param, 0); if (!align_expr) { yasm_error_set(YASM_ERROR_VALUE, - N_("argument to `%s' is not a power of two"), + N_("argument to `%s' is not an integer"), vp->val); return NULL; } align = yasm_intnum_get_uint(align_expr); /* Alignments must be a power of two. */ - if ((align & (align - 1)) != 0) { + if (!is_exp2(align)) { yasm_error_set(YASM_ERROR_VALUE, N_("argument to `%s' is not a power of two"), vp->val); diff --git a/modules/objfmts/coff/coff-objfmt.c b/modules/objfmts/coff/coff-objfmt.c index ad67543a..e7ae8611 100644 --- a/modules/objfmts/coff/coff-objfmt.c +++ b/modules/objfmts/coff/coff-objfmt.c @@ -1515,14 +1515,14 @@ coff_objfmt_section_switch(yasm_objfmt *objfmt, yasm_valparamhead *valparams, align_expr = yasm_expr_get_intnum(&vp->param, 0); if (!align_expr) { yasm_error_set(YASM_ERROR_VALUE, - N_("argument to `%s' is not a power of two"), + N_("argument to `%s' is not an integer"), vp->val); return NULL; } align = yasm_intnum_get_uint(align_expr); /* Alignments must be a power of two. */ - if ((align & (align - 1)) != 0) { + if (!is_exp2(align)) { yasm_error_set(YASM_ERROR_VALUE, N_("argument to `%s' is not a power of two"), vp->val); diff --git a/modules/objfmts/elf/elf-objfmt.c b/modules/objfmts/elf/elf-objfmt.c index c5947c5a..142e3955 100644 --- a/modules/objfmts/elf/elf-objfmt.c +++ b/modules/objfmts/elf/elf-objfmt.c @@ -248,7 +248,7 @@ elf_objfmt_output_align(FILE *f, unsigned int align) { long pos; unsigned long delta; - if ((align & (align-1)) != 0) + if (!is_exp2(align)) yasm_internal_error("requested alignment not a power of two"); pos = ftell(f); @@ -920,14 +920,14 @@ elf_objfmt_section_switch(yasm_objfmt *objfmt, yasm_valparamhead *valparams, align_expr = yasm_expr_get_intnum(&vp->param, 0); if (!align_expr) { yasm_error_set(YASM_ERROR_VALUE, - N_("argument to `%s' is not a power of two"), + N_("argument to `%s' is not an integer"), vp->val); return NULL; } align = yasm_intnum_get_uint(align_expr); /* Alignments must be a power of two. */ - if ((align & (align - 1)) != 0) { + if (!is_exp2(align)) { yasm_error_set(YASM_ERROR_VALUE, N_("argument to `%s' is not a power of two"), vp->val); @@ -1078,13 +1078,13 @@ elf_objfmt_common_declare(yasm_objfmt *objfmt, const char *name, align_expr = yasm_expr_get_intnum(&vp->param, 0); if (!align_expr) { yasm_error_set(YASM_ERROR_VALUE, - N_("alignment constraint is not a power of two")); + N_("alignment constraint is not an integer")); return sym; } addralign = yasm_intnum_get_uint(align_expr); /* Alignments must be a power of two. */ - if ((addralign & (addralign - 1)) != 0) { + if (!is_exp2(addralign)) { yasm_error_set(YASM_ERROR_VALUE, N_("alignment constraint is not a power of two")); return sym; diff --git a/modules/objfmts/rdf/rdf-objfmt.c b/modules/objfmts/rdf/rdf-objfmt.c index 7529b648..25a42444 100644 --- a/modules/objfmts/rdf/rdf-objfmt.c +++ b/modules/objfmts/rdf/rdf-objfmt.c @@ -1057,13 +1057,13 @@ rdf_objfmt_common_declare(yasm_objfmt *objfmt, const char *name, align_expr = yasm_expr_get_intnum(&vp->param, 0); if (!align_expr) { yasm_error_set(YASM_ERROR_VALUE, - N_("alignment constraint is not a power of two")); + N_("alignment constraint is not an integer")); return sym; } addralign = yasm_intnum_get_uint(align_expr); /* Alignments must be a power of two. */ - if ((addralign & (addralign - 1)) != 0) { + if (!is_exp2(addralign)) { yasm_error_set(YASM_ERROR_VALUE, N_("alignment constraint is not a power of two")); return sym; diff --git a/modules/objfmts/xdf/xdf-objfmt.c b/modules/objfmts/xdf/xdf-objfmt.c index 27efe4d3..81b6050f 100644 --- a/modules/objfmts/xdf/xdf-objfmt.c +++ b/modules/objfmts/xdf/xdf-objfmt.c @@ -739,14 +739,14 @@ xdf_objfmt_section_switch(yasm_objfmt *objfmt, yasm_valparamhead *valparams, align_expr = yasm_expr_get_intnum(&vp->param, 0); if (!align_expr) { yasm_error_set(YASM_ERROR_VALUE, - N_("argument to `%s' is not a power of two"), + N_("argument to `%s' is not an integer"), vp->val); return NULL; } align = yasm_intnum_get_uint(align_expr); /* Alignments must be a power of two. */ - if ((align & (align - 1)) != 0) { + if (!is_exp2(align)) { yasm_error_set(YASM_ERROR_VALUE, N_("argument to `%s' is not a power of two"), vp->val); diff --git a/util.h b/util.h index 01b200f9..2384e008 100644 --- a/util.h +++ b/util.h @@ -151,6 +151,13 @@ d = BC_COUNT(d, 4); \ } while (0) +/** Determine if a value is exactly a power of 2. Zero is treated as a power + * of two. + * \param x value + * \return Nonzero if x is a power of 2. + */ +#define is_exp2(x) ((x & (x - 1)) == 0) + #ifndef NELEMS /** Get the number of elements in an array. * \internal -- 2.40.0