/src/poppler/splash/SplashMath.h
Line | Count | Source |
1 | | //======================================================================== |
2 | | // |
3 | | // SplashMath.h |
4 | | // |
5 | | //======================================================================== |
6 | | |
7 | | //======================================================================== |
8 | | // |
9 | | // Modified under the Poppler project - http://poppler.freedesktop.org |
10 | | // |
11 | | // All changes made under the Poppler project to this file are licensed |
12 | | // under GPL version 2 or later |
13 | | // |
14 | | // Copyright (C) 2009-2011, 2025, 2026 Albert Astals Cid <aacid@kde.org> |
15 | | // Copyright (C) 2017 Adrian Johnson <ajohnson@redneon.com> |
16 | | // Copyright (C) 2020 Jean Ghali <jghali@libertysurf.fr> |
17 | | // |
18 | | // To see a description of the changes please see the Changelog file that |
19 | | // came with your tarball or type make ChangeLog if you are building from git |
20 | | // |
21 | | //======================================================================== |
22 | | |
23 | | #ifndef SPLASHMATH_H |
24 | | #define SPLASHMATH_H |
25 | | |
26 | | #include "poppler-config.h" |
27 | | |
28 | | #include <cmath> |
29 | | #include "SplashTypes.h" |
30 | | |
31 | | static inline double splashAbs(double x) |
32 | 0 | { |
33 | 0 | return fabs(x); |
34 | 0 | } Unexecuted instantiation: SplashOutputDev.cc:splashAbs(double) Unexecuted instantiation: Splash.cc:splashAbs(double) Unexecuted instantiation: SplashClip.cc:splashAbs(double) Unexecuted instantiation: SplashFontEngine.cc:splashAbs(double) Unexecuted instantiation: SplashXPath.cc:splashAbs(double) Unexecuted instantiation: SplashXPathScanner.cc:splashAbs(double) Unexecuted instantiation: SplashFTFont.cc:splashAbs(double) |
35 | | |
36 | | static inline int splashFloor(double x) |
37 | 0 | { |
38 | | #if defined(__GNUC__) && defined(__i386__) |
39 | | // floor() and (int)() are implemented separately, which results |
40 | | // in changing the FPCW multiple times - so we optimize it with |
41 | | // some inline assembly |
42 | | unsigned short oldCW, newCW, t; |
43 | | int result; |
44 | | |
45 | | __asm__ volatile("fldl %4\n" |
46 | | "fnstcw %0\n" |
47 | | "movw %0, %3\n" |
48 | | "andw $0xf3ff, %3\n" |
49 | | "orw $0x0400, %3\n" |
50 | | "movw %3, %1\n" // round down |
51 | | "fldcw %1\n" |
52 | | "fistpl %2\n" |
53 | | "fldcw %0\n" |
54 | | : "=m"(oldCW), "=m"(newCW), "=m"(result), "=r"(t) |
55 | | : "m"(x)); |
56 | | return result; |
57 | | #elif defined(_WIN32) && defined(_M_IX86) |
58 | | // floor() and (int)() are implemented separately, which results |
59 | | // in changing the FPCW multiple times - so we optimize it with |
60 | | // some inline assembly |
61 | | unsigned short oldCW, newCW; |
62 | | int result; |
63 | | |
64 | | __asm fld QWORD PTR x; |
65 | | __asm fnstcw WORD PTR oldCW; |
66 | | __asm mov ax, WORD PTR oldCW; |
67 | | __asm and ax, 0xf3ff; |
68 | | __asm or ax, 0x0400; |
69 | | __asm mov WORD PTR newCW, ax; // round down |
70 | | __asm fldcw WORD PTR newCW; |
71 | | __asm fistp DWORD PTR result; |
72 | | __asm fldcw WORD PTR oldCW; |
73 | | return result; |
74 | | #else |
75 | 0 | if (x > 0) { |
76 | 0 | return static_cast<int>(x); |
77 | 0 | } |
78 | 0 | return static_cast<int>(floor(x)); |
79 | |
|
80 | 0 | #endif |
81 | 0 | } Unexecuted instantiation: SplashOutputDev.cc:splashFloor(double) Unexecuted instantiation: Splash.cc:splashFloor(double) Unexecuted instantiation: SplashClip.cc:splashFloor(double) Unexecuted instantiation: SplashFontEngine.cc:splashFloor(double) Unexecuted instantiation: SplashXPath.cc:splashFloor(double) Unexecuted instantiation: SplashXPathScanner.cc:splashFloor(double) Unexecuted instantiation: SplashFTFont.cc:splashFloor(double) |
82 | | |
83 | | static inline int splashCeil(double x) |
84 | 0 | { |
85 | | #if defined(__GNUC__) && defined(__i386__) |
86 | | // ceil() and (int)() are implemented separately, which results |
87 | | // in changing the FPCW multiple times - so we optimize it with |
88 | | // some inline assembly |
89 | | unsigned short oldCW, newCW, t; |
90 | | int result; |
91 | | |
92 | | __asm__ volatile("fldl %4\n" |
93 | | "fnstcw %0\n" |
94 | | "movw %0, %3\n" |
95 | | "andw $0xf3ff, %3\n" |
96 | | "orw $0x0800, %3\n" |
97 | | "movw %3, %1\n" // round up |
98 | | "fldcw %1\n" |
99 | | "fistpl %2\n" |
100 | | "fldcw %0\n" |
101 | | : "=m"(oldCW), "=m"(newCW), "=m"(result), "=r"(t) |
102 | | : "m"(x)); |
103 | | return result; |
104 | | #elif defined(_WIN32) && defined(_M_IX86) |
105 | | // ceil() and (int)() are implemented separately, which results |
106 | | // in changing the FPCW multiple times - so we optimize it with |
107 | | // some inline assembly |
108 | | unsigned short oldCW, newCW; |
109 | | int result; |
110 | | |
111 | | __asm fld QWORD PTR x; |
112 | | __asm fnstcw WORD PTR oldCW; |
113 | | __asm mov ax, WORD PTR oldCW; |
114 | | __asm and ax, 0xf3ff; |
115 | | __asm or ax, 0x0800; |
116 | | __asm mov WORD PTR newCW, ax; // round up |
117 | | __asm fldcw WORD PTR newCW; |
118 | | __asm fistp DWORD PTR result; |
119 | | __asm fldcw WORD PTR oldCW; |
120 | | return result; |
121 | | #else |
122 | 0 | return static_cast<int>(ceil(x)); |
123 | 0 | #endif |
124 | 0 | } Unexecuted instantiation: SplashOutputDev.cc:splashCeil(double) Unexecuted instantiation: Splash.cc:splashCeil(double) Unexecuted instantiation: SplashClip.cc:splashCeil(double) Unexecuted instantiation: SplashFontEngine.cc:splashCeil(double) Unexecuted instantiation: SplashXPath.cc:splashCeil(double) Unexecuted instantiation: SplashXPathScanner.cc:splashCeil(double) Unexecuted instantiation: SplashFTFont.cc:splashCeil(double) |
125 | | |
126 | | static inline int splashRound(double x) |
127 | 0 | { |
128 | | #if defined(__GNUC__) && defined(__i386__) |
129 | | // this could use round-to-nearest mode and avoid the "+0.5", |
130 | | // but that produces slightly different results (because i+0.5 |
131 | | // sometimes rounds up and sometimes down using the even rule) |
132 | | unsigned short oldCW, newCW, t; |
133 | | int result; |
134 | | |
135 | | x += 0.5; |
136 | | __asm__ volatile("fldl %4\n" |
137 | | "fnstcw %0\n" |
138 | | "movw %0, %3\n" |
139 | | "andw $0xf3ff, %3\n" |
140 | | "orw $0x0400, %3\n" |
141 | | "movw %3, %1\n" // round down |
142 | | "fldcw %1\n" |
143 | | "fistpl %2\n" |
144 | | "fldcw %0\n" |
145 | | : "=m"(oldCW), "=m"(newCW), "=m"(result), "=r"(t) |
146 | | : "m"(x)); |
147 | | return result; |
148 | | #elif defined(_WIN32) && defined(_M_IX86) |
149 | | // this could use round-to-nearest mode and avoid the "+0.5", |
150 | | // but that produces slightly different results (because i+0.5 |
151 | | // sometimes rounds up and sometimes down using the even rule) |
152 | | unsigned short oldCW, newCW; |
153 | | int result; |
154 | | |
155 | | x += 0.5; |
156 | | __asm fld QWORD PTR x; |
157 | | __asm fnstcw WORD PTR oldCW; |
158 | | __asm mov ax, WORD PTR oldCW; |
159 | | __asm and ax, 0xf3ff; |
160 | | __asm or ax, 0x0400; |
161 | | __asm mov WORD PTR newCW, ax; // round down |
162 | | __asm fldcw WORD PTR newCW; |
163 | | __asm fistp DWORD PTR result; |
164 | | __asm fldcw WORD PTR oldCW; |
165 | | return result; |
166 | | #else |
167 | 0 | return splashFloor(x + 0.5); |
168 | 0 | #endif |
169 | 0 | } Unexecuted instantiation: SplashOutputDev.cc:splashRound(double) Unexecuted instantiation: Splash.cc:splashRound(double) Unexecuted instantiation: SplashClip.cc:splashRound(double) Unexecuted instantiation: SplashFontEngine.cc:splashRound(double) Unexecuted instantiation: SplashXPath.cc:splashRound(double) Unexecuted instantiation: SplashXPathScanner.cc:splashRound(double) Unexecuted instantiation: SplashFTFont.cc:splashRound(double) |
170 | | |
171 | | static inline double splashAvg(double x, double y) |
172 | 0 | { |
173 | 0 | return 0.5 * (x + y); |
174 | 0 | } Unexecuted instantiation: SplashOutputDev.cc:splashAvg(double, double) Unexecuted instantiation: Splash.cc:splashAvg(double, double) Unexecuted instantiation: SplashClip.cc:splashAvg(double, double) Unexecuted instantiation: SplashFontEngine.cc:splashAvg(double, double) Unexecuted instantiation: SplashXPath.cc:splashAvg(double, double) Unexecuted instantiation: SplashXPathScanner.cc:splashAvg(double, double) Unexecuted instantiation: SplashFTFont.cc:splashAvg(double, double) |
175 | | |
176 | | static inline double splashSqrt(double x) |
177 | 0 | { |
178 | 0 | return sqrt(x); |
179 | 0 | } Unexecuted instantiation: SplashOutputDev.cc:splashSqrt(double) Unexecuted instantiation: Splash.cc:splashSqrt(double) Unexecuted instantiation: SplashClip.cc:splashSqrt(double) Unexecuted instantiation: SplashFontEngine.cc:splashSqrt(double) Unexecuted instantiation: SplashXPath.cc:splashSqrt(double) Unexecuted instantiation: SplashXPathScanner.cc:splashSqrt(double) Unexecuted instantiation: SplashFTFont.cc:splashSqrt(double) |
180 | | |
181 | | static inline double splashPow(double x, double y) |
182 | 0 | { |
183 | 0 | return pow(x, y); |
184 | 0 | } Unexecuted instantiation: SplashOutputDev.cc:splashPow(double, double) Unexecuted instantiation: Splash.cc:splashPow(double, double) Unexecuted instantiation: SplashClip.cc:splashPow(double, double) Unexecuted instantiation: SplashFontEngine.cc:splashPow(double, double) Unexecuted instantiation: SplashXPath.cc:splashPow(double, double) Unexecuted instantiation: SplashXPathScanner.cc:splashPow(double, double) Unexecuted instantiation: SplashFTFont.cc:splashPow(double, double) |
185 | | |
186 | | static inline double splashDist(double x0, double y0, double x1, double y1) |
187 | 0 | { |
188 | 0 | double dx, dy; |
189 | 0 | dx = x1 - x0; |
190 | 0 | dy = y1 - y0; |
191 | 0 | return splashSqrt(dx * dx + dy * dy); |
192 | 0 | } Unexecuted instantiation: SplashOutputDev.cc:splashDist(double, double, double, double) Unexecuted instantiation: Splash.cc:splashDist(double, double, double, double) Unexecuted instantiation: SplashClip.cc:splashDist(double, double, double, double) Unexecuted instantiation: SplashFontEngine.cc:splashDist(double, double, double, double) Unexecuted instantiation: SplashXPath.cc:splashDist(double, double, double, double) Unexecuted instantiation: SplashXPathScanner.cc:splashDist(double, double, double, double) Unexecuted instantiation: SplashFTFont.cc:splashDist(double, double, double, double) |
193 | | |
194 | | static inline bool splashCheckDet(double m11, double m12, double m21, double m22, double epsilon) |
195 | 0 | { |
196 | 0 | return fabs(m11 * m22 - m12 * m21) >= epsilon; |
197 | 0 | } Unexecuted instantiation: SplashOutputDev.cc:splashCheckDet(double, double, double, double, double) Unexecuted instantiation: Splash.cc:splashCheckDet(double, double, double, double, double) Unexecuted instantiation: SplashClip.cc:splashCheckDet(double, double, double, double, double) Unexecuted instantiation: SplashFontEngine.cc:splashCheckDet(double, double, double, double, double) Unexecuted instantiation: SplashXPath.cc:splashCheckDet(double, double, double, double, double) Unexecuted instantiation: SplashXPathScanner.cc:splashCheckDet(double, double, double, double, double) Unexecuted instantiation: SplashFTFont.cc:splashCheckDet(double, double, double, double, double) |
198 | | |
199 | | #endif |