-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmallInteger.h
More file actions
85 lines (68 loc) · 1.93 KB
/
SmallInteger.h
File metadata and controls
85 lines (68 loc) · 1.93 KB
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
/*
* Copyright (c) 2020 Javier Pimas & LabWare
*
* This program and the accompanying materials are made available under
* the terms of the MIT license, see LICENSE file.
*
* SPDX-License-Identifier: MIT
*/
#ifndef _SMALL_INTEGER_H_
#define _SMALL_INTEGER_H_
#include "Util.h"
struct Object;
/**
* Class `SmallInteger` represents an immediate small integer, encoded
* in a pointer. It is signed and tagged with 1 in its least significant
* bit. It is always held as a pointer.
*/
struct SmallInteger
{
static const intptr_t SMALLINT_MIN = INTPTR_MIN >> 1;
static const intptr_t SMALLINT_MAX = INTPTR_MAX >> 1;
/**
* Cast back into an opaque Object type
**/
operator Object*() {return (Object*)(void*)this; }
/**
* Cast back into an opaque Object type
**/
Object* object() { return (Object*)this; }
/**
* Return reference to a SmallInteger object with
* given `intVal`
*/
static SmallInteger* from(intptr_t intVal)
{
ASSERT(SMALLINT_MIN <= intVal && intVal <= SMALLINT_MAX);
return (SmallInteger*)(((uintptr_t)intVal << 1) | 1);
}
/**
* Return a reference to SmallInteger object holding
* a given `ptrVal` pointer encoded as "small pointer"
*/
static SmallInteger* from(void* ptrVal)
{
ASSERT(!((uintptr_t)ptrVal & 1));
return (SmallInteger*)((uintptr_t)ptrVal | 1);
}
/**
* Assuming `this` encodes a SmallInteger, decode its
* (signed) integer value
*/
intptr_t asNative()
{
ASSERT(this->object()->isSmallInteger());
return (intptr_t)this >> 1;
}
/**
* Assuming `this` encodes a pointer stored as a SmallInteger,
* return the value of the original pointer
*/
template <typename T = void*>
T asPointer()
{
ASSERT(this->object()->isSmallInteger());
return (T)((intptr_t)this & (intptr_t)~1);
}
};
#endif /* _SMALL_INTEGER_H_ */