Guest User

Untitled

a guest
Jul 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. int v1 = -2; // 0xfe
  2. unsigned long v2=(unsigned long)v1; // 0xfffe, I want 0x00fe
  3.  
  4. unsigned long v2=(unsigned int)v1; // 0x00fe
  5.  
  6. uint64 target = mem[index] & mask;
  7. uint64 v;
  8. if (value < 0) {
  9. switch (bits) {
  10. case 8:
  11. v = (uint8)value;
  12. break;
  13. case 16:
  14. v = (uint16)value;
  15. break;
  16. case 32:
  17. v = (uint32)value;
  18. break;
  19. }
  20. } else {
  21. v = value;
  22. }
  23. v = v << lShift;
  24. target |= v;
  25. mem[index] = target;
  26.  
  27. template <typename IType> struct ToULong;
  28.  
  29. template <> struct ToULong<signed char>
  30. {
  31. static inline unsigned long int get(signed char c) { return (unsigned char)(c); }
  32. };
  33.  
  34. template <> struct ToULong<signed short int>
  35. {
  36. static inline unsigned long int get(signed short int c) { return (unsigned short int)(c); }
  37. };
  38.  
  39. /* ... signed int, signed long int, signed long long int ... */
  40.  
  41. template <typename IType>
  42. struct Foo
  43. {
  44. unsigned lont int get_data() const { return ToULong<IType>::get(m_data); }
  45. private:
  46. IType m_data;
  47. }
  48.  
  49. unsigned long int toULong( char c) { return (unsigned char)(c); }
  50. unsigned long int toULong(signed char c) { return (unsigned char)(c); }
  51. unsigned long int toULong(signed short int c) { return (unsigned short int)(c); }
  52. unsigned long int toULong(signed int c) { return (unsigned int)(c); }
  53. unsigned long int toULong(signed long int c) { return (unsigned long int)(c); }
  54.  
  55. #include <type_traits>
  56. v= static_cast<std::make_unsigned<decltype(value)>::type>(value);
  57.  
  58. union u1 {
  59. short int si;
  60. unsigned long int uli;
  61.  
  62. unsigned long int stub;
  63.  
  64. operator unsigned long int () {return uli;};
  65. public:
  66. u1(short int nsi) : stub(0) {si = nsi;}
  67.  
  68. };
  69.  
  70. template <typename Tint> uint64 ToMemdata(Tint value) {
  71. return (uint64)value;};
  72. template <> uint64 ToMemdata<int8>(int8 value) {
  73. return (uint64)((uint8)value);};
  74. template <> uint64 ToMemdata<int16>(int16 value) {
  75. return (uint64)((uint16)value);};
  76. template <> uint64 ToMemdata<int32>(int32 value) {
  77. return (uint64)((uint32)value);};
  78. template <> uint64 ToMemdata<int64>(int64 value) {
  79. return (uint64)((uint64)value);};
  80.  
  81. template <typename Tint> void packedWrite(Tint value, int vectorIndex, uint64* pData) {
  82.  
  83. uint64 v = ToMemdata(value);
  84. // This call eliminates a run time test for minus and a switch statement
  85. // Instead the compiler does it based on the template specialization
  86.  
  87. uint64 aryix, itemofs;
  88. vectorArrayIndex(vectorIndex, &aryix, &itemofs); // get the memory index and the byte offset
  89. uint64 mask = vectorItemMask(itemofs); // get the mask for the particular byte
  90. uint64 aryData = pData[aryix]; // get the word in memory
  91. aryData &= mask; // mask it
  92. uint64 lShift = (uint64)(itemofs * sizeof(Tint) * 8);
  93. uint64 d = v << lShift; // shift the value into the byte position
  94. aryData |= d; // put the value into memory
  95. pData[aryix] = aryData;
  96. }
  97.  
  98. int v1 = -2; // 0xfe
  99. unsigned long v2=*(unsigned long *)&v1;
  100.  
  101. unsigned long v2 = 0;
  102. v2 = v2 | v1;
Add Comment
Please, Sign In to add comment