Cfish icon indicating copy to clipboard operation
Cfish copied to clipboard

Have you tried juggling those flags?

Open tthsqe12 opened this issue 7 years ago • 4 comments

Did you see https://developers.redhat.com/blog/2016/02/25/new-asm-flags-feature-for-x86-in-gcc-6/ ? If I had known about this earlier, I might not have tried to write asmfish in asm. It really does work, and I have tried it myself for overflow https://github.com/tthsqe12/cas/blob/master/types.h#L182

tthsqe12 avatar Feb 24 '18 00:02 tthsqe12

Interesting! I did not know about this. Last time I looked the closest one could get to directly using the condition flags was with "asm goto".

syzygy1 avatar Feb 24 '18 01:02 syzygy1

I should add that the register-memory form of bt is exceedingly slow compared to the register-register version. Took forever to track down that bottleneck

tthsqe12 avatar Feb 24 '18 02:02 tthsqe12

Unfortunately gcc isn't always being clever.

#include <inttypes.h>
#include <stdio.h>

static __inline__ int test_sq(uint64_t b, uint64_t s)
{
  int bit;
  __asm__("btq\t%2, %1" : "=@ccc" (bit) : "r" (b), "Ir" ((uint64_t)s));
  return bit;
}

int f(uint64_t b, uint64_t s)
{
  if (!test_sq(b, s))
    return 0;
  printf("test\n");
  return 1;
}

compiles to (AT&T notation):

f:
	btq	%rsi, %rdi
	jc	.L13
	setc	%al
	movzbl	%al, %eax
	ret
.L13:
	subq	$8, %rsp
	movl	$.LC0, %edi
	call	puts
	movl	$1, %eax
	addq	$8, %rsp
	ret

If I return 1 instead, the setc and movzbl instructions are replaced with "movl $1, %eax".

syzygy1 avatar Feb 26 '18 21:02 syzygy1

Do you have a bool type? I suppose gcc is getting confused by booleans https://godbolt.org/g/SZwmPb

tthsqe12 avatar Feb 27 '18 10:02 tthsqe12