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

Đang mở Phù hợp với người mới
#125 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
74/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
cmake, dart
Lĩnh vực
backend, build-system

Hướng nghiên cứu

Bắt đầu với packages/forge2d/hook/build.dart và kiểm tra cách tệp truyền các cờ trình biên dịch cho CBuilder.library ở từng target. So sánh các thiết lập của hook với tùy chọn toán học xác định của Box2D v3.1.1 trong CMakeLists.txt ở thư mục gốc; issue cho biết các hash mô phỏng trên arm64 và x86_64 khớp nhau khi tắt phép co dấu phẩy động. Hoàn thành khi hook áp dụng cờ phù hợp cho các target clang và GCC, đồng thời kết quả mô phỏng khớp nhau trên các kiến trúc đó.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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();
}
Ngôn ngữ chính
Dart
Star
203
Fork
31
Merge trung bình
27 phút
Pull request đã merge (30 ngày)
3

Chuẩn bị môi trường

  • Không có Dockerfile hay tệp Docker Compose
  • Có mẫu pull request
  • Không có hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue tương tự

Thêm issue về Dart

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.