From d8580cf84d3040956bdf8d799b02ad060b144ed9 Mon Sep 17 00:00:00 2001 From: Richard Braun Date: Mon, 5 May 2014 21:49:58 +0200 Subject: kern/bitmap: add cmp/and/or/xor operations --- kern/bitmap.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'kern/bitmap.c') diff --git a/kern/bitmap.c b/kern/bitmap.c index c2a4cbf8..af19805f 100644 --- a/kern/bitmap.c +++ b/kern/bitmap.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Richard Braun. + * Copyright (c) 2013-2014 Richard Braun. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,6 +18,41 @@ #include #include #include +#include + +int +bitmap_cmp(const unsigned long *a, const unsigned long *b, int nr_bits) +{ + unsigned long last_a, last_b, mask; + int n, rv; + + n = BITMAP_LONGS(nr_bits) - 1; + + if (n != 0) { + rv = memcmp(a, b, n * sizeof(unsigned long)); + + if (rv != 0) + return rv; + + nr_bits -= n * LONG_BIT; + } + + last_a = a[n]; + last_b = b[n]; + + if (nr_bits != LONG_BIT) { + mask = (1UL << nr_bits) - 1; + last_a &= mask; + last_b &= mask; + } + + if (last_a == last_b) + return 0; + else if (last_a < last_b) + return -1; + else + return 1; +} static inline unsigned long bitmap_find_next_compute_complement(unsigned long word, int nr_bits) -- cgit v1.2.3