Rizin
unix-like reverse engineering framework and cli tools
double.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2015 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_reg.h>
5 #include <rz_util.h>
6 
7 #if __WINDOWS__
8 #define CASTLDBL (double)
9 #else
10 #define CASTLDBL
11 #endif
12 
13 // TODO: add support for 80bit floating point value
14 
15 // long double = 128 bit
17  RzRegSet *regset;
18  double vld = 0.0f;
19  int off;
20  double ret = 0.0f;
21  if (!reg || !item) {
22  return 0LL;
23  }
24  off = BITS2BYTES(item->offset);
25  regset = &reg->regset[item->arena];
26  switch (item->size) {
27  case 64:
28  if (regset->arena->size - off - 1 >= 0) {
29  memcpy(&vld, regset->arena->bytes + off, sizeof(double));
30  ret = vld;
31  }
32  break;
33  default:
34  eprintf("rz_reg_set_double: Bit size %d not supported\n", item->size);
35  return 0.0f;
36  }
37  return ret;
38 }
39 
41  ut8 *src;
42 
43  if (!item) {
44  eprintf("rz_reg_set_value: item is NULL\n");
45  return false;
46  }
47  switch (item->size) {
48  case 64:
49  // FIXME: endian
50  src = (ut8 *)&value;
51  break;
52  default:
53  eprintf("rz_reg_set_double: Bit size %d not supported\n", item->size);
54  return false;
55  }
56  if (reg->regset[item->arena].arena->size - BITS2BYTES(item->offset) - BITS2BYTES(item->size) >= 0) {
57  rz_mem_copybits(reg->regset[item->arena].arena->bytes +
58  BITS2BYTES(item->offset),
59  src, item->size);
60  return true;
61  }
62  eprintf("rz_reg_set_value: Cannot set %s to %lf\n", item->name, value);
63  return false;
64 }
65 
66 // long double = 80 bit
68  RzRegSet *regset;
69  long double vld = 0.0f;
70  int off;
71  long double ret = 0.0f;
72  if (!reg || !item) {
73  return 0LL;
74  }
75  off = BITS2BYTES(item->offset);
76  regset = &reg->regset[item->arena];
77  switch (item->size) {
78  case 80:
79  case 96:
80  case 128:
81  case 256:
82  if (regset->arena->size - off - 1 >= 0) {
83  memcpy(&vld, regset->arena->bytes + off, sizeof(long double));
84  ret = vld;
85  }
86  break;
87  default:
88  eprintf("rz_reg_get_longdouble: Bit size %d not supported\n", item->size);
89  return 0.0f;
90  }
91  return ret;
92 }
93 
94 RZ_API bool rz_reg_set_longdouble(RzReg *reg, RzRegItem *item, long double value) {
95  ut8 *src = NULL;
96 
97  if (!item) {
98  eprintf("rz_reg_set_value: item is NULL\n");
99  return false;
100  }
101  switch (item->size) {
102  case 80:
103  case 96:
104  case 128:
105  // FIXME: endian
106  src = (ut8 *)&value;
107  break;
108  default:
109  eprintf("rz_reg_set_longdouble: Bit size %d not supported\n", item->size);
110  return false;
111  }
112  if (reg->regset[item->arena].arena->size - BITS2BYTES(item->offset) - BITS2BYTES(item->size) >= 0) {
113  rz_mem_copybits(reg->regset[item->arena].arena->bytes +
114  BITS2BYTES(item->offset),
115  src, item->size);
116  return true;
117  }
118 
119  eprintf("rz_reg_set_value: Cannot set %s to %" LDBLFMT "\n", item->name, CASTLDBL value);
120  return false;
121 }
122 
123 /* floating point . deprecate maybe? */
125  // TODO
126  return 0.0f;
127 }
128 
130  return false;
131 }
lzma_index * src
Definition: index.h:567
static int value
Definition: cmd_api.c:93
#define RZ_API
#define NULL
Definition: cris-opc.c:27
#define CASTLDBL
Definition: double.c:10
RZ_API bool rz_reg_set_longdouble(RzReg *reg, RzRegItem *item, long double value)
Definition: double.c:94
RZ_API bool rz_reg_set_double(RzReg *reg, RzRegItem *item, double value)
Definition: double.c:40
RZ_API long double rz_reg_get_longdouble(RzReg *reg, RzRegItem *item)
Definition: double.c:67
RZ_API float rz_reg_get_float(RzReg *reg, RzRegItem *item)
Definition: double.c:124
RZ_API bool rz_reg_set_float(RzReg *reg, RzRegItem *item, float value)
Definition: double.c:129
RZ_API double rz_reg_get_double(RzReg *reg, RzRegItem *item)
Definition: double.c:16
#define reg(n)
uint8_t ut8
Definition: lh5801.h:11
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
int off
Definition: pal.c:13
#define eprintf(x, y...)
Definition: rlcc.c:7
RZ_API void rz_mem_copybits(ut8 *dst, const ut8 *src, int bits)
Definition: mem.c:79
#define LDBLFMT
Definition: rz_types.h:401
#define BITS2BYTES(x)
Definition: rz_types.h:280
ut8 * bytes
Definition: rz_reg.h:131
int size
in bits> 8,16,32,64 ... 128/256
Definition: rz_reg.h:120
int arena
In which arena is this reg living. Usually equals type.
Definition: rz_reg.h:127
int offset
Offset into register profile in bits.
Definition: rz_reg.h:121
char * name
Definition: rz_reg.h:118
RzRegArena * arena
Definition: rz_reg.h:136