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
|
/*
** Copyright (C) 2018 Martin Brain
**
** See the file LICENSE for licensing information.
*/
/*
** common.h
**
** Martin Brain
** martin.brain@cs.ox.ac.uk
** 05/08/14
**
** Commonly used utility functions.
**
*/
#include <assert.h>
#include <stdint.h>
#include "symfpu/utils/properties.h"
#ifndef SYMFPU_COMMON
#define SYMFPU_COMMON
namespace symfpu {
template <class T>
T previousPowerOfTwo (T x) {
assert(x > 1);
//PRECONDITION(x > 1);
T current = 1;
T next = current << 1;
while (next < x) {
current = next;
next <<= 1;
}
return current;
}
template <class T>
T leftmostBit (T x) {
assert(x > 1);
//PRECONDITION(x > 1);
T current = 1;
T next = current << 1;
while (next <= x) {
current = next;
next <<= 1;
}
return current;
}
// The number of bits required to represent a number
// == the position of the leading 0 + 1
// == ceil(log_2(value + 1))
template <class T>
T bitsToRepresent (const T value) {
T i = 0;
//unsigned T working = *((unsigned T)&value); // Implementation defined for signed types
T working = value;
while (working != 0) {
++i;
working >>= 1;
}
return i;
}
template <class T>
T positionOfLeadingOne (const T value) {
//PRECONDITION(value != 0);
assert(value != 0);
T i = 0;
//unsigned T working = *((unsigned T)&value); // Implementation defined for signed types
T working = value;
while (working != 0) {
++i;
working >>= 1;
}
return i - 1;
}
}
#endif
|