Graphviz 16.1.1~dev.20261012.0359
Loading...
Searching...
No Matches
gv_math.h
Go to the documentation of this file.
1
4
5#pragma once
6
7#include <assert.h>
8#include <float.h>
9#include <limits.h>
10#include <stdbool.h>
11#include <stddef.h>
12#include <string.h>
13
15static inline int fcmp(double a, double b) {
16 if (a < b) {
17 return -1;
18 }
19 if (a > b) {
20 return 1;
21 }
22 return 0;
23}
24
26static inline int imax(int a, int b) { return a > b ? a : b; }
27
29static inline size_t zmax(size_t a, size_t b) { return a > b ? a : b; }
30
32static inline size_t zmin(size_t a, size_t b) { return a < b ? a : b; }
33
35static inline int imin(int a, int b) { return a < b ? a : b; }
36
49static inline bool is_exactly_equal(double a, double b) {
50 return memcmp(&a, &b, sizeof(a)) == 0;
51}
52
66static inline bool is_exactly_pos_zero(double v) {
67 return is_exactly_equal(v, 0);
68}
69
75static inline int scale_clamp(int original, double scale) {
76 assert(original >= 0);
77
78 if (scale < 0) {
79 return 0;
80 }
81
82 if (scale > 1 && original > INT_MAX / scale) {
83 return INT_MAX;
84 }
85
86 return (int)(original * scale);
87}
88
90enum { BYTES_PER_PIXEL = 4 };
91
108static inline void argb2rgba(size_t width, size_t height, unsigned char *data) {
109 assert(data != NULL || (width == 0 && height == 0));
110
111 // indices to color bytes in each format
112 enum { Ba = 0, Ga = 1, Ra = 2, Aa = 3 };
113 enum { Rb = 0, Gb = 1, Bb = 2, Ab = 3 };
114
115 for (size_t y = 0; y < height; ++y) {
116 for (size_t x = 0; x < width; ++x) {
117 const unsigned char red = data[Ra];
118 const unsigned char blue = data[Ba];
119 data[Rb] = red;
120 data[Bb] = blue;
121 data += BYTES_PER_PIXEL;
122 }
123 }
124}
125
133#define SWAP(a, b) \
134 do { \
135 /* trigger a -Wcompare-distinct-pointer-types compiler warning if `a` */ \
136 /* and `b` have differing types */ \
137 (void)((a) == (b)); \
138 \
139 /* Swap their targets. Contemporary compilers will optimize the `memcpy`s \
140 * into direct writes for primitive types. \
141 */ \
142 char tmp_[sizeof(*(a))]; \
143 memcpy(tmp_, (a), sizeof(*(a))); \
144 *(a) = *(b); \
145 memcpy((b), tmp_, sizeof(*(b))); \
146 } while (0)
147
155static inline int d2i(double v) {
156 if (v > INT_MAX) {
157 return INT_MAX;
158 }
159 if (v < INT_MIN) {
160 return INT_MIN;
161 }
162 return (int)v;
163}
164
174static inline float d2f(double v) {
175 if (v > FLT_MAX) {
176 return FLT_MAX;
177 }
178 if (v < -FLT_MAX) {
179 return -FLT_MAX;
180 }
181 return (float)v;
182}
static WUR pointf scale(double c, pointf p)
Definition geomprocs.h:148
node NULL
Definition grammar.y:181
static int d2i(double v)
Definition gv_math.h:153
@ BYTES_PER_PIXEL
Definition gv_math.h:90
static int scale_clamp(int original, double scale)
scale up or down a non-negative integer, clamping to [0, INT_MAX]
Definition gv_math.h:75
static size_t zmin(size_t a, size_t b)
minimum of two sizes
Definition gv_math.h:32
static bool is_exactly_equal(double a, double b)
are two values precisely the same?
Definition gv_math.h:49
static int imin(int a, int b)
minimum of two integers
Definition gv_math.h:35
static bool is_exactly_pos_zero(double v)
is a value precisely +0.0?
Definition gv_math.h:66
static int fcmp(double a, double b)
comparator for doubles
Definition gv_math.h:15
static size_t zmax(size_t a, size_t b)
maximum of two sizes
Definition gv_math.h:29
static void argb2rgba(size_t width, size_t height, unsigned char *data)
Definition gv_math.h:108
static int imax(int a, int b)
maximum of two integers
Definition gv_math.h:26
static float d2f(double v)
Definition gv_math.h:172