Changeset 26b713 in git for gfanlib/gfanlib_z.h
- Timestamp:
- Feb 13, 2013, 4:23:42 PM (11 years ago)
- Branches:
- (u'spielwiese', '17f1d200f27c5bd38f5dfc6e8a0879242279d1d8')
- Children:
- 81384bd682dd5f3d82dcdbb5f9b03b8e16f90d39
- Parents:
- 7b8818e8f761639646d742a3b11bca3b5a9408da
- git-author:
- Yue Ren <ren@mathematik.uni-kl.de>2013-02-13 16:23:42+01:00
- git-committer:
- Yue Ren <ren@mathematik.uni-kl.de>2013-03-14 15:32:21+01:00
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gfanlib/gfanlib_z.h
r7b8818 r26b713 11 11 #include <string.h> 12 12 #include <ostream> 13 14 #define OLD 1 15 #if OLD 13 16 #include "gmp.h" 14 17 … … 190 193 } 191 194 192 195 #else 196 namespace gfan{ 197 typedef signed long int word;//processor type for integers 198 typedef int64_t LimbWord;//memory word. Assumed to be at least as big as word 199 typedef uint64_t uLimbWord;//memory word. Assumed to be at least as big as word 200 const int limbSizeInBytes=8; 201 #include <stdint.h> 202 struct spec64malloc{ 203 int64_t data; 204 bool hasLimbs() 205 { 206 return data&1; 207 } 208 word fitsMask() 209 { 210 return 0xc000000000000000; 211 } 212 void assign(word value)//assuming data fits 213 { 214 data=value<<1; 215 } 216 void alloc(int nlimbs)//one limb is added since that will tell the number of remaining limbs 217 { 218 int64_t temp=(int64_t)malloc((nlimbs+1)*limbSizeInBytes); 219 assert(temp); 220 data=temp+1; 221 } 222 LimbWord *limbs()//assuming that limbs exist 223 { 224 return (LimbWord*)(data-1); 225 } 226 word nlimbs()//assuming limbs exist 227 { 228 return (word)*limbs(); 229 } 230 void copy(spec64malloc b) 231 { 232 if(hasLimbs()) 233 { 234 word n2=b.nlimbs()+1; 235 int64_t temp=(int64_t)malloc((n2)*limbSizeInBytes); 236 assert(temp); 237 238 data=temp+1; 239 memcpy((LimbWord*)temp,limbs(),n2*limbSizeInBytes); 240 } 241 else 242 { 243 data=b.data; 244 } 245 } 246 void doFree()//assuming that limbs exist 247 { 248 free((void*)(data-1)); 249 } 250 word valueToWord()//assume no limbs and that word is big enough to contain int64_t 251 { 252 return data>>1; 253 } 254 }; 255 template <struct spec> struct IntegerTemplate : public spec 256 { 257 private: 258 bool fits(word v) 259 { 260 return !((value_&fitsMask())^((value_<<1)&fitsMask)); 261 } 262 public: 263 static bool isField() 264 { 265 return false; 266 } 267 IntegerTemplate() 268 { 269 spec.data=0; 270 } 271 void assignWordNoFree() 272 { 273 if(fits(value_)) 274 { 275 assign(value); 276 } 277 else 278 { 279 alloc(1); 280 limbs()[0]=1; 281 limbs()[1]=value_; 282 } 283 } 284 IntegerTemplate(word value_) 285 { 286 assignWordNoFree(value_); 287 } 288 IntegerTemplate(IntegerTemplate const & value_) 289 { 290 if(value_.hasLimbs()) 291 { 292 copy(value_); 293 } 294 else 295 data=value.data; 296 } 297 /* Integer(mpz_t value_) 298 { 299 mpz_init_set(value,value_); 300 }*/ 301 ~IntegerTemplate() 302 { 303 if(hasLimbs())doFree(); 304 } 305 IntegerTemplate& operator=(const IntegerTemplate& a) 306 { 307 if(this!=&a) 308 { 309 if(hasLimps())doFree(); 310 copy(a); 311 } 312 return *this; 313 } 314 bool isZero()const{ 315 return data==0; 316 } 317 friend std::ostream &operator<<(std::ostream &f, IntegerTemplate const &a) 318 { 319 if(hasLimps()) 320 { 321 LimpWord *p=limbs(); 322 int l=*p++; 323 for(int i=0;i<l;i++) 324 f<<":"<<p[i]; 325 } 326 else 327 { 328 f<<valueToWord(); 329 } 330 return f; 331 } 332 LimbWord signExtension(LimbWord a) 333 { 334 return 0-(a<0); 335 } 336 void addWordToLimbs(word v) 337 { 338 int n=nlimbs(); 339 LimbWord V=v; 340 LimbWord *p=limbs()+1; 341 LimbWord s=signExtension(V); 342 for(int i=0;i<n;i++) 343 { 344 LimbWord r=V+*p; 345 bool carry=(uLimbWord)r<(uLimbWord)V; 346 V=carry+s; 347 } 348 349 } 350 351 352 IntegerTemplate& operator+=(const IntegerTemplate& a) 353 { 354 if(hasLimbs()||a.hasLimbs()) 355 { 356 if(!hasLimbs()) 357 { 358 359 } 360 else 361 { 362 } 363 364 } 365 else 366 { 367 word C=valueToWord(); 368 word A=a.valueToWord(); 369 word D=A+C; 370 assignWordNoFree(D); 371 } 372 return *this; 373 } 374 375 }; 376 377 typedef IntegerTemplate<spec64malloc> Integer; 378 }; 379 #endif 193 380 194 381 #endif /* LIB_Z_H_ */
Note: See TracChangeset
for help on using the changeset viewer.