Back to index

lightning-sunbird  0.9+nobinonly
cairo-wideint.h
Go to the documentation of this file.
00001 /*
00002  * $Id: cairo-wideint.h,v 1.6.4.1 2005/09/27 23:09:01 vladimir%pobox.com Exp $
00003  *
00004  * Copyright © 2004 Keith Packard
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it either under the terms of the GNU Lesser General Public
00008  * License version 2.1 as published by the Free Software Foundation
00009  * (the "LGPL") or, at your option, under the terms of the Mozilla
00010  * Public License Version 1.1 (the "MPL"). If you do not alter this
00011  * notice, a recipient may use your version of this file under either
00012  * the MPL or the LGPL.
00013  *
00014  * You should have received a copy of the LGPL along with this library
00015  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00017  * You should have received a copy of the MPL along with this library
00018  * in the file COPYING-MPL-1.1
00019  *
00020  * The contents of this file are subject to the Mozilla Public License
00021  * Version 1.1 (the "License"); you may not use this file except in
00022  * compliance with the License. You may obtain a copy of the License at
00023  * http://www.mozilla.org/MPL/
00024  *
00025  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
00026  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
00027  * the specific language governing rights and limitations.
00028  *
00029  * The Original Code is the cairo graphics library.
00030  *
00031  * The Initial Developer of the Original Code is Keith Packard
00032  *
00033  * Contributor(s):
00034  *     Keith R. Packard <keithp@keithp.com>
00035  *
00036  */
00037 
00038 #ifndef CAIRO_WIDEINT_H
00039 #define CAIRO_WIDEINT_H
00040 
00041 #if   HAVE_STDINT_H
00042 # include <stdint.h>
00043 #elif HAVE_INTTYPES_H
00044 # include <inttypes.h>
00045 #elif HAVE_SYS_INT_TYPES_H
00046 # include <sys/int_types.h>
00047 #elif defined(_MSC_VER)
00048   typedef __int8 int8_t;
00049   typedef unsigned __int8 uint8_t;
00050   typedef __int16 int16_t;
00051   typedef unsigned __int16 uint16_t;
00052   typedef __int32 int32_t;
00053   typedef unsigned __int32 uint32_t;
00054   typedef __int64 int64_t;
00055   typedef unsigned __int64 uint64_t;
00056 # ifndef HAVE_UINT64_T
00057 #  define HAVE_UINT64_T 1
00058 # endif
00059 #else
00060 #error Cannot find definitions for fixed-width integral types (uint8_t, uint32_t, etc.)
00061 #endif
00062 
00063 /*
00064  * 64-bit datatypes.  Two separate implementations, one using
00065  * built-in 64-bit signed/unsigned types another implemented
00066  * as a pair of 32-bit ints
00067  */
00068 
00069 #define I cairo_private
00070 
00071 #if !HAVE_UINT64_T
00072 
00073 typedef struct _cairo_uint64 {
00074     uint32_t  lo, hi;
00075 } cairo_uint64_t, cairo_int64_t;
00076 
00077 cairo_uint64_t I     _cairo_uint32_to_uint64 (uint32_t i);
00078 #define                     _cairo_uint64_to_uint32(a)  ((a).lo)
00079 cairo_uint64_t I     _cairo_uint64_add (cairo_uint64_t a, cairo_uint64_t b);
00080 cairo_uint64_t I     _cairo_uint64_sub (cairo_uint64_t a, cairo_uint64_t b);
00081 cairo_uint64_t I     _cairo_uint64_mul (cairo_uint64_t a, cairo_uint64_t b);
00082 cairo_uint64_t I     _cairo_uint32x32_64_mul (uint32_t a, uint32_t b);
00083 cairo_uint64_t I     _cairo_uint64_lsl (cairo_uint64_t a, int shift);
00084 cairo_uint64_t I     _cairo_uint64_rsl (cairo_uint64_t a, int shift);
00085 cairo_uint64_t I     _cairo_uint64_rsa (cairo_uint64_t a, int shift);
00086 int           I      _cairo_uint64_lt (cairo_uint64_t a, cairo_uint64_t b);
00087 int           I      _cairo_uint64_eq (cairo_uint64_t a, cairo_uint64_t b);
00088 cairo_uint64_t I     _cairo_uint64_negate (cairo_uint64_t a);
00089 #define                     _cairo_uint64_negative(a)   (((int32_t) ((a).hi)) < 0)
00090 cairo_uint64_t I     _cairo_uint64_not (cairo_uint64_t a);
00091 
00092 #define                     _cairo_uint64_to_int64(i)   (i)
00093 #define                     _cairo_int64_to_uint64(i)   (i)
00094 
00095 cairo_int64_t  I     _cairo_int32_to_int64(int32_t i);
00096 #define                     _cairo_int64_to_int32(a)    ((int32_t) _cairo_uint64_to_uint32(a))
00097 #define                     _cairo_int64_add(a,b)           _cairo_uint64_add (a,b)
00098 #define                     _cairo_int64_sub(a,b)           _cairo_uint64_sub (a,b)
00099 #define                     _cairo_int64_mul(a,b)           _cairo_uint64_mul (a,b)
00100 cairo_int64_t  I     _cairo_int32x32_64_mul (int32_t a, int32_t b);
00101 int           I      _cairo_int64_lt (cairo_uint64_t a, cairo_uint64_t b);
00102 #define                     _cairo_int64_eq(a,b)     _cairo_uint64_eq (a,b)
00103 #define                     _cairo_int64_lsl(a,b)           _cairo_uint64_lsl (a,b)
00104 #define                     _cairo_int64_rsl(a,b)           _cairo_uint64_rsl (a,b)
00105 #define                     _cairo_int64_rsa(a,b)           _cairo_uint64_rsa (a,b)
00106 #define                     _cairo_int64_negate(a)          _cairo_uint64_negate(a)
00107 #define                     _cairo_int64_negative(a)    (((int32_t) ((a).hi)) < 0)
00108 #define                     _cairo_int64_not(a)      _cairo_uint64_not(a)
00109 
00110 #else
00111 
00112 typedef uint64_t    cairo_uint64_t;
00113 typedef int64_t          cairo_int64_t;
00114 
00115 #define                     _cairo_uint32_to_uint64(i)  ((uint64_t) (i))
00116 #define                     _cairo_uint64_to_uint32(i)  ((uint32_t) (i))
00117 #define                     _cairo_uint64_add(a,b)          ((a) + (b))
00118 #define                     _cairo_uint64_sub(a,b)          ((a) - (b))
00119 #define                     _cairo_uint64_mul(a,b)          ((a) * (b))
00120 #define                     _cairo_uint32x32_64_mul(a,b)       ((uint64_t) (a) * (b))
00121 #define                     _cairo_uint64_lsl(a,b)          ((a) << (b))
00122 #define                     _cairo_uint64_rsl(a,b)          ((uint64_t) (a) >> (b))
00123 #define                     _cairo_uint64_rsa(a,b)          ((uint64_t) ((int64_t) (a) >> (b)))
00124 #define                     _cairo_uint64_lt(a,b)           ((a) < (b))
00125 #define                     _cairo_uint64_eq(a,b)           ((a) == (b))
00126 #define                     _cairo_uint64_negate(a)         ((uint64_t) -((int64_t) (a)))
00127 #define                     _cairo_uint64_negative(a)   ((int64_t) (a) < 0)
00128 #define                     _cairo_uint64_not(a)     (~(a))
00129 
00130 #define                     _cairo_uint64_to_int64(i)   ((int64_t) (i))
00131 #define                     _cairo_int64_to_uint64(i)   ((uint64_t) (i))
00132 
00133 #define                     _cairo_int32_to_int64(i)    ((int64_t) (i))
00134 #define                     _cairo_int64_to_int32(i)    ((int32_t) (i))
00135 #define                     _cairo_int64_add(a,b)           ((a) + (b))
00136 #define                     _cairo_int64_sub(a,b)           ((a) - (b))
00137 #define                     _cairo_int64_mul(a,b)           ((a) * (b))
00138 #define                     _cairo_int32x32_64_mul(a,b) ((int64_t) (a) * (b))
00139 #define                     _cairo_int64_lt(a,b)     ((a) < (b))
00140 #define                     _cairo_int64_eq(a,b)     ((a) == (b))
00141 #define                     _cairo_int64_lsl(a,b)           ((a) << (b))
00142 #define                     _cairo_int64_rsl(a,b)           ((int64_t) ((uint64_t) (a) >> (b)))
00143 #define                     _cairo_int64_rsa(a,b)           ((int64_t) (a) >> (b))
00144 #define                     _cairo_int64_negate(a)          (-(a))
00145 #define                     _cairo_int64_negative(a)    ((a) < 0)
00146 #define                     _cairo_int64_not(a)      (~(a))
00147 
00148 #endif
00149 
00150 /*
00151  * 64-bit comparisions derived from lt or eq
00152  */
00153 #define                     _cairo_uint64_le(a,b)           (!_cairo_uint64_gt(a,b))
00154 #define                     _cairo_uint64_ne(a,b)           (!_cairo_uint64_eq(a,b))
00155 #define                     _cairo_uint64_ge(a,b)           (!_cairo_uint64_lt(a,b))
00156 #define                     _cairo_uint64_gt(a,b)           _cairo_uint64_lt(b,a)
00157 
00158 #define                     _cairo_int64_le(a,b)     (!_cairo_int64_gt(a,b))
00159 #define                     _cairo_int64_ne(a,b)     (!_cairo_int64_eq(a,b))
00160 #define                     _cairo_int64_ge(a,b)     (!_cairo_int64_lt(a,b))
00161 #define                     _cairo_int64_gt(a,b)     _cairo_int64_lt(b,a)
00162 
00163 /*
00164  * As the C implementation always computes both, create
00165  * a function which returns both for the 'native' type as well
00166  */
00167 
00168 typedef struct _cairo_uquorem64 {
00169     cairo_uint64_t   quo;
00170     cairo_uint64_t   rem;
00171 } cairo_uquorem64_t;
00172 
00173 typedef struct _cairo_quorem64 {
00174     cairo_int64_t    quo;
00175     cairo_int64_t    rem;
00176 } cairo_quorem64_t;
00177 
00178 cairo_uquorem64_t I
00179 _cairo_uint64_divrem (cairo_uint64_t num, cairo_uint64_t den);
00180 
00181 cairo_quorem64_t I
00182 _cairo_int64_divrem (cairo_int64_t num, cairo_int64_t den);
00183 
00184 /*
00185  * 128-bit datatypes.  Again, provide two implementations in
00186  * case the machine has a native 128-bit datatype.  GCC supports int128_t
00187  * on ia64
00188  */
00189  
00190 #if !HAVE_UINT128_T
00191 
00192 typedef struct cairo_uint128 {
00193     cairo_uint64_t   lo, hi;
00194 } cairo_uint128_t, cairo_int128_t;
00195 
00196 cairo_uint128_t I    _cairo_uint32_to_uint128 (uint32_t i);
00197 cairo_uint128_t I    _cairo_uint64_to_uint128 (cairo_uint64_t i);
00198 #define                     _cairo_uint128_to_uint64(a) ((a).lo)
00199 #define                     _cairo_uint128_to_uint32(a) _cairo_uint64_to_uint32(_cairo_uint128_to_uint64(a))
00200 cairo_uint128_t I    _cairo_uint128_add (cairo_uint128_t a, cairo_uint128_t b);
00201 cairo_uint128_t I    _cairo_uint128_sub (cairo_uint128_t a, cairo_uint128_t b);
00202 cairo_uint128_t I    _cairo_uint128_mul (cairo_uint128_t a, cairo_uint128_t b);
00203 cairo_uint128_t I    _cairo_uint64x64_128_mul (cairo_uint64_t a, cairo_uint64_t b);
00204 cairo_uint128_t I    _cairo_uint128_lsl (cairo_uint128_t a, int shift);
00205 cairo_uint128_t I    _cairo_uint128_rsl (cairo_uint128_t a, int shift);
00206 cairo_uint128_t I    _cairo_uint128_rsa (cairo_uint128_t a, int shift);
00207 int            I     _cairo_uint128_lt (cairo_uint128_t a, cairo_uint128_t b);
00208 int            I     _cairo_uint128_eq (cairo_uint128_t a, cairo_uint128_t b);
00209 cairo_uint128_t I    _cairo_uint128_negate (cairo_uint128_t a);
00210 #define                     _cairo_uint128_negative(a)  (_cairo_uint64_negative(a.hi))
00211 cairo_uint128_t I    _cairo_uint128_not (cairo_uint128_t a);
00212 
00213 #define                     _cairo_uint128_to_int128_(i)       (i)
00214 #define                     _cairo_int128_to_uint128(i) (i)
00215 
00216 cairo_int128_t  I    _cairo_int32_to_int128 (int32_t i);
00217 cairo_int128_t  I    _cairo_int64_to_int128 (cairo_int64_t i);
00218 #define                     _cairo_int128_to_int64(a)   ((cairo_int64_t) (a).lo)
00219 #define                     _cairo_int128_to_int32(a)   _cairo_int64_to_int32(_cairo_int128_to_int64(a))
00220 #define                     _cairo_int128_add(a,b)          _cairo_uint128_add(a,b)
00221 #define                     _cairo_int128_sub(a,b)          _cairo_uint128_sub(a,b)
00222 #define                     _cairo_int128_mul(a,b)          _cairo_uint128_mul(a,b)
00223 cairo_int128_t I _cairo_int64x64_128_mul (cairo_int64_t a, cairo_int64_t b);
00224 #define                     _cairo_int128_lsl(a,b)          _cairo_uint128_lsl(a,b)
00225 #define                     _cairo_int128_rsl(a,b)          _cairo_uint128_rsl(a,b)
00226 #define                     _cairo_int128_rsa(a,b)          _cairo_uint128_rsa(a,b)
00227 int            I     _cairo_int128_lt (cairo_int128_t a, cairo_int128_t b);
00228 #define                     _cairo_int128_eq(a,b)           _cairo_uint128_eq (a,b)
00229 #define                     _cairo_int128_negate(a)         _cairo_uint128_negate(a)
00230 #define                     _cairo_int128_negative(a)   (_cairo_uint128_negative(a))
00231 #define                     _cairo_int128_not(a)     _cairo_uint128_not(a)
00232 
00233 #else  /* !HAVE_UINT128_T */
00234 
00235 typedef uint128_t    cairo_uint128_t;
00236 typedef int128_t     cairo_int128_t;
00237 
00238 #define                     _cairo_uint32_to_uint128(i) ((uint128_t) (i))
00239 #define                     _cairo_uint64_to_uint128(i) ((uint128_t) (i))
00240 #define                     _cairo_uint128_to_uint64(i) ((uint64_t) (i))
00241 #define                     _cairo_uint128_to_uint32(i) ((uint32_t) (i))
00242 #define                     _cairo_uint128_add(a,b)         ((a) + (b))
00243 #define                     _cairo_uint128_sub(a,b)         ((a) - (b))
00244 #define                     _cairo_uint128_mul(a,b)         ((a) * (b))
00245 #define                     _cairo_uint64x64_128_mul(a,b)      ((uint128_t) (a) * (b))
00246 #define                     _cairo_uint128_lsl(a,b)         ((a) << (b))
00247 #define                     _cairo_uint128_rsl(a,b)         ((uint128_t) (a) >> (b))
00248 #define                     _cairo_uint128_rsa(a,b)         ((uint128_t) ((int128_t) (a) >> (b)))
00249 #define                     _cairo_uint128_lt(a,b)          ((a) < (b))
00250 #define                     _cairo_uint128_eq(a,b)          ((a) == (b))
00251 #define                     _cairo_uint128_negate(a)    ((uint128_t) -((int128_t) (a)))
00252 #define                     _cairo_uint128_negative(a)  ((int128_t) (a) < 0)
00253 #define                     _cairo_uint128_not(a)           (~(a))
00254 
00255 #define                     _cairo_uint128_to_int128(i) ((int128_t) (i))
00256 #define                     _cairo_int128_to_uint128(i) ((uint128_t) (i))
00257 
00258 #define                     _cairo_int32_to_int128(i)   ((int128_t) (i))
00259 #define                     _cairo_int64_to_int128(i)   ((int128_t) (i))
00260 #define                     _cairo_int128_to_int64(i)   ((int64_t) (i))
00261 #define                     _cairo_int128_to_int32(i)   ((int32_t) (i))
00262 #define                     _cairo_int128_add(a,b)          ((a) + (b))
00263 #define                     _cairo_int128_sub(a,b)          ((a) - (b))
00264 #define                     _cairo_int128_mul(a,b)          ((a) * (b))
00265 #define                     _cairo_int64x64_128_mul(a,b) ((int128_t) (a) * (b))
00266 #define                     _cairo_int128_lt(a,b)           ((a) < (b))
00267 #define                     _cairo_int128_eq(a,b)           ((a) == (b))
00268 #define                     _cairo_int128_lsl(a,b)          ((a) << (b))
00269 #define                     _cairo_int128_rsl(a,b)          ((int128_t) ((uint128_t) (a) >> (b)))
00270 #define                     _cairo_int128_rsa(a,b)          ((int128_t) (a) >> (b))
00271 #define                     _cairo_int128_negate(a)         (-(a))
00272 #define                     _cairo_int128_negative(a)   ((a) < 0)
00273 #define                     _cairo_int128_not(a)     (~(a))
00274 
00275 #endif /* HAVE_UINT128_T */
00276 
00277 typedef struct _cairo_uquorem128 {
00278     cairo_uint128_t  quo;
00279     cairo_uint128_t  rem;
00280 } cairo_uquorem128_t;
00281 
00282 typedef struct _cairo_quorem128 {
00283     cairo_int128_t   quo;
00284     cairo_int128_t   rem;
00285 } cairo_quorem128_t;
00286 
00287 cairo_uquorem128_t I
00288 _cairo_uint128_divrem (cairo_uint128_t num, cairo_uint128_t den);
00289 
00290 cairo_quorem128_t I
00291 _cairo_int128_divrem (cairo_int128_t num, cairo_int128_t den);
00292 
00293 #define                     _cairo_uint128_le(a,b)          (!_cairo_uint128_gt(a,b))
00294 #define                     _cairo_uint128_ne(a,b)          (!_cairo_uint128_eq(a,b))
00295 #define                     _cairo_uint128_ge(a,b)          (!_cairo_uint128_lt(a,b))
00296 #define                     _cairo_uint128_gt(a,b)          _cairo_uint128_lt(b,a)
00297 
00298 #define                     _cairo_int128_le(a,b)           (!_cairo_int128_gt(a,b))
00299 #define                     _cairo_int128_ne(a,b)           (!_cairo_int128_eq(a,b))
00300 #define                     _cairo_int128_ge(a,b)           (!_cairo_int128_lt(a,b))
00301 #define                     _cairo_int128_gt(a,b)           _cairo_int128_lt(b,a)
00302 
00303 #undef I
00304 
00305 #endif /* CAIRO_WIDEINT_H */