Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smaller pointers... possible? (without a lower spec system)

In this 2010 paper[1] on raycasting sparse voxel octrees (SVOs) (apologies; the paper takes a while to load), section 3 indicates an interesting memory setup to save space on voxel data, which almost invariably is very large.

They specify a 15-bit relative pointer, with a 1-bit flag to specify whether a far pointer is needed (if the volume data is too large, the flag is set, and the 15-bit pointer is considered to point to a secondary, far pointer).

What's being done to achieve this? Is this something to do with CUDA / the GPU? Is it done through a custom allocator of some sort, in C++ code?

How would this be done in C++, if at all?

[1]Efficient Sparse Voxel Octrees: Samuli Laine, Tero Karras; NVIDIA Research

like image 806
Engineer Avatar asked Nov 16 '11 18:11

Engineer


People also ask

Why are pointers different sizes on different machines?

It depends upon different issues like Operating system, CPU architecture etc. Usually it depends upon the word size of underlying processor for example for a 32 bit computer the pointer size can be 4 bytes for a 64 bit computer the pointer size can be 8 bytes. So for a specific architecture pointer size will be fixed.

Are pointers more efficient?

If you use them carefully, pointers can reduce the amount of program code you need to write, thereby increasing your program's efficiency and enabling you to use less memory. (Your program can run faster because it does not have to duplicate the data in memory).

What is the purpose of pointers?

Pointers are used extensively in both C and C++ for three main purposes: to allocate new objects on the heap, to pass functions to other functions. to iterate over elements in arrays or other data structures.

What are pointers in programming?

A pointer is a variable that stores the memory address of another variable as its value. A pointer variable points to a data type (like int ) of the same type, and is created with the * operator.


2 Answers

Well, you can always manually store the memory in an array and use integer indexes as "pointers".

like image 164
hugomg Avatar answered Sep 24 '22 03:09

hugomg


In C/C++ you can interpret numbers as pointers in any way that you like - but it means that you wouldn't be able to simply dereference / add / subtract and do other normal pointer things . Instead you would have to use functions converting to a 'true' pointer to do these sorts of operations.

In C++ you could wrap all this up very neatly inside a class that presents a pointer-like interface by implementing operator*, opeartor->, and the arithmetic operators.

Just based on glancing at figure 2, this is what an implementation of the proposed pointers might look like (note that if you actually do this, then you're going to want to make sure the class isn't padded out to 32 or 64 bits anyway, and that they are allocated on 2-byte boundaries - there are normally compiler-specific directives to control this.)

class SmallChildPointer{
    public:
        SmallChildPointer(Child* bigChildPointer)
            : value(){
            ptrdiff_t offset = bigChildPointer - base_address;
            if(offset > 0x7fff)
                throw std::runtime_error("too big for a near pointer...");
            value = uint16_t(offset & 0x7fff);
        }

        Child* operator->() const{
            return (Child*)(base_address + value);
        }

        Child const& operator*() const{
            return *(Child const*)(base_address + value);
        }

        Child& operator*(){
            return *(Child*)(base_address + value);
        }

        // do this once, before constructing any of these!
        static void setTheGlobalBaseAddress(void* here){
             base_address = here;
        }

    private:
        uint16_t value;
        static void* base_address;
};
like image 38
James Avatar answered Sep 25 '22 03:09

James