2 * \file src/hid/gcode/auxiliary.h
4 * \brief This header file collects some general-purpose macros (and
5 * static inline functions) that are used in various places.
9 * <h1><b>Copyright.</b></h1>\n
11 * PCB, interactive printed circuit board design
13 * Copyright (C) 2001-2007 Peter Selinger.
15 * This file is part of Potrace. It is free software and it is covered
16 * by the GNU General Public License. See the file COPYING for details.
26 /* ---------------------------------------------------------------------- */
27 /* point arithmetic */
29 #include "potracelib.h"
36 typedef struct point_s point_t
;
38 typedef potrace_dpoint_t dpoint_t
;
41 * \brief Convert point_t to dpoint_t.
43 static inline dpoint_t
53 * \brief Range over the straight line segment [a,b] when lambda ranges
56 static inline dpoint_t
57 interval (double lambda
, dpoint_t a
, dpoint_t b
)
61 res
.x
= a
.x
+ lambda
* (b
.x
- a
.x
);
62 res
.y
= a
.y
+ lambda
* (b
.y
- a
.y
);
66 /* ---------------------------------------------------------------------- */
67 /* some useful macros. */
69 /* integer arithmetic */
74 * The "mod" macro works correctly for negative a.
75 * Also note that the test for a>=n, while redundant, speeds up the mod
76 * function by 70% in the average case (significant since the program
77 * spends about 16% of its time here - or 40% without the test).
82 return a
>= n
? a
% n
: a
>= 0 ? a
: n
- 1 - (-1 - a
) % n
;
86 * \brief The "floordiv" macro returns the largest integer
87 * <= a/n, and again this works correctly for negative a, as long as
88 * a,n are integers and n>0.
91 floordiv (int a
, int n
)
93 return a
>= 0 ? a
/ n
: -1 - (-1 - a
) / n
;
96 /* Note: the following work for integers and other numeric types. */
103 #define sign(x) ((x)>0 ? 1 : (x)<0 ? -1 : 0)
104 #define abs(a) ((a)>0 ? (a) : -(a))
105 #define min(a,b) ((a)<(b) ? (a) : (b))
106 #define max(a,b) ((a)>(b) ? (a) : (b))
107 #define sq(a) ((a)*(a))
108 #define cu(a) ((a)*(a)*(a))
110 #endif /* AUXILIARY_H */