Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have you tried juggling those flags? #44

Open
tthsqe12 opened this issue Feb 24, 2018 · 4 comments
Open

Have you tried juggling those flags? #44

tthsqe12 opened this issue Feb 24, 2018 · 4 comments

Comments

@tthsqe12
Copy link

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

@syzygy1
Copy link
Owner

syzygy1 commented Feb 24, 2018

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".

@tthsqe12
Copy link
Author

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

@syzygy1
Copy link
Owner

syzygy1 commented Feb 26, 2018

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".

@tthsqe12
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants