fork download
  1. #ifdef _MSC_VER
  2. #pragma comment(linker, "/SUBSYSTEM:CONSOLE /ENTRY:mainCRTStartup")
  3. #endif
  4. #include <limits.h>
  5. #include <stdint.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9.  
  10. #define SHR_BYTES( VAL, BYTES ) (VAL >> (CHAR_BIT * BYTES))
  11.  
  12. #if UINT_MAX == UCHAR_MAX
  13. #define INT_SIZE 1
  14. #elif UINT_MAX == USHRT_MAX
  15. #define INT_SIZE 2
  16. #elif SHR_BYTES(UINT_MAX,4) == 0
  17. #define INT_SIZE 4
  18. #else
  19. #define INT_SIZE 8
  20. #endif
  21.  
  22. #if SHR_BYTES(ULONG_MAX,4) == 0
  23. #define LONG_SIZE 4
  24. #else
  25. #define LONG_SIZE 8
  26. #endif
  27.  
  28. #define LLONG_SIZE 8
  29.  
  30. #define SHRT_BIT (2 * CHAR_BIT)
  31. #define INT_BIT (INT_SIZE * CHAR_BIT)
  32. #define LONG_BIT (LONG_SIZE * CHAR_BIT)
  33. #define LLONG_BIT (8 * CHAR_BIT)
  34.  
  35. #ifndef __nothrow
  36. #if defined( __attribute__ )
  37. #define __nothrow __attribute__((nothrow))
  38. #elif defined( __declspec )
  39. #define __nothrow __declspec(nothrow)
  40. #elif defined(__nothrow__)
  41. #define __nothrow __nothrow
  42. #else
  43. #define __nothrow
  44. #endif
  45. #endif
  46.  
  47. #ifndef __stdcall
  48. #if defined( __attribute__ )
  49. #define __stdcall __attribute__((stdcall))
  50. #elif defined( __declspec )
  51. #define __stdcall __declspec(stdcall)
  52. #elif defined( __stdcall__ )
  53. #define __stdcall __stdcall__
  54. #else
  55. #define __stdcall
  56. #endif
  57. #endif
  58.  
  59. typedef unsigned char uchar;
  60. typedef signed char schar;
  61. typedef unsigned short ushort;
  62. typedef unsigned int uint;
  63. typedef unsigned long ulong;
  64. #define LLONG long long int
  65. typedef LLONG llong;
  66. typedef unsigned LLONG ullong;
  67. typedef union PTRU_
  68. {
  69. void *vptr;
  70. uchar *cptr;
  71. uint *iptr;
  72. intptr_t sptr;
  73. uintptr_t uptr;
  74. } PTRU;
  75. typedef struct PTRS_
  76. {
  77. void *SRC;
  78. size_t size;
  79. char endian;
  80. char padding[3];
  81. PTRU beg;
  82. PTRU end;
  83. schar pos1;
  84. schar pos2;
  85. schar neg1;
  86. schar neg2;
  87. } PTRS;
  88.  
  89. #define LIMITS(UI,SI,UMAX,SMAX,SMIN,ONE)\
  90.   static const uchar UI##_bit = sizeof(SI) * CHAR_BIT;\
  91.   static const uchar UI##_mov = (sizeof(SI) * CHAR_BIT) - 1;\
  92.   static const UI UI##_end = ONE << ((sizeof(SI) * CHAR_BIT) - 1);\
  93.   static const UI UI##_max = UMAX;\
  94.   static const SI SI##_max = SMAX;\
  95.   static const SI SI##_min = SMIN
  96. LIMITS( uchar, schar, UCHAR_MAX, SCHAR_MAX, SCHAR_MIN, 1U );
  97. LIMITS( ushort, short, USHRT_MAX, SHRT_MAX, SHRT_MIN, 1U );
  98. LIMITS( uint, int, UINT_MAX, INT_MAX, INT_MIN, 1U );
  99. LIMITS( ulong, long, ULONG_MAX, LONG_MAX, LONG_MIN, 1UL );
  100. LIMITS( ullong, llong, ULLONG_MAX, LLONG_MAX, LLONG_MIN, 1ULL );
  101.  
  102. #define UPTR_IS0( M_0, M_LAST, M_I1, M_I2 ) (!M_0 || !M_LAST || M_I1 == 0 || M_I2 == 0)
  103. PTRS
  104. __nothrow __stdcall
  105. endian( void *SRC, size_t srcSize, char srcEndian )
  106. {
  107. PTRS num;
  108. PTRU BEG, END;
  109. num.SRC = SRC;
  110. num.size = srcSize;
  111. num.endian = srcEndian;
  112. BEG.vptr = SRC;
  113. END.uptr = BEG.uptr + ( srcSize - 1 );
  114. switch( num.endian )
  115. {
  116. // 0x78563412
  117. case 'B':
  118. case 'b':
  119. num.pos1 = num.pos2 = -1;
  120. num.end = BEG;
  121. num.beg = END;
  122. return num;
  123. // 0x12345678
  124. case 'L':
  125. case 'l':
  126. num.pos1 = num.pos2 = 1;
  127. num.end = END;
  128. num.beg = BEG;
  129. goto done;
  130. // 0x56781234
  131. case 'R':
  132. case 'r':
  133. num.pos1 = -1;
  134. num.pos2 = 4;
  135. num.end.uptr = ++BEG.uptr;
  136. num.beg.uptr = --END.uptr;
  137. goto done;
  138. }
  139. // 0x34127856
  140. num.endian = 'M';
  141. num.pos1 = 1;
  142. num.pos2 = -4;
  143. num.end.uptr = --END.uptr;
  144. num.beg.uptr = ++BEG.uptr;
  145. done:
  146. num.neg1 = -num.pos1;
  147. num.neg2 = -num.pos2;
  148. return num;
  149. }
  150. uchar
  151. __nothrow __stdcall
  152. eis0( PTRS src, void **S )
  153. {
  154. for( uchar i = 1; src.size--; src.end.uptr += i ? src.neg1 : src.neg2, i ^= 1 )
  155. {
  156. if( *src.end.cptr )
  157. {
  158. if( S ) { *S = src.end.vptr; }
  159. return 0;
  160. }
  161. }
  162. if( S ) { *S = NULL; }
  163. return 1;
  164. }
  165. uchar
  166. __nothrow __stdcall
  167. eiseq( PTRS src, PTRS num, void **S, void **N )
  168. {
  169. for( uchar i = 1; src.size && num.size;
  170. src.end.uptr += i ? src.neg1 : src.neg2,
  171. num.end.uptr += i ? num.neg1 : num.neg2,
  172. --src.size, --num.size, i ^= 1 )
  173. {
  174. if( *src.end.cptr != *num.end.cptr ) { goto stop; }
  175. }
  176. if( !src.size )
  177. {
  178. if( N ) { *N = num.end.vptr; }
  179. if( S ) { *S = src.end.vptr; }
  180. if( !num.size ) { return 1; }
  181. return 0;
  182. }
  183. if( !num.size )
  184. {
  185. stop:
  186. if( N ) { *N = num.end.vptr; }
  187. if( S ) { *S = src.end.vptr; }
  188. return 0;
  189. }
  190. if( N ) { *N = num.end.vptr; }
  191. if( S ) { *S = src.end.vptr; }
  192. return 1;
  193. }
  194. uchar
  195. __nothrow __stdcall
  196. emteq( PTRS src, PTRS num, void **S, void **N )
  197. {
  198. void *s, *n;
  199. if( !S ) { S = &s; }
  200. if( !N ) { N = &n; }
  201. if( eiseq( src, num, S, N ) || **( ( uchar** )S ) > **( ( uchar** )N ) )
  202. { return 1; }
  203. return 0;
  204. }
  205. uchar
  206. __nothrow __stdcall
  207. emt( PTRS src, PTRS num, void **S, void **N )
  208. {
  209. void *s, *n;
  210. if( !S ) { S = &s; }
  211. if( !N ) { N = &n; }
  212. if( !eiseq( src, num, S, N ) && **( ( uchar** )S ) > **( ( uchar** )N ) )
  213. { return 1; }
  214. return 0;
  215. }
  216. uchar
  217. __nothrow __stdcall
  218. elteq( PTRS src, PTRS num, void **S, void **N )
  219. {
  220. void *s, *n;
  221. if( !S ) { S = &s; }
  222. if( !N ) { N = &n; }
  223. if( eiseq( src, num, S, N ) || **( ( uchar** )S ) < **( ( uchar** )N ) )
  224. { return 1; }
  225. return 0;
  226. }
  227. uchar
  228. __nothrow __stdcall
  229. elt( PTRS src, PTRS num, void **S, void **N )
  230. {
  231. void *s, *n;
  232. if( !S ) { S = &s; }
  233. if( !N ) { N = &n; }
  234. if( !eiseq( src, num, S, N ) && **( ( uchar** )S ) < **( ( uchar** )N ) )
  235. { return 1; }
  236. return 0;
  237. }
  238. PTRS
  239. __nothrow __stdcall
  240. eshr( PTRS src, ullong bits )
  241. {
  242. if( !bits ) { return src; }
  243. PTRS def = src;
  244. PTRS dst = src;
  245. uchar di = 1, ni = 1, rbits = 0, lbits = 0;
  246. while( bits >= uchar_bit && dst.size-- )
  247. {
  248. dst.beg.uptr += di ? dst.pos1 : dst.pos2;
  249. bits -= uchar_bit;
  250. }
  251. if( bits >= uchar_bit )
  252. {
  253. ( void )memset( src.SRC, 0, src.size );
  254. return def;
  255. }
  256. if( dst.size < src.size )
  257. {
  258. while( dst.size-- )
  259. {
  260. *src.beg.cptr = *dst.beg.cptr;
  261. *dst.beg.cptr = 0;
  262. dst.beg.uptr += di ? dst.pos1 : dst.pos2;
  263. src.beg.uptr += ni ? src.pos1 : src.pos2;
  264. }
  265. }
  266. ullong move = CHAR_BIT - bits;
  267. while( src.size-- )
  268. {
  269. rbits = lbits;
  270. lbits = *src.end.cptr << move;
  271. *src.end.cptr >>= bits;
  272. *src.end.cptr |= rbits;
  273. src.end.uptr += ni ? src.neg1 : src.neg2;
  274. ni ^= 1;
  275. }
  276. return def;
  277. }
  278. PTRS
  279. __nothrow __stdcall
  280. eshl( PTRS src, ullong bits )
  281. {
  282. if( !bits ) { return src; }
  283. PTRS def = src;
  284. PTRS dst = src;
  285. uchar di = 1, si = 1, rbits = 0, lbits = 0;
  286. while( bits >= uchar_bit && dst.size-- )
  287. {
  288. dst.end.uptr += di ? dst.neg1 : dst.neg2;
  289. bits -= uchar_bit;
  290. }
  291. if( bits >= uchar_bit )
  292. {
  293. ( void )memset( src.SRC, 0, src.size );
  294. return def;
  295. }
  296. if( dst.size < src.size )
  297. {
  298. while( dst.size-- )
  299. {
  300. *src.end.cptr = *dst.end.cptr;
  301. *dst.end.cptr = 0;
  302. dst.end.uptr += di ? dst.neg1 : dst.neg2;
  303. src.end.uptr += si ? src.neg1 : src.neg2;
  304. }
  305. }
  306. si = 1;
  307. ullong move = CHAR_BIT - bits;
  308. while( src.size-- )
  309. {
  310. lbits = rbits;
  311. rbits = *src.beg.cptr >> move;
  312. *src.beg.cptr <<= bits;
  313. *src.beg.cptr |= lbits;
  314. src.beg.uptr += si ? src.pos1 : src.pos2;
  315. si ^= 1;
  316. }
  317. return def;
  318. }
  319. PTRS
  320. __nothrow __stdcall
  321. eshl1( PTRS src )
  322. {
  323. PTRS def = src;
  324. uchar i = 1, rbits = 0, lbits = 0;
  325. for( ; src.size--; i ^= 1 )
  326. {
  327. lbits = rbits;
  328. rbits = *src.beg.cptr >> uchar_mov;
  329. *src.beg.cptr <<= 1;
  330. *src.beg.cptr |= lbits;
  331. src.beg.uptr += i ? src.pos1 : src.pos2;
  332. }
  333. return def;
  334. }
  335. PTRS
  336. __nothrow __stdcall
  337. eshr1( PTRS src )
  338. {
  339. PTRS def = src;
  340. uchar i = 1, rbits = 0, lbits = 0;
  341. for( ; src.size--; i ^= 1 )
  342. {
  343. rbits = lbits;
  344. lbits = *src.beg.cptr << uchar_mov;
  345. *src.beg.cptr >>= 1;
  346. *src.beg.cptr |= rbits;
  347. src.beg.uptr += i ? src.pos1 : src.pos2;
  348. }
  349. return def;
  350. }
  351. PTRS
  352. __nothrow __stdcall
  353. eand( PTRS src, PTRS num )
  354. {
  355. PTRS def = src;
  356. for( uchar i = 1; src.size-- && num.size--; i ^= 1 )
  357. {
  358. *src.beg.cptr &= *num.beg.cptr;
  359. src.beg.uptr += i ? src.pos1 : src.pos2;
  360. num.beg.uptr += i ? num.pos1 : src.pos2;
  361. }
  362. return def;
  363. }
  364. uchar
  365. __nothrow __stdcall
  366. einc( PTRS num )
  367. {
  368. uchar bit, i = 1;
  369. for( ; num.size--; i ^= 1, num.beg.uptr += i ? num.pos1 : num.pos2 )
  370. {
  371. for( bit = 1; bit; bit <<= 1 )
  372. {
  373. if( *num.beg.cptr & bit )
  374. {
  375. *num.beg.cptr ^= bit;
  376. }
  377. else
  378. {
  379. *num.beg.cptr |= bit;
  380. return 1;
  381. }
  382. }
  383. }
  384. return 1;
  385. }
  386. PTRS
  387. __nothrow __stdcall
  388. eadd( PTRS src, PTRS num )
  389. {
  390. PTRS def = src;
  391. uchar carry = 0, bit;
  392. for( uchar i = 1; src.size-- && num.size--; i ^= 1,
  393. src.beg.uptr += i ? src.pos1 : src.pos2,
  394. num.beg.uptr += i ? num.pos1 : num.pos2 )
  395. {
  396. for( bit = 1; bit; bit <<= 1 )
  397. {
  398. if( carry )
  399. {
  400. if( *src.beg.cptr & bit )
  401. {
  402. *src.beg.cptr ^= bit;
  403. }
  404. else
  405. {
  406. *src.beg.cptr |= bit;
  407. carry = 0;
  408. }
  409. }
  410. if( *src.beg.cptr & bit )
  411. {
  412. if( *num.beg.cptr & bit )
  413. {
  414. *src.beg.cptr ^= bit;
  415. carry = 1;
  416. }
  417. }
  418. else if( *num.beg.cptr & bit )
  419. {
  420. *src.beg.cptr |= bit;
  421. }
  422. }
  423. }
  424. return def;
  425. }
  426. PTRS
  427. __nothrow __stdcall
  428. esub( PTRS src, PTRS num )
  429. {
  430. PTRS def = src;
  431. uchar carry = 0, bit = uchar_end, clr = 0, i = 1;
  432. for( size_t srcSize = src.size; !( *src.end.cptr ) && srcSize--; i ^= 1 )
  433. {
  434. src.end.uptr += i ? src.neg1 : src.neg2;
  435. }
  436. while( bit && !( *src.end.cptr & bit ) )
  437. {
  438. clr |= bit;
  439. bit >>= 1;
  440. }
  441. clr = ~clr;
  442. for( i = 1; src.size-- && num.size--;
  443. src.beg.uptr += i ? src.pos1 : src.pos2,
  444. num.beg.uptr += i ? num.pos1 : num.pos2, i ^= 1 )
  445. {
  446. for( bit = 1; bit; bit <<= 1 )
  447. {
  448. if( carry )
  449. {
  450. if( *( src.beg.cptr ) & bit )
  451. {
  452. *( src.beg.cptr ) ^= bit;
  453. carry = 0;
  454. }
  455. else
  456. {
  457. *( src.beg.cptr ) |= bit;
  458. }
  459. }
  460. if( *( src.beg.cptr ) & bit )
  461. {
  462. if( *( num.beg.cptr ) & bit )
  463. {
  464. *( src.beg.cptr ) ^= bit;
  465. }
  466. }
  467. else if( *( num.beg.cptr ) & bit )
  468. {
  469. *( src.beg.cptr ) |= bit;
  470. carry = 1;
  471. }
  472. }
  473. }
  474. *( src.beg.cptr ) &= clr;
  475. return def;
  476. }
  477. PTRS
  478. __nothrow __stdcall
  479. ecpy( PTRS src, PTRS num )
  480. {
  481. PTRS def = src;
  482. for( uchar i = 1; src.size-- && num.size--; i ^= 1,
  483. src.beg.uptr += i ? src.pos1 : src.pos2,
  484. num.beg.uptr += i ? num.pos1 : num.pos2 )
  485. {
  486. *src.beg.cptr = *num.beg.cptr;
  487. }
  488. return def;
  489. }
  490.  
  491. PTRS
  492. __nothrow __stdcall
  493. ediv( PTRS quo, PTRS src, PTRS num, ullong *radix, ullong radix_max )
  494. {
  495. if( !quo.SRC || !quo.size ) { return ( PTRS ) { NULL }; }
  496. ( void )memset( quo.SRC, 0, quo.size );
  497. if( eis0( src, NULL ) || eis0( num, NULL ) ) { return quo; }
  498. void *TMP = calloc( 1, src.size );
  499. if( !TMP ) return ( PTRS ) { NULL };
  500. PTRS tmp = endian( TMP, src.size, src.endian );
  501. uchar i = 1, bit;
  502. size_t size = src.size;
  503. //
  504. for( ; !( *src.end.cptr ) && size--; i ^= 1 )
  505. {
  506. src.end.uptr += i ? src.neg1 : src.neg2;
  507. }
  508. //*/
  509. //PTRS def = src;
  510. for( ; size--;//&& emteq( src, num, NULL, NULL );
  511. src.end.uptr += i ? src.neg1 : src.neg2, i ^= 1 )
  512. {
  513. for( bit = uchar_end; bit; bit >>= 1 )
  514. {
  515. ( void )eshl1( quo );
  516. ( void )eshl1( tmp );
  517. if( *src.end.cptr & bit )
  518. {
  519. *tmp.beg.cptr |= 1;
  520. *src.end.cptr ^= bit;
  521. }
  522. if( emteq( tmp, num, NULL, NULL ) )
  523. {
  524. *quo.beg.cptr |= 1;
  525. ( void )esub( tmp, num );
  526. }
  527. }
  528. }
  529. if( radix )
  530. {
  531. for( *radix = 0; *radix < radix_max && !eis0( tmp, NULL ); ++( *radix ) )
  532. {
  533. ( void )eshl1( quo );
  534. ( void )eshl1( tmp );
  535. if( emteq( tmp, num, NULL, NULL ) )
  536. {
  537. *quo.beg.cptr |= 1;
  538. ( void )esub( tmp, num );
  539. }
  540. }
  541. }
  542. ( void )memcpy( src.SRC, TMP, tmp.size );
  543. //free( tmp.SRC );
  544. return quo;
  545. }
  546.  
  547. // Normal Shift & Add Algorithm
  548. PTRS
  549. __nothrow __stdcall
  550. emul( PTRS src, PTRS num )
  551. {
  552. if( UPTR_IS0( src.SRC, src.size, src.pos1, src.pos2 )
  553. || UPTR_IS0( num.SRC, num.size, num.pos1, num.pos2 ) )
  554. return ( PTRS ) {NULL};
  555. if( eis0( src, NULL ) )
  556. { return src; }
  557. if( eis0( num, NULL ) )
  558. {
  559. ( void )memset( src.SRC, 0, src.size );
  560. return src;
  561. }
  562. PTRS def = src;
  563. uchar *TMP = calloc( src.size, 1 );
  564. if( !TMP ) return ( ( PTRS ) {NULL} );
  565. PTRS tmp = endian( TMP, src.size, src.endian );
  566. uchar bit;
  567. for( uchar i = 1; num.size--; i ^= 1, num.beg.uptr += i ? num.pos1 : num.pos2 )
  568. {
  569. for( bit = 1; bit; bit <<= 1, eshl1( src ) )
  570. {
  571. if( *num.beg.cptr & bit ) { ( void )eadd( tmp, src ); }
  572. }
  573. }
  574. ( void )memcpy( src.SRC, TMP, src.size );
  575. free( TMP );
  576. return def;
  577. }
  578. char
  579. __nothrow __stdcall
  580. getIntEndianEx( schar *i1, schar *i2 )
  581. {
  582. uint num = 0x12345678;
  583. uchar *buf = ( uchar* )( &num );
  584. switch( buf[0] )
  585. {
  586. // 0x123465678
  587. case 0x12:
  588. if( i1 ) { *i1 = -1; }
  589. if( i2 ) { *i2 = -1; }
  590. return 'B';
  591. // 0x78563412
  592. case 0x78:
  593. if( i1 ) { *i1 = 1; }
  594. if( i2 ) { *i2 = 1; }
  595. return 'L';
  596. // 0x56781234
  597. case 0x56:
  598. if( i1 ) { *i1 = 1; }
  599. if( i2 ) { *i2 = -4; }
  600. return 'R';
  601. }
  602. // 0x34127856
  603. if( i1 ) { *i1 = -1; }
  604. if( i2 ) { *i2 = 3; }
  605. return 'M';
  606. }
  607. #define getIntEndian() getIntEndianEx(NULL, NULL)
  608. char
  609. __nothrow __stdcall
  610. getShortEndianEx( schar *i )
  611. {
  612. ushort value = 0x1234;
  613. uchar *buff = ( uchar* )( &value );
  614. switch( *buff )
  615. {
  616. case 0x12:
  617. if( i ) { *i = -1; }
  618. return 'B';
  619. case 0x34:
  620. if( i ) { *i = 1; }
  621. return 'L';
  622. }
  623. if( i ) { *i = 0; }
  624. return 0;
  625. }
  626. #define getShortEndian() getShortEndianEx(NULL)
  627. PTRS
  628. __nothrow __stdcall
  629. emul_karatsuba( PTRS SRC, PTRS NUM )
  630. {
  631. //#error "Bug causing write to BEFORE buffer"
  632. if( SRC.size < 2 || NUM.size < 2
  633. || elteq( SRC, endian( ( void* )&ushort_max, sizeof( short ),
  634. getShortEndian() ), NULL, NULL ) )
  635. {
  636. return emul( SRC, NUM );
  637. }
  638. PTRS low1 = endian( SRC.SRC, SRC.size / 2, SRC.endian ), high1 = low1;
  639. PTRS low2 = endian( NUM.SRC, NUM.size / 2, NUM.endian ), high2 = low2;
  640. void *SCPY = malloc( low1.size );
  641. if( !SCPY ) return ( PTRS ) {NULL};
  642. void *NCPY = malloc( low2.size );
  643. if( !NCPY )
  644. {
  645. free( SCPY );
  646. return ( PTRS ) {NULL};
  647. }
  648. PTRS scpy = endian( SCPY, low1.size, SRC.endian );
  649. PTRS ncpy = endian( NCPY, low2.size, NUM.endian );
  650. uchar si = 1, ni = 1;
  651. ( void )memcpy( SCPY, SRC.SRC, low1.size );
  652. ( void )memcpy( NCPY, NUM.SRC, low2.size );
  653. while( SRC.size-- > low1.size )
  654. {
  655. low1.end.uptr += si ? low1.neg1 : low1.neg2;
  656. scpy.end.uptr += si ? low1.neg1 : low1.neg2;
  657. si ^= 1;
  658. }
  659. while( NUM.size-- > low2.size )
  660. {
  661. low2.end.uptr += ni ? low2.neg1 : low2.neg2;
  662. ncpy.end.uptr += ni ? low2.neg1 : low2.neg2;
  663. ni ^= 1;
  664. }
  665. PTRS z0 = emul_karatsuba( endian( scpy.beg.vptr, low1.size, SRC.endian ),
  666. endian( ncpy.beg.vptr, low2.size, NUM.endian ) );
  667. PTRS z2 = emul_karatsuba( endian( scpy.end.vptr, low1.size, SRC.endian ),
  668. endian( ncpy.end.vptr, low2.size, NUM.endian ) );
  669. PTRS z1 = emul_karatsuba( eadd( low1, high1 ), eadd( low2, high2 ) );
  670. eadd( eadd( emul( esub( esub( z1, z2 ), z0 ), endian( &low1.size,
  671. sizeof( size_t ), getIntEndian() ) ), z0 ), emul( z2, endian( &low1.size,
  672. sizeof( size_t ), getIntEndian() ) ) );
  673. free( SCPY );
  674. free( NCPY );
  675. return low1;
  676. }
  677. void
  678. __nothrow __stdcall
  679. putt( char *text, uchar result )
  680. {
  681. fputs( text, stdout );
  682. puts( result ? " succeeded" : " failed" );
  683. }
  684. void
  685. __nothrow __stdcall
  686. puti( char* text, ullong i1, ullong i2, ullong q1, ullong q2 )
  687. {
  688. ullong last = LLONG_BIT, i = last - 1, bit = 1;
  689. char b1[ LLONG_BIT + 1] = "";
  690. char b2[ LLONG_BIT + 1] = "";
  691. char b3[ LLONG_BIT + 1] = "";
  692. char b4[ LLONG_BIT + 1] = "";
  693. do
  694. {
  695. b1[i] = ( i1 & bit ) ? '1' : '0';
  696. b2[i] = ( i2 & bit ) ? '1' : '0';
  697. b3[i] = ( q1 & bit ) ? '1' : '0';
  698. b4[i] = ( q2 & bit ) ? '1' : '0';
  699. bit <<= 1;
  700. --i;
  701. }
  702. while( bit );
  703. b1[last] = b2[last] = b3[last] = b4[last] = '\0';
  704. puts( text );
  705. puts( b1 );
  706. puts( b2 );
  707. puts( b3 );
  708. puts( b4 );
  709. }
  710.  
  711. int
  712. main( void )
  713. {
  714. ullong i1 = 9999, i2 = 9999 << 1, q1 = 0, q2 = 0, i = 0;
  715. PTRS Q1 = endian( &q1, sizeof( llong ), getIntEndian() );
  716. //PTRS Q2 = endian( &q2, sizeof( int ), getIntEndian());
  717. PTRS I = endian( &i, sizeof( llong ), getIntEndian() );
  718. PTRS I1 = endian( &i1, sizeof( llong ), getIntEndian() );
  719. //*
  720. PTRS I2 = endian( &i2, sizeof( llong ), getIntEndian() );
  721. putt( "eiseq", !eiseq( I1, I2, NULL, NULL ) );
  722. putt( " emt", !emt( I1, I2, NULL, NULL ) );
  723. putt( "emteq", !emteq( I1, I2, NULL, NULL ) );
  724. putt( " elt", elt( I1, I2, NULL, NULL ) );
  725. putt( "elteq", elteq( I1, I2, NULL, NULL ) );
  726. putt( " eis0", !eis0( I1, NULL ) );
  727. //*/
  728. eshl( I1, 1 );
  729. puti( "<< 1:", i1, i2, q1, q2 );
  730. i2 >>= 7;
  731. eshr( I1, 7 );
  732. puti( ">> 7:", i1, i2, q1, q2 );
  733. i = 177777;
  734. i2 += i;
  735. eadd( I1, I );
  736. puti( "+ 177777:", i1, i2, q1, q2 );
  737. i = 3;
  738. for( i1 = 0; i1 < 3; ++i1 ) { putt( "emteq", !emteq( I1, I, NULL, NULL ) ); }
  739. for( ; i1 < 8; ++i1 ) { putt( "emteq", emteq( I1, I, NULL, NULL ) ); }
  740. i1 = i2 = 3;
  741. i2 -= i;
  742. esub( I1, I );
  743. puti( "3 - 3:", i1, i2, q1, q2 );
  744. i = 55;
  745. i1 = i2 = 55;
  746. i2 *= i;
  747. emul( I1, I );
  748. puti( "55 * 55:", i1, i2, q1, q2 );
  749. i = 88963;
  750. i2 *= i;
  751. emul_karatsuba( I1, I );
  752. puti( "* 88963:", i1, i2, q1, q2 );
  753. i = 3;
  754. q2 = i2 % i;
  755. i2 /= i;
  756. ediv( Q1, I1, I, NULL, 0 );
  757. puti( "/ 3:", i1, i2, q1, q2 );
  758. i1 = q1;
  759. q1 = 0;
  760. q2 = 0;
  761. puts( "Press any key to continue..." );
  762. fgetc( stdin );
  763. return 0;
  764. }
  765.  
Success #stdin #stdout 0s 2160KB
stdin
Standard input is empty
stdout
eiseq succeeded
  emt succeeded
emteq succeeded
  elt succeeded
elteq succeeded
 eis0 succeeded
<< 1:
0000000000000000000000000000000000000000000000000100111000011110
0000000000000000000000000000000000000000000000000100111000011110
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
>> 7:
0000000000000000000000000000000000000000000000000000000010011100
0000000000000000000000000000000000000000000000000000000010011100
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
+ 177777:
0000000000000000000000000000000000000000000000101011011100001101
0000000000000000000000000000000000000000000000101011011100001101
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
emteq succeeded
emteq succeeded
emteq succeeded
emteq succeeded
emteq succeeded
emteq succeeded
emteq succeeded
emteq succeeded
3 - 3:
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
55 * 55:
0000000000000000000000000000000000000000000000000000101111010001
0000000000000000000000000000000000000000000000000000101111010001
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
* 88963:
0000000000000000000000000000000000010000000010100101011011110011
0000000000000000000000000000000000010000000010100101011011110011
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
/ 3:
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000101010110001100011110100110
0000000000000000000000000000000000000101010110001100011110100110
0000000000000000000000000000000000000000000000000000000000000001
Press any key to continue...