~ubuntu-branches/ubuntu/jaunty/ghostscript/jaunty-updates

« back to all changes in this revision

Viewing changes to base/ttcalc.c

  • Committer: Bazaar Package Importer
  • Author(s): Till Kamppeter
  • Date: 2009-01-20 16:40:45 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090120164045-lnfhi0n30o5lwhwa
Tags: 8.64.dfsg.1~svn9377-0ubuntu1
* New upstream release (SVN rev 9377)
   o Fixes many bugs concerning PDF rendering, to make the PDF printing
     workflow correctly working.
   o Fixes long-standing bugs in many drivers, like input paper tray and
     duplex options not working for the built-in PCL 4, 5, 5c, 5e, and
     6/XL drivers, PDF input not working for bjc600, bjc800, and cups
     output devices, several options not working and uninitialized
     memory with cups output device.
   o Merged nearly all patches of the Ubuntu and Debian packages upstream.
   o Fixes LP: #317810, LP: #314439, LP: #314018.
* debian/patches/03_libpaper_support.dpatch,
  debian/patches/11_gs-cjk_font_glyph_handling_fix.dpatch,
  debian/patches/12_gs-cjk_vertical_writing_metrics_fix.dpatch,
  debian/patches/13_gs-cjk_cjkps_examples.dpatch,
  debian/patches/20_bbox_segv_fix.dpatch,
  debian/patches/21_brother_7x0_gdi_fix.dpatch,
  debian/patches/22_epsn_margin_workaround.dpatch,
  debian/patches/24_gs_man_fix.dpatch,
  debian/patches/25_toolbin_insecure_tmp_usage_fix.dpatch,
  debian/patches/26_assorted_script_fixes.dpatch,
  debian/patches/29_gs_css_fix.dpatch,
  debian/patches/30_ps2pdf_man_improvement.dpatch,
  debian/patches/31_fix-gc-sigbus.dpatch,
  debian/patches/34_ftbfs-on-hurd-fix.dpatch,
  debian/patches/35_disable_libcairo.dpatch,
  debian/patches/38_pxl-duplex.dpatch,
  debian/patches/39_pxl-resolution.dpatch,
  debian/patches/42_gs-init-ps-delaybind-fix.dpatch,
  debian/patches/45_bjc600-bjc800-pdf-input.dpatch,
  debian/patches/48_cups-output-device-pdf-duplex-uninitialized-memory-fix.dpatch,
  debian/patches/50_lips4-floating-point-exception.dpatch,
  debian/patches/52_cups-device-logging.dpatch,
  debian/patches/55_pcl-input-slot-fix.dpatch,
  debian/patches/57_pxl-input-slot-fix.dpatch,
  debian/patches/60_pxl-cups-driver-pdf.dpatch,
  debian/patches/62_onebitcmyk-pdf.dpatch,
  debian/patches/65_too-big-temp-files-1.dpatch,
  debian/patches/67_too-big-temp-files-2.dpatch,
  debian/patches/70_take-into-account-data-in-stream-buffer-before-refill.dpatch:
  Removed, applied upstream.
* debian/patches/01_docdir_fix_for_debian.dpatch,
  debian/patches/02_gs_man_fix_debian.dpatch,
  debian/patches/01_docdir-fix-for-debian.dpatch,
  debian/patches/02_docdir-fix-for-debian.dpatch: Renamed patches to
  make merging with Debian easier.
* debian/patches/32_improve-handling-of-media-size-changes-from-gv.dpatch, 
  debian/patches/33_bad-params-to-xinitimage-on-large-bitmaps.dpatch:
  regenerated for new source directory structure.
* debian/rules: Corrected paths to remove cidfmap (it is in Resource/Init/
  in GS 8.64) and to install headers (source paths are psi/ and base/ now).
* debian/rules: Remove all fontmaps, as DeFoMa replaces them.
* debian/local/pdftoraster/pdftoraster.c,
  debian/local/pdftoraster/pdftoraster.convs, debian/rules: Removed
  added pdftoraster filter and use the one which comes with Ghostscript.
* debian/ghostscript.links: s/8.63/8.64/

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
 
2
   All Rights Reserved.
 
3
  
 
4
   This software is provided AS-IS with no warranty, either express or
 
5
   implied.
 
6
 
 
7
   This software is distributed under license and may not be copied, modified
 
8
   or distributed except as expressly authorized under the terms of that
 
9
   license.  Refer to licensing information at http://www.artifex.com/
 
10
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
 
11
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
 
12
*/
 
13
 
 
14
/* $Id: ttcalc.c 8250 2007-09-25 13:31:24Z giles $ */
 
15
 
 
16
/* Changes after FreeType: cut out the TrueType instruction interpreter. */
 
17
 
 
18
/*******************************************************************
 
19
 *
 
20
 *  ttcalc.c
 
21
 *
 
22
 *    Arithmetic Computations (body).
 
23
 *
 
24
 *  Copyright 1996-1998 by
 
25
 *  David Turner, Robert Wilhelm, and Werner Lemberg.
 
26
 *
 
27
 *  This file is part of the FreeType project, and may only be used
 
28
 *  modified and distributed under the terms of the FreeType project
 
29
 *  license, LICENSE.TXT.  By continuing to use, modify, or distribute 
 
30
 *  this file you indicate that you have read the license and
 
31
 *  understand and accept it fully.
 
32
 *
 
33
 ******************************************************************/
 
34
 
 
35
#include "ttmisc.h"
 
36
 
 
37
#include "ttcalc.h"
 
38
 
 
39
/* support for 1-complement arithmetic has been totally dropped in this */
 
40
/* release. You can still write your own code if you need it..          */
 
41
 
 
42
  static const long  Roots[63] =
 
43
  {
 
44
       1,    1,    2,     3,     4,     5,     8,    11,
 
45
      16,   22,   32,    45,    64,    90,   128,   181,
 
46
     256,  362,  512,   724,  1024,  1448,  2048,  2896,
 
47
    4096, 5892, 8192, 11585, 16384, 23170, 32768, 46340,
 
48
 
 
49
      65536,   92681,  131072,   185363,   262144,   370727,
 
50
     524288,  741455, 1048576,  1482910,  2097152,  2965820,
 
51
    4194304, 5931641, 8388608, 11863283, 16777216, 23726566,
 
52
 
 
53
      33554432,   47453132,   67108864,   94906265,
 
54
     134217728,  189812531,  268435456,  379625062,
 
55
     536870912,  759250125, 1073741824, 1518500250,
 
56
    2147483647
 
57
  };
 
58
 
 
59
 
 
60
#ifdef LONG64
 
61
 
 
62
  Int32  MulDiv( Int32  a, Int32  b, Int32  c )
 
63
  {
 
64
    Int32  s;
 
65
 
 
66
    s  = a; a = ABS(a);
 
67
    s ^= b; b = ABS(b);
 
68
    s ^= c; c = ABS(c);
 
69
 
 
70
    a = (Int64)a * b / c;
 
71
    return ((s < 0) ? -a : a);
 
72
  }
 
73
 
 
74
 
 
75
  Int32  MulDiv_Round( Int32  a, Int32  b, Int32  c )
 
76
  {
 
77
    int  s;
 
78
 
 
79
    s  = a; a = ABS(a);
 
80
    s ^= b; b = ABS(b);
 
81
    s ^= c; c = ABS(c);
 
82
 
 
83
    a = ((Int64)a * b + c/2) / c;
 
84
    return ((s < 0) ? -a : a);
 
85
  }
 
86
 
 
87
 
 
88
  static Int  Order64( Int64  z )
 
89
  {
 
90
    int  j = 0;
 
91
    while ( z )
 
92
    {
 
93
      z = (unsigned INT64)z >> 1;
 
94
      j++;
 
95
    }
 
96
    return j - 1;
 
97
  }
 
98
 
 
99
 
 
100
  Int32  Sqrt64( Int64  l )
 
101
  {
 
102
    Int64  r, s;
 
103
 
 
104
    if ( l <= 0 ) return 0;
 
105
    if ( l == 1 ) return 1;
 
106
 
 
107
    r = Roots[Order64( l )];
 
108
 
 
109
    do
 
110
    {
 
111
      s = r;
 
112
      r = ( r + l/r ) >> 1;
 
113
    }
 
114
    while ( r > s || r*r > l );
 
115
 
 
116
    return r;
 
117
  }
 
118
 
 
119
#else /* LONG64 */
 
120
 
 
121
  Int32  MulDiv( Int32  a, Int32  b, Int32  c )
 
122
  {
 
123
    Int64  temp;
 
124
    Int32  s;
 
125
 
 
126
    s  = a; a = ABS(a);
 
127
    s ^= b; b = ABS(b);
 
128
    s ^= c; c = ABS(c);
 
129
 
 
130
    MulTo64( a, b, &temp );
 
131
    a = Div64by32( &temp, c );
 
132
 
 
133
    return ((s < 0) ? -a : a);
 
134
  }
 
135
 
 
136
 
 
137
  Int32  MulDiv_Round( Int32  a, Int32  b, Int32  c )
 
138
  {
 
139
    Int64  temp, temp2;
 
140
    Int32  s;
 
141
 
 
142
    s  = a; a = ABS(a);
 
143
    s ^= b; b = ABS(b);
 
144
    s ^= c; c = ABS(c);
 
145
 
 
146
    MulTo64( a, b, &temp );
 
147
    temp2.hi = (Int32)(c >> 31);
 
148
    temp2.lo = (Word32)(c / 2);
 
149
    Add64( &temp, &temp2, &temp );
 
150
    a = Div64by32( &temp, c );
 
151
 
 
152
    return ((s < 0) ? -a : a);
 
153
  }
 
154
 
 
155
 
 
156
  static void  Neg64__( Int64*  x )
 
157
  {
 
158
    /* Remember that -(0x80000000) == 0x80000000 with 2-complement! */
 
159
    /* We take care of that here.                                   */
 
160
 
 
161
    x->hi ^= 0xFFFFFFFF;
 
162
    x->lo ^= 0xFFFFFFFF;
 
163
    x->lo++;
 
164
 
 
165
    if ( !x->lo )
 
166
    {
 
167
      x->hi++;
 
168
      if ( (Int32)x->hi == 0x80000000 )  /* Check -MaxInt32 - 1 */
 
169
      {
 
170
        x->lo--;
 
171
        x->hi--;  /* We return 0x7FFFFFFF! */
 
172
      }
 
173
    }
 
174
  }
 
175
 
 
176
 
 
177
  void  Add64( Int64*  x, Int64*  y, Int64*  z )
 
178
  {
 
179
    register Word32  lo, hi;
 
180
 
 
181
    hi = x->hi + y->hi;
 
182
    lo = x->lo + y->lo;
 
183
 
 
184
    if ( y->lo )
 
185
      if ( (Word32)x->lo >= (Word32)(-y->lo) ) hi++;
 
186
 
 
187
    z->lo = lo;
 
188
    z->hi = hi;
 
189
  }
 
190
 
 
191
 
 
192
  void  Sub64( Int64*  x, Int64*  y, Int64*  z )
 
193
  {
 
194
    register Word32  lo, hi;
 
195
 
 
196
    hi = x->hi - y->hi;
 
197
    lo = x->lo - y->lo;
 
198
 
 
199
    if ( x->lo < y->lo ) hi--;
 
200
 
 
201
    z->lo = lo;
 
202
    z->hi = hi;
 
203
  }
 
204
 
 
205
 
 
206
  void  MulTo64( Int32  x, Int32  y, Int64*  z )
 
207
  {
 
208
    Int32   s;
 
209
    Word32  lo1, hi1, lo2, hi2, lo, hi, i1, i2;
 
210
 
 
211
    s  = x; x = ABS(x);
 
212
    s ^= y; y = ABS(y);
 
213
 
 
214
    lo1 = x & 0x0000FFFF;  hi1 = x >> 16;
 
215
    lo2 = y & 0x0000FFFF;  hi2 = y >> 16;
 
216
 
 
217
    lo = lo1*lo2;
 
218
    i1 = lo1*hi2;
 
219
    i2 = lo2*hi1;
 
220
    hi = hi1*hi2;
 
221
 
 
222
    /* Check carry overflow of i1 + i2 */
 
223
 
 
224
    if ( i2 )
 
225
    {
 
226
      if ( i1 >= (Word32)-i2 ) hi += 1 << 16;
 
227
      i1 += i2;
 
228
    }
 
229
 
 
230
    i2 = i1 >> 16;
 
231
    i1 = i1 << 16;
 
232
 
 
233
    /* Check carry overflow of i1 + lo */
 
234
    if ( i1 )
 
235
    {
 
236
      if ( lo >= (Word32)-i1 ) hi++;
 
237
      lo += i1;
 
238
    }
 
239
 
 
240
    hi += i2;
 
241
 
 
242
    z->lo = lo;
 
243
    z->hi = hi;
 
244
 
 
245
    if (s < 0) Neg64__( z );
 
246
  }
 
247
 
 
248
 
 
249
  Int32  Div64by32( Int64*  x, Int32  y )
 
250
  {
 
251
    Int32   s;
 
252
    Word32  q, r, i, lo;
 
253
 
 
254
    s  = x->hi; if (s<0) Neg64__(x);
 
255
    s ^= y;     y = ABS(y);
 
256
 
 
257
    /* Shortcut */
 
258
    if ( x->hi == 0 )
 
259
    {
 
260
      q = x->lo / y;
 
261
      return ((s<0) ? -q : q);
 
262
    }
 
263
 
 
264
    r  = x->hi;
 
265
    lo = x->lo;
 
266
 
 
267
    if ( r >= (Word32)y )   /* we know y is to be treated as unsigned here */
 
268
      return ( (s<0) ? 0x80000001 : 0x7FFFFFFF );
 
269
                            /* Return Max/Min Int32 if divide overflow */
 
270
                            /* This includes division by zero!         */
 
271
    q = 0;
 
272
    for ( i = 0; i < 32; i++ )
 
273
    {
 
274
      r <<= 1;
 
275
      q <<= 1;
 
276
      r  |= lo >> 31;
 
277
 
 
278
      if ( r >= (Word32)y )
 
279
      {
 
280
        r -= y;
 
281
        q |= 1;
 
282
      }
 
283
      lo <<= 1;
 
284
    }
 
285
 
 
286
    return ( (s<0) ? -q : q );
 
287
  }
 
288
 
 
289
 
 
290
  Int  Order64( Int64*  z )
 
291
  {
 
292
    Word32  i;
 
293
    int     j;
 
294
 
 
295
    if ( z->hi )
 
296
    {
 
297
      i = z->hi;
 
298
      j = 32;
 
299
    }
 
300
    else
 
301
    {
 
302
      i = z->lo;
 
303
      j = 0;
 
304
    }
 
305
 
 
306
    while ( i > 0 )
 
307
    {
 
308
      i >>= 1;
 
309
      j++;
 
310
    }
 
311
    return j-1;
 
312
  }
 
313
 
 
314
 
 
315
  Int32  Sqrt64( Int64*  l )
 
316
  {
 
317
    Int64  l2;
 
318
    Int32  r, s;
 
319
 
 
320
    if ( (Int32)l->hi < 0          ||
 
321
        (l->hi == 0 && l->lo == 0) )  return 0;
 
322
 
 
323
    s = Order64( l );
 
324
    if ( s == 0 ) return 1;
 
325
 
 
326
    r = Roots[s];
 
327
    do
 
328
    {
 
329
      s = r;
 
330
      r = ( r + Div64by32(l,r) ) >> 1;
 
331
      MulTo64( r, r,   &l2 );
 
332
      Sub64  ( l, &l2, &l2 );
 
333
    }
 
334
    while ( r > s || (Int32)l2.hi < 0 );
 
335
 
 
336
    return r;
 
337
  }
 
338
 
 
339
#endif /* LONG64 */
 
340
 
 
341
#if 0  /* unused by the rest of the library */
 
342
 
 
343
  Int  Order32( Int32  z )
 
344
  {
 
345
    int j;
 
346
 
 
347
    j = 0;
 
348
    while ( z )
 
349
    {
 
350
      z = (Word32)z >> 1;
 
351
      j++;
 
352
    }
 
353
    return j - 1;
 
354
  }
 
355
 
 
356
 
 
357
  Int32  Sqrt32( Int32  l )
 
358
  {
 
359
    Int32  r, s;
 
360
 
 
361
    if ( l <= 0 ) return 0;
 
362
    if ( l == 1 ) return 1;
 
363
 
 
364
    r = Roots[Order32( l )];
 
365
    do
 
366
    {
 
367
      s = r;
 
368
      r = ( r + l/r ) >> 1;
 
369
    }
 
370
    while ( r > s || r*r > l );
 
371
    return r;
 
372
  }
 
373
 
 
374
#endif
 
375
 
 
376
/* END */