Skip to content

Commit 4c9404c

Browse files
committed
Build: every strategy TU is compiled with -ffp-contract=off, as libpineforge is (lane FPC)
libpineforge.a and libpineforge_kernel.a are compiled with -ffp-contract=off (TradingView's runtime has no FMA, so every multiply-add rounds twice), but the option was PRIVATE: no consumer inherited it. A strategy TU inlines the header-side arithmetic (ta::*, series math) beside the generated Pine expressions, and on ARM64 / FMA-enabled x86 it fused them. Measured at d292f19: 89 of the 312 corpus strategy objects held 194 scalar FMAs on this Mac (the same 89/194 on spark, GCC 13.3 aarch64), 80 of the 201 bench slots 497; the library objects none. - CMakeLists.txt: both library targets add -ffp-contract=off as an INTERFACE compile option. The library's own flags are unchanged (no library object recompiles); every target that links pineforge / pineforge_kernel (corpus, bench, tutorial, tests, find_package consumers through install(EXPORT)) now compiles with it. With it the 312 corpus and 201 bench strategy objects hold 0 fused instructions, and their trades are byte-identical to before on macOS arm64 and Linux aarch64 (312/312 against scripts/corpus_parity_baseline.txt, 201/201 bench slots). - benchmarks/run_all.sh: the hand-compiled canonical indicator runner, which inlines ta::*, gets the flag too. - cmake/smoke_consumer: the find_package consumer ci_verify builds refuses a package whose PineForge::pineforge / PineForge::kernel lost the option, and its run fails when its own multiply-add was fused (FMA hosts). Fail-before against the d292f19 install: configure stops with "PineForge::pineforge does not carry -ffp-contract=off to its consumers (INTERFACE_COMPILE_OPTIONS: _pf_options-NOTFOUND)". - docs: README's Pine -> .so recipe, integration-cmake (what the package carries, Makefile / pkg-config recipes), ci.md and install.md (the smoke checks; install.md named the binary smoke_consumer, it is smoke_version), and two CMakeLists.txt anchors the ten inserted lines moved. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> (cherry picked from commit 6ce79fb4a513f72b1899bcd2b20e7f702e99afff) INT19 conflict resolution (docs only): one design-doc hunk where both INT18 and this commit re-anchored digits (this commit: CMakeLists.txt:427-428 -> 437-438 for the ten lines it adds); kept the base's digits -- the citations are re-anchored once, at the end of INT19. Every other hunk applied as written.
1 parent 48b3e23 commit 4c9404c

9 files changed

Lines changed: 66 additions & 9 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,16 @@ foreach(_pf_library ${PINEFORGE_LIBRARY_TARGETS})
209209
-ffp-contract=off
210210
)
211211

212+
# The same rule travels to every consumer. A strategy translation unit
213+
# (a corpus / bench / tutorial generated.cpp, or anything a downstream
214+
# find_package(PineForge) project links) inlines the header-side
215+
# arithmetic -- ta::* classes, series math -- next to the generated Pine
216+
# expressions, so compiled with contraction on it fuses the multiply-adds
217+
# the library's own objects never fuse. INTERFACE, not PUBLIC: the
218+
# library's own flags above stay exactly as they are. install(EXPORT)
219+
# carries it into PineForgeTargets.cmake.
220+
target_compile_options(${_pf_library} INTERFACE -ffp-contract=off)
221+
212222
target_include_directories(${_pf_library}
213223
PUBLIC
214224
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ validation scoreboard below measures.
128128
```bash
129129
pip install pineforge-codegen
130130
pineforge-codegen strategy.pine -o generated.cpp
131-
c++ -std=c++17 -shared -fPIC generated.cpp -lpineforge -o strategy.so
131+
c++ -std=c++17 -ffp-contract=off -shared -fPIC generated.cpp -lpineforge -o strategy.so
132132
python3 scripts/run_strategy.py . # or drive it over the C ABI
133133
```
134134

‎benchmarks/run_all.sh‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,9 @@ if [[ "${SKIP_PINEFORGE:-0}" != "1" && "${SKIP_INDICATORS:-0}" != "1" ]]; then
241241
if [[ ! -x "${CANON_BIN}" || "${BENCH_DIR}/runners/run_pineforge_canonical.cpp" -nt "${CANON_BIN}" \
242242
|| "${ROOT_DIR}/build/lib/libpineforge.a" -nt "${CANON_BIN}" ]]; then
243243
log "building PineForge canonical indicator runner"
244-
c++ -std=c++17 -O2 -I "${ROOT_DIR}/include" \
244+
# -ffp-contract=off as libpineforge and every strategy target: the
245+
# ta::* arithmetic inlined here must round like TradingView's (no FMA).
246+
c++ -std=c++17 -O2 -ffp-contract=off -I "${ROOT_DIR}/include" \
245247
"${BENCH_DIR}/runners/run_pineforge_canonical.cpp" \
246248
-L "${ROOT_DIR}/build/lib" \
247249
-Wl,-force_load,"${ROOT_DIR}/build/lib/libpineforge.a" \

‎cmake/smoke_consumer/CMakeLists.txt‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,17 @@ project(pineforge_smoke_consumer LANGUAGES C CXX)
33

44
find_package(PineForge REQUIRED)
55

6+
# The package carries libpineforge's no-contraction rule to its consumers
7+
# (the root CMakeLists.txt: INTERFACE -ffp-contract=off): a strategy TU built
8+
# against an export that lost it fuses multiply-adds the library never fuses.
9+
foreach(_pf_target PineForge::pineforge PineForge::kernel)
10+
get_target_property(_pf_options ${_pf_target} INTERFACE_COMPILE_OPTIONS)
11+
if(NOT "-ffp-contract=off" IN_LIST _pf_options)
12+
message(FATAL_ERROR
13+
"${_pf_target} does not carry -ffp-contract=off to its consumers "
14+
"(INTERFACE_COMPILE_OPTIONS: ${_pf_options})")
15+
endif()
16+
endforeach()
17+
618
add_executable(smoke_version main.c)
719
target_link_libraries(smoke_version PRIVATE PineForge::pineforge)

‎cmake/smoke_consumer/main.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
#include <pineforge/pineforge.h>
22
#include <stdio.h>
33

4+
/* 1 when this TU fused x * x - p into one multiply-add. x * x - p is then the
5+
* rounding error of x * x (2^-60 here); rounded twice, as TradingView's runtime
6+
* and libpineforge round it, it is 0. x and y are two reads of one volatile,
7+
* so no compiler can share a single product between p and the subtraction.
8+
* Only a target with an FMA instruction (ARM64, x86-64 with FMA enabled) can
9+
* tell the two apart. */
10+
static int fused_multiply_add(void)
11+
{
12+
volatile double in = 1.0 + 0x1p-30;
13+
double x = in;
14+
double y = in;
15+
double p = y * y;
16+
return x * x - p != 0.0;
17+
}
18+
419
int main(void)
520
{
21+
if (fused_multiply_add()) {
22+
fprintf(stderr, "smoke consumer: a multiply-add was fused; "
23+
"PineForge's -ffp-contract=off did not reach this TU\n");
24+
return 1;
25+
}
626
pf_version_t v = pf_version_get();
727
printf("%d.%d.%d\n", (int)v.major, (int)v.minor, (int)v.patch);
828
return 0;

‎docs/adr/0001-kernel-adapter-boundary.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ citations, 264 are in `src/source/` and its headers; 6 sit in kernel files
339339
under `examples/native/` — sixteen C++ and two C — each including only
340340
`<pineforge/native_host.hpp>` (or `native_c_api.h`), linking `PineForge::kernel`, and checking
341341
its own numbers before it prints its summary line. `PINEFORGE_BUILD_EXAMPLES`
342-
(`CMakeLists.txt:37`, default OFF) builds them (`add_subdirectory` `CMakeLists.txt:305`) and
342+
(`CMakeLists.txt:37`, default OFF) builds them (`add_subdirectory` `CMakeLists.txt:315`) and
343343
registers each as a CTest row that runs through `examples/native/run_example.cmake`, which
344344
fails on a nonzero exit, a signal, a timeout **or** a missing summary line — so a row asserts
345345
both, which a bare `PASS_REGULAR_EXPRESSION` cannot. `scripts/test_example_runner.py` proves

‎docs/ci.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ python3 scripts/ci_verify.py release --build-dir build --jobs 4
2121
The command runs source guards, configures and rebuilds every enabled target,
2222
prepares the real historical ABI providers, runs CTest, installs the package,
2323
and builds and runs the `find_package` consumer. The smoke test compares the
24-
installed library's reported version with `VERSION`. After a successful build,
25-
a failing test suite does not hide a separate install or package failure.
24+
installed library's reported version with `VERSION`; its configure refuses a
25+
package whose `PineForge::pineforge` or `PineForge::kernel` no longer carries
26+
`-ffp-contract=off` to consumers, and on an FMA-capable host (macOS arm64) its
27+
run fails when a multiply-add in the consumer's own TU was fused. After a
28+
successful build, a failing test suite does not hide a separate install or
29+
package failure.
2630
Release and native profiles also install into a disposable prefix, remove the
2731
source-only header trees, and compile the native public roots and examples;
2832
the preflight source guard rejects those includes before a build.

‎docs/pages/install.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ A self-contained smoke test ships under `cmake/smoke_consumer/`:
8181
```bash
8282
cmake -S cmake/smoke_consumer -B build-smoke
8383
cmake --build build-smoke
84-
./build-smoke/smoke_consumer # prints the runtime version
84+
./build-smoke/smoke_version # prints the runtime version
8585
```
8686

8787
If this prints the version you installed (`0.14.0` for this tree's `VERSION`)
88-
you're done.
88+
you're done. The configure step also checks that the package hands its
89+
consumers `-ffp-contract=off`, and the program exits 1 instead if its own
90+
multiply-add was fused.

‎docs/pages/integration-cmake.md‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ carries:
2323
- the include directory containing `<pineforge/pineforge.h>`
2424
- the static library `libpineforge.a`
2525
- the standard C++ runtime (since the static lib is C++)
26+
- the compile option `-ffp-contract=off`, so a translation unit that links
27+
it (a strategy's `generated.cpp` above all) rounds every `*` and `+` on its
28+
own, as `libpineforge.a` and TradingView's runtime do, instead of fusing
29+
them into one FMA on ARM64 or FMA-enabled x86 (`PineForge::kernel`
30+
carries it too)
2631

2732
## Locating a non-default install
2833

@@ -49,7 +54,7 @@ later 0.x.y) is the recommended pin.
4954
## Linking from a hand-written Makefile
5055

5156
```make
52-
CFLAGS += -I$(PREFIX)/include
57+
CFLAGS += -I$(PREFIX)/include -ffp-contract=off
5358
LDFLAGS += -L$(PREFIX)/lib
5459
LDLIBS += -lpineforge -lstdc++ -lm
5560

@@ -59,6 +64,8 @@ runner: runner.o
5964

6065
`-lstdc++` is required even from C TUs because the runtime is C++ inside.
6166
`-lm` covers the `math.h` calls inside the runtime's TA classes.
67+
`-ffp-contract=off` is the option the CMake package hands every consumer;
68+
compile a strategy's `generated.cpp` with it too (`CXXFLAGS`).
6269
6370
## Linking from pkg-config
6471
@@ -75,7 +82,7 @@ libdir=${prefix}/lib
7582
Name: pineforge
7683
Description: Deterministic PineScript v6 backtest runtime
7784
Version: 0.14.0
78-
Cflags: -I${includedir}
85+
Cflags: -I${includedir} -ffp-contract=off
7986
Libs: -L${libdir} -lpineforge -lstdc++ -lm
8087
```
8188

0 commit comments

Comments
 (0)