Debian Codegraph @

texk/dvipdfm-x/numbers.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata,
4 the dvipdfmx project team.
5
6 Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "system.h"
28 #include "error.h"
29 #include "mfileio.h"
30 #include "numbers.h"
31
32 unsigned char get_unsigned_byte (FILE *file)
33 {
34 int ch;
35 if ((ch = fgetc (file)) < 0)
36 ERROR ("File ended prematurely\n");
37 return (unsigned char) ch;
38 }
39
40 void skip_bytes (unsigned int n, FILE *file)
41 {
42 while (n-- > 0)
43 get_unsigned_byte(file);
44 }
45
46 signed char get_signed_byte (FILE *file)
47 {
48 int byte;
49 byte = get_unsigned_byte(file);
50 if (byte >= 0x80)
51 byte -= 0x100;
52 return (signed char) byte;
53 }
54
55 unsigned short get_unsigned_pair (FILE *file)
56 {
57 unsigned short pair = get_unsigned_byte(file);
58 pair = (pair << 8) | get_unsigned_byte(file);
59 return pair;
60 }
61
62 unsigned short sget_unsigned_pair (unsigned char *s)
63 {
64 unsigned short pair = *(s++);
65 pair = (pair << 8) | *(s++);
66 return pair;
67 }
68
69 signed short get_signed_pair (FILE *file)
70 {
71 signed short pair = get_signed_byte(file);
72 pair = (pair << 8) | get_unsigned_byte(file);
73 return pair;
74 }
75
76
77 unsigned int get_unsigned_triple(FILE *file)
78 {
79 int i;
80 unsigned int triple = 0;
81 for (i=0; i<3; i++) {
82 triple = (triple << 8) | get_unsigned_byte(file);
83 }
84 return triple;
85 }
86
87 signed int get_signed_triple(FILE *file)
88 {
89 int i;
90 signed int triple = get_signed_byte(file);
91 for (i=0; i<2; i++) {
92 triple = (triple << 8) | get_unsigned_byte(file);
93 }
94 return triple;
95 }
96
97 int32_t get_signed_quad(FILE *file)
98 {
99 int i;
100 int32_t quad = get_signed_byte(file);
101 for (i=0; i<3; i++) {
102 quad = (quad << 8) | get_unsigned_byte(file);
103 }
104 return quad;
105 }
106
107 uint32_t get_unsigned_quad(FILE *file)
108 {
109 int i;
110 uint32_t quad = 0;
111 for (i=0; i<4; i++) {
112 quad = (quad << 8) | get_unsigned_byte(file);
113 }
114 return quad;
115 }
116
117 int32_t get_unsigned_num (FILE *file, unsigned char num)
118 {
119 int32_t val = get_unsigned_byte (file);
120 switch (num) {
121 case 3: if (val > 0x7f)
122 val -= 0x100;
123 val = (val << 8) | get_unsigned_byte (file);
124 case 2: val = (val << 8) | get_unsigned_byte (file);
125 case 1: val = (val << 8) | get_unsigned_byte (file);
126 default: break;
127 }
128 return val;
129 }
130
131 /* Compute a signed quad that must be positive */
132 uint32_t get_positive_quad (FILE *file, const char *type, const char *name)
133 {
134 int32_t val = get_signed_quad (file);
135 if (val < 0)
136 ERROR ("Bad %s: negative %s: %d", type, name, val);
137 return (uint32_t)val;
138 }
139
140 int32_t sqxfw (int32_t sq, fixword fw)
141 {
142 int sign = 1;
143 uint32_t a, b, c, d, ad, bd, bc, ac;
144 uint32_t e, f, g, h, i, j, k;
145 int32_t result;
146 /* Make positive. */
147 if (sq < 0) {
148 sign = -sign;
149 sq = -sq;
150 }
151 if (fw < 0) {
152 sign = -sign;
153 fw = -fw;
154 }
155 a = ((uint32_t) sq) >> 16;
156 b = ((uint32_t) sq) & 0xffffu;
157 c = ((uint32_t) fw) >> 16;
158 d = ((uint32_t) fw) & 0xffffu;
159 ad = a*d; bd = b*d; bc = b*c; ac = a*c;
160 e = bd >> 16;
161 f = ad >> 16;
162 g = ad & 0xffffu;
163 h = bc >> 16;
164 i = bc & 0xffffu;
165 j = ac >> 16;
166 k = ac & 0xffffu;
167 result = (e+g+i + (1<<3)) >> 4; /* 1<<3 is for rounding */
168 result += (f+h+k) << 12;
169 result += j << 28;
170 return (sign > 0) ? result : -result;
171 }