x86-64 calling convention assumes return registers are zeroed? [duplicate] - standards

This question already has answers here:
Why do x86-64 instructions on 32-bit registers zero the upper part of the full 64-bit register?
(4 answers)
Closed 6 years ago.
Given the code:
uint64_t f() { return 42; }
compilers output the following assembly for x86-64:
movl $42, %eax
ret
I'm raising my eyebrows, because the return value should be 64-bit, but the compilers appear to think it is safe to assume that the return registers %rax and %rdi are zero. I'm seem to be unable to find anything about this in the x86-64 ABI. Can somebody point to the basis for this assumption?

Setting EAX, etc., clears the upper 32-bits anyway.
finding reference...
Actually it should be closed as a dupe of this question.

Related

In Rust, why doesn't &v[1..] panic when the size of vector v is one? [duplicate]

This question already has answers here:
Why can I start a slice past the end of a vector in Rust?
(2 answers)
Closed 1 year ago.
In the gcd example in Programming Rust, 2nd Edition, why doesn't &numbers[1..] cause an out-of-bounds error in the statement
for m in &numbers[1..]
when the size of vector numbers is one? Doesn't numbers[1] address the second element which is one element beyond the end of the vector?
For example, I expected gcd 45 to panic, but instead it reports a greatest common divisor of 45:
ubuntu#development1:~/Documents/projects/rust/programming_in_rust/examples/gcd$ cargo run 45
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/gcd 45`
The greatest common divisor of [45] is 45
Cause the doc say so:
Panics if begin does not point to the starting byte offset of a
character (as defined by is_char_boundary), or if begin > len. source
Since len is 1 in your case 1.. doesn't panic and return a empty slice. Equivalent of 1..1 in your case.

What is the owner of literal reference such as &4 in Rust? [duplicate]

This question already has answers here:
In Rust, can you own a string literal?
(1 answer)
Why can I return a reference to a local literal but not a variable?
(1 answer)
Why is it legal to borrow a temporary?
(3 answers)
Closed 3 years ago.
Normally, there is only one owner for a specific value (except for things like Rc<T>). Then what is the owner of the value 4 below since the variable myVar borrows it from something? I want to know what is that something.
let myVar = &4;
Literals, be they:
number literals, like 4
string literals, like "Hello, World"
Have a 'static lifetime as their value is hard-coded into the library or executable itself. For example, on Linux, they would be found either in the .text segment or the .rodata segment of the ELF binary.
In that sense, you can think of them as being owned by the program itself.

Dereference pointers in XMM register (gather)

If I have some pointer or pointer-like values packed into an SSE or AVX register, is there any particularly efficient way to dereference them, into another such register? ("Particularly efficient" meaning "more efficient than just using memory for the values".) Is there any way to dereference them all without writing an intermediate copy of the register out to memory?
Edit for clarification: that means, assuming 32-bit pointers and SSE, to index into four arbitrary memory areas at once with the four sections of an XMM register and return four results at once to another register. Or as close to "at once" as possible. (/edit)
Edit2: thanks to PaulR's answer I guess the terminology I'm looking for is "gather", and the question therefore is "what's the best way to implement gather for systems pre-AVX2?".
I assume there isn't an instruction for this since ...well, one doesn't appear to exist as far as I can tell and anyway it doesn't seem to be what SSE is designed for at all.
("Pointer-like value" meaning something like an integer index into an array pretending to be the heap; mechanically very different but conceptually the same thing. If, say, one wanted to use 32-bit or even 16-bit values regardless of the native pointer size, to fit more values in a register.)
Two possible reason I can think of why one might want to do this:
thought it might be interesting to explore using the SSE registers for general-purpose... stuff, perhaps to have four identical 'threads' processing potentially completely unrelated/non-contiguous data, slicing through the registers "vertically" rather than "horizontally" (i.e. instead of the way they were designed to be used).
to build something like romcc if for some reason (probably not a good one), one didn't want to write anything to memory, and therefore would need more register storage.
This might sound like an XY problem, but it isn't, it's just curiosity/stupidity. I'll go looking for nails once I have my hammer.
The question is not entirely clear, but if you want to dereference vector register elements then the only instructions which might help you here are AVX2's gathered loads, e.g. _mm256_i32gather_epi32 et al. See the AVX2 section of the Intel Intrinsics Guide.
SYNOPSIS
__m256i _mm256_i32gather_epi32 (int const* base_addr, __m256i vindex, const int scale)
#include "immintrin.h"
Instruction: vpgatherdd ymm, vm32x, ymm
CPUID Flag : AVX2
DESCRIPTION
Gather 32-bit integers from memory using 32-bit indices. 32-bit elements are loaded from addresses starting at base_addr and offset by each 32-bit element in vindex (each index is scaled by the factor in scale). Gathered elements are merged into dst. scale should be 1, 2, 4 or 8.
OPERATION
FOR j := 0 to 7
i := j*32
dst[i+31:i] := MEM[base_addr + SignExtend(vindex[i+31:i])*scale]
ENDFOR
dst[MAX:256] := 0
So if I understood this correctly, your title is misleading and you really want to:
index into the concatenation of all XMM registers
with an index held in a part of an XMM register
Right?
That's hard. And a little weird, but I'm OK with that.
Assuming crazy tricks are allowed, I propose self-modifying code: (not tested)
pextrb eax, xmm?, ? // question marks are the position of the pointer
mov edx, eax
shr eax, 1
and eax, 0x38
add eax, 0xC0 // C0 makes "hack" put its result in eax
mov [hack+4], al // xmm{al}
and edx, 15
mov [hack+5], dl // byte [dl] of xmm reg
call hack
pinsrb xmm?, eax, ? // put value back somewhere
...
hack:
db 66 0F 3A 14 00 00 // pextrb ?, ? ,?
ret
As far as I know, you can't do that with full ymm registers (yet?). With some more effort, you could extend it to xmm8-xmm15. It's easily adjustable to other "pointer" sizes and other element sizes.

MIPS; getting the LSB of a hexadecimal value

I understand that there are a few questions already addressing this. However, my question varies in some sort. Suppose I have to get the LSB of a value (hexadecimal) stored in a register; for e.g;
If register $t0 contains the value 0xA4, I need to obtain and store the value 4
If register $t0 contains the value 0xBF, I need to obtain and store the value F
I understand that the bitwise ANDoperation works for decimal values. Could someone please provide some assistance as to how I go about getting the LSB?
Kind regards
You can easily AND the number from which you want to extract the LSB itself like this:
0xA4 AND 0x0F
is same as (in binary)
10100100b AND 00001111
this basically means that only last four digits will be extracted from binary number and that is the LSB you want.
All the binary operations works on general purpose registers along w/ masks which are merely pure numbers (regardless of their underlying basis repr)
Even though x86 isn't MIPS you should have something like this
and EAX, 0xF

What is maximum number value of quint64 in Qt? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Where are the limits for Qt types?
I need maximum number of quint64 for example we have a macro for ulong ULONG_MAX
Is there any equal macro for quint64?
You can find an answer in the official documentation:
quint64 is a typedef for unsigned long long int (unsigned __int64 on Windows). This type is guaranteed to be 64-bit on all platforms supported by Qt.
So, quint64 is a 64-bit type for unsigned integers.
We can find its maximum value this way:
2^64-1 = 18446744073709551615
As it was told here, you can get the same result by including #include <limits>, and the output result of
std::numeric_limits<quint64>::max();

Resources