DEF defines the units it uses with the UNITS
command.
UNITS DISTANCE MICRONS 1000 ;
Typically the units are 1000 or 2000 database units (DBU) per micron. DBUs are integers, so the distance resolution is typically 0.001 um or 1nm.
OpenDB uses an int
to represent a DBU, which on most hardware is 4
bytes. This means a database coordinate can be
This section is important as we cover important math considerations for your datatype choice when dealing with large numbers.
Since chip coordinates cannot be negative, it would make sense to use an
unsigned int
to represent a distance. This conveys the fact that it
can never be negative and doubles the maximum possible distance that can
be represented. The problem, however, is that doing subtraction with unsigned numbers
is dangerous because the differences can be negative. An unsigned
negative number looks like a very very big number. So this is a very bad
idea and leads to bugs.
Note that calculating an area with int
values is problematic. An
int * int
does not fit in an int
. Our suggestion is to use int64_t
in this situation. Although long
"works", its size is implementation-dependent.
It has been noticed that some programs use double
to calculate distances.
This can be problematic, as double
have a mantissa of 52 bits, which means that
the largest possible integer value that can be represented without loss is int64_t
. As a result, if you are doing an area calculation on a large chip
that is more than
Not only is a double
less capable than an int64_t
, but using it
tells any reader of the code that the value can be a real number, such as
Circling back to LEF, we see that unlike DEF the distances are real
numbers like 1.3 even though LEF also has a distance unit statement. We
suspect this is a historical artifact of a mistake made in the early
definition of the LEF file format. The reason it is a mistake is because
decimal fractions cannot be represented exactly in binary floating-point.
For example,
OpenDB uses int
to represent LEF distances, just as with DEF. This solves
the problem by multiplying distances by a decimal constant (distance
units) to convert the distance to an integer. In the future I would like
to see OpenDB use a dbu
typedef instead of int
everywhere.
We have also noticed RePlAce, OpenDP, TritonMacroPlace and OpenNPDN all using
double
or float
to represent distances. This can be problematic, as
floating-point numbers cannot always represent exact fractions. As a result,
these tools need to round
or floor
the results of their calculations, which
can introduce errors. Additionally, some of these tools reinvent the wheel
by implementing their own rounding functions, as we shall see in the example below.
This can lead to inconsistencies and is highly discouraged.
(int) x_coord + 0.5
Worse than using a double
is using a float
, because the mantissa
is only 23 bits, so the maximum exactly representable integer is int
.
When a value has to be snapped to a grid such as the pitch of a layer,
the calculation can be done with a simple divide using int
, which
floor
the result. For example, to snap a coordinate to the pitch of a
layer the following can be used:
int x, y;
inst->getOrigin(x, y);
int pitch = layer->getPitch();
int x_snap = (x / pitch) * pitch;
The use of rounding in existing code that uses floating-point
representations is to compensate for the inability to represent floating-point
fractions exactly. Results like