summaryrefslogtreecommitdiff
path: root/rts/linker/util.h
blob: 650e7f443a83f7efe3ecfeb57624c9401d6b6f48 (plain)
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
#ifndef RTS_LINKER_UTIL_H
#define RTS_LINKER_UTIL_H

#include <stdint.h>
#include <stdbool.h>
#include "LinkerInternals.h"

// Signed extend a number to a 32-bit int.
// Does the given signed integer fit into the given bit width?
static inline int32_t
sign_extend32(uint32_t bits, uint32_t x)
{
    return ((int32_t) (x << (32 - bits))) >> (32 - bits);
}

// Does the given signed integer fit into the given bit width?
static inline bool
is_int(uint32_t bits, int32_t x)
{
    return bits > 32 || (-(1 << (bits-1)) <= x
                         && x < (1 << (bits-1)));
}

static inline bool
is_int64(uint32_t bits, int64_t x) {
    return bits > 64 || (-((int64_t)1 << (bits-1)) <= x
                         && x < ((int64_t)1 << (bits-1)));
}

#endif //RTS_LINKER_UTIL_H