-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathconvert.c
55 lines (51 loc) · 1.33 KB
/
convert.c
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
#include "balrt.h"
#include "balrt_inline.h"
TaggedPtr _bal_convert_to_float(TaggedPtr tp) {
double d;
switch (getTag(tp) & UT_MASK) {
case TAG_INT:
d = taggedToInt(tp);
break;
case TAG_DECIMAL:
d = _bal_decimal_to_float(tp);
break;
default:
return tp;
}
return floatToTagged(d);
}
TaggedWithOverflow _bal_convert_to_int(TaggedPtr tp) {
IntWithOverflow intRes;
TaggedWithOverflow res;
switch (getTag(tp) & UT_MASK) {
case TAG_FLOAT:
intRes = _bal_float_to_int(taggedToFloat(tp));
break;
case TAG_DECIMAL:
intRes = _bal_decimal_to_int(tp);
break;
default:
res.overflow = false;
res.ptr = tp;
return res;
}
res.overflow = intRes.overflow;
if (!res.overflow) {
res.ptr = intToTagged(intRes.value);
}
return res;
}
TaggedPtrPanicCode _bal_convert_to_decimal(TaggedPtr tp) {
TaggedPtrPanicCode res;
switch (getTag(tp) & UT_MASK) {
case TAG_INT:
res.ptr = _bal_decimal_from_int(taggedToInt(tp));
break;
case TAG_FLOAT:
return _bal_decimal_from_float(taggedToFloat(tp));
default:
res.ptr = tp;
}
res.panicCode = 0;
return res;
}