Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
__builtin_expect
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jul 21, 2022
1 parent b32fb1c commit aab7a91
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cl/codebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ func binaryOp(ctx *blockCtx, op token.Token, v *cast.Node) {
adjustIntConst(ctx, ret, ret.Type)
}

func compareOp(ctx *blockCtx, op token.Token, src ast.Node) {
ctx.cb.BinaryOp(op, src)
}

func untypedZeroToNil(v *gox.Element) {
v.Type = types.Typ[types.UntypedNil]
v.Val = &ast.Ident{Name: "nil"}
Expand Down
5 changes: 5 additions & 0 deletions cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ func compileCallExpr(ctx *blockCtx, v *ast.Node) {
compileExpr(ctx, v.Inner[2])
cb.Assign(1)
return
case "__builtin_expect":
compileExpr(ctx, v.Inner[1])
compileExpr(ctx, v.Inner[2])
compareOp(ctx, token.EQL, ctx.goNode(v))
return
}
}
for i := 0; i < n; i++ {
Expand Down
10 changes: 10 additions & 0 deletions testdata/operator/operator.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include <stdio.h>
#include <string.h>

#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)

void f() {
double a;
float b;
Expand All @@ -14,6 +17,12 @@ int g(int ret) {
return ret;
}

void test() {
if (likely(g(100))) {
printf("likely(g(100))\n");
}
}

void h() {
g(1) || g(0);
g(0) || g(1);
Expand Down Expand Up @@ -44,5 +53,6 @@ int main() {
pfoo->msg[2] = '!', printf("%s\n", foo.msg);
h();
printf("%d\n", (int)((-0x2000ULL << (8*sizeof(long)-1)) | (4096ULL -1)));
test();
return 0;
}

0 comments on commit aab7a91

Please sign in to comment.