Skip to content

Commit

Permalink
pref: support -fast-math, passing either -ffast-math or /fp:fast (f…
Browse files Browse the repository at this point in the history
…or msvc) to the C backend, and `$if fast_math {` to detect it at comptime
  • Loading branch information
spytheman committed Sep 5, 2023
1 parent dee8fb9 commit 639f128
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion doc/docs.md
Expand Up @@ -5613,7 +5613,7 @@ Full list of builtin options:
| `mac`, `darwin`, `ios`, | `clang`, `mingw` | `i386`, `arm32` | `js`, `glibc`, `prealloc` |
| `android`, `mach`, `dragonfly` | `msvc` | `x64`, `x32` | `no_bounds_checking`, `freestanding` |
| `gnu`, `hpux`, `haiku`, `qnx` | `cplusplus` | `little_endian`, `big_endian` | `no_segfault_handler`, `no_backtrace` |
| `solaris`, `termux` | | | `no_main` |
| `solaris`, `termux` | | | `no_main`, 'fast_math' |
#### `$embed_file`
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/ast/comptime_valid_idents.v
Expand Up @@ -4,14 +4,14 @@ pub const (
valid_comptime_if_os = ['windows', 'ios', 'macos', 'mach', 'darwin', 'hpux', 'gnu',
'qnx', 'linux', 'freebsd', 'openbsd', 'netbsd', 'bsd', 'dragonfly', 'android', 'termux',
'solaris', 'haiku', 'serenity', 'vinix']
valid_comptime_compression_types = ['none', 'zlib']
valid_comptime_if_compilers = ['gcc', 'tinyc', 'clang', 'mingw', 'msvc', 'cplusplus']
valid_comptime_if_platforms = ['amd64', 'i386', 'aarch64', 'arm64', 'arm32', 'rv64', 'rv32']
valid_comptime_if_cpu_features = ['x64', 'x32', 'little_endian', 'big_endian']
valid_comptime_if_other = ['apk', 'js', 'debug', 'prod', 'test', 'glibc', 'prealloc',
'no_bounds_checking', 'freestanding', 'threads', 'js_node', 'js_browser', 'js_freestanding',
'interpreter', 'es5', 'profile', 'wasm32', 'wasm32_emscripten', 'wasm32_wasi']
'interpreter', 'es5', 'profile', 'wasm32', 'wasm32_emscripten', 'wasm32_wasi', 'fast_math']
valid_comptime_not_user_defined = all_valid_comptime_idents()
valid_comptime_compression_types = ['none', 'zlib']
)

fn all_valid_comptime_idents() []string {
Expand Down
8 changes: 8 additions & 0 deletions vlib/v/gen/c/comptime.v
Expand Up @@ -1157,6 +1157,14 @@ fn (mut g Gen) comptime_if_to_ifdef(name string, is_comptime_option bool) !strin
'big_endian' {
return 'TARGET_ORDER_IS_BIG'
}
'fast_math' {
if g.pref.ccompiler_type == .msvc {
// turned on by: `-cflags /fp:fast`
return '_M_FP_FAST'
}
// turned on by: `-cflags -ffast-math`
return '__FAST_MATH__'
}
else {
if is_comptime_option
|| (g.pref.compile_defines_all.len > 0 && name in g.pref.compile_defines_all) {
Expand Down
8 changes: 8 additions & 0 deletions vlib/v/help/build/build-c.txt
Expand Up @@ -318,3 +318,11 @@ see also `v help build`.
It may be decreased, to reduce the memory footprint of programs that launch
hundreds/thousands of threads, but where each of the threads does not need
a big stack.

-fast-math
When present, pass either -ffast-math or /fp:fast (for msvc) to the C compiler.
See https://en.wikipedia.org/wiki/Floating-point_arithmetic#%22Fast_math%22_optimization
https://learn.microsoft.com/en-us/cpp/build/reference/fp-specify-floating-point-behavior?view=msvc-170&redirectedfrom=MSDN
https://clang.llvm.org/docs/UsersManual.html#cmdoption-ffast-math
https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-ffast-math

17 changes: 14 additions & 3 deletions vlib/v/pref/pref.v
Expand Up @@ -227,11 +227,13 @@ pub mut:
message_limit int = 150 // the maximum amount of warnings/errors/notices that will be accumulated
nofloat bool // for low level code, like kernels: replaces f32 with u32 and f64 with u64
use_coroutines bool // experimental coroutines
fast_math bool // -fast-math will pass either -ffast-math or /fp:fast (for msvc) to the C backend
// checker settings:
checker_match_exhaustive_cutoff_limit int = 12
thread_stack_size int = 8388608 // Change with `-thread-stack-size 4194304`. Note: on macos it was 524288, which is too small for more complex programs with many nested callexprs.
wasm_stack_top int = 1024 + (16 * 1024) // stack size for webassembly backend
wasm_validate bool // validate webassembly code, by calling `wasm-validate`
// wasm settings:
wasm_stack_top int = 1024 + (16 * 1024) // stack size for webassembly backend
wasm_validate bool // validate webassembly code, by calling `wasm-validate`
}

pub fn parse_args(known_external_commands []string, args []string) (&Preferences, string) {
Expand Down Expand Up @@ -395,6 +397,9 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
res.nofloat = true
res.compile_defines_all << 'nofloat' // so that `$if nofloat? {` works
}
'-fast-math' {
res.fast_math = true
}
'-e' {
res.is_eval_argument = true
res.eval_argument = cmdline.option(current_args, '-e', '')
Expand Down Expand Up @@ -903,7 +908,13 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
eprintln('Cannot save output binary in a .v file.')
exit(1)
}

if res.fast_math {
if res.ccompiler_type == .msvc {
res.cflags += ' /fp:fast'
} else {
res.cflags += ' -ffast-math'
}
}
if res.is_eval_argument {
// `v -e "println(2+5)"`
run_code_in_tmp_vfile_and_exit(args, mut res, '-e', 'vsh', res.eval_argument)
Expand Down

0 comments on commit 639f128

Please sign in to comment.