Skip to content

Commit 331178b

Browse files
authored
ast,cgen,rand: reduce interpolations when a method is not found in Table.find_method/2; merge rand.constants back to rand.v (#23660)
1 parent c9235b8 commit 331178b

5 files changed

Lines changed: 20 additions & 26 deletions

File tree

‎vlib/rand/constants/constants.v‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎vlib/rand/rand.v‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ module rand
66

77
import math.bits
88
import rand.config
9-
import rand.constants
109
import rand.wyrand
1110
import time
1211

@@ -165,13 +164,13 @@ pub fn (mut rng PRNG) i64() i64 {
165164
// int31 returns a positive pseudorandom 31-bit `int`.
166165
@[inline]
167166
pub fn (mut rng PRNG) int31() int {
168-
return int(rng.u32() & constants.u31_mask) // Set the 32nd bit to 0.
167+
return int(rng.u32() & u32(0x7FFFFFFF)) // Set the 32nd bit to 0.
169168
}
170169

171170
// int63 returns a positive pseudorandom 63-bit `i64`.
172171
@[inline]
173172
pub fn (mut rng PRNG) int63() i64 {
174-
return i64(rng.u64() & constants.u63_mask) // Set the 64th bit to 0.
173+
return i64(rng.u64() & u64(0x7FFFFFFFFFFFFFFF)) // Set the 64th bit to 0.
175174
}
176175

177176
// intn returns a pseudorandom `int` in range `[0, max)`.
@@ -221,11 +220,17 @@ pub fn (mut rng PRNG) i64_in_range(min i64, max i64) !i64 {
221220
return min + rng.i64n(max - min)!
222221
}
223222

223+
// smallest mantissa with exponent 0 (un normalized)
224+
const reciprocal_2_23rd = 1.0 / f64(u32(1) << 23)
225+
const reciprocal_2_52nd = 1.0 / f64(u64(1) << 52)
226+
const ieee754_mantissa_f32_mask = (u32(1) << 23) - 1 // 23 bits for f32
227+
const ieee754_mantissa_f64_mask = (u64(1) << 52) - 1 // 52 bits for f64
228+
224229
// f32 returns a pseudorandom `f32` value in range `[0, 1)`
225230
// using rng.u32() multiplied by an f64 constant.
226231
@[inline]
227232
pub fn (mut rng PRNG) f32() f32 {
228-
return f32((rng.u32() >> 9) * constants.reciprocal_2_23rd)
233+
return f32((rng.u32() >> 9) * reciprocal_2_23rd)
229234
}
230235

231236
// f32cp returns a pseudorandom `f32` value in range `[0, 1)`
@@ -258,15 +263,15 @@ pub fn (mut rng PRNG) f32cp() f32 {
258263
}
259264

260265
// Assumes little-endian IEEE floating point.
261-
x = (exp << 23) | (x >> 8) & constants.ieee754_mantissa_f32_mask
266+
x = (exp << 23) | (x >> 8) & ieee754_mantissa_f32_mask
262267
return bits.f32_from_bits(x)
263268
}
264269

265270
// f64 returns a pseudorandom `f64` value in range `[0, 1)`
266271
// using rng.u64() multiplied by a constant.
267272
@[inline]
268273
pub fn (mut rng PRNG) f64() f64 {
269-
return f64((rng.u64() >> 12) * constants.reciprocal_2_52nd)
274+
return f64((rng.u64() >> 12) * reciprocal_2_52nd)
270275
}
271276

272277
// f64cp returns a pseudorandom `f64` value in range `[0, 1)`
@@ -298,7 +303,7 @@ pub fn (mut rng PRNG) f64cp() f64 {
298303
if bitcount > 11 {
299304
x = rng.u64()
300305
}
301-
x = (exp << 52) | (x & constants.ieee754_mantissa_f64_mask)
306+
x = (exp << 52) | (x & ieee754_mantissa_f64_mask)
302307
return bits.f64_from_bits(x)
303308
}
304309

‎vlib/v/ast/table.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ pub fn (t &Table) find_method(s &TypeSymbol, name string) !Fn {
340340
}
341341
ts = t.type_symbols[ts.parent_idx]
342342
}
343-
return error('unknown method `${name}`')
343+
return error('unknown method')
344344
}
345345

346346
@[params]

‎vlib/v/checker/fn.v‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,9 @@ fn (mut c Checker) method_call(mut node ast.CallExpr, mut continue_check &bool)
20932093
if final_left_sym.kind == .aggregate {
20942094
// the error message contains the problematic type
20952095
unknown_method_msg = err.msg()
2096+
if unknown_method_msg == 'unknown method' {
2097+
unknown_method_msg += ' `' + method_name + '`'
2098+
}
20962099
}
20972100
}
20982101

‎vlib/v/gen/c/assign.v‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,17 +1217,18 @@ fn (mut g Gen) gen_cross_tmp_variable(left []ast.Expr, val ast.Expr) {
12171217
}
12181218
ast.InfixExpr {
12191219
sym := g.table.sym(val.left_type)
1220-
if _ := g.table.find_method(sym, val.op.str()) {
1220+
svalop := val.op.str()
1221+
if _ := g.table.find_method(sym, svalop) {
12211222
left_styp := g.styp(val.left_type.set_nr_muls(0))
12221223
g.write2(left_styp, '_')
1223-
g.write2(util.replace_op(val.op.str()), '(')
1224+
g.write2(util.replace_op(svalop), '(')
12241225
g.gen_cross_tmp_variable(left, val.left)
12251226
g.write(', ')
12261227
g.gen_cross_tmp_variable(left, val.right)
12271228
g.write(')')
12281229
} else {
12291230
g.gen_cross_tmp_variable(left, val.left)
1230-
g.write(val.op.str())
1231+
g.write(svalop)
12311232
g.gen_cross_tmp_variable(left, val.right)
12321233
}
12331234
}

0 commit comments

Comments
 (0)