Skip to content

Commit

Permalink
compilers/clike: Speedup cross_compute_int
Browse files Browse the repository at this point in the history
Expand the expression passed into cross_compute_int using the
preprocessor first and then try to evaluate the expanded expression
using the host machine compiler and test if the result is valid.
  • Loading branch information
sp1ritCS committed Nov 20, 2024
1 parent 6f67b10 commit 95390c0
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions mesonbuild/compilers/mixins/clike.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,18 @@ def cross_compute_int(self, expression: str, low: T.Optional[int], high: T.Optio
if self._compile_int(f'{expression} == {guess}', prefix, env, extra_args, dependencies):
return guess

# Try to expand the expresseion and evaluate it on the build machines compiler
if self.language in env.coredata.compilers.build:
expanded, _ = self.get_define(expression, prefix, env, extra_args, dependencies, False)
evaluate_expanded = f'''
#include <stdio.h>
#include <stdint.h>
int main(void) {{ int expression = {expanded}; printf("%d", expression); return 0; }}'''
run = env.coredata.compilers.build[self.language].run(evaluate_expanded, env)
if run and run.compiled and run.returncode == 0:
if self._compile_int(f'{expression} == {run.stdout}', prefix, env, extra_args, dependencies):
return int(run.stdout)

# If no bounds are given, compute them in the limit of int32
maxint = 0x7fffffff
minint = -0x80000000
Expand Down

0 comments on commit 95390c0

Please sign in to comment.