root/test/util/opal_bit_ops.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main
  2. hibit
  3. test_hibit
  4. cube_dim
  5. test_cube_dim
  6. next_poweroftwo
  7. test_next_poweroftwo
  8. next_poweroftwo_inclusive
  9. test_next_poweroftwo_inclusive

   1 /*
   2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
   3  *                         University Research and Technology
   4  *                         Corporation.  All rights reserved.
   5  * Copyright (c) 2004-2005 The University of Tennessee and The University
   6  *                         of Tennessee Research Foundation.  All rights
   7  *                         reserved.
   8  * Copyright (c) 2004-2011 High Performance Computing Center Stuttgart,
   9  *                         University of Stuttgart.  All rights reserved.
  10  * Copyright (c) 2004-2005 The Regents of the University of California.
  11  *                         All rights reserved.
  12  * Copyright (c) 2013 Cisco Systems, Inc.  All rights reserved.
  13  * Copyright (c) 2018      Amazon.com, Inc. or its affiliates.  All Rights reserved.
  14  * $COPYRIGHT$
  15  *
  16  * Additional copyrights may follow
  17  *
  18  * $HEADER$
  19  */
  20 
  21 #include "opal_config.h"
  22 
  23 #include <stdio.h>
  24 #include <string.h>
  25 
  26 #include "support.h"
  27 #include "opal/util/bit_ops.h"
  28 #include "opal/util/output.h"
  29 #include "opal/util/printf.h"
  30 
  31 /*
  32 #define DEBUG
  33 */
  34 
  35 static int test_hibit(int value, int start);
  36 static int test_cube_dim(int value);
  37 static int test_next_poweroftwo(int value);
  38 static int test_next_poweroftwo_inclusive(int value);
  39 
  40 int main(int argc, char* argv[])
  41 {
  42     int i;
  43     int vals[] = {0, 1, 2, 3, 4, 5, 127, 128, 129, (1 << 29) -1, (1 << 29), (1 << 29) +1, (1 << 30) -1, (1 << 30) /* And NOT (1 << 30) +1 */};
  44     test_init("opal_bit_ops()");
  45 
  46 #ifdef DEBUG
  47     printf ("Test usage: ./opal_bit_ops [VALUES]\n");
  48 #endif
  49 
  50     if (1 < argc) {
  51         for (i = 1; i < argc; i++) {
  52             int value;
  53             value = atoi (argv[i]);
  54             printf ("Testing %d. argument test_next_poweroftwo(%d): %s\n",
  55                     i, value, test_next_poweroftwo(value) ? "correct" : "wrong");
  56         }
  57     }
  58 
  59     for (i = 0; i < (int)(sizeof(vals)/sizeof(vals[0])); i++) {
  60         test_hibit (vals[i], 8 * sizeof(int) -2);
  61         test_hibit (vals[i], 3);
  62         test_cube_dim (vals[i]);
  63         test_next_poweroftwo (vals[i]);
  64         test_next_poweroftwo_inclusive (vals[i]);
  65     }
  66 
  67     /* All done */
  68     return test_finalize();
  69 }
  70 
  71 
  72 /* REFERENCE FUNCTION */
  73 static int hibit(int value, int start)
  74 {
  75     unsigned int mask;
  76 
  77     --start;
  78     mask = 1 << start;
  79 
  80     for (; start >= 0; --start, mask >>= 1) {
  81         if (value & mask) {
  82             break;
  83         }
  84     }
  85 
  86     return start;
  87 }
  88 
  89 static int test_hibit(int value, int start)
  90 {
  91     int out;
  92     int bit = hibit (value, start);
  93 
  94 #ifdef DEBUG
  95     printf ("test_hibit(): value:%d expect:%d\n",
  96             value, bit);
  97 #endif
  98 
  99     if (bit == (out = opal_hibit (value, start))) {
 100         test_success();
 101         return 1;
 102     } else {
 103         char * msg;
 104         opal_asprintf(&msg, "Mismatch for hibit (w/ start:%d): value:%d, expected:%d got:%d\n",
 105                  start, value, bit, out);
 106         test_failure(msg);
 107         free(msg);
 108     }
 109     return 0;
 110 }
 111 
 112 
 113 /* REFERENCE FUNCTION */
 114 static int cube_dim(int value)
 115 {
 116     int dim, size;
 117 
 118     for (dim = 0, size = 1; size < value; ++dim, size <<= 1);
 119 
 120     return dim;
 121 }
 122 
 123 static int test_cube_dim(int value)
 124 {
 125     int out;
 126     int dim = cube_dim (value);
 127 
 128 #ifdef DEBUG
 129     printf ("test_cube_dim(): value:%d expect:%d\n",
 130             value, dim);
 131 #endif
 132 
 133     if (dim == (out = opal_cube_dim (value))) {
 134         test_success();
 135         return 1;
 136     } else {
 137         char * msg;
 138         opal_asprintf(&msg, "Mismatch for cube_dim: value:%d, expected:%d got:%d\n",
 139                  value, dim, out);
 140         test_failure(msg);
 141         free(msg);
 142     }
 143     return 0;
 144 }
 145 
 146 
 147 /* REFERENCE FUNCTION */
 148 static int next_poweroftwo(int value)
 149 {
 150     int power2;
 151 
 152     for (power2 = 1; value; value >>=1, power2 <<=1) /* empty */;
 153 
 154     return power2;
 155 }
 156 
 157 
 158 static int test_next_poweroftwo(int value)
 159 {
 160     int out;
 161     int power2 = next_poweroftwo (value);
 162 
 163 #ifdef DEBUG
 164     printf ("test_next_poweroftwo(): value:%d expect:%d\n",
 165             value, power2);
 166 #endif
 167 
 168     if (power2 == (out = opal_next_poweroftwo (value))) {
 169         test_success();
 170         return 1;
 171     } else {
 172         char * msg;
 173         opal_asprintf(&msg, "Mismatch for power-of-two: value:%d, expected:%d got:%d\n",
 174                  value, power2, out);
 175         test_failure(msg);
 176         free(msg);
 177     }
 178     return 0;
 179 }
 180 
 181 
 182 
 183 /* REFERENCE FUNCTION */
 184 static int next_poweroftwo_inclusive(int value)
 185 {
 186     int power2 = 1;
 187 
 188     while ( power2 < value )
 189         power2 <<= 1;
 190 
 191     return power2;
 192 }
 193 
 194 static int test_next_poweroftwo_inclusive(int value)
 195 {
 196     int out;
 197     int power2 = next_poweroftwo_inclusive (value);
 198 
 199 #ifdef DEBUG
 200     printf ("test_next_poweroftwo(): value:%d expect:%d\n",
 201             value, power2);
 202 #endif
 203 
 204     if (power2 == (out = opal_next_poweroftwo_inclusive (value))) {
 205         test_success();
 206         return 1;
 207     } else {
 208         char * msg;
 209         opal_asprintf(&msg, "Mismatch for power-of-two-inclusive: value:%d, expected:%d got:%d\n",
 210                  value, power2, out);
 211         test_failure(msg);
 212         free(msg);
 213     }
 214 
 215     return 0;
 216 }
 217 
 218 
 219 
 220 

/* [<][>][^][v][top][bottom][index][help] */