Skip to content

Commit aad14df

Browse files
committed
Switch to std::round() and a common routine for real to uint64_t
1 parent eff75f8 commit aad14df

File tree

9 files changed

+27
-79
lines changed

9 files changed

+27
-79
lines changed

eval_tree.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,8 +1727,7 @@ NetECReal* NetESFunc::evaluate_itor_(const NetExpr*arg_) const
17271727
return new NetECReal(verireal(0.0));
17281728
}
17291729

1730-
if (arg >= 0.0) arg = floor(arg + 0.5);
1731-
else arg = ceil(arg - 0.5);
1730+
arg = std::round(arg);
17321731

17331732
return new NetECReal(verireal(arg));
17341733
}

verinum.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ using namespace std;
3737
*/
3838
extern "C" long int lround(double x)
3939
{
40-
if (x >= 0.0)
41-
return (long)floor(x+0.5);
42-
else
43-
return (long)ceil(x-0.5);
40+
return (long)std::round(x);
4441
}
4542
#endif
4643

verireal.cc

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999-2021 Stephen Williams ([email protected])
2+
* Copyright (c) 1999-2025 Stephen Williams ([email protected])
33
*
44
* This source code is free software; you can redistribute it
55
* and/or modify it in source code form under the terms of the GNU
@@ -71,15 +71,8 @@ long verireal::as_long() const
7171
double out = value_;
7272
double outf;
7373

74-
if (out >= 0.0) {
75-
outf = floor(out);
76-
if (out >= (outf + 0.5))
77-
outf += 1.0;
78-
} else {
79-
outf = ceil(out);
80-
if (out <= (outf - 0.5))
81-
outf -= 1.0;
82-
}
74+
outf = std::round(out);
75+
8376
return (long) outf;
8477
}
8578

@@ -88,15 +81,8 @@ int64_t verireal::as_long64(int shift) const
8881
double out = value_ * pow(10.0,shift);
8982
double outf;
9083

91-
if (out >= 0.0) {
92-
outf = floor(out);
93-
if (out >= (outf + 0.5))
94-
outf += 1.0;
95-
} else {
96-
outf = ceil(out);
97-
if (out <= (outf - 0.5))
98-
outf -= 1.0;
99-
}
84+
outf = std::round(out);
85+
10086
return (int64_t) outf;
10187
}
10288

vvp/config.h.in

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,7 @@ typedef unsigned long vvp_time64_t;
145145
*/
146146
inline long int lround(double x)
147147
{
148-
if (x >= 0.0)
149-
return (long)floor(x+0.5);
150-
else
151-
return (long)ceil(x-0.5);
148+
return (long)std::round(x);
152149
}
153150
#endif /* HAVE_LROUND */
154151

@@ -160,10 +157,7 @@ inline long int lround(double x)
160157
*/
161158
inline int64_t i64round(double x)
162159
{
163-
if (x >= 0.0)
164-
return (int64_t)floor(x+0.5);
165-
else
166-
return (int64_t)ceil(x-0.5);
160+
return (int64_t)std::round(x);
167161
}
168162
#else /* HAVE_LLROUND */
169163
# define i64round llround

vvp/vpi_callback.cc

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -949,23 +949,6 @@ void vvp_signal_value::get_signal_value(struct t_vpi_value*vp)
949949
}
950950
}
951951

952-
static double vlg_round(double rval)
953-
{
954-
if (rval >= 0.0) {
955-
return floor(rval + 0.5);
956-
} else {
957-
return ceil(rval - 0.5);
958-
}
959-
}
960-
961-
static uint64_t vlg_round_to_u64(double rval)
962-
{
963-
// Directly casting a negative double to an unsigned integer types is
964-
// undefined behavior and behaves differently on different architectures.
965-
// Cast to signed integer first to get the behavior we want.
966-
return static_cast<uint64_t>(static_cast<int64_t>(vlg_round(rval)));
967-
}
968-
969952
static void real_signal_value(struct t_vpi_value*vp, double rval)
970953
{
971954
static const size_t RBUF_SIZE = 64 + 1;
@@ -984,7 +967,7 @@ static void real_signal_value(struct t_vpi_value*vp, double rval)
984967
if (rval != rval || (rval && (rval == 0.5*rval))) {
985968
rval = 0.0;
986969
} else {
987-
rval = vlg_round(rval);
970+
rval = std::round(rval);
988971
}
989972
vp->value.integer = (PLI_INT32)rval;
990973
break;
@@ -993,7 +976,7 @@ static void real_signal_value(struct t_vpi_value*vp, double rval)
993976
if (std::isnan(rval))
994977
snprintf(rbuf, RBUF_SIZE, "%s", "nan");
995978
else
996-
snprintf(rbuf, RBUF_SIZE, "%0.0f", vlg_round(rval));
979+
snprintf(rbuf, RBUF_SIZE, "%0.0f", std::round(rval));
997980
vp->value.str = rbuf;
998981
break;
999982

vvp/vpi_priv.cc

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -880,14 +880,7 @@ static PLI_INT32 get_real_as_int(double real)
880880
return 0;
881881
}
882882

883-
/* Round away from zero. */
884-
if (real >= 0.0) {
885-
rtn = floor(real);
886-
if (real >= (rtn + 0.5)) rtn += 1.0;
887-
} else {
888-
rtn = ceil(real);
889-
if (real <= (rtn - 0.5)) rtn -= 1.0;
890-
}
883+
rtn = std::round(real);
891884

892885
return (PLI_INT32) rtn;
893886
}

vvp/vpi_priv.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,4 +1157,12 @@ extern int vpip_delay_selection;
11571157
/* A flag to disable output to MCD bit 0. */
11581158
extern bool vpip_mcd0_disable;
11591159

1160+
inline uint64_t vlg_round_to_u64(double rval)
1161+
{
1162+
// Directly casting a negative double to an unsigned integer types is
1163+
// undefined behavior and behaves differently on different architectures.
1164+
// Cast to signed integer first to get the behavior we want.
1165+
return static_cast<uint64_t>(static_cast<int64_t>(std::round(rval)));
1166+
}
1167+
11601168
#endif /* IVL_vpi_priv_H */

vvp/vpi_vthr_vector.cc

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,6 @@ static int vthr_word_get(int code, vpiHandle ref)
8787
}
8888
}
8989

90-
static double vlg_round(double rval)
91-
{
92-
if (rval >= 0.0) {
93-
return floor(rval + 0.5);
94-
} else {
95-
return ceil(rval - 0.5);
96-
}
97-
}
98-
9990
static void vthr_real_get_value(vpiHandle ref, s_vpi_value*vp)
10091
{
10192
const __vpiVThrWord*obj = dynamic_cast<__vpiVThrWord*>(ref);
@@ -127,7 +118,7 @@ static void vthr_real_get_value(vpiHandle ref, s_vpi_value*vp)
127118
if (val != val || (val && (val == 0.5*val))) {
128119
val = 0.0;
129120
} else {
130-
val = vlg_round(val);
121+
val = std::round(val);
131122
}
132123
vp->value.integer = (PLI_INT32)val;
133124
break;
@@ -136,30 +127,30 @@ static void vthr_real_get_value(vpiHandle ref, s_vpi_value*vp)
136127
if (std::isnan(val))
137128
snprintf(rbuf, RBUF_USE_SIZE, "%s", "nan");
138129
else
139-
snprintf(rbuf, RBUF_USE_SIZE, "%0.0f", vlg_round(val));
130+
snprintf(rbuf, RBUF_USE_SIZE, "%0.0f", std::round(val));
140131
vp->value.str = rbuf;
141132
break;
142133

143134
case vpiOctStrVal:
144-
snprintf(rbuf, RBUF_USE_SIZE, "%" PRIo64, (uint64_t)vlg_round(val));
135+
snprintf(rbuf, RBUF_USE_SIZE, "%" PRIo64, vlg_round_to_u64(val));
145136
vp->value.str = rbuf;
146137
break;
147138

148139
case vpiHexStrVal:
149-
snprintf(rbuf, RBUF_USE_SIZE, "%" PRIx64, (uint64_t)vlg_round(val));
140+
snprintf(rbuf, RBUF_USE_SIZE, "%" PRIx64, vlg_round_to_u64(val));
150141
vp->value.str = rbuf;
151142
break;
152143

153144
case vpiBinStrVal: {
154-
uint64_t vali = (uint64_t)vlg_round(val);
145+
uint64_t vali = vlg_round_to_u64(val);
155146
unsigned len = 0;
156147

157148
while (vali > 0) {
158149
len += 1;
159150
vali /= 2;
160151
}
161152

162-
vali = (uint64_t)vlg_round(val);
153+
vali = vlg_round_to_u64(val);
163154
for (unsigned idx = 0 ; idx < len ; idx += 1) {
164155
rbuf[len-idx-1] = (vali & 1)? '1' : '0';
165156
vali /= 2;

vvp/vthread.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2380,10 +2380,7 @@ bool of_CVT_SR(vthread_t thr, vvp_code_t cp)
23802380
bool of_CVT_UR(vthread_t thr, vvp_code_t cp)
23812381
{
23822382
double r = thr->pop_real();
2383-
if (r >= 0.0)
2384-
thr->words[cp->bit_idx[0]].w_uint = (uint64_t)floor(r+0.5);
2385-
else
2386-
thr->words[cp->bit_idx[0]].w_uint = (uint64_t)ceil(r-0.5);
2383+
thr->words[cp->bit_idx[0]].w_uint = vlg_round_to_u64(r);
23872384

23882385
return true;
23892386
}

0 commit comments

Comments
 (0)