Commit 67fc07d3fba681f3362f7644a69b7a581a2670e8
1 parent
9278480e
Fix overflow when multiplying two large positive numbers.
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@3429 c046a42c-6fe2-441c-8c8c-71466251a162
Showing
1 changed file
with
4 additions
and
2 deletions
host-utils.c
@@ -43,7 +43,8 @@ void muls64(int64_t *phigh, int64_t *plow, int64_t a, int64_t b) | @@ -43,7 +43,8 @@ void muls64(int64_t *phigh, int64_t *plow, int64_t a, int64_t b) | ||
43 | ph = (a >> 32) * (b >> 32); | 43 | ph = (a >> 32) * (b >> 32); |
44 | 44 | ||
45 | ph += (int64_t)pm1 >> 32; | 45 | ph += (int64_t)pm1 >> 32; |
46 | - pm1 = (uint64_t)((uint32_t)pm1) + pm2 + (pl >> 32); | 46 | + ph += (int64_t)pm2 >> 32; |
47 | + pm1 = (uint64_t)((uint32_t)pm1) + (uint64_t)((uint32_t)pm2) + (pl >> 32); | ||
47 | 48 | ||
48 | *phigh = ph + ((int64_t)pm1 >> 32); | 49 | *phigh = ph + ((int64_t)pm1 >> 32); |
49 | *plow = (pm1 << 32) + (uint32_t)pl; | 50 | *plow = (pm1 << 32) + (uint32_t)pl; |
@@ -67,7 +68,8 @@ void mulu64(uint64_t *phigh, uint64_t *plow, uint64_t a, uint64_t b) | @@ -67,7 +68,8 @@ void mulu64(uint64_t *phigh, uint64_t *plow, uint64_t a, uint64_t b) | ||
67 | ph = (a >> 32) * (b >> 32); | 68 | ph = (a >> 32) * (b >> 32); |
68 | 69 | ||
69 | ph += pm1 >> 32; | 70 | ph += pm1 >> 32; |
70 | - pm1 = (uint64_t)((uint32_t)pm1) + pm2 + (pl >> 32); | 71 | + ph += pm2 >> 32; |
72 | + pm1 = (uint64_t)((uint32_t)pm1) + (uint64_t)((uint32_t)pm2) + (pl >> 32); | ||
71 | 73 | ||
72 | *phigh = ph + (pm1 >> 32); | 74 | *phigh = ph + (pm1 >> 32); |
73 | *plow = (pm1 << 32) + (uint32_t)pl; | 75 | *plow = (pm1 << 32) + (uint32_t)pl; |