Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int v1 = -2; // 0xfe
- unsigned long v2=(unsigned long)v1; // 0xfffe, I want 0x00fe
- unsigned long v2=(unsigned int)v1; // 0x00fe
- uint64 target = mem[index] & mask;
- uint64 v;
- if (value < 0) {
- switch (bits) {
- case 8:
- v = (uint8)value;
- break;
- case 16:
- v = (uint16)value;
- break;
- case 32:
- v = (uint32)value;
- break;
- }
- } else {
- v = value;
- }
- v = v << lShift;
- target |= v;
- mem[index] = target;
- template <typename IType> struct ToULong;
- template <> struct ToULong<signed char>
- {
- static inline unsigned long int get(signed char c) { return (unsigned char)(c); }
- };
- template <> struct ToULong<signed short int>
- {
- static inline unsigned long int get(signed short int c) { return (unsigned short int)(c); }
- };
- /* ... signed int, signed long int, signed long long int ... */
- template <typename IType>
- struct Foo
- {
- unsigned lont int get_data() const { return ToULong<IType>::get(m_data); }
- private:
- IType m_data;
- }
- unsigned long int toULong( char c) { return (unsigned char)(c); }
- unsigned long int toULong(signed char c) { return (unsigned char)(c); }
- unsigned long int toULong(signed short int c) { return (unsigned short int)(c); }
- unsigned long int toULong(signed int c) { return (unsigned int)(c); }
- unsigned long int toULong(signed long int c) { return (unsigned long int)(c); }
- #include <type_traits>
- v= static_cast<std::make_unsigned<decltype(value)>::type>(value);
- union u1 {
- short int si;
- unsigned long int uli;
- unsigned long int stub;
- operator unsigned long int () {return uli;};
- public:
- u1(short int nsi) : stub(0) {si = nsi;}
- };
- template <typename Tint> uint64 ToMemdata(Tint value) {
- return (uint64)value;};
- template <> uint64 ToMemdata<int8>(int8 value) {
- return (uint64)((uint8)value);};
- template <> uint64 ToMemdata<int16>(int16 value) {
- return (uint64)((uint16)value);};
- template <> uint64 ToMemdata<int32>(int32 value) {
- return (uint64)((uint32)value);};
- template <> uint64 ToMemdata<int64>(int64 value) {
- return (uint64)((uint64)value);};
- template <typename Tint> void packedWrite(Tint value, int vectorIndex, uint64* pData) {
- uint64 v = ToMemdata(value);
- // This call eliminates a run time test for minus and a switch statement
- // Instead the compiler does it based on the template specialization
- uint64 aryix, itemofs;
- vectorArrayIndex(vectorIndex, &aryix, &itemofs); // get the memory index and the byte offset
- uint64 mask = vectorItemMask(itemofs); // get the mask for the particular byte
- uint64 aryData = pData[aryix]; // get the word in memory
- aryData &= mask; // mask it
- uint64 lShift = (uint64)(itemofs * sizeof(Tint) * 8);
- uint64 d = v << lShift; // shift the value into the byte position
- aryData |= d; // put the value into memory
- pData[aryix] = aryData;
- }
- int v1 = -2; // 0xfe
- unsigned long v2=*(unsigned long *)&v1;
- unsigned long v2 = 0;
- v2 = v2 | v1;
Add Comment
Please, Sign In to add comment