File: opal_bit_ops.c

package info (click to toggle)
openmpi 5.0.8-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 201,684 kB
  • sloc: ansic: 613,078; makefile: 42,353; sh: 11,194; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,179; python: 1,859; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (220 lines) | stat: -rw-r--r-- 5,078 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2005 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2011 High Performance Computing Center Stuttgart,
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2013 Cisco Systems, Inc.  All rights reserved.
 * Copyright (c) 2018      Amazon.com, Inc. or its affiliates.  All Rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#include "opal_config.h"

#include <stdio.h>
#include <string.h>

#include "support.h"
#include "opal/util/bit_ops.h"
#include "opal/util/output.h"
#include "opal/util/printf.h"

/*
#define DEBUG
*/

static int test_hibit(int value, int start);
static int test_cube_dim(int value);
static int test_next_poweroftwo(int value);
static int test_next_poweroftwo_inclusive(int value);

int main(int argc, char* argv[])
{
    int i;
    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 */};
    test_init("opal_bit_ops()");

#ifdef DEBUG
    printf ("Test usage: ./opal_bit_ops [VALUES]\n");
#endif

    if (1 < argc) {
        for (i = 1; i < argc; i++) {
            int value;
            value = atoi (argv[i]);
            printf ("Testing %d. argument test_next_poweroftwo(%d): %s\n",
                    i, value, test_next_poweroftwo(value) ? "correct" : "wrong");
        }
    }

    for (i = 0; i < (int)(sizeof(vals)/sizeof(vals[0])); i++) {
        test_hibit (vals[i], 8 * sizeof(int) -2);
        test_hibit (vals[i], 3);
        test_cube_dim (vals[i]);
        test_next_poweroftwo (vals[i]);
        test_next_poweroftwo_inclusive (vals[i]);
    }

    /* All done */
    return test_finalize();
}


/* REFERENCE FUNCTION */
static int hibit(int value, int start)
{
    unsigned int mask;

    --start;
    mask = 1 << start;

    for (; start >= 0; --start, mask >>= 1) {
        if (value & mask) {
            break;
        }
    }

    return start;
}

static int test_hibit(int value, int start)
{
    int out;
    int bit = hibit (value, start);

#ifdef DEBUG
    printf ("test_hibit(): value:%d expect:%d\n",
            value, bit);
#endif

    if (bit == (out = opal_hibit (value, start))) {
        test_success();
        return 1;
    } else {
        char * msg;
        opal_asprintf(&msg, "Mismatch for hibit (w/ start:%d): value:%d, expected:%d got:%d\n",
                 start, value, bit, out);
        test_failure(msg);
        free(msg);
    }
    return 0;
}


/* REFERENCE FUNCTION */
static int cube_dim(int value)
{
    int dim, size;

    for (dim = 0, size = 1; size < value; ++dim, size <<= 1);

    return dim;
}

static int test_cube_dim(int value)
{
    int out;
    int dim = cube_dim (value);

#ifdef DEBUG
    printf ("test_cube_dim(): value:%d expect:%d\n",
            value, dim);
#endif

    if (dim == (out = opal_cube_dim (value))) {
        test_success();
        return 1;
    } else {
        char * msg;
        opal_asprintf(&msg, "Mismatch for cube_dim: value:%d, expected:%d got:%d\n",
                 value, dim, out);
        test_failure(msg);
        free(msg);
    }
    return 0;
}


/* REFERENCE FUNCTION */
static int next_poweroftwo(int value)
{
    int power2;

    for (power2 = 1; value; value >>=1, power2 <<=1) /* empty */;

    return power2;
}


static int test_next_poweroftwo(int value)
{
    int out;
    int power2 = next_poweroftwo (value);

#ifdef DEBUG
    printf ("test_next_poweroftwo(): value:%d expect:%d\n",
            value, power2);
#endif

    if (power2 == (out = opal_next_poweroftwo (value))) {
        test_success();
        return 1;
    } else {
        char * msg;
        opal_asprintf(&msg, "Mismatch for power-of-two: value:%d, expected:%d got:%d\n",
                 value, power2, out);
        test_failure(msg);
        free(msg);
    }
    return 0;
}



/* REFERENCE FUNCTION */
static int next_poweroftwo_inclusive(int value)
{
    int power2 = 1;

    while ( power2 < value )
        power2 <<= 1;

    return power2;
}

static int test_next_poweroftwo_inclusive(int value)
{
    int out;
    int power2 = next_poweroftwo_inclusive (value);

#ifdef DEBUG
    printf ("test_next_poweroftwo(): value:%d expect:%d\n",
            value, power2);
#endif

    if (power2 == (out = opal_next_poweroftwo_inclusive (value))) {
        test_success();
        return 1;
    } else {
        char * msg;
        opal_asprintf(&msg, "Mismatch for power-of-two-inclusive: value:%d, expected:%d got:%d\n",
                 value, power2, out);
        test_failure(msg);
        free(msg);
    }

    return 0;
}