From: David Howells <dhowells@redhat.com>
To: Yuri Tikhonov <yur@emcraft.com>
Cc: Wolfgang Denk <wd@denx.de>, Detlev Zundel <dzu@denx.de>,
linux-kernel@vger.kernel.org, Milton Miller <miltonm@bga.com>,
linuxppc-dev@ozlabs.org, Al Viro <viro@ZenIV.linux.org.uk>,
Geert Uytterhoeven <Geert.Uytterhoeven@sonycom.com>,
Ilya Yanok <yanok@emcraft.com>
Subject: Re: Re[2]: [PATCH] fork_init: fix division by zero
Date: Wed, 10 Dec 2008 13:06:44 +0000 [thread overview]
Message-ID: <5545.1228914404@redhat.com> (raw)
In-Reply-To: <1795455197.20081210132912@emcraft.com>
Yuri Tikhonov <yur@emcraft.com> wrote:
> Here we believe in preprocessor: since all PAGE_SIZE, 8, and
> THREAD_SIZE are the constants we expect it will calculate this.
The preprocessor shouldn't be calculating this. I believe it will _only_
calculate expressions for #if. In the situation you're referring to, it
should perform a substitution and nothing more. The preprocessor doesn't
necessarily know how to handle the types involved.
In any case, there's an easy way to find out: you can ask the compiler to give
you the result of running the source through the preprocessor only. For
instance, if you run this:
#define PAGE_SIZE 4096
#define THREAD_SIZE 8192
unsigned long mempages;
unsigned long jump(void)
{
unsigned long max_threads;
max_threads = mempages * PAGE_SIZE / (8 * THREAD_SIZE);
return max_threads;
}
through "gcc -E", you get:
# 1 "calc.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "calc.c"
unsigned long mempages;
unsigned long jump(void)
{
unsigned long max_threads;
max_threads = mempages * 4096 / (8 * 8192);
return max_threads;
}
> In any case, adding braces as follows probably would be better:
>
> + max_threads = mempages * (PAGE_SIZE / (8 * THREAD_SIZE));
I think you mean brackets, not braces '{}'.
> Right ?
Definitely not.
I added this function to the above:
unsigned long alt(void)
{
unsigned long max_threads;
max_threads = mempages * (PAGE_SIZE / (8 * THREAD_SIZE));
return max_threads;
}
and ran it through "gcc -S -O2" for x86_64:
jump:
movq mempages(%rip), %rax
salq $12, %rax
shrq $16, %rax
ret
alt:
xorl %eax, %eax
ret
Note the difference? In jump(), x86_64 first multiplies mempages by 4096, and
_then_ divides by 8*8192.
In alt(), it just returns 0 because the compiler realised that you're
multiplying by 0.
If you're going to bracket the expression, it must be:
max_threads = (mempages * PAGE_SIZE) / (8 * THREAD_SIZE);
which should be superfluous.
> E.g. here is the result from this line as produced by cross-gcc
> 4.2.2:
>
> lis r9,0
> rlwinm r29,r29,2,16,29
> stw r29,0(r9)
>
> As you see - only rotate-left, i.e. multiplication to the constant.
Ummm... On powerpc, I believe rotate-left would be a division as it does the
bit-numbering and the bit direction the opposite way to more familiar CPUs
such as x86.
David
WARNING: multiple messages have this Message-ID (diff)
From: David Howells <dhowells@redhat.com>
To: Yuri Tikhonov <yur@emcraft.com>
Cc: dhowells@redhat.com, Al Viro <viro@ZenIV.linux.org.uk>,
Wolfgang Denk <wd@denx.de>, Detlev Zundel <dzu@denx.de>,
linux-kernel@vger.kernel.org, Milton Miller <miltonm@bga.com>,
linuxppc-dev@ozlabs.org,
Geert Uytterhoeven <Geert.Uytterhoeven@sonycom.com>,
Ilya Yanok <yanok@emcraft.com>
Subject: Re: Re[2]: [PATCH] fork_init: fix division by zero
Date: Wed, 10 Dec 2008 13:06:44 +0000 [thread overview]
Message-ID: <5545.1228914404@redhat.com> (raw)
In-Reply-To: <1795455197.20081210132912@emcraft.com>
Yuri Tikhonov <yur@emcraft.com> wrote:
> Here we believe in preprocessor: since all PAGE_SIZE, 8, and
> THREAD_SIZE are the constants we expect it will calculate this.
The preprocessor shouldn't be calculating this. I believe it will _only_
calculate expressions for #if. In the situation you're referring to, it
should perform a substitution and nothing more. The preprocessor doesn't
necessarily know how to handle the types involved.
In any case, there's an easy way to find out: you can ask the compiler to give
you the result of running the source through the preprocessor only. For
instance, if you run this:
#define PAGE_SIZE 4096
#define THREAD_SIZE 8192
unsigned long mempages;
unsigned long jump(void)
{
unsigned long max_threads;
max_threads = mempages * PAGE_SIZE / (8 * THREAD_SIZE);
return max_threads;
}
through "gcc -E", you get:
# 1 "calc.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "calc.c"
unsigned long mempages;
unsigned long jump(void)
{
unsigned long max_threads;
max_threads = mempages * 4096 / (8 * 8192);
return max_threads;
}
> In any case, adding braces as follows probably would be better:
>
> + max_threads = mempages * (PAGE_SIZE / (8 * THREAD_SIZE));
I think you mean brackets, not braces '{}'.
> Right ?
Definitely not.
I added this function to the above:
unsigned long alt(void)
{
unsigned long max_threads;
max_threads = mempages * (PAGE_SIZE / (8 * THREAD_SIZE));
return max_threads;
}
and ran it through "gcc -S -O2" for x86_64:
jump:
movq mempages(%rip), %rax
salq $12, %rax
shrq $16, %rax
ret
alt:
xorl %eax, %eax
ret
Note the difference? In jump(), x86_64 first multiplies mempages by 4096, and
_then_ divides by 8*8192.
In alt(), it just returns 0 because the compiler realised that you're
multiplying by 0.
If you're going to bracket the expression, it must be:
max_threads = (mempages * PAGE_SIZE) / (8 * THREAD_SIZE);
which should be superfluous.
> E.g. here is the result from this line as produced by cross-gcc
> 4.2.2:
>
> lis r9,0
> rlwinm r29,r29,2,16,29
> stw r29,0(r9)
>
> As you see - only rotate-left, i.e. multiplication to the constant.
Ummm... On powerpc, I believe rotate-left would be a division as it does the
bit-numbering and the bit direction the opposite way to more familiar CPUs
such as x86.
David
next prev parent reply other threads:[~2008-12-10 13:07 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-12-09 17:44 [PATCH] fork_init: fix division by zero Yuri Tikhonov
2008-12-09 17:44 ` Yuri Tikhonov
2008-12-10 8:44 ` Geert Uytterhoeven
2008-12-10 8:44 ` Geert Uytterhoeven
2008-12-10 10:01 ` Re[2]: " Yuri Tikhonov
2008-12-10 10:01 ` Yuri Tikhonov
2008-12-10 10:17 ` Al Viro
2008-12-10 10:17 ` Al Viro
2008-12-10 10:29 ` Re[2]: " Yuri Tikhonov
2008-12-10 10:29 ` Yuri Tikhonov
2008-12-10 13:06 ` David Howells [this message]
2008-12-10 13:06 ` David Howells
2008-12-10 13:09 ` David Howells
2008-12-10 13:09 ` David Howells
2008-12-10 13:15 ` Geert Uytterhoeven
2008-12-10 13:15 ` Geert Uytterhoeven
2008-12-10 13:25 ` Re[4]: " Yuri Tikhonov
2008-12-10 13:25 ` Yuri Tikhonov
2008-12-10 21:50 ` Re[2]: " Paul Mackerras
2008-12-10 21:50 ` Paul Mackerras
2008-12-10 17:25 ` Scott Wood
2008-12-10 17:25 ` Scott Wood
2008-12-10 17:56 ` Re[2]: " Yuri Tikhonov
2008-12-10 17:56 ` Yuri Tikhonov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5545.1228914404@redhat.com \
--to=dhowells@redhat.com \
--cc=Geert.Uytterhoeven@sonycom.com \
--cc=dzu@denx.de \
--cc=linux-kernel@vger.kernel.org \
--cc=linuxppc-dev@ozlabs.org \
--cc=miltonm@bga.com \
--cc=viro@ZenIV.linux.org.uk \
--cc=wd@denx.de \
--cc=yanok@emcraft.com \
--cc=yur@emcraft.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.