last day (14 days later) » 

8:32 AM
0
A: set individual bit in AVX register (__m256i), need "random access" operator

zx485If you like to avoid a LUT, you can use BTS for setting a single bit (or BTR for clearing it, respectively). There seems to be no intrinsic for this instruction (at least in GCC), so inline-assembly is required (so for x86 architecture only). 0F AB /r --- BTS r/m32, r32 --- Store selected bit...

 
You know BTS with a memory operand is over 10 uops on recent Intel CPUs, right? Precisely because of its insane bit-string addressing where the address of the byte (or dword) to be modified isn't determined by the addressing-mode alone. And it will still cause a store-forwarding stall on reload. Still, interesting to point out.
IDK why you use those mov instructions in your inline-asm implementations, though. The don't even make anything any easier, because btsl %1, %0 should work just fine. Also, your inline asm doesn't use any clobbers to tell the compiler you're stepping on eax and edx. Also, you need "+m" to make it a read-write operand, not a write-only memory operand!
I fixed the bugs for you so I wouldn't have to downvote and then come back later.
 
@PeterCordes: Regarding your 10uops objection: May be, but it's just one instruction. I haven't checked if a combination of more elementary instructions would lower that count. Maybe, but IMHO it's a matter of taste (fewer ins vs. fewer uops) ;-) XLAT is another of that ins and I wonder why Intel does not improve it's microcode, although it's isomorphic to a combination of macro-opcodes???
@PeterCordes: Regarding your second objection considering the usage of MOVs. I tested it without and it didn't work as expected, because the parameter was accessed as EBP+8 which rendered the displacement addressing ineffective because the offset was added to EBP+8=EBP+8+x instead of the address [EBP+8]. So I designate your edit as invalid. Have you tested it?
@PeterCordes: You seem to be quick on downvoting for minor disagreements...why is that so?
 
I'm pretty sure you could beat this easily with AVX2 by using a vector shift+shuffle (with a shuffle mask generated with integer instructions and expanded with pmovzx or something). Avoiding the store-forwarding stall is huge.
I hold posts on SO to a high standard. If people didn't downvote things that weren't right, it would be less obvious that there were problems. I wasn't downvoting for suggesting a slow method, I was going to downvote because of the broken inline asm implementation. Using "=m" for an output-only operand was obviously fatally flawed. The missing clobber declaration for eax and edx is also subtly fatal. Lack of portability to 64-bit was also a problem.
However, you're right that the MOV instruction and then (%%edx) did provide the extra level of dereferencing, the same as *value. So yes, I was far too critical of that aspect of your code, sorry! That part wasn't actually broken, just wasting instructions by forcing the pointer to be in memory and then loading it with a MOV, instead of just asking the compiler to take care of that. I removed the silly MOV instructions and then noticed the missing dereference, but that was a problem I'd created. I'm sure it works now. The original version may happen to work, but wasn't safe.
GNU C inline asm is hard to get right. It's easy to make code that happens to work, but will break sometime in the future, maybe with a new compiler or changes in very distant code. So I think it's really important that any inline asm in SO posts anyone could learn from, or even copy&paste, is actually fully correct, and won't break anything when inlined with optimization. Optimal is nice, too, so I took the liberty of doing that. If the first or last insn of a GNU C inline asm statement is a MOV, you're almost always doing it wrong and should have used operand constraints instead.
 
@PeterCordes: Thanks for your comment(s) and improving my answer. But I think there is a huge space between a flawed answer and very low quality. If it's flawed and you see it, you edit it. If it's very low quality, flag it appropriately - as simple as that. But downvoting just for a minor issue is plainly evil (and I know about your "Someone's wrong on the internet"-philosophy). If you're so concerned about the quality of SO, think about permanently pissing off people who contribute not perfect answers. IMHO this would significantly reduce interesting input which could be improved...
@PeterCordes: Yes ("GNU C inline asm is hard to get"), you're right. I almost never use intrinsics, so I learned something for generating that answer. I mostly develop in assembler and then convert that to something else(like intrinsics in this case). Hence our argument(s) about coding style and in which aspects they differ :-) ... So I'm new to intrinsics and thanks for your corrections.
 
VLQ flags are reserved for extreme cases where moderators need to get involved because downvotes by the community aren't sufficient. My personal voting strategy for answers is usually only to downvote if the central premise is just plain wrong, or the code contains critical bugs like your inline asm did (telling the compiler something is an output-only when it's actually read-write is a critical bug, and will break horribly with optimization enabled, because the input will never be generated). In this case, I fully expected to remove my hypothetical downvote as soon as you saw&fixed it.
I half meant it as a joke, not an insult. I've written crappy answers, or answers with bugs, and gotten a downvote until I fixed them. That's how it should work. Also BTW, "intrinsics" are not the same thing as GNU C inline asm. An intrinsic is something like _pdep_u32(), or __builtin_bswap32(). Inline asm is just called inline asm. :) BTW, read gcc.gnu.org/wiki/DontUseInlineAsm if you haven't already. Lots of reasons to avoid it, including defeating optimizations like constant-propagation.
 
8:32 AM
@PeterCordes: Well, I think, we made our positions clear to each other. Are you ok with removing the bilateral aspects of this discussion in the comments by deleting them? Say yes, by deleting every non-topic comment, I promise to do accordingly. cya till next time and have a nice day.
@PeterCordes: Yes, I know, but there was no intrinsic for BTS or BTR.
 
Anyway, back to voting: it takes a pretty serious problem in an answer for me to downvote. There's a pretty wide range of quality where I don't downvote or upvote, and just leave a comment about anything that stops me from upvoting. (Or just move on). The question of when if ever to downvote has been discussed on meta, and the consensus is that answers with problems need downvotes.
Sure, let's copy this rambling discussion to chat so it sticks around for a while, and delete our comments.
 
I'm ok with that :-)
Done so, one too many, but whatever.
 

last day (14 days later) »