ICplxTrans' `to_bytes` and `from_bytes` use different primitive data types (int on write, float on read)

Open Beginner friendly
#2,554 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
70/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp, python
Domain
backend

Research direction

The bug is in the reader in src/db/db/dbBinarySerialize.h around lines 446-463, where ICplxTrans displacement is read as R (double) but written as D (int). Start by reproducing the Python round trip from the issue and checking the displacement after from_bytes. Done when ICplxTrans(Trans(x=10000, y=-2000)) survives to_bytes/from_bytes unchanged and the existing binary format still works.

Written by the indexing model from the issue text.

Description

Hi Matthias,

I found an asymmetry in ICplxTrans.to_bytes() and ICPlxTrans.from_bytes (a clanker helped me analyze the precise error which seems to come from klayout).

Small clanker analysis bleow:

In KLayout 0.30.13, an ICplxTrans with a displacement does not survive a to_bytes() / from_bytes() round trip.

import klayout.db as kdb

original = kdb.ICplxTrans(kdb.Trans(x=10000, y=-2000))
restored = kdb.ICplxTrans.from_bytes(original.to_bytes())

print(original)  # r0 *1 10000,-2000
print(restored)  # r0 *1 0,nan
assert restored == original  # fails

The displacement is present in the serialized bytes. In dbBinarySerialize.h (https://github.com/KLayout/klayout/blob/v0.30.13/src/db/db/dbBinarySerialize.h#L446-L463), the writer stores it using the output coordinate type D, but the reader reads it using the internal representation type R. For ICplxTrans, those are integer and double respectively, so the coordinate bytes are interpreted as floating point values.

Suggested minimal fix for the reader:

-  db::vector<R> d;
+  db::vector<D> d;
   read_binary (s, d);

   double asin, acos, mag;
   s >> asin >> acos >> mag;

-  t = db::complex_trans<C, D, R> (d, asin, acos, mag);
+  t = db::complex_trans<C, D, R> (db::vector<R> (d), asin, acos, mag);

This keeps the existing binary format and reads its displacement using the type with which it was written.

Dominant language
C++
Stars
1.2k
Forks
305
Avg merge
3d 2h
Merged PRs (30d)
11

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from KLayout/klayout

All issues in KLayout/klayout

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.