summaryrefslogtreecommitdiffstats
path: root/userspace/units/posix-bitops/posix-bitops.c
blob: c808e77779b08d20d6bbc7e092bf857ca05dd20f (plain) (blame)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
 * Copyright (c) 2018, NVIDIA CORPORATION.  All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <stdlib.h>

#include <unit/io.h>
#include <unit/unit.h>

#include <nvgpu/bitops.h>

#define NUM_WORDS 4

static unsigned long single_ulong_maps[] = {
	0UL,
	~0UL,
	0xff00ff00UL,
	0x00ff00ffUL,
	0xa5a5a5a5UL,
	0x0000ffffUL,
	0xffff0000UL,
	0x1UL,
	0x80000000UL,
	BIT(16),
};

/*
 * Can't fail - just some info prints.
 */
static int test_bitmap_info(struct unit_module *m, struct gk20a *g, void *args)
{
	unit_info(m, "sizeof(unsigned long) = %zu\n", sizeof(unsigned long));
	unit_info(m, "BITS_PER_LONG         = %lu\n", BITS_PER_LONG);

	return UNIT_SUCCESS;
}

static int test_ffs(struct unit_module *m, struct gk20a *g, void *args)
{
#define CHECK_FFS_WORD(w, answer)					\
	do {								\
		unsigned long ret = ffs(w);				\
									\
		if (ret != (answer))					\
			unit_return_fail(m,				\
					 "ffs(0x%016lx) = %lu "		\
					 "[expected %lu]\n",		\
					 w, ret, answer);		\
	} while (0)

	unsigned long i;

	CHECK_FFS_WORD(single_ulong_maps[0], BITS_PER_LONG - 1UL);
	CHECK_FFS_WORD(single_ulong_maps[1], 0UL);
	CHECK_FFS_WORD(single_ulong_maps[2], 8UL);
	CHECK_FFS_WORD(single_ulong_maps[3], 0UL);
	CHECK_FFS_WORD(single_ulong_maps[4], 0UL);
	CHECK_FFS_WORD(single_ulong_maps[5], 0UL);
	CHECK_FFS_WORD(single_ulong_maps[6], 16UL);
	CHECK_FFS_WORD(single_ulong_maps[7], 0UL);
	CHECK_FFS_WORD(single_ulong_maps[8], 31UL);
	CHECK_FFS_WORD(single_ulong_maps[9], 16UL);

#undef CHECK_FFS_WORD

	/*
	 * Also just test every bit to make sure we definitely cover all
	 * possible return values of the function.
	 */
	for (i = 0; i < BITS_PER_LONG; i++) {
		if (ffs(BIT(i)) != i)
			unit_return_fail(m, "ffs(1 << %lu) != %lu [%lu]!\n",
					 i, i, ffs(BIT(i)));
	}

	return UNIT_SUCCESS;
}

static int test_fls(struct unit_module *m, struct gk20a *g, void *args)
{
#define CHECK_FLS_WORD(w, answer)					\
	do {								\
		unsigned long ret = fls(w);				\
									\
		if (ret != (answer))					\
			unit_return_fail(m,				\
					 "fls(0x%016lx) = %lu "		\
					 "[expected = %lu]\n",		\
					 w, ret, answer);		\
	} while (0)

	unsigned long i;

	CHECK_FLS_WORD(single_ulong_maps[0], 0UL);
	CHECK_FLS_WORD(single_ulong_maps[1], BITS_PER_LONG - 1UL);
	CHECK_FLS_WORD(single_ulong_maps[2], 31UL);
	CHECK_FLS_WORD(single_ulong_maps[3], 23UL);
	CHECK_FLS_WORD(single_ulong_maps[4], 31UL);
	CHECK_FLS_WORD(single_ulong_maps[5], 15UL);
	CHECK_FLS_WORD(single_ulong_maps[6], 31UL);
	CHECK_FLS_WORD(single_ulong_maps[7], 0UL);
	CHECK_FLS_WORD(single_ulong_maps[8], 31UL);
	CHECK_FLS_WORD(single_ulong_maps[9], 16UL);

#undef CHECK_FLS_WORD

	for (i = 0; i < BITS_PER_LONG; i++) {
		if (fls(BIT(i)) != i)
			unit_return_fail(m, "fls(1 << %lu) != %lu! [%lu]\n",
					 i, i, fls(BIT(i)));
	}

	return UNIT_SUCCESS;
}

static int test_ffz(struct unit_module *m, struct gk20a *g, void *args)
{
	unsigned long i;

	/*
	 * Since ffz(w) is implemented as ffs(~w) this does less extensive
	 * testing; but it should still cover every line of ffs().
	 */

	for (i = 0; i < BITS_PER_LONG; i++) {
		if (ffz(~BIT(i)) != i)
			unit_return_fail(m, "ffz(~(1 << %lu)) != %lu! [%lu]\n",
					 i, i, ffz(BIT(i)));
	}

	return UNIT_SUCCESS;
}

struct test_find_bit_args {
	bool find_zeros;
};

static struct test_find_bit_args first_bit_args = {
	.find_zeros = false
};

static struct test_find_bit_args first_zero_args = {
	.find_zeros = true
};

static int test_find_first_bit(struct unit_module *m,
			       struct gk20a *g, void *__args)
{
	struct test_find_bit_args *args = __args;
	unsigned long words[NUM_WORDS];
	unsigned long word_idx, bit_idx;
	unsigned long (*finder_function)(const unsigned long *, unsigned long);
	unsigned long result;

	if (args->find_zeros)
		finder_function = find_first_zero_bit;
	else
		finder_function = find_first_bit;

	/*
	 * First test: verify that the size parameter works. We only need the
	 * first word for this.
	 */
	words[0] = ~0xffffUL;
	if (args->find_zeros)
		words[0] = 0xffff;

	if (finder_function(words, 8UL) != 8UL)
		unit_return_fail(m,
				 "find_first_%s(0x%lx, 8) -> %lu [WRONG]\n",
				 args->find_zeros ? "zero_bit" : "bit",
				 words[0], finder_function(words, 8UL));

	if (finder_function(words, 20UL) != 16UL)
		unit_return_fail(m,
				 "find_first_%s(0x%lx, 16) -> %lu [WRONG]\n",
				 args->find_zeros ? "zero_bit" : "bit",
				 words[0], finder_function(words, 20UL));

	/*
	 * Now make sure that for full/empty bitmap find_next_*() returns
	 * the size parameter.
	 */
	memset(words, args->find_zeros ? 0xff : 0x00, sizeof(words));
	result = finder_function(words, NUM_WORDS * BITS_PER_LONG);
	if (result != NUM_WORDS * BITS_PER_LONG)
		unit_return_fail(m, "find_first_%s() failed with empty map\n",
				 args->find_zeros ? "zero_bit" : "bit");

	/*
	 * Third test: set (or zero) the entire bitmap and incrementally clear
	 * bits. Check that we are correct even with multiple words.
	 */

	memset(words, args->find_zeros ? 0x00 : 0xff, sizeof(words));
	for (word_idx = 0; word_idx < NUM_WORDS; word_idx++) {
		for (bit_idx = 0; bit_idx < BITS_PER_LONG; bit_idx++) {
			unsigned long check =
				(word_idx * BITS_PER_LONG) + bit_idx;
			unsigned long answer =
				finder_function(words,
						NUM_WORDS * BITS_PER_LONG);

			if (answer != check)
				unit_return_fail(m,
						 "find_first_%s loop: "
						 "word_idx = %lu bit_idx = %lu "
						 "-> %lu [WRONG]\n",
						 args->find_zeros ? "zero_bit" : "bit",
						 word_idx, bit_idx, answer);

			/*
			 * Now set/clear this bit in preparation for the next
			 * test.
			 */
			if (args->find_zeros)
				words[word_idx] |= BIT(bit_idx);
			else
				words[word_idx] &= ~BIT(bit_idx);
		}
	}

	return UNIT_SUCCESS;
}

/*
 * Note: the find_first_bit() test also effectively tests the underlying
 * find_next_bit() code since find_first_bit() is just find_next_bit()
 * with a 0 start.
 */

static int test_find_next_bit(struct unit_module *m,
			      struct gk20a *g, void *__args)
{
	unsigned long words[NUM_WORDS];
	unsigned long i, result;

	/*
	 * Fully unset list. Should always return size.
	 */
	memset(words, 0x00, sizeof(words));
	for (i = 0; i < NUM_WORDS * BITS_PER_LONG; i++) {
		result = find_next_bit(words, NUM_WORDS * BITS_PER_LONG, i);

		if (result != NUM_WORDS * BITS_PER_LONG)
			unit_return_fail(m, "Fail: empty map (%lu)\n", i);
	}

	/*
	 * Use a fully set list but increment the offset.
	 */
	memset(words, 0xff, sizeof(words));
	for (i = 0; i < NUM_WORDS * BITS_PER_LONG; i++) {
		unsigned long first =
			find_next_bit(words, NUM_WORDS * BITS_PER_LONG, i);

		if (first != i)
			unit_return_fail(m,
					 "Fail: first = %lu; should be %lu\n",
					 first, i);
	}

	/*
	 * Start > n should return n.
	 */
#define TEST_START_GREATER_THAN_N(m, map, n, start)			\
	do {								\
		if (find_next_bit(map, n, start) != n)			\
			unit_return_fail(m,				\
					 "Start not greater than N ?? "	\
					 "start=%lu, N=%lu\n",		\
					 start, n);			\
	} while (0)

	TEST_START_GREATER_THAN_N(m, words, BITS_PER_LONG, BITS_PER_LONG + 1);
	TEST_START_GREATER_THAN_N(m, words, 32UL, 64UL);
	TEST_START_GREATER_THAN_N(m, words,
				  BITS_PER_LONG * 2, (BITS_PER_LONG * 2) + 1);
	TEST_START_GREATER_THAN_N(m, words, 0UL, 1UL);
	TEST_START_GREATER_THAN_N(m, words, 0UL, BITS_PER_LONG * 2);
	TEST_START_GREATER_THAN_N(m, words, 0UL, BITS_PER_LONG * NUM_WORDS + 1);

#undef TEST_START_GREATER_THAN_N

	return UNIT_SUCCESS;
}

#define TEST_BITMAP_SIZE (BITS_PER_LONG * 4)
/*
 * 32/64 bit invarient.
 */
static DECLARE_BITMAP(bmap_all_zeros, TEST_BITMAP_SIZE) =
{
	0x0UL, 0x0UL, 0x0UL, 0x0UL
};
static DECLARE_BITMAP(bmap_all_ones, TEST_BITMAP_SIZE) =
{
	~0x0UL, ~0x0UL, ~0x0UL, ~0x0UL
};

static int test_find_zero_area(struct unit_module *m,
			       struct gk20a *g, void *unused)
{
#define FAIL_MSG "Fail: bmap-test='%s' (i=%lu)\n"
#define FAIL_MSG_EX "Fail: bmap-test='%s' (i=%lu, j=%lu)\n"
	unsigned long i, j, result;
	unsigned long words[NUM_WORDS];

	for (i = 0; i < TEST_BITMAP_SIZE; i++) {
		result = bitmap_find_next_zero_area_off(bmap_all_zeros,
							TEST_BITMAP_SIZE,
							i,
							TEST_BITMAP_SIZE - i,
							0, 0);
		if (result != i)
			unit_return_fail(m, FAIL_MSG,
					 "all_zeros: alloc-to-end", i);

		result = bitmap_find_next_zero_area_off(bmap_all_zeros,
							TEST_BITMAP_SIZE,
							i,
							1,
							0, 0);
		if (result != i)
			unit_return_fail(m, FAIL_MSG,
					 "all_zeros: alloc-one-bit", i);

		result = bitmap_find_next_zero_area_off(bmap_all_zeros,
							TEST_BITMAP_SIZE,
							0,
							TEST_BITMAP_SIZE - i,
							0, 0);
		if (result != 0)
			unit_return_fail(m, FAIL_MSG,
					 "all_zeros: alloc-i-bits-at-0", i);
	}

	/*
	 * For the all ones bit map not a single alloc should succeed. We can
	 * just iterate through them all and make sure they all fail.
	 */
	for (i = 0; i < TEST_BITMAP_SIZE; i++) {
		for (j = 0; j < (TEST_BITMAP_SIZE - i); j++) {
			result = bitmap_find_next_zero_area_off(bmap_all_ones,
							TEST_BITMAP_SIZE,
							i,
							j,
							0, 0);
			if (result != TEST_BITMAP_SIZE)
				unit_return_fail(m, FAIL_MSG_EX,
						 "all_ones: failed", i, j);
		}
	}

	/*
	 * Alternating nibbles (4 bits). Make sure we don't start searching from
	 * too high in the bitmap since that will cause failures that are
	 * actually valid. This keeps the logic in the below loop a little more
	 * simple.
	 */
	memset(words, 0x0f, sizeof(words));
	for (i = 0; i < ((NUM_WORDS * BITS_PER_LONG) - 8); i++) {
		for (j = 0; j < ((NUM_WORDS * BITS_PER_LONG) - i - 8); j++) {
			result = bitmap_find_next_zero_area_off(words,
						NUM_WORDS * BITS_PER_LONG,
						i,
						j,
						0, 0);

			/*
			 * Should only return a valid result when j < 4 (since
			 * the map consists of 4 ones, then 4 zeros,
			 * alternating.
			 */
			if (j <= 4 && result >= (NUM_WORDS * BITS_PER_LONG))
				unit_return_fail(m, FAIL_MSG_EX,
						 "alternating-nibbles: failed",
						 i, j);
			if (j > 4 && result != (NUM_WORDS * BITS_PER_LONG))
				unit_return_fail(m, FAIL_MSG_EX,
						 "alternating-nibbles: failed",
						 i, j);

			result = bitmap_find_next_zero_area_off(words,
						NUM_WORDS * BITS_PER_LONG,
						i,
						(j % 4) + 1,
						0x3, 0);
			if (result % 8 != 4)
				unit_return_fail(m, FAIL_MSG_EX,
						 "basic-align_mask: failed",
						 i, j);


			result = bitmap_find_next_zero_area_off(words,
						NUM_WORDS * BITS_PER_LONG,
						i,
						(j % 2) + 1,
						0x7, 2);

			if (result % 8 != 6)
				unit_return_fail(m, FAIL_MSG_EX,
						 "basic-align_offset: failed",
						 i, j);
		}
	}

#undef FAIL_MSG
#undef FAIL_MSG_EX
	return UNIT_SUCCESS;
}

struct test_setclear_args {
	bool clear;
};

static struct test_setclear_args set_args = {
	.clear = false,
};

static struct test_setclear_args clear_args = {
	.clear = true,
};

static void __print_bitmap(unsigned long *map, unsigned long length)
{
	unsigned int idx, bidx;

	for (idx = 0; idx < (length / BITS_PER_LONG); idx++) {
		printf("  ");
		for (bidx = 0; bidx < BITS_PER_LONG; bidx++) {
			printf("%c", (map[idx] & BIT(bidx)) ? '1' : '0');
			if (bidx % 4 == 3)
				printf(" ");
		}
		printf("\n");
	}

	printf("\n");
}

/*
 * Verify the bits from i to i + len are set and no others are set. 'size' is in
 * bits (not words).
 */
static bool verify_set_buf(unsigned long *words, unsigned long size,
			   unsigned long i, unsigned long len,
			   bool invert)
{
	unsigned int idx, bidx;
	unsigned int bit, bit_value;
	unsigned int set_start, set_end;
	unsigned int set_value = invert ? 0 : 1;
	unsigned int clear_value = invert ? 1 : 0;

	set_start = i;
	set_end = i + len;

	for (idx = 0; idx < (size / BITS_PER_LONG); idx++) {
		for (bidx = 0; bidx < BITS_PER_LONG; bidx++) {
			bit = idx * BITS_PER_LONG + bidx;
			bit_value = !!(words[idx] & BIT(bidx));

			/*
			 * Bit inside set zone.
			 */
			if ((bit >= set_start && bit < set_end) &&
			    bit_value != set_value)
				return false;

			/*
			 * Bit outside set zone.
			 */
			if ((bit < set_start || bit >= set_end) &&
			    bit_value != clear_value)
				return false;
		}
	}

	return true;
}

static int test_single_bitops(struct unit_module *m,
			      struct gk20a *g, void *__args)
{
	unsigned long words[NUM_WORDS];
	unsigned int i;

	/*
	 * First set all the bits and make sure the words are set.
	 */
	for (i = 0; i < NUM_WORDS * BITS_PER_LONG; i++)
		set_bit(i, words);

	if (!verify_set_buf(words, NUM_WORDS * BITS_PER_LONG,
			    0, NUM_WORDS * BITS_PER_LONG, false)) {
		__print_bitmap(words, NUM_WORDS * BITS_PER_LONG);
		unit_return_fail(m, "set_bit: Failed to set a bit!\n");
	}

	/*
	 * Now make sure the test_bit works for set bits.
	 */
	for (i = 0; i < NUM_WORDS * BITS_PER_LONG; i++)
		if (!test_bit(i, words))
			unit_return_fail(m, "test_bit: bit %d failed!\n", i);

	for (i = 0; i < NUM_WORDS * BITS_PER_LONG; i++)
		clear_bit(i, words);

	if (!verify_set_buf(words, NUM_WORDS * BITS_PER_LONG,
			    0, NUM_WORDS * BITS_PER_LONG, true)) {
		__print_bitmap(words, NUM_WORDS * BITS_PER_LONG);
		unit_return_fail(m, "clear_bit: Failed to set a bit!\n");
	}

	for (i = 0; i < NUM_WORDS * BITS_PER_LONG; i++)
		if (test_bit(i, words))
			unit_return_fail(m, "test_bit: bit %d failed!\n", i);

	return UNIT_SUCCESS;
}

static int test_bit_setclear(struct unit_module *m,
			     struct gk20a *g, void *__args)
{
	struct test_setclear_args *args = __args;
	void (*testfn)(int, volatile unsigned long *) =
		args->clear ? clear_bit : set_bit;
	unsigned long words[NUM_WORDS];
	unsigned int i;

	memset(words, args->clear ? 0xff : 0x0, sizeof(words));

	for (i = 0; i < NUM_WORDS * BITS_PER_LONG; i++)
		testfn(i, words);

	if (!verify_set_buf(words, NUM_WORDS * BITS_PER_LONG,
			    0, NUM_WORDS * BITS_PER_LONG, args->clear)) {
		__print_bitmap(words, NUM_WORDS * BITS_PER_LONG);
		unit_return_fail(m, "%s_bit: Failed to %s a bit!\n",
				 args->clear ? "clear" : "set",
				 args->clear ? "clear" : "set");
	}

	return UNIT_SUCCESS;
}

static int test_test_and_setclear_bit(struct unit_module *m,
				      struct gk20a *g, void *__args)
{
	struct test_setclear_args *args = __args;
	bool (*testfn)(int, volatile unsigned long *) =
		args->clear ? test_and_clear_bit : test_and_set_bit;
	bool (*testfn_reset)(int, volatile unsigned long *) =
		args->clear ? test_and_set_bit : test_and_clear_bit;
	unsigned long words[NUM_WORDS];
	unsigned int i;

	memset(words, args->clear ? 0xff : 0x0, sizeof(words));

	/*
	 * First we will set/clear the bits. Then we will clear/set the bits
	 * (i.e do the opposite of this loop).
	 */
	for (i = 0; i < NUM_WORDS * BITS_PER_LONG; i++) {
		bool status = testfn(i, words);

		if (status != args->clear) {
			__print_bitmap(words, NUM_WORDS * BITS_PER_LONG);
			unit_return_fail(m, "test_and_%s_bit: Failed at %d\n",
					 args->clear ? "clear" : "set", i);
		}
	}

	for (i = 0; i < NUM_WORDS * BITS_PER_LONG; i++) {
		bool status = testfn_reset(i, words);

		if (status == args->clear) {
			__print_bitmap(words, NUM_WORDS * BITS_PER_LONG);
			unit_return_fail(m, "test_and_%s_bit: Failed at %d\n",
					 args->clear ? "set" : "clear", i);
		}
	}

	/* The bitmap should be the same as we started with. */
	if (!verify_set_buf(words, NUM_WORDS * BITS_PER_LONG,
			    0, NUM_WORDS * BITS_PER_LONG, !args->clear)) {
		__print_bitmap(words, NUM_WORDS * BITS_PER_LONG);
		unit_return_fail(m,
				 "test_and_%s_bit: Final bitmap is wrong!\n",
				 args->clear ? "set" : "clear");
	}

	return UNIT_SUCCESS;
}

static int test_bitmap_setclear(struct unit_module *m,
				struct gk20a *g, void *__args)
{
	struct test_setclear_args *args = __args;
	void (*testfn)(unsigned long *, unsigned int, int) =
		args->clear ? bitmap_clear : bitmap_set;
	unsigned long words[NUM_WORDS];
	unsigned long i, j;
	int set_char = args->clear ? 0xff : 0x0;

	/*
	 * Run through all combos of set/clear for a 4 word bitmap.
	 */
	for (i = 0; i < NUM_WORDS * BITS_PER_LONG; i++) {
		for (j = 0; j < (NUM_WORDS * BITS_PER_LONG) - i; j++) {
			/*
			 * Just make sure we start in a known state.
			 */
			memset(words, set_char, sizeof(words));

			testfn(words, i, j);

			if (!verify_set_buf(words, NUM_WORDS * BITS_PER_LONG,
					    i, j, args->clear)) {
				__print_bitmap(words, NUM_WORDS * BITS_PER_LONG);
				unit_return_fail(m,
						 "%s: fail at i,j = %lu,%lu\n",
						 args->clear ? "clear" : "set",
						 i, j);
			}
		}
	}

	return UNIT_SUCCESS;
}



struct unit_module_test posix_bitops_tests[] = {
	UNIT_TEST(info,                test_bitmap_info, NULL),
	UNIT_TEST(ffs,                 test_ffs, NULL),
	UNIT_TEST(fls,                 test_fls, NULL),
	UNIT_TEST(ffz,                 test_ffz, NULL),
	UNIT_TEST(find_first_bit,      test_find_first_bit, &first_bit_args),
	UNIT_TEST(find_first_zero_bit, test_find_first_bit, &first_zero_args),
	UNIT_TEST(find_next_bit,       test_find_next_bit, NULL),
	UNIT_TEST(find_zero_area,      test_find_zero_area, NULL),
	UNIT_TEST(single_bitops,       test_single_bitops, NULL),
	UNIT_TEST(bit_set,             test_bit_setclear, &set_args),
	UNIT_TEST(bit_clear,           test_bit_setclear, &clear_args),
	UNIT_TEST(test_and_set_bit,    test_test_and_setclear_bit, &set_args),
	UNIT_TEST(test_and_clear_bit,  test_test_and_setclear_bit, &clear_args),
	UNIT_TEST(bitmap_set,          test_bitmap_setclear, &set_args),
	UNIT_TEST(bitmap_clear,        test_bitmap_setclear, &clear_args),
};

UNIT_MODULE(posix_bitops, posix_bitops_tests, UNIT_PRIO_POSIX_TEST);