  Combining Freiman's theorem with Balog-Szemeredi-Gowers theorem one can show
that if an additive set has large additive energy, then a large piece of the
set is contained in a generalized arithmetic progression of small rank and
size. In this paper, we prove the above statement with the optimal bound for
the rank of the progression. The proof strategy involves studying upper bounds
for additive energy of subsets of R^d and Z^d.
