Skip to contentSkip to conversation

Add __builtin_popcountl

PATCH In review Matthew Wilcox 3 participants 4 messages Oct 26, 2006 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add __builtin_popcountl

IA-64 uses __builtin_popcountl() which wasn't a function sparse knew about. This patch adds it. I've never looked at sparse before, so this is a hack-and-run job. Please feel free to rewrite it if it doesn't suit.

symbol.c +29 -0
index 6c91112..388e19d 100644
@@ -592,6 +592,29 @@ static int evaluate_choose(struct expres
 	return 1;
 }
 
+static int arguments_popcountl(struct expression *expr)
+{
+	struct expression_list *arglist = expr->args;
+	struct expression *arg;
+	int i = 0;
+
+	FOR_EACH_PTR (arglist, arg) {
+		if (!evaluate_expression(arg))
+			return 0;
+		i++;
+	} END_FOR_EACH_PTR(arg);
+	if (i < 1) {
+		sparse_error(expr->pos,
+			     "not enough arguments for __builtin_popcountl");
+		return 0;
+	} if (i > 1) {
+		sparse_error(expr->pos,
+			     "too many arguments for __builtin_popcountl");
+		return 0;
+	}
+	return 1;
+}
+
 static int expand_expect(struct expression *expr, int cost)
 {
 	struct expression *arg = first_ptr_list((struct ptr_list *) expr->args);
@@ -744,6 +767,11 @@ static struct symbol_op choose_op = {
 	.args = arguments_choose,
 };
 
+static struct symbol_op popcountl_op = {
+	.evaluate = evaluate_to_integer,
+	.args = arguments_popcountl,
+};
+
 /*
  * Builtin functions
  */
@@ -754,6 +782,7 @@ static struct sym_init eval_init_table[]
 	{ "__builtin_warning", &builtin_fn_type, MOD_TOPLEVEL, &warning_op },
 	{ "__builtin_expect", &builtin_fn_type, MOD_TOPLEVEL, &expect_op },
 	{ "__builtin_choose_expr", &builtin_fn_type, MOD_TOPLEVEL, &choose_op },
+	{ "__builtin_popcountl", &builtin_fn_type, MOD_TOPLEVEL, &popcountl_op },
 	{ NULL,		NULL,		0 }
 };
 
#1 lore raw
Quoted 4 lines · Matthew Wilcox

IA-64 uses __builtin_popcountl() which wasn't a function sparse knew about. This patch adds it. I've never looked at sparse before, so this is a hack-and-run job. Please feel free to rewrite it if it doesn't suit.

AFAICS, that's way over the top. That kind of stuff is needed only when builtin can't be described as a normal function. This, OTOH...

lib.c +10 -0
@@ -467,6 +467,16 @@ void declare_builtin_functions(void)
 	add_pre_buffer("extern void __builtin_trap(void);\n");
 	add_pre_buffer("extern int __builtin_ffs(int);\n");
 	add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
+	add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
+	add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
+	add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
+	add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
+	add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
+	add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
+	add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
+	add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
+	add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
+	add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
 }
 
 void create_builtin_stream(void)
#2 lore raw
Quoted 3 lines · Al Viro

AFAICS, that's way over the top. That kind of stuff is needed only when builtin can't be described as a normal function. This, OTOH...

On the other hand, adding "true" builtin functions is actually a lot cheaper than adding bogus code to be parsed explicitly.

So I'd actually prefer a way to have a table to describe and initialize these things, as a way to make it a lot more efficient, and without the silly preprocessor buffer hacks..

The preprocessor hack is certainly simple, but it's not what you really want in real life.

Linus

#3 lore raw
Quoted 10 lines · Linus Torvalds
Quoted 3 lines · Al Viro

AFAICS, that's way over the top. That kind of stuff is needed only when builtin can't be described as a normal function. This, OTOH...

On the other hand, adding "true" builtin functions is actually a lot cheaper than adding bogus code to be parsed explicitly.

Quoted 6 lines

So I'd actually prefer a way to have a table to describe and initialize these things, as a way to make it a lot more efficient, and without the silly preprocessor buffer hacks..

The preprocessor hack is certainly simple, but it's not what you really want in real life.

Well... Keep in mind that those _are_ functions; i.e. unlike something like __builtin_choose_expr() you can pass such puppy to a function expecting a callback, etc.

So it's not enough to special-case them; we really need types for those symbols. I don't think that it will be more efficient in the end.