╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Value.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2493 else 2494 b_ptr.byte_offset - a_ptr.byte_offset; 2495 2496 const need_bytes_diff = elem_count * a_elem_ty.abiSize(zcu); 2497 return bytes_diff < need_bytes_diff; 2498 } 2499 } 2500 2501 /// `lhs` and `rhs` are both scalar numeric values (int or float). 2502 /// Supports comparisons between heterogeneous types. 2503 /// If `lhs` or `rhs` is undef, returns `false`. 2504 pub fn eqlScalarNum(lhs: Value, rhs: Value, zcu: *Zcu) bool { 2505 if (lhs.isUndef(zcu)) return false; 2506 if (rhs.isUndef(zcu)) return false; 2507 2508 if (lhs.isFloat(zcu) or rhs.isFloat(zcu)) { 2509 const lhs_f128 = lhs.toFloat(f128, zcu); 2510 const rhs_f128 = rhs.toFloat(f128, zcu); 2511 return lhs_f128 == rhs_f128; 2512 } 2513 2514 if (lhs.getUnsignedInt(zcu)) |lhs_u64| { 2515 if (rhs.getUnsignedInt(zcu)) |rhs_u64| { 2516 return lhs_u64 == rhs_u64; 2517 } 2518 } 2519 2520 var lhs_bigint_space: BigIntSpace = undefined; 2521 var rhs_bigint_space: BigIntSpace = undefined; 2522 const lhs_bigint = lhs.toBigInt(&lhs_bigint_space, zcu); 2523 const rhs_bigint = rhs.toBigInt(&rhs_bigint_space, zcu); 2524 return lhs_bigint.eql(rhs_bigint); 2525 } 2526 2527 /// Asserts the value is an integer, and the destination type is ComptimeInt or Int. 2528 /// Vectors are also accepted. Vector results are reduced with AND. 2529 /// 2530 /// If provided, `vector_index` reports the first element that failed the range check. 2531 pub fn intFitsInType( 2532 val: Value, 2533 ty: Type, 2534 vector_index: ?*usize, 2535 zcu: *const Zcu, 2536 ) bool { 2537 if (ty.toIntern() == .comptime_int_type) return true; 2538 const info = ty.intInfo(zcu); 2539 switch (val.toIntern()) { 2540 .zero_usize, .zero_u8 => return true, 2541 else => switch (zcu.intern_pool.indexToKey(val.toIntern())) { 2542 .undef => return true, 2543 .@"extern", .func, .ptr => { 2544 const target = zcu.getTarget(); 2545 const ptr_bits = target.ptrBitWidth(); 2546 return switch (info.signedness) { 2547 .signed => info.bits > ptr_bits, 2548 .unsigned => info.bits >= ptr_bits, 2549 }; 2550 }, 2551 .int => |int| { 2552 var buffer: InternPool.Key.Int.Storage.BigIntSpace = undefined; 2553 const big_int = int.storage.toBigInt(&buffer); 2554 return big_int.fitsInTwosComp(info.signedness, info.bits); 2555 }, 2556 .aggregate => |aggregate| { 2557 assert(ty.zigTypeTag(zcu) == .vector); 2558 return switch (aggregate.storage) { 2559 .bytes => |bytes| for (bytes.toSlice(ty.vectorLen(zcu), &zcu.intern_pool), 0..) |byte, i| { 2560 if (byte == 0) continue; 2561 const actual_needed_bits = std.math.log2(byte) + 1 + @intFromBool(info.signedness == .signed); 2562 if (info.bits >= actual_needed_bits) continue; 2563 if (vector_index) |vi| vi.* = i; 2564 break false; 2565 } else true, 2566 .elems, .repeated_elem => for (switch (aggregate.storage) { 2567 .bytes => unreachable, 2568 .elems => |elems| elems, 2569 .repeated_elem => |elem| @as(*const [1]InternPool.Index, &elem), 2570 }, 0..) |elem, i| { 2571 if (Value.fromInterned(elem).intFitsInType(ty.scalarType(zcu), null, zcu)) continue; 2572 if (vector_index) |vi| vi.* = i; 2573 break false; 2574 } else true, 2575 }; 2576 }, 2577 else => unreachable, 2578 }, 2579 } 2580 } 2581