Build hook omits Box2D's -ffp-contract=off, so arm64 and x64 simulate differently

未关闭 适合新手
#125 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
74/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
cmake, dart

调研方向

从 packages/forge2d/hook/build.dart 开始,检查它如何为每个 target 将编译器标志传递给 CBuilder.library。将 hook 的设置与根目录 CMakeLists.txt 中 Box2D v3.1.1 的确定性数学选项进行比较;issue 报告称,禁用浮点收缩后,arm64 和 x86_64 的模拟哈希值一致。完成标准是 hook 为 clang 和 GCC target 应用合适的标志,并且这些架构上的模拟结果一致。

由索引模型根据 Issue 内容生成。

描述

Box2D v3 is deterministic across platforms only when compiled without floating-point contraction. Upstream sets this in its root CMakeLists.txt under "Deterministic math" (v3.1.1):

add_compile_options(-ffp-contract=off)

The reasoning is in Erin Catto's determinism post. Forge2D's build hook (packages/forge2d/hook/build.dart) compiles the vendored sources without this flag, so clang's default contraction applies.

What I measured

forge2d 0.15.2 (Box2D v3.1.1), Dart 3.13.5, Apple M1 Pro. The x64 runs used the x64 Dart SDK under Rosetta, so the hook built an x86_64 library.

  • The arm64 libbox2d.dylib contains 1,552 fused multiply-add instructions (otool -tv | grep -cE 'fmadd|fmsub|fnmadd|fnmsub|fmla|fmls'). Built with -ffp-contract=off, it contains none.
  • A test world of 12 dynamic circles among static circles and walls, zero gravity, stepped 600 times at 0.1 s, hashed over every body's position and rotation:
Build arm64 x86_64
forge2d 0.15.2 as published 8957222861739655168 1316962272182009856
hook patched with -ffp-contract=off 1316962272182009856 1316962272182009856

With the flag, arm64 and x86_64 agree bit for bit. Without it, they diverge.

Suggested change

Pass the flag to CBuilder.library for clang and GCC targets, for example:

flags: os == OS.windows ? const [] : const ['-ffp-contract=off'],

I did not test Windows with MSVC, so I can't say which MSVC option, if any, is needed there.

Test world
import 'dart:typed_data';
import 'package:forge2d/forge2d.dart';

void main() {
  final world = World(gravity: Vector2.zero());
  final walls = world.createBody();
  final c = [Vector2(-8, -5), Vector2(8, -5), Vector2(8, 5), Vector2(-8, 5)];
  for (var i = 0; i < 4; i++) {
    walls.createShape(Segment(point1: c[i], point2: c[(i + 1) % 4]));
  }
  world.createBody(BodyDef(position: Vector2(1, 0.3))).createShape(Circle(radius: 0.6));
  world.createBody(BodyDef(position: Vector2(-2, -1))).createShape(Circle(radius: 0.35));
  final bodies = [
    for (var i = 0; i < 12; i++)
      world.createBody(BodyDef(type: BodyType.dynamic,
          position: Vector2(-6.0 + i, 2.0 - (i % 3)),
          linearVelocity: Vector2(3.0 - i * 0.41, 1.3 + i * 0.17),
          angularVelocity: 0.3 * i))
        ..createShape(Circle(radius: 0.5),
            ShapeDef(material: SurfaceMaterial(friction: 0.2, restitution: 0.5)))
  ];
  final b = ByteData(8);
  var h = 0;
  for (var t = 0; t < 600; t++) {
    world.step(0.1);
    for (final body in bodies) {
      for (final v in [body.position.x, body.position.y, body.rotation.cos, body.rotation.sin]) {
        b.setFloat64(0, v);
        h = (h * 31 + b.getInt64(0)) & 0x7FFFFFFFFFFFFFFF;
      }
    }
  }
  print(h);
  world.destroy();
}
主要语言
Dart
星标
203
派生
31
平均合并
27 分钟
30 天内合并 PR
3

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 有 Pull Request 模板
  • 没有贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

相似的 Issue

更多 Dart Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。